-
Notifications
You must be signed in to change notification settings - Fork 47
/
parser_test.c
49 lines (45 loc) · 1.91 KB
/
parser_test.c
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
#include <stddef.h>
#include "../user/wsh/parse.h"
#include <assert.h>
#include <string.h>
void test_given_cmdline_when_4_pipes_should_return_4(){
struct cmdLine cmd;
int ret = parse("ls | grep | wc | cat", &cmd);
assert(ret == 0);
assert(cmd.numCommands == 4);
assert(strcmp(cmd.argv[cmd.cmdStart[0]], "ls") == 0);
assert(strcmp(cmd.argv[cmd.cmdStart[1]], "grep") == 0);
assert(strcmp(cmd.argv[cmd.cmdStart[2]], "wc") == 0);
assert(strcmp(cmd.argv[cmd.cmdStart[3]], "cat") == 0);
}
void test_given_cmdline_when_args_4_pipes_should_return_4(){
struct cmdLine cmd;
int ret = parse("ls -lah bin | grep snake | wc | cat", &cmd);
assert(ret == 0);
assert(cmd.numCommands == 4);
assert(strcmp(cmd.argv[cmd.cmdStart[0]], "ls") == 0);
assert(strcmp(cmd.argv[cmd.cmdStart[1]], "grep") == 0);
assert(strcmp(cmd.argv[cmd.cmdStart[2]], "wc") == 0);
assert(strcmp(cmd.argv[cmd.cmdStart[3]], "cat") == 0);
}
void test_given_cmdline_when_args_5_pipes_should_return_5(){
struct cmdLine cmd;
int ret = parse("ls -lah bin | grep snake | wc -l | cat | cat", &cmd);
assert(ret == 0);
assert(cmd.numCommands == 5);
assert(strcmp(cmd.argv[cmd.cmdStart[0]], "ls") == 0);
assert(strcmp(cmd.argv[cmd.cmdStart[1]], "grep") == 0);
assert(strcmp(cmd.argv[cmd.cmdStart[2]], "wc") == 0);
assert(strcmp(cmd.argv[cmd.cmdStart[3]], "cat") == 0);
assert(strcmp(cmd.argv[cmd.cmdStart[4]], "cat") == 0);
}
void test_given_cmdline_when_line_break_should_return_3(){
struct cmdLine cmd;
int ret = parse("echo abc\\\\ndef\\\\nghn | tail -n 2 | grep def", &cmd);
assert(ret == 0);
assert(cmd.numCommands == 3);
assert(strcmp(cmd.argv[cmd.cmdStart[0]], "echo") == 0);
assert(strcmp(cmd.argv[cmd.cmdStart[0] + 1], "abc\\\\ndef\\\\nghn") == 0);
assert(strcmp(cmd.argv[cmd.cmdStart[1]], "tail") == 0);
assert(strcmp(cmd.argv[cmd.cmdStart[2]], "grep") == 0);
}