-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_test.cc
147 lines (126 loc) · 6.33 KB
/
flag_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/flags/flag.h"
#include <string>
#include "gtest/gtest.h"
#include "absl/flags/reflection.h"
#include "absl/time/civil_time.h"
#include "absl/time/time.h"
ABSL_FLAG(absl::CivilSecond, test_flag_civil_second,
absl::CivilSecond(2015, 1, 2, 3, 4, 5), "");
ABSL_FLAG(absl::CivilMinute, test_flag_civil_minute,
absl::CivilMinute(2015, 1, 2, 3, 4), "");
ABSL_FLAG(absl::CivilHour, test_flag_civil_hour, absl::CivilHour(2015, 1, 2, 3),
"");
ABSL_FLAG(absl::CivilDay, test_flag_civil_day, absl::CivilDay(2015, 1, 2), "");
ABSL_FLAG(absl::CivilMonth, test_flag_civil_month, absl::CivilMonth(2015, 1),
"");
ABSL_FLAG(absl::CivilYear, test_flag_civil_year, absl::CivilYear(2015), "");
ABSL_FLAG(absl::Duration, test_duration_flag, absl::Seconds(5),
"For testing support for Duration flags");
ABSL_FLAG(absl::Time, test_time_flag, absl::InfinitePast(),
"For testing support for Time flags");
namespace {
bool SetFlagValue(absl::string_view flag_name, absl::string_view value) {
auto* flag = absl::FindCommandLineFlag(flag_name);
if (!flag) return false;
std::string err;
return flag->ParseFrom(value, &err);
}
bool GetFlagValue(absl::string_view flag_name, std::string& value) {
auto* flag = absl::FindCommandLineFlag(flag_name);
if (!flag) return false;
value = flag->CurrentValue();
return true;
}
TEST(CivilTime, FlagSupport) {
// Tests the default setting of the flags.
const absl::CivilSecond kDefaultSec(2015, 1, 2, 3, 4, 5);
EXPECT_EQ(absl::CivilSecond(kDefaultSec),
absl::GetFlag(FLAGS_test_flag_civil_second));
EXPECT_EQ(absl::CivilMinute(kDefaultSec),
absl::GetFlag(FLAGS_test_flag_civil_minute));
EXPECT_EQ(absl::CivilHour(kDefaultSec),
absl::GetFlag(FLAGS_test_flag_civil_hour));
EXPECT_EQ(absl::CivilDay(kDefaultSec),
absl::GetFlag(FLAGS_test_flag_civil_day));
EXPECT_EQ(absl::CivilMonth(kDefaultSec),
absl::GetFlag(FLAGS_test_flag_civil_month));
EXPECT_EQ(absl::CivilYear(kDefaultSec),
absl::GetFlag(FLAGS_test_flag_civil_year));
// Sets flags to a new value.
const absl::CivilSecond kNewSec(2016, 6, 7, 8, 9, 10);
absl::SetFlag(&FLAGS_test_flag_civil_second, absl::CivilSecond(kNewSec));
absl::SetFlag(&FLAGS_test_flag_civil_minute, absl::CivilMinute(kNewSec));
absl::SetFlag(&FLAGS_test_flag_civil_hour, absl::CivilHour(kNewSec));
absl::SetFlag(&FLAGS_test_flag_civil_day, absl::CivilDay(kNewSec));
absl::SetFlag(&FLAGS_test_flag_civil_month, absl::CivilMonth(kNewSec));
absl::SetFlag(&FLAGS_test_flag_civil_year, absl::CivilYear(kNewSec));
EXPECT_EQ(absl::CivilSecond(kNewSec),
absl::GetFlag(FLAGS_test_flag_civil_second));
EXPECT_EQ(absl::CivilMinute(kNewSec),
absl::GetFlag(FLAGS_test_flag_civil_minute));
EXPECT_EQ(absl::CivilHour(kNewSec),
absl::GetFlag(FLAGS_test_flag_civil_hour));
EXPECT_EQ(absl::CivilDay(kNewSec), absl::GetFlag(FLAGS_test_flag_civil_day));
EXPECT_EQ(absl::CivilMonth(kNewSec),
absl::GetFlag(FLAGS_test_flag_civil_month));
EXPECT_EQ(absl::CivilYear(kNewSec),
absl::GetFlag(FLAGS_test_flag_civil_year));
}
TEST(Duration, FlagSupport) {
EXPECT_EQ(absl::Seconds(5), absl::GetFlag(FLAGS_test_duration_flag));
absl::SetFlag(&FLAGS_test_duration_flag, absl::Seconds(10));
EXPECT_EQ(absl::Seconds(10), absl::GetFlag(FLAGS_test_duration_flag));
EXPECT_TRUE(SetFlagValue("test_duration_flag", "20s"));
EXPECT_EQ(absl::Seconds(20), absl::GetFlag(FLAGS_test_duration_flag));
std::string current_flag_value;
EXPECT_TRUE(GetFlagValue("test_duration_flag", current_flag_value));
EXPECT_EQ("20s", current_flag_value);
}
TEST(Time, FlagSupport) {
EXPECT_EQ(absl::InfinitePast(), absl::GetFlag(FLAGS_test_time_flag));
const absl::Time t = absl::FromCivil(absl::CivilSecond(2016, 1, 2, 3, 4, 5),
absl::UTCTimeZone());
absl::SetFlag(&FLAGS_test_time_flag, t);
EXPECT_EQ(t, absl::GetFlag(FLAGS_test_time_flag));
// Successful parse
EXPECT_TRUE(SetFlagValue("test_time_flag", "2016-01-02T03:04:06Z"));
EXPECT_EQ(t + absl::Seconds(1), absl::GetFlag(FLAGS_test_time_flag));
EXPECT_TRUE(SetFlagValue("test_time_flag", "2016-01-02T03:04:07.0Z"));
EXPECT_EQ(t + absl::Seconds(2), absl::GetFlag(FLAGS_test_time_flag));
EXPECT_TRUE(SetFlagValue("test_time_flag", "2016-01-02T03:04:08.000Z"));
EXPECT_EQ(t + absl::Seconds(3), absl::GetFlag(FLAGS_test_time_flag));
EXPECT_TRUE(SetFlagValue("test_time_flag", "2016-01-02T03:04:09+00:00"));
EXPECT_EQ(t + absl::Seconds(4), absl::GetFlag(FLAGS_test_time_flag));
EXPECT_TRUE(SetFlagValue("test_time_flag", "2016-01-02T03:04:05.123+00:00"));
EXPECT_EQ(t + absl::Milliseconds(123), absl::GetFlag(FLAGS_test_time_flag));
EXPECT_TRUE(SetFlagValue("test_time_flag", "2016-01-02T03:04:05.123+08:00"));
EXPECT_EQ(t + absl::Milliseconds(123) - absl::Hours(8),
absl::GetFlag(FLAGS_test_time_flag));
EXPECT_TRUE(SetFlagValue("test_time_flag", "infinite-future"));
EXPECT_EQ(absl::InfiniteFuture(), absl::GetFlag(FLAGS_test_time_flag));
EXPECT_TRUE(SetFlagValue("test_time_flag", "infinite-past"));
EXPECT_EQ(absl::InfinitePast(), absl::GetFlag(FLAGS_test_time_flag));
EXPECT_FALSE(SetFlagValue("test_time_flag", "2016-01-02T03:04:06"));
EXPECT_FALSE(SetFlagValue("test_time_flag", "2016-01-02"));
EXPECT_FALSE(SetFlagValue("test_time_flag", "2016-01-02Z"));
EXPECT_FALSE(SetFlagValue("test_time_flag", "2016-01-02+00:00"));
EXPECT_FALSE(SetFlagValue("test_time_flag", "2016-99-99T03:04:06Z"));
EXPECT_TRUE(SetFlagValue("test_time_flag", "2016-01-02T03:04:05Z"));
std::string current_flag_value;
EXPECT_TRUE(GetFlagValue("test_time_flag", current_flag_value));
EXPECT_EQ("2016-01-02T03:04:05+00:00", current_flag_value);
}
} // namespace