1
0
mirror of https://github.com/golang/go synced 2024-11-18 21:24:44 -07:00
go/internal/span/token_test.go
Heschi Kreinick 761dbfd69d internal/span: support line directives
When //line directives are in play, the ast.File's Offset function will
return offsets in the generated file. We want offsets in the authored
file, so we need to pass a Converter for the authored file, in addition
to the ast.File for the generated file. For the same reason, we have to
start (Range).Span() by translating into positions in the authored file,
then calculate offsets from that.

A lot of call sites outside of the LSP don't pass the Converter, but
they probably don't matter much. I think everything inside does because
it ends up using mappedRange.

Updates golang/go#35720.

Change-Id: I7be09b3a50720b078e862d48cfdb02208f8187ae
Reviewed-on: https://go-review.googlesource.com/c/tools/+/208501
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-11-25 19:20:43 +00:00

82 lines
1.9 KiB
Go

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package span_test
import (
"fmt"
"go/token"
"path"
"testing"
"golang.org/x/tools/internal/span"
)
var testdata = []struct {
uri string
content []byte
}{
{"/a.go", []byte(`
// file a.go
package test
`)},
{"/b.go", []byte(`
//
//
// file b.go
package test`)},
{"/c.go", []byte(`
// file c.go
package test`)},
}
var tokenTests = []span.Span{
span.New(span.FileURI("/a.go"), span.NewPoint(1, 1, 0), span.Point{}),
span.New(span.FileURI("/a.go"), span.NewPoint(3, 7, 20), span.NewPoint(3, 7, 20)),
span.New(span.FileURI("/b.go"), span.NewPoint(4, 9, 15), span.NewPoint(4, 13, 19)),
span.New(span.FileURI("/c.go"), span.NewPoint(4, 1, 26), span.Point{}),
}
func TestToken(t *testing.T) {
fset := token.NewFileSet()
files := map[span.URI]*token.File{}
for _, f := range testdata {
file := fset.AddFile(f.uri, -1, len(f.content))
file.SetLinesForContent(f.content)
files[span.FileURI(f.uri)] = file
}
for _, test := range tokenTests {
f := files[test.URI()]
c := span.NewTokenConverter(fset, f)
t.Run(path.Base(f.Name()), func(t *testing.T) {
checkToken(t, c, span.New(
test.URI(),
span.NewPoint(test.Start().Line(), test.Start().Column(), 0),
span.NewPoint(test.End().Line(), test.End().Column(), 0),
), test)
checkToken(t, c, span.New(
test.URI(),
span.NewPoint(0, 0, test.Start().Offset()),
span.NewPoint(0, 0, test.End().Offset()),
), test)
})
}
}
func checkToken(t *testing.T, c *span.TokenConverter, in, expect span.Span) {
rng, err := in.Range(c)
if err != nil {
t.Error(err)
}
gotLoc, err := rng.Span()
if err != nil {
t.Error(err)
}
expected := fmt.Sprintf("%+v", expect)
got := fmt.Sprintf("%+v", gotLoc)
if expected != got {
t.Errorf("For %v expected %q got %q", in, expected, got)
}
}