-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
110 lines (99 loc) · 2.83 KB
/
test.cpp
File metadata and controls
110 lines (99 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "EasyCLI.hpp"
#include <gtest/gtest.h>
COMMAND_FUNCTION(multiply) {
CommandOutput out;
try {
int a = std::stoi(args.arguments[0]);
int b = std::stoi(args.arguments[1]);
out.out = std::to_string(a * b);
out.success = true;
} catch (std::exception &e) {
out.out = e.what();
out.success = false;
}
return out;
}
TEST(EasyCliTest, MultiplyTest) {
EasyCLI cli;
cli.RegisterCommand("multiply", multiply);
CommandOutput out = cli.Execute("multiply 2 3");
EXPECT_EQ(out.out, "6");
EXPECT_TRUE(out.success);
}
COMMAND_FUNCTION(echo) {
CommandOutput out;
try {
for (auto &arg : args.arguments) {
out.out += arg + " ";
}
out.out.pop_back();
out.success = true;
} catch (std::exception &e) {
out.out = e.what();
out.success = false;
}
return out;
}
TEST(EasyCliTest, EchoTest) {
EasyCLI cli;
cli.RegisterCommand("echo", echo);
CommandOutput out = cli.Execute("echo Hello World");
EXPECT_EQ(out.out, "Hello World");
EXPECT_TRUE(out.success);
}
COMMAND_FUNCTION(greet) {
CommandOutput out;
try {
out.out = "Hello, " + args.arguments[0] + "!";
out.success = true;
} catch (std::exception &e) {
out.out = e.what();
out.success = false;
}
return out;
}
TEST(EasyCliTest, GreetTest) {
EasyCLI cli;
cli.RegisterCommand("greet", greet);
CommandOutput out = cli.Execute("greet World");
EXPECT_EQ(out.out, "Hello, World!");
EXPECT_TRUE(out.success);
}
COMMAND_FUNCTION(flag) {
CommandOutput out;
try {
if (args.flags_contains("flag")) {
out.out = "Flag is set";
} else {
out.out = "Flag is not set";
}
} catch (std::exception &e) {
out.out = e.what();
out.success = false;
}
return out;
}
TEST(EasyCliTest, FlagTest) {
EasyCLI cli;
cli.RegisterCommand("flag", flag);
CommandOutput out = cli.Execute("flag -flag");
EXPECT_EQ(out.out, "Flag is set");
EXPECT_TRUE(out.success);
}
TEST(EasyCliTest, GetCommandsStringTest) {
EasyCLI cli;
cli.RegisterCommand("greet", greet);
cli.RegisterCommand("echo", echo);
cli.RegisterCommand("multiply", multiply);
std::vector<std::string> commands = cli.GetCommandsString();
bool contains_echo = std::find(commands.begin(), commands.end(), "echo") != commands.end();
bool contains_greet = std::find(commands.begin(), commands.end(), "greet") != commands.end();
bool contains_multiply = std::find(commands.begin(), commands.end(), "multiply") != commands.end();
EXPECT_TRUE(contains_echo);
EXPECT_TRUE(contains_greet);
EXPECT_TRUE(contains_multiply);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}