-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser_test.go
126 lines (115 loc) · 2.99 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
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
package main
import (
"testing"
)
func isRuleEqual(ruleA, ruleB Rule) bool {
if ruleA.target != ruleB.target ||
ruleA.dependencies != ruleB.dependencies ||
ruleA.lineNumber != ruleB.lineNumber ||
(ruleA.commands == nil) != (ruleB.commands == nil) ||
len(ruleA.commands) != len(ruleB.commands) {
return false
}
for i := range ruleA.commands {
if ruleA.commands[i] != ruleB.commands[i] {
return false
}
}
return true
}
func assertRulesEqual(t *testing.T, result, expected []Rule) {
if (result == nil) != (expected == nil) || len(result) != len(expected) {
t.Error("result and expected do not match")
t.Logf("got %v, expected %v", result, expected)
return
}
for i := range result {
if !isRuleEqual(result[i], expected[i]) {
t.Errorf("got %v, expected %v", result[i], expected[i])
}
}
}
func assertParsed(t *testing.T, fileContent []string, expected []Rule) {
content := NewParsedContent("fileName", fileContent)
content.Parse()
assertRulesEqual(t, content.Rules, expected)
}
func TestParserNoDependencies(t *testing.T) {
fileContent := []string{
"target_1:",
"target_2:",
}
expected := []Rule{
{"target_1", "", []string{}, 0},
{"target_2", "", []string{}, 1},
}
assertParsed(t, fileContent, expected)
}
func TestParserWithCommands(t *testing.T) {
fileContent := []string{
"target_1: dependencies_1",
"\tcommand_1",
"\t\tcommand_2",
"",
"\tcommand_3",
"not_command",
"target_2:",
"not_command",
"not_target",
"\tnot_command",
}
expected := []Rule{
{"target_1", "dependencies_1", []string{"command_1", "command_2", "", "command_3"}, 0},
{"target_2", "", []string{}, 6},
}
assertParsed(t, fileContent, expected)
}
func TestParserComments(t *testing.T) {
fileContent := []string{
"#target_1: dependencies_1",
"\t# target_2: dependencies_2",
"target_3: dependencies_3 # comment",
}
expected := []Rule{
{"target_3", "dependencies_3", []string{}, 2},
}
assertParsed(t, fileContent, expected)
}
func TestParserIgnoreCommentInQuotes(t *testing.T) {
fileContent := []string{
"target_1: dependencies_1 \"# in quotes\"",
}
expected := []Rule{
{"target_1", "dependencies_1 \"# in quotes\"", []string{}, 0},
}
assertParsed(t, fileContent, expected)
}
func TestParserMultilineComments(t *testing.T) {
fileContent := []string{
"target_1: dependencies_1 # comment \\",
"target_2: dependencies_2 \\",
"target_3: dependencies_3 \\ comment",
"target_4: dependencies_4",
"# comment \\",
"target_5: dependencies_5",
}
expected := []Rule{
{"target_1", "dependencies_1", []string{}, 0},
{"target_4", "dependencies_4", []string{""}, 3},
}
assertParsed(t, fileContent, expected)
}
func TestParserNotSpecialTarget(t *testing.T) {
targetName := ".phony"
result := isSpecialTarget(targetName)
if result {
t.Errorf("%s detected as special target", targetName)
}
}
func TestParserSpecialTarget(t *testing.T) {
targetName := ".PHONY"
result := isSpecialTarget(targetName)
if !result {
t.Errorf("%s not detected as special target", targetName)
}
}