util: Add a "command" unit test for the m5 utility.
[gem5.git] / util / m5 / src / command.test.cc
1 /*
2 * Copyright 2020 Google Inc.
3 *
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.
14 *
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.
26 */
27
28 #include <gtest/gtest.h>
29
30 // For EXPECT_THAT and HasSubstr
31 #include <gmock/gmock.h>
32
33 #include "args.hh"
34 #include "command.hh"
35
36 bool ran_test1 = false;
37
38 // A dummy class so we can make a reference to pass around.
39 class DispatchTable {};
40
41 DispatchTable dt;
42
43 void
44 do_test1(const DispatchTable &dt, Args &args)
45 {
46 ran_test1 = true;
47 }
48
49 bool ran_test2 = false;
50
51 void
52 do_test2(const DispatchTable &dt, Args &args)
53 {
54 ran_test2 = true;
55 }
56
57 TEST(CommandTest, OneCommandNoArgs)
58 {
59 Command test1("test1", 0, 0, do_test1, "");
60
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);
66
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);
71
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);
76 ran_test1 = false;
77
78 // Try with no command at all.
79 Args args4({});
80 EXPECT_FALSE(Command::run(dt, args4));
81 EXPECT_FALSE(ran_test1);
82 }
83
84 TEST(CommandTest, OneCommandSomeArgs)
85 {
86 Command test1("test1", 2, 3, do_test1, "");
87
88 // Too few arguments.
89 EXPECT_FALSE(ran_test1);
90 Args args1({ "test1" });
91 EXPECT_FALSE(Command::run(dt, args1));
92 EXPECT_FALSE(ran_test1);
93
94 // Too many arguments.
95 Args args2({ "test1", "arg1", "arg2", "arg3", "arg4" });
96 EXPECT_FALSE(Command::run(dt, args2));
97 EXPECT_FALSE(ran_test1);
98
99 // Just enough arguments.
100 Args args3({ "test1", "arg1", "arg2" });
101 EXPECT_TRUE(Command::run(dt, args3));
102 EXPECT_TRUE(ran_test1);
103 ran_test1 = false;
104
105 // Almost too many arguments.
106 Args args4({ "test1", "arg1", "arg2", "arg3" });
107 EXPECT_TRUE(Command::run(dt, args4));
108 EXPECT_TRUE(ran_test1);
109 ran_test1 = false;
110 }
111
112 TEST(CommandTest, TwoCommands)
113 {
114 Command test1("test1", 0, 0, do_test1, "");
115 Command test2("test2", 1, 1, do_test2, "");
116
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);
122
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);
128
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;
135
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;
142 }
143
144 TEST(CommandTest, Usage)
145 {
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";
150
151 Command test1(name1, 0, 0, do_test1, usage1);
152 Command test2(name2, 0, 0, do_test2, usage2);
153
154 auto summary = Command::usageSummary();
155
156 EXPECT_THAT(summary, testing::HasSubstr(name1));
157 EXPECT_THAT(summary, testing::HasSubstr(usage1));
158
159 EXPECT_THAT(summary, testing::HasSubstr(name2));
160 EXPECT_THAT(summary, testing::HasSubstr(usage2));
161 }