2 * Copyright 2020 Google Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <gtest/gtest.h>
30 // For EXPECT_THAT and HasSubstr
31 #include <gmock/gmock.h>
34 #include "call_type.hh"
36 // Simple substitute definitions for Args and DispatchTable, with an int so
37 // we can tell instances apart.
38 class DispatchTable
{ public: int i
; };
40 class TestCallType
: public CallType
43 bool isDefault() const override
{ return testIsDefault
; }
47 testInitHappened
= true;
51 void printBrief(std::ostream
&os
) const override
{ os
<< testBrief
; }
52 void printDesc(std::ostream
&os
) const override
{ os
<< testDesc
; }
54 TestCallType(const std::string
&_name
) : CallType(_name
) {}
56 static std::map
<std::string
, CallType
&> &testGetMap() { return map(); }
58 // Usage strings to return.
59 std::string testBrief
;
62 // Return this dispatch table when requested.
63 DispatchTable testDt
= { 0 };
64 const DispatchTable
&getDispatch() const override
{ return testDt
; }
66 // Whether this call type should be considered default.
67 bool testIsDefault
= false;
69 // Whether init has been called.
70 bool testInitHappened
= false;
73 TEST(CallTypeTest
, Constructor
)
75 auto &map
= TestCallType::testGetMap();
77 // There should be no call types yet.
78 EXPECT_EQ(map
.size(), 0);
81 TestCallType
test_ct("test_ct");
83 // Set the dispatch table to something we'll recognize.
84 test_ct
.testDt
.i
= 0xaa55;
86 // Verify that the list of all call types has one in it now.
87 EXPECT_EQ(map
.size(), 1);
89 // Verify that that was our call type by verifying that the dispatch table
91 EXPECT_EQ(map
.begin()->second
.getDispatch().i
, 0xaa55);
94 TEST(CallTypeTest
, DetectOne
)
96 auto &map
= TestCallType::testGetMap();
98 // One option selected.
99 TestCallType
option1("option1");
100 option1
.testIsDefault
= true;
101 option1
.testDt
.i
= 1;
103 EXPECT_EQ(map
.size(), 1);
105 Args
args1({"--option1"});
107 EXPECT_FALSE(option1
.testInitHappened
);
109 auto *ct
= CallType::detect(args1
);
111 // Verify that we selected the only option.
112 EXPECT_TRUE(option1
.testInitHappened
);
113 EXPECT_EQ(ct
, &option1
);
115 // One option, selecting the default.
116 option1
.testInitHappened
= false;
118 // Args will not match.
119 Args
args2({"--option2"});
121 auto *def_ct
= CallType::detect(args2
);
123 // Verify that the one option was defaulted to.
124 EXPECT_TRUE(option1
.testInitHappened
);
125 EXPECT_EQ(def_ct
, &option1
);
128 TEST(CallTypeTest
, DetectTwo
)
130 auto &map
= TestCallType::testGetMap();
132 // One of two options selected.
133 TestCallType
option1("option1");
134 option1
.testIsDefault
= true;
135 option1
.testDt
.i
= 1;
137 TestCallType
option2("option2");
138 option2
.testIsDefault
= false;
139 option2
.testDt
.i
= 2;
141 EXPECT_EQ(map
.size(), 2);
143 // Select the first option.
144 Args
args1({"--option1"});
146 EXPECT_FALSE(option1
.testInitHappened
);
147 EXPECT_FALSE(option2
.testInitHappened
);
149 auto *ct1
= CallType::detect(args1
);
151 // Verify that we selected the first option.
152 EXPECT_TRUE(option1
.testInitHappened
);
153 EXPECT_FALSE(option2
.testInitHappened
);
154 EXPECT_EQ(ct1
, &option1
);
156 option1
.testInitHappened
= false;
157 option2
.testInitHappened
= false;
159 // Select the second option.
160 Args
args2({"--option2"});
162 auto *ct2
= CallType::detect(args2
);
164 // Verify that we selected the second option.
165 EXPECT_FALSE(option1
.testInitHappened
);
166 EXPECT_TRUE(option2
.testInitHappened
);
167 EXPECT_EQ(ct2
, &option2
);
169 option1
.testInitHappened
= false;
170 option2
.testInitHappened
= false;
172 // Default to the first option.
173 Args
args3({"--option3"});
175 auto *def_ct1
= CallType::detect(args3
);
177 // Verify that we selected the first option.
178 EXPECT_TRUE(option1
.testInitHappened
);
179 EXPECT_FALSE(option2
.testInitHappened
);
180 EXPECT_EQ(def_ct1
, &option1
);
182 option1
.testInitHappened
= false;
183 option2
.testInitHappened
= false;
185 // Default to the second option.
186 option1
.testIsDefault
= false;
187 option2
.testIsDefault
= true;
189 auto *def_ct2
= CallType::detect(args3
);
191 // Verify that we selected the second option.
192 EXPECT_FALSE(option1
.testInitHappened
);
193 EXPECT_TRUE(option2
.testInitHappened
);
194 EXPECT_EQ(def_ct2
, &option2
);
196 option1
.testInitHappened
= false;
197 option2
.testInitHappened
= false;
200 TEST(CallTypeTest
, Usage
)
202 auto &map
= TestCallType::testGetMap();
204 TestCallType
ct1("ct1");
205 ct1
.testBrief
= "brief 1";
206 ct1
.testDesc
= "A longer description of call type 1, which is long.";
208 TestCallType
ct2("ct2");
209 ct2
.testBrief
= "short 2";
210 ct2
.testDesc
= "Very verbose text saying what call type 2 is, "
211 "and is different from 1.";
213 EXPECT_EQ(map
.size(), 2);
215 auto summary
= CallType::usageSummary();
217 // For now, just expect that the brief and full descriptive text shows up
218 // in the summary somewhere.
220 // More strict checks might test that things were placed in the right
221 // order, were on their own lines when appropriate, etc.
222 EXPECT_THAT(summary
, testing::HasSubstr(ct1
.testBrief
));
223 EXPECT_THAT(summary
, testing::HasSubstr(ct1
.testDesc
));
224 EXPECT_THAT(summary
, testing::HasSubstr(ct2
.testBrief
));
225 EXPECT_THAT(summary
, testing::HasSubstr(ct2
.testDesc
));
226 EXPECT_THAT(summary
, testing::HasSubstr(ct2
.testDesc
));