template_string_test.cpp 2.5 KB

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