-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
72 lines (61 loc) · 1.68 KB
/
parser_test.go
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
package pipeparser
import "testing"
func TestParse(t *testing.T) {
testingData := []struct {
input string
output []string
}{
{"cat test.txt", []string{"cat", "test.txt"}},
{"ls -l -a", []string{"ls", "-l", "-a"}},
{"ls -la", []string{"ls", "-la"}},
{"awk '{print $1}'", []string{"awk", "{print $1}"}},
{"ps -u test", []string{"ps", "-u", "test"}},
}
for _, data := range testingData {
results := parse(data.input)
for i := 0; i < len(results); i++ {
if results[i] != data.output[i] {
t.Errorf("Unmached result, input: %s, expected: %s, result: %s", data.input, data.output, results)
}
}
}
}
func TestBuild(t *testing.T) {
testingData := []struct {
input string
expectedLen int
}{
{"cat test.txt", 1},
{"ls -l -a", 1},
{"ls -la", 1},
{"awk '{print $1}'", 1},
{"ps -u test", 1},
{"cat test.txt | grep test", 2},
{"cat test.txt | grep test | grep test2", 3},
{"cat test.txt | grep test | grep test2 | awk '{print $1}'", 4},
}
for _, data := range testingData {
results := buildCommands(data.input)
if len(results) != data.expectedLen {
t.Errorf("Unmached number of commands: input: %s, expected: %d, result: %d", data.input, data.expectedLen, len(results))
}
}
}
func TestRun(t *testing.T) {
testingData := []struct {
input string
hasErrors bool
}{
{"notarealcommand", true},
{"ls", false},
}
for _, data := range testingData {
result, err := Run(data.input)
if data.hasErrors && err == nil {
t.Errorf("An error was expected: input: %s, result: %d", data.input, result)
}
if !data.hasErrors && err != nil {
t.Errorf("An error occured: input: %s, result: %d, err: %s", data.input, result, err)
}
}
}