-
Notifications
You must be signed in to change notification settings - Fork 159
/
parse_include_preprocessor_line_test.go
113 lines (106 loc) · 2.95 KB
/
parse_include_preprocessor_line_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
package preprocessor
import (
"fmt"
"testing"
)
func TestParseIncludePreproccessorLine(t *testing.T) {
testCases := []struct {
inputLine string
out entity
}{
{
inputLine: `# 1 "/usr/include/x86_64-linux-gnu/bits/sys_errlist.h" 1 3 4`,
out: entity{
include: "/usr/include/x86_64-linux-gnu/bits/sys_errlist.h",
positionInSource: 1,
},
},
{
inputLine: `# 26 "/usr/include/x86_64-linux-gnu/bits/sys_errlist.h" 3 4`,
out: entity{
include: "/usr/include/x86_64-linux-gnu/bits/sys_errlist.h",
positionInSource: 26,
},
},
{
inputLine: `# 854 "/usr/include/stdio.h" 2 3 4`,
out: entity{
include: "/usr/include/stdio.h",
positionInSource: 854,
},
},
{
inputLine: `# 2 "f.c" 2`,
out: entity{
include: "f.c",
positionInSource: 2,
},
},
{
inputLine: `# 2 "f.c"`,
out: entity{
include: "f.c",
positionInSource: 2,
},
},
{
inputLine: `# 30 "/usr/lib/llvm-3.8/bin/../lib/clang/3.8.0/include/stdarg.h" 3 4`,
out: entity{
include: "/usr/lib/llvm-3.8/bin/../lib/clang/3.8.0/include/stdarg.h",
positionInSource: 30,
},
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("Test:%d", i), func(t *testing.T) {
actual, err := parseIncludePreprocessorLine(tc.inputLine)
if err != nil {
t.Fatal(err)
}
if len(actual.include) == 0 {
t.Fatal("Cannot parse, because result is empty")
}
if actual.include != tc.out.include {
t.Fatalf("Cannot parse line: \"%s\". Result: \"%s\". Expected: \"%s\"", tc.inputLine, actual.include, tc.out.include)
}
if actual.positionInSource != tc.out.positionInSource {
t.Fatalf("Cannot parse source position in line: \"%s\". Result: \"%d\". Expected: \"%d\"", tc.inputLine, actual.positionInSource, tc.out.positionInSource)
}
})
}
}
func TestParseIncludePreproccessorLineFail1(t *testing.T) {
inputLine := `# A "/usr/include/stdio.h" 2 3 4`
_, err := parseIncludePreprocessorLine(inputLine)
if err == nil {
t.Fatal("Cannot found error in positionInSource")
}
}
func TestParseIncludePreproccessorLineFail2(t *testing.T) {
inputLine := ` # 850 "/usr/include/stdio.h" 2 3 4`
_, err := parseIncludePreprocessorLine(inputLine)
if err == nil {
t.Fatal("Cannot give error if first symbol is not #")
}
}
func TestParseIncludePreproccessorLineFail3(t *testing.T) {
inputLine := `# 850`
_, err := parseIncludePreprocessorLine(inputLine)
if err == nil {
t.Fatal("Cannot give error if line hanen't include string")
}
}
func TestParseIncludePreproccessorLineFail4(t *testing.T) {
inputLine := `# 850 "/usr/include`
_, err := parseIncludePreprocessorLine(inputLine)
if err == nil {
t.Fatal("Cannot give error if wrong format of include line")
}
}
func TestParseIncludePreproccessorLineFail5(t *testing.T) {
inputLine := `# 850`
_, err := parseIncludePreprocessorLine(inputLine)
if err == nil {
t.Fatal("Cannot give error if haven`t include line")
}
}