2019-02-12 11:13:49 -07:00
|
|
|
// 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"
|
|
|
|
"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
|
2019-10-07 11:52:07 -06:00
|
|
|
package test`)},
|
|
|
|
{"/c.go", []byte(`
|
|
|
|
// file c.go
|
|
|
|
package test`)},
|
2019-02-12 11:13:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var tokenTests = []span.Span{
|
2019-03-15 11:19:43 -06:00
|
|
|
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)),
|
2019-10-07 11:52:07 -06:00
|
|
|
span.New(span.FileURI("/c.go"), span.NewPoint(4, 1, 26), span.Point{}),
|
2019-02-12 11:13:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-03-15 11:19:43 -06:00
|
|
|
f := files[test.URI()]
|
2019-02-12 11:13:49 -07:00
|
|
|
c := span.NewTokenConverter(fset, f)
|
2019-03-15 11:19:43 -06:00
|
|
|
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)
|
2019-02-12 11:13:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkToken(t *testing.T, c *span.TokenConverter, in, expect span.Span) {
|
2019-03-15 11:19:43 -06:00
|
|
|
rng, err := in.Range(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
gotLoc, err := rng.Span()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-02-12 11:13:49 -07:00
|
|
|
expected := fmt.Sprintf("%+v", expect)
|
|
|
|
got := fmt.Sprintf("%+v", gotLoc)
|
|
|
|
if expected != got {
|
2019-10-07 11:52:07 -06:00
|
|
|
t.Errorf("For %v expected %q got %q", in, expected, got)
|
2019-02-12 11:13:49 -07:00
|
|
|
}
|
|
|
|
}
|