util: Add stub unit tests for the call types in 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 bool
44 do_test1(const DispatchTable &dt, Args &args)
45 {
46 ran_test1 = true;
47 return true;
48 }
49
50 bool ran_test2 = false;
51
52 bool
53 do_test2(const DispatchTable &dt, Args &args)
54 {
55 ran_test2 = true;
56 return true;
57 }
58
59 TEST(CommandTest, OneCommandNoArgs)
60 {
61 Command test1("test1", 0, 0, do_test1, "");
62
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);
68
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);
73
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);
78 ran_test1 = false;
79
80 // Try with no command at all.
81 Args args4({});
82 EXPECT_FALSE(Command::run(dt, args4));
83 EXPECT_FALSE(ran_test1);
84 }
85
86 TEST(CommandTest, OneCommandSomeArgs)
87 {
88 Command test1("test1", 2, 3, do_test1, "");
89
90 // Too few arguments.
91 EXPECT_FALSE(ran_test1);
92 Args args1({ "test1" });
93 EXPECT_FALSE(Command::run(dt, args1));
94 EXPECT_FALSE(ran_test1);
95
96 // Too many arguments.
97 Args args2({ "test1", "arg1", "arg2", "arg3", "arg4" });
98 EXPECT_FALSE(Command::run(dt, args2));
99 EXPECT_FALSE(ran_test1);
100
101 // Just enough arguments.
102 Args args3({ "test1", "arg1", "arg2" });
103 EXPECT_TRUE(Command::run(dt, args3));
104 EXPECT_TRUE(ran_test1);
105 ran_test1 = false;
106
107 // Almost too many arguments.
108 Args args4({ "test1", "arg1", "arg2", "arg3" });
109 EXPECT_TRUE(Command::run(dt, args4));
110 EXPECT_TRUE(ran_test1);
111 ran_test1 = false;
112 }
113
114 TEST(CommandTest, TwoCommands)
115 {
116 Command test1("test1", 0, 0, do_test1, "");
117 Command test2("test2", 1, 1, do_test2, "");
118
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);
124
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);
130
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;
137
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;
144 }
145
146 TEST(CommandTest, Usage)
147 {
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";
152
153 Command test1(name1, 0, 0, do_test1, usage1);
154 Command test2(name2, 0, 0, do_test2, usage2);
155
156 auto summary = Command::usageSummary();
157
158 EXPECT_THAT(summary, testing::HasSubstr(name1));
159 EXPECT_THAT(summary, testing::HasSubstr(usage1));
160
161 EXPECT_THAT(summary, testing::HasSubstr(name2));
162 EXPECT_THAT(summary, testing::HasSubstr(usage2));
163 }