-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscan_test.go
99 lines (84 loc) · 2.28 KB
/
scan_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
package cppdep
import (
"reflect"
"strings"
"testing"
)
func TestScanner(t *testing.T) {
source :=
`#include <mytest>
#include <stdio.h>
#define MY_CONSTANT 1
#include "localfile.h"
#include "subdir/file.h"`
s := NewScanner(strings.NewReader(source))
var includes []string
var types []int
for s.Scan() {
includes = append(includes, s.Text())
types = append(types, s.Type())
}
expectedIncludes := []string{
"mytest",
"stdio.h",
"localfile.h",
"subdir/file.h",
}
expectedTypes := []int{
BracketIncludeType,
BracketIncludeType,
QuoteIncludeType,
QuoteIncludeType,
}
if !reflect.DeepEqual(includes, expectedIncludes) {
t.Errorf("Include list not as expected.\ngot:%v\nexp:%v\n", includes, expectedIncludes)
}
if !reflect.DeepEqual(types, expectedTypes) {
t.Errorf("types list not as expected.\ngot:%v\nexp:%v\n", types, expectedTypes)
}
}
func TestFastScanner(t *testing.T) {
source :=
`// This is my comment
#include <after_comment>
#include <after_blank_line.h>
#define MY_CONSTANT
#include "after_define.h"
#define MY_FUNC(a,b) \
(a \
+ b)
#include "after_multi_line_define.h"
/* This is
a multi line
comment */
#include "after_multi_line_comment.h"
int myFunc(); // make sure this doesn't count as comment
#include <after_func.h>`
s := NewFastScanner(strings.NewReader(source))
var includes []string
for s.Scan() {
includes = append(includes, s.Text())
}
contains := func(list []string, target string) bool {
for _, item := range list {
if target == item {
return true
}
}
return false
}
switch {
case contains(includes, "after_func.h"):
t.Errorf("Found header after function define")
case !contains(includes, "after_comment"):
t.Error("Failed to find include following a single line comment")
case !contains(includes, "after_blank_line.h"):
t.Error("Failed to find include following an empty line")
case !contains(includes, "after_define.h"):
t.Error("Failed to find include following precompiler statement")
case !contains(includes, "after_multi_line_define.h"):
t.Error("Failed to find include following multi line precompiler statement")
case !contains(includes, "after_multi_line_comment.h"):
t.Error("Failed to find include following multi line comment")
}
}