-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.cpp
139 lines (115 loc) · 3.34 KB
/
tests.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "crash.cpp"
#include "command.cpp"
#include <gtest/gtest.h>
#include <vector>
#include <string>
#include <map>
#define ENV_PATH "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
extern char ** environ;
namespace {
class Test_Crash : public ::testing::Test {
protected:
string input;
string prompt;
crash * cr;
command * cmd;
vector<string> args;
map<string, string> * myAliasMap;
Test_Crash() {
}
virtual ~Test_Crash() {
}
virtual void SetUp() {
input = "ls -l -a";
prompt = "Enter a command >";
myAliasMap = new map<string, string>;
cr = new crash(input, prompt, environ, myAliasMap);
args = {"-l", "-a"};
cmd = new command("ls", args, ENV_PATH);
}
virtual void TearDown() {
}
};
TEST_F(Test_Crash, SplitInput) {
vector<string> splitInput = command::split(input, ' ');
ASSERT_EQ(splitInput[0], "ls");
ASSERT_EQ(splitInput[1], "-l");
ASSERT_EQ(splitInput[2], "-a");
}
TEST_F(Test_Crash, FindPathEnvironmentVariable) {
cr->findPATH();
ASSERT_EQ(cr->path, ENV_PATH);
}
TEST_F(Test_Crash, SplitEnvironmentPath) {
cr->findPATH();
vector<string> paths = command::split(cr->path, ':');
ASSERT_EQ(paths[0], "/usr/local/sbin");
ASSERT_EQ(paths[5], "/bin");
}
TEST_F(Test_Crash, cmdExists) {
ASSERT_TRUE(cmd->cmdExists("/bin/"));
}
TEST_F(Test_Crash, findCmdPath) {
cr->findPATH();
string expected = "/bin/ls";
cmd->findCmdPath();
string result = cmd->cmdPath;
ASSERT_EQ(expected, result);
}
TEST_F(Test_Crash, badCommandCleanExit) {
args.clear();
command * cmd_bad = new command("lssss", args, ENV_PATH);
string expected = "";
string result = cmd_bad->findCmdPath();
ASSERT_EQ(expected, result);
}
TEST_F(Test_Crash, splitCanHandleMissingTokens) {
vector<string> splitInput = command::split(input, '|');
ASSERT_EQ(splitInput[0], "ls -l -a");
string input2 = "ls -l -a | wc -w";
splitInput = command::split(input2, '|');
ASSERT_EQ(splitInput[0], "ls -l -a ");
ASSERT_EQ(splitInput[1], " wc -w");
}
TEST_F(Test_Crash, cleanArgs) {
args = {"$HOME", "tmp/*.txt", "tmp/nope.*"};
command * cmd_cleanArgs = new command("echo", args, ENV_PATH);
cmd_cleanArgs->cleanArgs();
int expected = 1;
int result = cmd_cleanArgs->args.size();
ASSERT_EQ(expected, result);
}
TEST_F(Test_Crash, globExpand) {
args = {"tmp/*.txt", "tmp/nope.*"};
string expected1 = "tmp/nope.txt";
string expected2 = "tmp/test.txt";
command * cmd_glob = new command("touch", args, ENV_PATH);
cmd_glob->globExpand();
string result1 = cmd_glob->args[1];
string result2 = cmd_glob->args[2];
ASSERT_EQ(expected1, result1);
ASSERT_EQ(expected2, result2);
}
TEST_F(Test_Crash, createAlias) {
ASSERT_EQ(0, cr->aliasMap->size());
string ret = cr->createAlias("dir=ls");
string ret1 = cr->createAlias("ff=ls");
ASSERT_EQ("dir", ret);
ASSERT_EQ("ff", ret1);
ASSERT_EQ(2, cr->aliasMap->size());
string expected = "ls";
string result = cr->aliasMap->at("dir");
ASSERT_EQ(expected, result);
ASSERT_EQ("ls", cr->aliasMap->at("ff"));
}
TEST_F(Test_Crash, getAlias) {
string ret = cr->createAlias("ff=ls");
string expected = "ls";
string result = cr->getAlias("ff");
ASSERT_EQ(expected, result);
}
} // Namespace
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}