template_string_test.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "common/template_string.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <type_traits>
  8. namespace Carbon {
  9. namespace {
  10. using ::testing::StrEq;
  11. template <TemplateString S>
  12. constexpr auto TemplateAsStringRef() -> llvm::StringRef {
  13. return S;
  14. }
  15. template <TemplateString S>
  16. constexpr auto TemplateAsStringLiteral() -> llvm::StringLiteral {
  17. return S;
  18. }
  19. template <TemplateString S>
  20. constexpr auto TemplateAsCStr() -> const char* {
  21. return S.c_str();
  22. }
  23. // An overload that will be active when it is passed a valid `TemplateString`.
  24. // Returns a true type to allow detection of a valid `TemplateString` argument.
  25. template <TemplateString /*Unused*/>
  26. constexpr auto IsValidTemplateString(int /*unused*/) -> std::true_type {
  27. return {};
  28. }
  29. // A struct that can be used as a template parameter for any template argument.
  30. struct AnythingAsTemplateArg {
  31. // An implicit constructor that can accept any argument and discards it.
  32. template <typename T>
  33. // NOLINTNEXTLINE(bugprone-forwarding-reference-overload)
  34. explicit(false) constexpr AnythingAsTemplateArg(T&& /*unused*/) {}
  35. };
  36. // An overload that will be active for any template argument. Returns a false
  37. // type and is used to detect when a template argument cannot correctly match a
  38. // `TemplateString`.
  39. template <AnythingAsTemplateArg /*Unused*/>
  40. constexpr auto IsValidTemplateString(...) -> std::false_type {
  41. return {};
  42. }
  43. // Compile time tests with `static_assert`
  44. static_assert(TemplateAsStringRef<"test">().size() == 4,
  45. "Not usable in a `constexpr` context.");
  46. static_assert(TemplateAsStringLiteral<"test">().size() == 4,
  47. "Not usable in a `constexpr` context.");
  48. static_assert(__builtin_strlen(TemplateAsCStr<"test">()) == 4,
  49. "Not usable in a `constexpr` context.");
  50. // The string must not contain embedded nulls.
  51. static_assert(IsValidTemplateString<"test">(0));
  52. static_assert(!IsValidTemplateString<"test\0test">(0));
  53. // The string must be null-terminated.
  54. using FourChars = char[4];
  55. static_assert(IsValidTemplateString<FourChars{'t', 'e', 's', 0}>(0));
  56. static_assert(!IsValidTemplateString<FourChars{'t', 'e', 's', 't'}>(0));
  57. TEST(TemplateStringTest, Test) {
  58. EXPECT_THAT(TemplateAsStringRef<"test">(), StrEq("test"));
  59. EXPECT_THAT(TemplateAsStringLiteral<"test">(), StrEq("test"));
  60. EXPECT_THAT(TemplateAsCStr<"test">(), StrEq("test"));
  61. constexpr char GoodStr[5] = {'t', 'e', 's', 't', '\0'};
  62. static_assert(IsValidTemplateString<GoodStr>(0));
  63. EXPECT_THAT(TemplateAsStringRef<GoodStr>(), StrEq("test"));
  64. EXPECT_THAT(TemplateAsStringLiteral<GoodStr>(), StrEq("test"));
  65. constexpr char BadStr[4] = {'t', 'e', 's', 't'};
  66. static_assert(!IsValidTemplateString<BadStr>(0));
  67. }
  68. } // namespace
  69. } // namespace Carbon