1 |
747 |
jeremybenn |
// Copyright 2010 The Go Authors. All rights reserved.
|
2 |
|
|
// Use of this source code is governed by a BSD-style
|
3 |
|
|
// license that can be found in the LICENSE file.
|
4 |
|
|
|
5 |
|
|
package token
|
6 |
|
|
|
7 |
|
|
import (
|
8 |
|
|
"fmt"
|
9 |
|
|
"testing"
|
10 |
|
|
)
|
11 |
|
|
|
12 |
|
|
func checkPos(t *testing.T, msg string, p, q Position) {
|
13 |
|
|
if p.Filename != q.Filename {
|
14 |
|
|
t.Errorf("%s: expected filename = %q; got %q", msg, q.Filename, p.Filename)
|
15 |
|
|
}
|
16 |
|
|
if p.Offset != q.Offset {
|
17 |
|
|
t.Errorf("%s: expected offset = %d; got %d", msg, q.Offset, p.Offset)
|
18 |
|
|
}
|
19 |
|
|
if p.Line != q.Line {
|
20 |
|
|
t.Errorf("%s: expected line = %d; got %d", msg, q.Line, p.Line)
|
21 |
|
|
}
|
22 |
|
|
if p.Column != q.Column {
|
23 |
|
|
t.Errorf("%s: expected column = %d; got %d", msg, q.Column, p.Column)
|
24 |
|
|
}
|
25 |
|
|
}
|
26 |
|
|
|
27 |
|
|
func TestNoPos(t *testing.T) {
|
28 |
|
|
if NoPos.IsValid() {
|
29 |
|
|
t.Errorf("NoPos should not be valid")
|
30 |
|
|
}
|
31 |
|
|
var fset *FileSet
|
32 |
|
|
checkPos(t, "nil NoPos", fset.Position(NoPos), Position{})
|
33 |
|
|
fset = NewFileSet()
|
34 |
|
|
checkPos(t, "fset NoPos", fset.Position(NoPos), Position{})
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
var tests = []struct {
|
38 |
|
|
filename string
|
39 |
|
|
source []byte // may be nil
|
40 |
|
|
size int
|
41 |
|
|
lines []int
|
42 |
|
|
}{
|
43 |
|
|
{"a", []byte{}, 0, []int{}},
|
44 |
|
|
{"b", []byte("01234"), 5, []int{0}},
|
45 |
|
|
{"c", []byte("\n\n\n\n\n\n\n\n\n"), 9, []int{0, 1, 2, 3, 4, 5, 6, 7, 8}},
|
46 |
|
|
{"d", nil, 100, []int{0, 5, 10, 20, 30, 70, 71, 72, 80, 85, 90, 99}},
|
47 |
|
|
{"e", nil, 777, []int{0, 80, 100, 120, 130, 180, 267, 455, 500, 567, 620}},
|
48 |
|
|
{"f", []byte("package p\n\nimport \"fmt\""), 23, []int{0, 10, 11}},
|
49 |
|
|
{"g", []byte("package p\n\nimport \"fmt\"\n"), 24, []int{0, 10, 11}},
|
50 |
|
|
{"h", []byte("package p\n\nimport \"fmt\"\n "), 25, []int{0, 10, 11, 24}},
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
func linecol(lines []int, offs int) (int, int) {
|
54 |
|
|
prevLineOffs := 0
|
55 |
|
|
for line, lineOffs := range lines {
|
56 |
|
|
if offs < lineOffs {
|
57 |
|
|
return line, offs - prevLineOffs + 1
|
58 |
|
|
}
|
59 |
|
|
prevLineOffs = lineOffs
|
60 |
|
|
}
|
61 |
|
|
return len(lines), offs - prevLineOffs + 1
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
func verifyPositions(t *testing.T, fset *FileSet, f *File, lines []int) {
|
65 |
|
|
for offs := 0; offs < f.Size(); offs++ {
|
66 |
|
|
p := f.Pos(offs)
|
67 |
|
|
offs2 := f.Offset(p)
|
68 |
|
|
if offs2 != offs {
|
69 |
|
|
t.Errorf("%s, Offset: expected offset %d; got %d", f.Name(), offs, offs2)
|
70 |
|
|
}
|
71 |
|
|
line, col := linecol(lines, offs)
|
72 |
|
|
msg := fmt.Sprintf("%s (offs = %d, p = %d)", f.Name(), offs, p)
|
73 |
|
|
checkPos(t, msg, f.Position(f.Pos(offs)), Position{f.Name(), offs, line, col})
|
74 |
|
|
checkPos(t, msg, fset.Position(p), Position{f.Name(), offs, line, col})
|
75 |
|
|
}
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
func makeTestSource(size int, lines []int) []byte {
|
79 |
|
|
src := make([]byte, size)
|
80 |
|
|
for _, offs := range lines {
|
81 |
|
|
if offs > 0 {
|
82 |
|
|
src[offs-1] = '\n'
|
83 |
|
|
}
|
84 |
|
|
}
|
85 |
|
|
return src
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
func TestPositions(t *testing.T) {
|
89 |
|
|
const delta = 7 // a non-zero base offset increment
|
90 |
|
|
fset := NewFileSet()
|
91 |
|
|
for _, test := range tests {
|
92 |
|
|
// verify consistency of test case
|
93 |
|
|
if test.source != nil && len(test.source) != test.size {
|
94 |
|
|
t.Errorf("%s: inconsistent test case: expected file size %d; got %d", test.filename, test.size, len(test.source))
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
// add file and verify name and size
|
98 |
|
|
f := fset.AddFile(test.filename, fset.Base()+delta, test.size)
|
99 |
|
|
if f.Name() != test.filename {
|
100 |
|
|
t.Errorf("expected filename %q; got %q", test.filename, f.Name())
|
101 |
|
|
}
|
102 |
|
|
if f.Size() != test.size {
|
103 |
|
|
t.Errorf("%s: expected file size %d; got %d", f.Name(), test.size, f.Size())
|
104 |
|
|
}
|
105 |
|
|
if fset.File(f.Pos(0)) != f {
|
106 |
|
|
t.Errorf("%s: f.Pos(0) was not found in f", f.Name())
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
// add lines individually and verify all positions
|
110 |
|
|
for i, offset := range test.lines {
|
111 |
|
|
f.AddLine(offset)
|
112 |
|
|
if f.LineCount() != i+1 {
|
113 |
|
|
t.Errorf("%s, AddLine: expected line count %d; got %d", f.Name(), i+1, f.LineCount())
|
114 |
|
|
}
|
115 |
|
|
// adding the same offset again should be ignored
|
116 |
|
|
f.AddLine(offset)
|
117 |
|
|
if f.LineCount() != i+1 {
|
118 |
|
|
t.Errorf("%s, AddLine: expected unchanged line count %d; got %d", f.Name(), i+1, f.LineCount())
|
119 |
|
|
}
|
120 |
|
|
verifyPositions(t, fset, f, test.lines[0:i+1])
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
// add lines with SetLines and verify all positions
|
124 |
|
|
if ok := f.SetLines(test.lines); !ok {
|
125 |
|
|
t.Errorf("%s: SetLines failed", f.Name())
|
126 |
|
|
}
|
127 |
|
|
if f.LineCount() != len(test.lines) {
|
128 |
|
|
t.Errorf("%s, SetLines: expected line count %d; got %d", f.Name(), len(test.lines), f.LineCount())
|
129 |
|
|
}
|
130 |
|
|
verifyPositions(t, fset, f, test.lines)
|
131 |
|
|
|
132 |
|
|
// add lines with SetLinesForContent and verify all positions
|
133 |
|
|
src := test.source
|
134 |
|
|
if src == nil {
|
135 |
|
|
// no test source available - create one from scratch
|
136 |
|
|
src = makeTestSource(test.size, test.lines)
|
137 |
|
|
}
|
138 |
|
|
f.SetLinesForContent(src)
|
139 |
|
|
if f.LineCount() != len(test.lines) {
|
140 |
|
|
t.Errorf("%s, SetLinesForContent: expected line count %d; got %d", f.Name(), len(test.lines), f.LineCount())
|
141 |
|
|
}
|
142 |
|
|
verifyPositions(t, fset, f, test.lines)
|
143 |
|
|
}
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
func TestLineInfo(t *testing.T) {
|
147 |
|
|
fset := NewFileSet()
|
148 |
|
|
f := fset.AddFile("foo", fset.Base(), 500)
|
149 |
|
|
lines := []int{0, 42, 77, 100, 210, 220, 277, 300, 333, 401}
|
150 |
|
|
// add lines individually and provide alternative line information
|
151 |
|
|
for _, offs := range lines {
|
152 |
|
|
f.AddLine(offs)
|
153 |
|
|
f.AddLineInfo(offs, "bar", 42)
|
154 |
|
|
}
|
155 |
|
|
// verify positions for all offsets
|
156 |
|
|
for offs := 0; offs <= f.Size(); offs++ {
|
157 |
|
|
p := f.Pos(offs)
|
158 |
|
|
_, col := linecol(lines, offs)
|
159 |
|
|
msg := fmt.Sprintf("%s (offs = %d, p = %d)", f.Name(), offs, p)
|
160 |
|
|
checkPos(t, msg, f.Position(f.Pos(offs)), Position{"bar", offs, 42, col})
|
161 |
|
|
checkPos(t, msg, fset.Position(p), Position{"bar", offs, 42, col})
|
162 |
|
|
}
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
func TestFiles(t *testing.T) {
|
166 |
|
|
fset := NewFileSet()
|
167 |
|
|
for i, test := range tests {
|
168 |
|
|
fset.AddFile(test.filename, fset.Base(), test.size)
|
169 |
|
|
j := 0
|
170 |
|
|
fset.Iterate(func(f *File) bool {
|
171 |
|
|
if f.Name() != tests[j].filename {
|
172 |
|
|
t.Errorf("expected filename = %s; got %s", tests[j].filename, f.Name())
|
173 |
|
|
}
|
174 |
|
|
j++
|
175 |
|
|
return true
|
176 |
|
|
})
|
177 |
|
|
if j != i+1 {
|
178 |
|
|
t.Errorf("expected %d files; got %d", i+1, j)
|
179 |
|
|
}
|
180 |
|
|
}
|
181 |
|
|
}
|