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
15 #include "absl/strings/internal/string_constant.h"
16
17 #include "absl/meta/type_traits.h"
18 #include "gmock/gmock.h"
19 #include "gtest/gtest.h"
20
21 namespace {
22
23 using absl::strings_internal::MakeStringConstant;
24
25 struct Callable {
operator ()__anon28545b5f0111::Callable26 constexpr absl::string_view operator()() const {
27 return absl::string_view("Callable", 8);
28 }
29 };
30
TEST(StringConstant,Traits)31 TEST(StringConstant, Traits) {
32 constexpr auto str = MakeStringConstant(Callable{});
33 using T = decltype(str);
34
35 EXPECT_TRUE(std::is_empty<T>::value);
36 EXPECT_TRUE(std::is_trivial<T>::value);
37 EXPECT_TRUE(absl::is_trivially_default_constructible<T>::value);
38 EXPECT_TRUE(absl::is_trivially_copy_constructible<T>::value);
39 EXPECT_TRUE(absl::is_trivially_move_constructible<T>::value);
40 EXPECT_TRUE(absl::is_trivially_destructible<T>::value);
41 }
42
TEST(StringConstant,MakeFromCallable)43 TEST(StringConstant, MakeFromCallable) {
44 constexpr auto str = MakeStringConstant(Callable{});
45 using T = decltype(str);
46 EXPECT_EQ(Callable{}(), T::value);
47 EXPECT_EQ(Callable{}(), str());
48 }
49
TEST(StringConstant,MakeFromStringConstant)50 TEST(StringConstant, MakeFromStringConstant) {
51 // We want to make sure the StringConstant itself is a valid input to the
52 // factory function.
53 constexpr auto str = MakeStringConstant(Callable{});
54 constexpr auto str2 = MakeStringConstant(str);
55 using T = decltype(str2);
56 EXPECT_EQ(Callable{}(), T::value);
57 EXPECT_EQ(Callable{}(), str2());
58 }
59
60 } // namespace
61