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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
package gopls
import (
"testing"
)
func TestSourceMap_RoundTrip(t *testing.T) {
type tc struct {
mapping Mapping
tuiLine int
tuiCol int
wantLine int
wantCol int
}
tests := map[string]tc{
"exact start of mapping": {
mapping: Mapping{TuiLine: 5, TuiCol: 10, GoLine: 20, GoCol: 4, Length: 8},
tuiLine: 5,
tuiCol: 10,
wantLine: 5,
wantCol: 10,
},
"middle of mapping": {
mapping: Mapping{TuiLine: 5, TuiCol: 10, GoLine: 20, GoCol: 4, Length: 8},
tuiLine: 5,
tuiCol: 14,
wantLine: 5,
wantCol: 14,
},
"end of mapping": {
mapping: Mapping{TuiLine: 5, TuiCol: 10, GoLine: 20, GoCol: 4, Length: 8},
tuiLine: 5,
tuiCol: 18,
wantLine: 5,
wantCol: 18,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
sm := NewSourceMap()
sm.AddMapping(tt.mapping)
goLine, goCol, found := sm.TuiToGo(tt.tuiLine, tt.tuiCol)
if !found {
t.Fatalf("TuiToGo(%d, %d) not found", tt.tuiLine, tt.tuiCol)
}
tuiLine, tuiCol, found := sm.GoToTui(goLine, goCol)
if !found {
t.Fatalf("GoToTui(%d, %d) not found", goLine, goCol)
}
if tuiLine != tt.wantLine || tuiCol != tt.wantCol {
t.Errorf("round-trip: got (%d, %d), want (%d, %d)", tuiLine, tuiCol, tt.wantLine, tt.wantCol)
}
})
}
}
func TestSourceMap_TuiToGo(t *testing.T) {
type tc struct {
mappings []Mapping
tuiLine int
tuiCol int
wantLine int
wantCol int
wantOk bool
}
tests := map[string]tc{
"position within mapping": {
mappings: []Mapping{{TuiLine: 3, TuiCol: 5, GoLine: 10, GoCol: 2, Length: 6}},
tuiLine: 3,
tuiCol: 8,
wantLine: 10,
wantCol: 5,
wantOk: true,
},
"position outside mapping": {
mappings: []Mapping{{TuiLine: 3, TuiCol: 5, GoLine: 10, GoCol: 2, Length: 6}},
tuiLine: 3,
tuiCol: 0,
wantLine: 3,
wantCol: 0,
wantOk: false,
},
"wrong line": {
mappings: []Mapping{{TuiLine: 3, TuiCol: 5, GoLine: 10, GoCol: 2, Length: 6}},
tuiLine: 4,
tuiCol: 5,
wantLine: 4,
wantCol: 5,
wantOk: false,
},
"multiple mappings": {
mappings: []Mapping{
{TuiLine: 3, TuiCol: 5, GoLine: 10, GoCol: 2, Length: 6},
{TuiLine: 5, TuiCol: 10, GoLine: 15, GoCol: 8, Length: 4},
},
tuiLine: 5,
tuiCol: 12,
wantLine: 15,
wantCol: 10,
wantOk: true,
},
"empty source map": {
mappings: nil,
tuiLine: 0,
tuiCol: 0,
wantLine: 0,
wantCol: 0,
wantOk: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
sm := NewSourceMap()
for _, m := range tt.mappings {
sm.AddMapping(m)
}
goLine, goCol, found := sm.TuiToGo(tt.tuiLine, tt.tuiCol)
if found != tt.wantOk {
t.Fatalf("TuiToGo found = %v, want %v", found, tt.wantOk)
}
if goLine != tt.wantLine || goCol != tt.wantCol {
t.Errorf("TuiToGo = (%d, %d), want (%d, %d)", goLine, goCol, tt.wantLine, tt.wantCol)
}
})
}
}
func TestSourceMap_GoToTui(t *testing.T) {
type tc struct {
mappings []Mapping
goLine int
goCol int
wantLine int
wantCol int
wantOk bool
}
tests := map[string]tc{
"position within mapping": {
mappings: []Mapping{{TuiLine: 3, TuiCol: 5, GoLine: 10, GoCol: 2, Length: 6}},
goLine: 10,
goCol: 4,
wantLine: 3,
wantCol: 7,
wantOk: true,
},
"position outside mapping": {
mappings: []Mapping{{TuiLine: 3, TuiCol: 5, GoLine: 10, GoCol: 2, Length: 6}},
goLine: 10,
goCol: 0,
wantLine: 10,
wantCol: 0,
wantOk: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
sm := NewSourceMap()
for _, m := range tt.mappings {
sm.AddMapping(m)
}
tuiLine, tuiCol, found := sm.GoToTui(tt.goLine, tt.goCol)
if found != tt.wantOk {
t.Fatalf("GoToTui found = %v, want %v", found, tt.wantOk)
}
if tuiLine != tt.wantLine || tuiCol != tt.wantCol {
t.Errorf("GoToTui = (%d, %d), want (%d, %d)", tuiLine, tuiCol, tt.wantLine, tt.wantCol)
}
})
}
}
func TestSourceMap_Operations(t *testing.T) {
sm := NewSourceMap()
if sm.Len() != 0 {
t.Errorf("new source map Len() = %d, want 0", sm.Len())
}
sm.AddMapping(Mapping{TuiLine: 1, TuiCol: 0, GoLine: 5, GoCol: 0, Length: 10})
sm.AddMapping(Mapping{TuiLine: 2, TuiCol: 0, GoLine: 6, GoCol: 0, Length: 8})
if sm.Len() != 2 {
t.Errorf("after adding 2 mappings, Len() = %d, want 2", sm.Len())
}
if !sm.IsInGoExpression(1, 5) {
t.Error("IsInGoExpression(1, 5) should be true")
}
if sm.IsInGoExpression(3, 0) {
t.Error("IsInGoExpression(3, 0) should be false")
}
sm.Clear()
if sm.Len() != 0 {
t.Errorf("after Clear(), Len() = %d, want 0", sm.Len())
}
}
|