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
)
49 bool ran_test2
= false;
52 do_test2(const DispatchTable
&dt
, Args
&args
)
57 TEST(CommandTest
, OneCommandNoArgs
)
59 Command
test1("test1", 0, 0, do_test1
, "");
61 // Try to run the command with an extra argument, expecting it to fail.
62 EXPECT_FALSE(ran_test1
);
63 Args
args1({ "test1", "extra" });
64 EXPECT_FALSE(Command::run(dt
, args1
));
65 EXPECT_FALSE(ran_test1
);
67 // Try to run with an unrecognized command.
68 Args
args2({ "bad_command" });
69 EXPECT_FALSE(Command::run(dt
, args2
));
70 EXPECT_FALSE(ran_test1
);
72 // Run with the right name and number of arguments.
73 Args
args3({ "test1" });
74 EXPECT_TRUE(Command::run(dt
, args3
));
75 EXPECT_TRUE(ran_test1
);
78 // Try with no command at all.
80 EXPECT_FALSE(Command::run(dt
, args4
));
81 EXPECT_FALSE(ran_test1
);
84 TEST(CommandTest
, OneCommandSomeArgs
)
86 Command
test1("test1", 2, 3, do_test1
, "");
89 EXPECT_FALSE(ran_test1
);
90 Args
args1({ "test1" });
91 EXPECT_FALSE(Command::run(dt
, args1
));
92 EXPECT_FALSE(ran_test1
);
94 // Too many arguments.
95 Args
args2({ "test1", "arg1", "arg2", "arg3", "arg4" });
96 EXPECT_FALSE(Command::run(dt
, args2
));
97 EXPECT_FALSE(ran_test1
);
99 // Just enough arguments.
100 Args
args3({ "test1", "arg1", "arg2" });
101 EXPECT_TRUE(Command::run(dt
, args3
));
102 EXPECT_TRUE(ran_test1
);
105 // Almost too many arguments.
106 Args
args4({ "test1", "arg1", "arg2", "arg3" });
107 EXPECT_TRUE(Command::run(dt
, args4
));
108 EXPECT_TRUE(ran_test1
);
112 TEST(CommandTest
, TwoCommands
)
114 Command
test1("test1", 0, 0, do_test1
, "");
115 Command
test2("test2", 1, 1, do_test2
, "");
117 // Try a bad command name.
118 Args
args1({ "bad_command" });
119 EXPECT_FALSE(Command::run(dt
, args1
));
120 EXPECT_FALSE(ran_test1
);
121 EXPECT_FALSE(ran_test2
);
123 // Try the right command with the wrong number of arguments.
124 Args
args2({ "test1", "arg1" });
125 EXPECT_FALSE(Command::run(dt
, args2
));
126 EXPECT_FALSE(ran_test1
);
127 EXPECT_FALSE(ran_test2
);
129 // Run the first command.
130 Args
args3({ "test1" });
131 EXPECT_TRUE(Command::run(dt
, args3
));
132 EXPECT_TRUE(ran_test1
);
133 EXPECT_FALSE(ran_test2
);
134 ran_test1
= ran_test2
= false;
136 // Run the second command.
137 Args
args4({ "test2", "arg1" });
138 EXPECT_TRUE(Command::run(dt
, args4
));
139 EXPECT_FALSE(ran_test1
);
140 EXPECT_TRUE(ran_test2
);
141 ran_test1
= ran_test2
= false;
144 TEST(CommandTest
, Usage
)
146 std::string name1
= "test1";
147 std::string usage1
= "first test usage string";
148 std::string name2
= "test2";
149 std::string usage2
= "second test usage string";
151 Command
test1(name1
, 0, 0, do_test1
, usage1
);
152 Command
test2(name2
, 0, 0, do_test2
, usage2
);
154 auto summary
= Command::usageSummary();
156 EXPECT_THAT(summary
, testing::HasSubstr(name1
));
157 EXPECT_THAT(summary
, testing::HasSubstr(usage1
));
159 EXPECT_THAT(summary
, testing::HasSubstr(name2
));
160 EXPECT_THAT(summary
, testing::HasSubstr(usage2
));