OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package scanner | 5 package scanner |
6 | 6 |
7 import ( | 7 import ( |
8 "go/token" | 8 "go/token" |
9 "os" | 9 "os" |
| 10 "path/filepath" |
10 "testing" | 11 "testing" |
11 ) | 12 ) |
12 | 13 |
13 | 14 |
14 var fset = token.NewFileSet() | 15 var fset = token.NewFileSet() |
15 | 16 |
16 | 17 |
17 const /* class */ ( | 18 const /* class */ ( |
18 special = iota | 19 special = iota |
19 literal | 20 literal |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 | 437 |
437 // if the input ended in newlines, the input must tokenize the | 438 // if the input ended in newlines, the input must tokenize the |
438 // same with or without those newlines | 439 // same with or without those newlines |
439 for i := len(line) - 1; i >= 0 && line[i] == '\n'; i-- { | 440 for i := len(line) - 1; i >= 0 && line[i] == '\n'; i-- { |
440 checkSemi(t, line[0:i], AllowIllegalChars|InsertSemis) | 441 checkSemi(t, line[0:i], AllowIllegalChars|InsertSemis) |
441 checkSemi(t, line[0:i], AllowIllegalChars|InsertSemis|Sc
anComments) | 442 checkSemi(t, line[0:i], AllowIllegalChars|InsertSemis|Sc
anComments) |
442 } | 443 } |
443 } | 444 } |
444 } | 445 } |
445 | 446 |
446 | |
447 var segments = []struct { | 447 var segments = []struct { |
448 srcline string // a line of source text | 448 srcline string // a line of source text |
449 filename string // filename for current token | 449 filename string // filename for current token |
450 line int // line number for current token | 450 line int // line number for current token |
451 }{ | 451 }{ |
452 // exactly one token per line since the test consumes one token per segm
ent | 452 // exactly one token per line since the test consumes one token per segm
ent |
453 » {" line1", "dir/TestLineComments", 1}, | 453 » {" line1", filepath.Join("dir", "TestLineComments"), 1}, |
454 » {"\nline2", "dir/TestLineComments", 2}, | 454 » {"\nline2", filepath.Join("dir", "TestLineComments"), 2}, |
455 » {"\nline3 //line File1.go:100", "dir/TestLineComments", 3}, // bad line
comment, ignored | 455 » {"\nline3 //line File1.go:100", filepath.Join("dir", "TestLineComments"
), 3}, // bad line comment, ignored |
456 » {"\nline4", "dir/TestLineComments", 4}, | 456 » {"\nline4", filepath.Join("dir", "TestLineComments"), 4}, |
457 » {"\n//line File1.go:100\n line100", "dir/File1.go", 100}, | 457 » {"\n//line File1.go:100\n line100", filepath.Join("dir", "File1.go"), 1
00}, |
458 » {"\n//line File2.go:200\n line200", "dir/File2.go", 200}, | 458 » {"\n//line File2.go:200\n line200", filepath.Join("dir", "File2.go"), 2
00}, |
459 {"\n//line :1\n line1", "dir", 1}, | 459 {"\n//line :1\n line1", "dir", 1}, |
460 » {"\n//line foo:42\n line42", "dir/foo", 42}, | 460 » {"\n//line foo:42\n line42", filepath.Join("dir", "foo"), 42}, |
461 » {"\n //line foo:42\n line44", "dir/foo", 44}, // bad line com
ment, ignored | 461 » {"\n //line foo:42\n line44", filepath.Join("dir", "foo"), 44},
// bad line comment, ignored |
462 » {"\n//line foo 42\n line46", "dir/foo", 46}, // bad line com
ment, ignored | 462 » {"\n//line foo 42\n line46", filepath.Join("dir", "foo"), 46},
// bad line comment, ignored |
463 » {"\n//line foo:42 extra text\n line48", "dir/foo", 48}, // bad line com
ment, ignored | 463 » {"\n//line foo:42 extra text\n line48", filepath.Join("dir", "foo"), 48
}, // bad line comment, ignored |
464 » {"\n//line /bar:42\n line42", "/bar", 42}, | 464 » {"\n//line /bar:42\n line42", string(filepath.Separator) + "bar", 42}, |
465 » {"\n//line ./foo:42\n line42", "dir/foo", 42}, | 465 » {"\n//line ./foo:42\n line42", filepath.Join("dir", "foo"), 42}, |
466 » {"\n//line a/b/c/File1.go:100\n line100", "dir/a/b/c/File1.go", 100}, | 466 » {"\n//line a/b/c/File1.go:100\n line100", filepath.Join("dir", "a", "b"
, "c", "File1.go"), 100}, |
467 } | 467 } |
468 | 468 |
469 | 469 |
470 // Verify that comments of the form "//line filename:line" are interpreted corre
ctly. | 470 // Verify that comments of the form "//line filename:line" are interpreted corre
ctly. |
471 func TestLineComments(t *testing.T) { | 471 func TestLineComments(t *testing.T) { |
472 // make source | 472 // make source |
473 var src string | 473 var src string |
474 for _, e := range segments { | 474 for _, e := range segments { |
475 src += e.srcline | 475 src += e.srcline |
476 } | 476 } |
477 | 477 |
478 // verify scan | 478 // verify scan |
479 var S Scanner | 479 var S Scanner |
480 » file := fset.AddFile("dir/TestLineComments", fset.Base(), len(src)) | 480 » file := fset.AddFile(filepath.Join("dir", "TestLineComments"), fset.Base
(), len(src)) |
481 S.Init(file, []byte(src), nil, 0) | 481 S.Init(file, []byte(src), nil, 0) |
482 for _, s := range segments { | 482 for _, s := range segments { |
483 p, _, lit := S.Scan() | 483 p, _, lit := S.Scan() |
484 pos := file.Position(p) | 484 pos := file.Position(p) |
485 checkPos(t, string(lit), p, token.Position{s.filename, pos.Offse
t, s.line, pos.Column}) | 485 checkPos(t, string(lit), p, token.Position{s.filename, pos.Offse
t, s.line, pos.Column}) |
486 } | 486 } |
487 | 487 |
488 if S.ErrorCount != 0 { | 488 if S.ErrorCount != 0 { |
489 t.Errorf("found %d errors", S.ErrorCount) | 489 t.Errorf("found %d errors", S.ErrorCount) |
490 } | 490 } |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 {"\"abc\x00def\"", token.STRING, 4, "illegal character NUL"}, | 663 {"\"abc\x00def\"", token.STRING, 4, "illegal character NUL"}, |
664 {"\"abc\x80def\"", token.STRING, 4, "illegal UTF-8 encoding"}, | 664 {"\"abc\x80def\"", token.STRING, 4, "illegal UTF-8 encoding"}, |
665 } | 665 } |
666 | 666 |
667 | 667 |
668 func TestScanErrors(t *testing.T) { | 668 func TestScanErrors(t *testing.T) { |
669 for _, e := range errors { | 669 for _, e := range errors { |
670 checkError(t, e.src, e.tok, e.pos, e.err) | 670 checkError(t, e.src, e.tok, e.pos, e.err) |
671 } | 671 } |
672 } | 672 } |
OLD | NEW |