"io/ioutil"
"os"
"path/filepath"
+ "runtime"
"testing"
)
func checkPos(t *testing.T, lit string, p token.Pos, expected token.Position) {
pos := fset.Position(p)
- if pos.Filename != expected.Filename {
+ // Check cleaned filenames so that we don't have to worry about
+ // different os.PathSeparator values.
+ if pos.Filename != expected.Filename && filepath.Clean(pos.Filename) != filepath.Clean(expected.Filename) {
t.Errorf("bad filename for %q: got %s, expected %s", lit, pos.Filename, expected.Filename)
}
if pos.Offset != expected.Offset {
{"\n //line foo:42\n line43", "foo\t", 44, 0}, // bad line comment, ignored (use existing, prior filename)
{"\n//line foo 42\n line44", "foo\t", 46, 0}, // bad line comment, ignored (use existing, prior filename)
{"\n//line /bar:42\n line45", "/bar", 42, 0},
- {"\n//line ./foo:42\n line46", "./foo", 42, 0},
+ {"\n//line ./foo:42\n line46", "foo", 42, 0},
{"\n//line a/b/c/File1.go:100\n line100", "a/b/c/File1.go", 100, 0},
{"\n//line c:\\bar:42\n line200", "c:\\bar", 42, 0},
{"\n//line c:\\dir\\File1.go:100\n line201", "c:\\dir\\File1.go", 100, 0},
{"\n/*line foo:100:10*/\n\nf2", "foo", 102, 1}, // absolute column since on new line
}
+var dirsegments = []segment{
+ // exactly one token per line since the test consumes one token per segment
+ {" line1", "TestLineDir/TestLineDirectives", 1, 3},
+ {"\n//line File1.go:100\n line100", "TestLineDir/File1.go", 100, 0},
+}
+
+var dirUnixSegments = []segment{
+ {"\n//line /bar:42\n line42", "/bar", 42, 0},
+}
+
+var dirWindowsSegments = []segment{
+ {"\n//line c:\\bar:42\n line42", "c:\\bar", 42, 0},
+}
+
// Verify that line directives are interpreted correctly.
func TestLineDirectives(t *testing.T) {
- // make source
+ testSegments(t, segments, "TestLineDirectives")
+ testSegments(t, dirsegments, "TestLineDir/TestLineDirectives")
+ if runtime.GOOS == "windows" {
+ testSegments(t, dirWindowsSegments, "TestLineDir/TestLineDirectives")
+ } else {
+ testSegments(t, dirUnixSegments, "TestLineDir/TestLineDirectives")
+ }
+}
+
+func testSegments(t *testing.T, segments []segment, filename string) {
var src string
for _, e := range segments {
src += e.srcline
// verify scan
var S Scanner
- file := fset.AddFile("TestLineDirectives", fset.Base(), len(src))
+ file := fset.AddFile(filename, fset.Base(), len(src))
S.Init(file, []byte(src), func(pos token.Position, msg string) { t.Error(Error{pos, msg}) }, dontInsertSemis)
for _, s := range segments {
p, _, lit := S.Scan()