template_string_test.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. namespace Carbon {
  8. namespace {
  9. using ::testing::StrEq;
  10. template <TemplateString S>
  11. constexpr auto FromTemplate() -> llvm::StringRef {
  12. return S;
  13. }
  14. template <TemplateString S>
  15. constexpr auto CStrFromTemplate() -> const char* {
  16. return S.c_str();
  17. }
  18. // An overload that will be active when it is passed a valid `TemplateString`.
  19. // Returns a true type to allow detection of a valid `TemplateString` argument.
  20. template <TemplateString /*Unused*/>
  21. constexpr auto IsValidTemplateString(int /*unused*/) -> std::true_type {
  22. return {};
  23. }
  24. // A struct that can be used as a template parameter for any template argument.
  25. struct AnythingAsTemplateArg {
  26. // An implicit constructor that can accept any argument and discards it.
  27. template <typename T>
  28. // NOLINTNEXTLINE(google-explicit-constructor,bugprone-forwarding-reference-overload)
  29. constexpr AnythingAsTemplateArg(T&& /*unused*/) {}
  30. };
  31. // An overload that will be active for any template argument. Returns a false
  32. // type and is used to detect when a template argument cannot correctly match a
  33. // `TemplateString`.
  34. template <AnythingAsTemplateArg /*Unused*/>
  35. constexpr auto IsValidTemplateString(...) -> std::false_type {
  36. return {};
  37. }
  38. // Compile time tests with `static_assert`
  39. static_assert(FromTemplate<"test">().size() == 4,
  40. "Not usable in a `constexpr` context.");
  41. static_assert(__builtin_strlen(CStrFromTemplate<"test">()) == 4,
  42. "Not usable in a `constexpr` context.");
  43. // The string must not contain embedded nulls.
  44. static_assert(IsValidTemplateString<"test">(0));
  45. static_assert(!IsValidTemplateString<"test\0test">(0));
  46. // The string must be null-terminated.
  47. using FourChars = char[4];
  48. static_assert(IsValidTemplateString<FourChars{'t', 'e', 's', 0}>(0));
  49. static_assert(!IsValidTemplateString<FourChars{'t', 'e', 's', 't'}>(0));
  50. TEST(TemplateStringTest, Test) {
  51. EXPECT_THAT(FromTemplate<"test">(), StrEq("test"));
  52. EXPECT_THAT(CStrFromTemplate<"test">(), StrEq("test"));
  53. constexpr char GoodStr[5] = {'t', 'e', 's', 't', '\0'};
  54. static_assert(IsValidTemplateString<GoodStr>(0));
  55. EXPECT_THAT(FromTemplate<GoodStr>(), StrEq("test"));
  56. constexpr char BadStr[4] = {'t', 'e', 's', 't'};
  57. static_assert(!IsValidTemplateString<BadStr>(0));
  58. }
  59. } // namespace
  60. } // namespace Carbon