-
Notifications
You must be signed in to change notification settings - Fork 14
/
sourcemap_test.go
136 lines (129 loc) · 3.27 KB
/
sourcemap_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
127
128
129
130
131
132
133
134
135
136
package lua
import (
"testing"
)
var codeExamples = map[string]string{
"single-line": "line1",
"inline": "line1\nline2\nline3",
"inline-trailing": "line1\nline2\nline3\n",
"multi-line": `line1
line2
line3
line4
line6`,
}
type TestConfig struct {
path string
src string
}
type TestCase struct {
line int
expectedPath string
expectedRelativeLine int
expectError bool
}
func TestAffectedSource(t *testing.T) {
tests := map[string]struct {
config []TestConfig
cases []TestCase
}{
"Empty case": {
config: []TestConfig{},
cases: []TestCase{
{1, "", 0, true},
},
},
"Simple case": {
config: []TestConfig{
{"test.src", codeExamples["single-line"]},
},
cases: []TestCase{
{1, "test.src", 1, false},
},
},
"Single inline": {
config: []TestConfig{
{"test1.src", codeExamples["inline"]},
},
cases: []TestCase{
{1, "test1.src", 1, false},
{3, "test1.src", 3, false},
},
},
"Single inline trailing delimiter": {
config: []TestConfig{
{"test1.src", codeExamples["inline-trailing"]},
},
cases: []TestCase{
{1, "test1.src", 1, false},
{3, "test1.src", 3, false},
{4, "", 0, true},
},
},
"Multiple sources of single lines": {
config: []TestConfig{
{"test1.src", codeExamples["single-line"]},
{"test2.src", codeExamples["single-line"]},
},
cases: []TestCase{
{1, "test1.src", 1, false},
{2, "test2.src", 1, false},
},
},
"Multiple sources": {
config: []TestConfig{
{"test1.src", codeExamples["single-line"]},
{"test2.src", codeExamples["single-line"]},
{"test3.src", codeExamples["multi-line"]},
{"test4.src", codeExamples["multi-line"]},
{"test5.src", codeExamples["single-line"]},
},
cases: []TestCase{
{1, "test1.src", 1, false},
{2, "test2.src", 1, false},
{3, "test3.src", 1, false},
{5, "test3.src", 3, false},
{10, "test4.src", 2, false},
{14, "test4.src", 6, false},
{15, "test5.src", 1, false},
{16, "", 0, true},
},
},
"Returns only the affected file name": {
config: []TestConfig{
{"test1.src", codeExamples["single-line"]},
{"./test2.src", codeExamples["single-line"]},
{"/path/test3.src", codeExamples["single-line"]},
{"/path/path2/test4.src", codeExamples["single-line"]},
{"../path2/test5.src", codeExamples["single-line"]},
},
cases: []TestCase{
{1, "test1.src", 1, false},
{2, "test2.src", 1, false},
{3, "test3.src", 1, false},
{4, "test4.src", 1, false},
{5, "test5.src", 1, false},
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
sourceMap := NewSourceMap()
for _, cfg := range test.config {
sourceMap.Append(cfg.path, cfg.src)
}
for _, c := range test.cases {
affectedPath, relativeLine, err := sourceMap.AffectedSource(c.line)
if err != nil && !c.expectError {
t.Errorf("case '%s' return an unexpected error", name)
}
if relativeLine != c.expectedRelativeLine {
t.Errorf("unexpected relative line in '%s', expected=%d, got=%d", name, c.expectedRelativeLine, relativeLine)
}
if affectedPath != c.expectedPath {
t.Errorf("unexpected path in '%s', expected=%s, got=%s", name, c.expectedPath, affectedPath)
}
}
})
}
}