string_constant_test.cc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2020 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/strings/internal/string_constant.h"
  15. #include "absl/meta/type_traits.h"
  16. #include "gmock/gmock.h"
  17. #include "gtest/gtest.h"
  18. namespace {
  19. using absl::strings_internal::MakeStringConstant;
  20. struct Callable {
  21. constexpr absl::string_view operator()() const {
  22. return absl::string_view("Callable", 8);
  23. }
  24. };
  25. TEST(StringConstant, Traits) {
  26. constexpr auto str = MakeStringConstant(Callable{});
  27. using T = decltype(str);
  28. EXPECT_TRUE(std::is_empty<T>::value);
  29. EXPECT_TRUE(std::is_trivial<T>::value);
  30. EXPECT_TRUE(absl::is_trivially_default_constructible<T>::value);
  31. EXPECT_TRUE(absl::is_trivially_copy_constructible<T>::value);
  32. EXPECT_TRUE(absl::is_trivially_move_constructible<T>::value);
  33. EXPECT_TRUE(absl::is_trivially_destructible<T>::value);
  34. }
  35. TEST(StringConstant, MakeFromCallable) {
  36. constexpr auto str = MakeStringConstant(Callable{});
  37. using T = decltype(str);
  38. EXPECT_EQ(Callable{}(), T::value);
  39. EXPECT_EQ(Callable{}(), str());
  40. }
  41. TEST(StringConstant, MakeFromStringConstant) {
  42. // We want to make sure the StringConstant itself is a valid input to the
  43. // factory function.
  44. constexpr auto str = MakeStringConstant(Callable{});
  45. constexpr auto str2 = MakeStringConstant(str);
  46. using T = decltype(str2);
  47. EXPECT_EQ(Callable{}(), T::value);
  48. EXPECT_EQ(Callable{}(), str2());
  49. }
  50. } // namespace