-
Notifications
You must be signed in to change notification settings - Fork 3
/
comment_test.go
80 lines (65 loc) · 1.76 KB
/
comment_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
package gqlanalysis_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/vektah/gqlparser/v2/ast"
"github.com/gqlgo/gqlanalysis"
)
func TestReadComments(t *testing.T) {
t.Parallel()
type (
S = []string
I = []int
want struct {
comments S
lines I
cols I
err bool
}
)
cases := map[string]struct {
content string
want want
}{
"empty": {"", want{S{}, I{}, I{}, false}},
"normal": {" # test", want{S{"# test"}, I{1}, I{2}, false}},
"2line": {"\n# test", want{S{"# test"}, I{2}, I{1}, false}},
"2comments": {"# test1\n# test2", want{S{"# test1", "# test2"}, I{1, 2}, I{1, 1}, false}},
"2comments-sameline": {"# test1# test2", want{S{"# test1# test2"}, I{1}, I{1}, false}},
"double-sharp": {"## test1", want{S{"## test1"}, I{1}, I{1}, false}},
}
for name, tt := range cases {
name, tt := name, tt
t.Run(name, func(t *testing.T) {
t.Parallel()
src := &ast.Source{
Name: name,
Input: tt.content,
}
got, err := gqlanalysis.ReadComments(src)
switch {
case !tt.want.err && err != nil:
t.Fatal("unexpected error", err)
case tt.want.err && err == nil:
t.Fatal("the expected error does not occur", err)
}
comments := make([]string, len(got))
lines := make([]int, len(got))
cols := make([]int, len(got))
for i := range got {
comments[i] = got[i].Value
lines[i] = got[i].Pos.Line
cols[i] = got[i].Pos.Column
}
if diff := cmp.Diff(tt.want.comments, comments); diff != "" {
t.Error("comments", diff)
}
if diff := cmp.Diff(tt.want.lines, lines); diff != "" {
t.Error("lines", diff)
}
if diff := cmp.Diff(tt.want.cols, cols); diff != "" {
t.Error("cols", diff)
}
})
}
}