@@ -294,3 +294,55 @@ func TestReferenceInfoAddOverload(t *testing.T) {
294294 t .Error ("repeated AddOverload() did not produce equal references" )
295295 }
296296}
297+
298+ func TestNewSourceInfoRelative (t * testing.T ) {
299+ sourceInfo := ast .NewSourceInfo (
300+ mockRelativeSource (t ,
301+ "\n \n a || b ?\n cond1 :\n cond2" ,
302+ []int32 {1 , 2 , 13 , 25 },
303+ common .NewLocation (2 , 1 )))
304+ tests := []struct {
305+ loc common.Location
306+ offset int32
307+ }{
308+ // All locations specify a line number starting at 1
309+ // The location of line 2, offset 1 is the same as the
310+ // relative offset at location 1, 0 (offset 2)
311+ {loc : common .NewLocation (1 , 0 ), offset : 2 },
312+ // Equivalent to line 3, column 4
313+ {loc : common .NewLocation (2 , 3 ), offset : 6 },
314+ // Equivalent to line 4, column 2
315+ {loc : common .NewLocation (3 , 1 ), offset : 15 },
316+ }
317+ for _ , tst := range tests {
318+ gotOffset := sourceInfo .ComputeOffset (int32 (tst .loc .Line ()), int32 (tst .loc .Column ()))
319+ if gotOffset != tst .offset {
320+ t .Errorf ("ComputeOffset() got %v, wanted %v" , gotOffset , tst .offset )
321+ }
322+ }
323+ }
324+
325+ func mockRelativeSource (t testing.TB , text string , lineOffsets []int32 , baseLocation common.Location ) common.Source {
326+ t .Helper ()
327+ return & mockSource {
328+ Source : common .NewTextSource (text ),
329+ lineOffsets : lineOffsets ,
330+ baseLocation : baseLocation }
331+ }
332+
333+ type mockSource struct {
334+ common.Source
335+ lineOffsets []int32
336+ baseLocation common.Location
337+ }
338+
339+ func (src * mockSource ) LineOffsets () []int32 {
340+ return src .lineOffsets
341+ }
342+
343+ func (src * mockSource ) OffsetLocation (offset int32 ) (common.Location , bool ) {
344+ if offset == 0 {
345+ return src .baseLocation , true
346+ }
347+ return src .Source .OffsetLocation (offset )
348+ }
0 commit comments