blob: 392833cf1592dc6636516baa78069993bbc4f2c8 [file] [log] [blame]
Austin Schuhb4691e92020-12-31 12:37:18 -08001// 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
21namespace {
22
23using absl::strings_internal::MakeStringConstant;
24
25struct Callable {
26 constexpr absl::string_view operator()() const {
27 return absl::string_view("Callable", 8);
28 }
29};
30
31TEST(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
43TEST(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
50TEST(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