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>
36 bool ran_test1
= false;
38 // A dummy class so we can make a reference to pass around.
39 class DispatchTable
{};
44 do_test1(const DispatchTable
&dt
, Args
&args
)
50 bool ran_test2
= false;
53 do_test2(const DispatchTable
&dt
, Args
&args
)
59 TEST(CommandTest
, OneCommandNoArgs
)
61 Command
test1("test1", 0, 0, do_test1
, "");
63 // Try to run the command with an extra argument, expecting it to fail.
64 EXPECT_FALSE(ran_test1
);
65 Args
args1({ "test1", "extra" });
66 EXPECT_FALSE(Command::run(dt
, args1
));
67 EXPECT_FALSE(ran_test1
);
69 // Try to run with an unrecognized command.
70 Args
args2({ "bad_command" });
71 EXPECT_FALSE(Command::run(dt
, args2
));
72 EXPECT_FALSE(ran_test1
);
74 // Run with the right name and number of arguments.
75 Args
args3({ "test1" });
76 EXPECT_TRUE(Command::run(dt
, args3
));
77 EXPECT_TRUE(ran_test1
);
80 // Try with no command at all.
82 EXPECT_FALSE(Command::run(dt
, args4
));
83 EXPECT_FALSE(ran_test1
);
86 TEST(CommandTest
, OneCommandSomeArgs
)
88 Command
test1("test1", 2, 3, do_test1
, "");
91 EXPECT_FALSE(ran_test1
);
92 Args
args1({ "test1" });
93 EXPECT_FALSE(Command::run(dt
, args1
));
94 EXPECT_FALSE(ran_test1
);
96 // Too many arguments.
97 Args
args2({ "test1", "arg1", "arg2", "arg3", "arg4" });
98 EXPECT_FALSE(Command::run(dt
, args2
));
99 EXPECT_FALSE(ran_test1
);
101 // Just enough arguments.
102 Args
args3({ "test1", "arg1", "arg2" });
103 EXPECT_TRUE(Command::run(dt
, args3
));
104 EXPECT_TRUE(ran_test1
);
107 // Almost too many arguments.
108 Args
args4({ "test1", "arg1", "arg2", "arg3" });
109 EXPECT_TRUE(Command::run(dt
, args4
));
110 EXPECT_TRUE(ran_test1
);
114 TEST(CommandTest
, TwoCommands
)
116 Command
test1("test1", 0, 0, do_test1
, "");
117 Command
test2("test2", 1, 1, do_test2
, "");
119 // Try a bad command name.
120 Args
args1({ "bad_command" });
121 EXPECT_FALSE(Command::run(dt
, args1
));
122 EXPECT_FALSE(ran_test1
);
123 EXPECT_FALSE(ran_test2
);
125 // Try the right command with the wrong number of arguments.
126 Args
args2({ "test1", "arg1" });
127 EXPECT_FALSE(Command::run(dt
, args2
));
128 EXPECT_FALSE(ran_test1
);
129 EXPECT_FALSE(ran_test2
);
131 // Run the first command.
132 Args
args3({ "test1" });
133 EXPECT_TRUE(Command::run(dt
, args3
));
134 EXPECT_TRUE(ran_test1
);
135 EXPECT_FALSE(ran_test2
);
136 ran_test1
= ran_test2
= false;
138 // Run the second command.
139 Args
args4({ "test2", "arg1" });
140 EXPECT_TRUE(Command::run(dt
, args4
));
141 EXPECT_FALSE(ran_test1
);
142 EXPECT_TRUE(ran_test2
);
143 ran_test1
= ran_test2
= false;
146 TEST(CommandTest
, Usage
)
148 std::string name1
= "test1";
149 std::string usage1
= "first test usage string";
150 std::string name2
= "test2";
151 std::string usage2
= "second test usage string";
153 Command
test1(name1
, 0, 0, do_test1
, usage1
);
154 Command
test2(name2
, 0, 0, do_test2
, usage2
);
156 auto summary
= Command::usageSummary();
158 EXPECT_THAT(summary
, testing::HasSubstr(name1
));
159 EXPECT_THAT(summary
, testing::HasSubstr(usage1
));
161 EXPECT_THAT(summary
, testing::HasSubstr(name2
));
162 EXPECT_THAT(summary
, testing::HasSubstr(usage2
));