1 // Copyright 2018 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/base/log_severity.h"
16
17 #include <cstdint>
18 #include <ios>
19 #include <limits>
20 #include <ostream>
21 #include <sstream>
22 #include <string>
23 #include <tuple>
24
25 #include "gmock/gmock.h"
26 #include "gtest/gtest.h"
27 #include "absl/flags/internal/flag.h"
28 #include "absl/flags/marshalling.h"
29 #include "absl/strings/str_cat.h"
30
31 namespace {
32 using ::testing::Eq;
33 using ::testing::IsFalse;
34 using ::testing::IsTrue;
35 using ::testing::TestWithParam;
36 using ::testing::Values;
37
StreamHelper(absl::LogSeverity value)38 std::string StreamHelper(absl::LogSeverity value) {
39 std::ostringstream stream;
40 stream << value;
41 return stream.str();
42 }
43
TEST(StreamTest,Works)44 TEST(StreamTest, Works) {
45 EXPECT_THAT(StreamHelper(static_cast<absl::LogSeverity>(-100)),
46 Eq("absl::LogSeverity(-100)"));
47 EXPECT_THAT(StreamHelper(absl::LogSeverity::kInfo), Eq("INFO"));
48 EXPECT_THAT(StreamHelper(absl::LogSeverity::kWarning), Eq("WARNING"));
49 EXPECT_THAT(StreamHelper(absl::LogSeverity::kError), Eq("ERROR"));
50 EXPECT_THAT(StreamHelper(absl::LogSeverity::kFatal), Eq("FATAL"));
51 EXPECT_THAT(StreamHelper(static_cast<absl::LogSeverity>(4)),
52 Eq("absl::LogSeverity(4)"));
53 }
54
55 static_assert(
56 absl::flags_internal::IsAtomicFlagTypeTrait<absl::LogSeverity>::value,
57 "Flags of type absl::LogSeverity ought to be lock-free.");
58
59 using ParseFlagFromOutOfRangeIntegerTest = TestWithParam<int64_t>;
60 INSTANTIATE_TEST_SUITE_P(
61 Instantiation, ParseFlagFromOutOfRangeIntegerTest,
62 Values(static_cast<int64_t>(std::numeric_limits<int>::min()) - 1,
63 static_cast<int64_t>(std::numeric_limits<int>::max()) + 1));
TEST_P(ParseFlagFromOutOfRangeIntegerTest,ReturnsError)64 TEST_P(ParseFlagFromOutOfRangeIntegerTest, ReturnsError) {
65 const std::string to_parse = absl::StrCat(GetParam());
66 absl::LogSeverity value;
67 std::string error;
68 EXPECT_THAT(absl::ParseFlag(to_parse, &value, &error), IsFalse()) << value;
69 }
70
71 using ParseFlagFromAlmostOutOfRangeIntegerTest = TestWithParam<int>;
72 INSTANTIATE_TEST_SUITE_P(Instantiation,
73 ParseFlagFromAlmostOutOfRangeIntegerTest,
74 Values(std::numeric_limits<int>::min(),
75 std::numeric_limits<int>::max()));
TEST_P(ParseFlagFromAlmostOutOfRangeIntegerTest,YieldsExpectedValue)76 TEST_P(ParseFlagFromAlmostOutOfRangeIntegerTest, YieldsExpectedValue) {
77 const auto expected = static_cast<absl::LogSeverity>(GetParam());
78 const std::string to_parse = absl::StrCat(GetParam());
79 absl::LogSeverity value;
80 std::string error;
81 ASSERT_THAT(absl::ParseFlag(to_parse, &value, &error), IsTrue()) << error;
82 EXPECT_THAT(value, Eq(expected));
83 }
84
85 using ParseFlagFromIntegerMatchingEnumeratorTest =
86 TestWithParam<std::tuple<absl::string_view, absl::LogSeverity>>;
87 INSTANTIATE_TEST_SUITE_P(
88 Instantiation, ParseFlagFromIntegerMatchingEnumeratorTest,
89 Values(std::make_tuple("0", absl::LogSeverity::kInfo),
90 std::make_tuple(" 0", absl::LogSeverity::kInfo),
91 std::make_tuple("-0", absl::LogSeverity::kInfo),
92 std::make_tuple("+0", absl::LogSeverity::kInfo),
93 std::make_tuple("00", absl::LogSeverity::kInfo),
94 std::make_tuple("0 ", absl::LogSeverity::kInfo),
95 std::make_tuple("0x0", absl::LogSeverity::kInfo),
96 std::make_tuple("1", absl::LogSeverity::kWarning),
97 std::make_tuple("+1", absl::LogSeverity::kWarning),
98 std::make_tuple("2", absl::LogSeverity::kError),
99 std::make_tuple("3", absl::LogSeverity::kFatal)));
TEST_P(ParseFlagFromIntegerMatchingEnumeratorTest,YieldsExpectedValue)100 TEST_P(ParseFlagFromIntegerMatchingEnumeratorTest, YieldsExpectedValue) {
101 const absl::string_view to_parse = std::get<0>(GetParam());
102 const absl::LogSeverity expected = std::get<1>(GetParam());
103 absl::LogSeverity value;
104 std::string error;
105 ASSERT_THAT(absl::ParseFlag(to_parse, &value, &error), IsTrue()) << error;
106 EXPECT_THAT(value, Eq(expected));
107 }
108
109 using ParseFlagFromOtherIntegerTest =
110 TestWithParam<std::tuple<absl::string_view, int>>;
111 INSTANTIATE_TEST_SUITE_P(Instantiation, ParseFlagFromOtherIntegerTest,
112 Values(std::make_tuple("-1", -1),
113 std::make_tuple("4", 4),
114 std::make_tuple("010", 10),
115 std::make_tuple("0x10", 16)));
TEST_P(ParseFlagFromOtherIntegerTest,YieldsExpectedValue)116 TEST_P(ParseFlagFromOtherIntegerTest, YieldsExpectedValue) {
117 const absl::string_view to_parse = std::get<0>(GetParam());
118 const auto expected = static_cast<absl::LogSeverity>(std::get<1>(GetParam()));
119 absl::LogSeverity value;
120 std::string error;
121 ASSERT_THAT(absl::ParseFlag(to_parse, &value, &error), IsTrue()) << error;
122 EXPECT_THAT(value, Eq(expected));
123 }
124
125 using ParseFlagFromEnumeratorTest =
126 TestWithParam<std::tuple<absl::string_view, absl::LogSeverity>>;
127 INSTANTIATE_TEST_SUITE_P(
128 Instantiation, ParseFlagFromEnumeratorTest,
129 Values(std::make_tuple("INFO", absl::LogSeverity::kInfo),
130 std::make_tuple("info", absl::LogSeverity::kInfo),
131 std::make_tuple("kInfo", absl::LogSeverity::kInfo),
132 std::make_tuple("iNfO", absl::LogSeverity::kInfo),
133 std::make_tuple("kInFo", absl::LogSeverity::kInfo),
134 std::make_tuple("WARNING", absl::LogSeverity::kWarning),
135 std::make_tuple("warning", absl::LogSeverity::kWarning),
136 std::make_tuple("kWarning", absl::LogSeverity::kWarning),
137 std::make_tuple("WaRnInG", absl::LogSeverity::kWarning),
138 std::make_tuple("KwArNiNg", absl::LogSeverity::kWarning),
139 std::make_tuple("ERROR", absl::LogSeverity::kError),
140 std::make_tuple("error", absl::LogSeverity::kError),
141 std::make_tuple("kError", absl::LogSeverity::kError),
142 std::make_tuple("eRrOr", absl::LogSeverity::kError),
143 std::make_tuple("kErRoR", absl::LogSeverity::kError),
144 std::make_tuple("FATAL", absl::LogSeverity::kFatal),
145 std::make_tuple("fatal", absl::LogSeverity::kFatal),
146 std::make_tuple("kFatal", absl::LogSeverity::kFatal),
147 std::make_tuple("FaTaL", absl::LogSeverity::kFatal),
148 std::make_tuple("KfAtAl", absl::LogSeverity::kFatal)));
TEST_P(ParseFlagFromEnumeratorTest,YieldsExpectedValue)149 TEST_P(ParseFlagFromEnumeratorTest, YieldsExpectedValue) {
150 const absl::string_view to_parse = std::get<0>(GetParam());
151 const absl::LogSeverity expected = std::get<1>(GetParam());
152 absl::LogSeverity value;
153 std::string error;
154 ASSERT_THAT(absl::ParseFlag(to_parse, &value, &error), IsTrue()) << error;
155 EXPECT_THAT(value, Eq(expected));
156 }
157
158 using ParseFlagFromGarbageTest = TestWithParam<absl::string_view>;
159 INSTANTIATE_TEST_SUITE_P(Instantiation, ParseFlagFromGarbageTest,
160 Values("", "\0", " ", "garbage", "kkinfo", "I"));
TEST_P(ParseFlagFromGarbageTest,ReturnsError)161 TEST_P(ParseFlagFromGarbageTest, ReturnsError) {
162 const absl::string_view to_parse = GetParam();
163 absl::LogSeverity value;
164 std::string error;
165 EXPECT_THAT(absl::ParseFlag(to_parse, &value, &error), IsFalse()) << value;
166 }
167
168 using UnparseFlagToEnumeratorTest =
169 TestWithParam<std::tuple<absl::LogSeverity, absl::string_view>>;
170 INSTANTIATE_TEST_SUITE_P(
171 Instantiation, UnparseFlagToEnumeratorTest,
172 Values(std::make_tuple(absl::LogSeverity::kInfo, "INFO"),
173 std::make_tuple(absl::LogSeverity::kWarning, "WARNING"),
174 std::make_tuple(absl::LogSeverity::kError, "ERROR"),
175 std::make_tuple(absl::LogSeverity::kFatal, "FATAL")));
TEST_P(UnparseFlagToEnumeratorTest,ReturnsExpectedValueAndRoundTrips)176 TEST_P(UnparseFlagToEnumeratorTest, ReturnsExpectedValueAndRoundTrips) {
177 const absl::LogSeverity to_unparse = std::get<0>(GetParam());
178 const absl::string_view expected = std::get<1>(GetParam());
179 const std::string stringified_value = absl::UnparseFlag(to_unparse);
180 EXPECT_THAT(stringified_value, Eq(expected));
181 absl::LogSeverity reparsed_value;
182 std::string error;
183 EXPECT_THAT(absl::ParseFlag(stringified_value, &reparsed_value, &error),
184 IsTrue());
185 EXPECT_THAT(reparsed_value, Eq(to_unparse));
186 }
187
188 using UnparseFlagToOtherIntegerTest = TestWithParam<int>;
189 INSTANTIATE_TEST_SUITE_P(Instantiation, UnparseFlagToOtherIntegerTest,
190 Values(std::numeric_limits<int>::min(), -1, 4,
191 std::numeric_limits<int>::max()));
TEST_P(UnparseFlagToOtherIntegerTest,ReturnsExpectedValueAndRoundTrips)192 TEST_P(UnparseFlagToOtherIntegerTest, ReturnsExpectedValueAndRoundTrips) {
193 const absl::LogSeverity to_unparse =
194 static_cast<absl::LogSeverity>(GetParam());
195 const std::string expected = absl::StrCat(GetParam());
196 const std::string stringified_value = absl::UnparseFlag(to_unparse);
197 EXPECT_THAT(stringified_value, Eq(expected));
198 absl::LogSeverity reparsed_value;
199 std::string error;
200 EXPECT_THAT(absl::ParseFlag(stringified_value, &reparsed_value, &error),
201 IsTrue());
202 EXPECT_THAT(reparsed_value, Eq(to_unparse));
203 }
204 } // namespace
205