| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'package:test/test.dart'; | 5 import 'package:test/test.dart'; |
| 6 import 'package:source_span/source_span.dart'; | 6 import 'package:source_span/source_span.dart'; |
| 7 import 'package:source_span/src/colors.dart' as colors; | 7 import 'package:source_span/src/colors.dart' as colors; |
| 8 | 8 |
| 9 main() { | 9 main() { |
| 10 var file; | 10 var file; |
| 11 setUp(() { | 11 setUp(() { |
| 12 file = new SourceFile(""" | 12 file = new SourceFile(""" |
| 13 foo bar baz | 13 foo bar baz |
| 14 whiz bang boom | 14 whiz bang boom |
| 15 zip zap zop | 15 zip zap zop |
| 16 """, url: "foo.dart"); | 16 """); |
| 17 }); | 17 }); |
| 18 | 18 |
| 19 test("points to the span in the source", () { | 19 test("points to the span in the source", () { |
| 20 expect(file.span(4, 7).message("oh no"), equals(""" | 20 expect(file.span(4, 7).highlight(), equals(""" |
| 21 line 1, column 5 of foo.dart: oh no | |
| 22 foo bar baz | 21 foo bar baz |
| 23 ^^^""")); | 22 ^^^""")); |
| 24 }); | 23 }); |
| 25 | 24 |
| 26 test("gracefully handles a missing source URL", () { | 25 test("gracefully handles a missing source URL", () { |
| 27 var span = new SourceFile("foo bar baz").span(4, 7); | 26 var span = new SourceFile("foo bar baz").span(4, 7); |
| 28 expect(span.message("oh no"), equals(""" | 27 expect(span.highlight(), equals(""" |
| 29 line 1, column 5: oh no | |
| 30 foo bar baz | 28 foo bar baz |
| 31 ^^^""")); | 29 ^^^""")); |
| 32 }); | 30 }); |
| 33 | 31 |
| 34 test("highlights the first line of a multiline span", () { | 32 test("highlights the first line of a multiline span", () { |
| 35 expect(file.span(4, 20).message("oh no"), equals(""" | 33 expect(file.span(4, 20).highlight(), equals(""" |
| 36 line 1, column 5 of foo.dart: oh no | |
| 37 foo bar baz | 34 foo bar baz |
| 38 ^^^^^^^^""")); | 35 ^^^^^^^^""")); |
| 39 }); | 36 }); |
| 40 | 37 |
| 41 test("works for a point span", () { | 38 test("works for a point span", () { |
| 42 expect(file.location(4).pointSpan().message("oh no"), equals(""" | 39 expect(file.location(4).pointSpan().highlight(), equals(""" |
| 43 line 1, column 5 of foo.dart: oh no | |
| 44 foo bar baz | 40 foo bar baz |
| 45 ^""")); | 41 ^""")); |
| 46 }); | 42 }); |
| 47 | 43 |
| 48 test("works for a point span at the end of a line", () { | 44 test("works for a point span at the end of a line", () { |
| 49 expect(file.location(11).pointSpan().message("oh no"), equals(""" | 45 expect(file.location(11).pointSpan().highlight(), equals(""" |
| 50 line 1, column 12 of foo.dart: oh no | |
| 51 foo bar baz | 46 foo bar baz |
| 52 ^""")); | 47 ^""")); |
| 53 }); | 48 }); |
| 54 | 49 |
| 55 test("works for a point span at the end of the file", () { | 50 test("works for a point span at the end of the file", () { |
| 56 expect(file.location(38).pointSpan().message("oh no"), equals(""" | 51 expect(file.location(38).pointSpan().highlight(), equals(""" |
| 57 line 3, column 12 of foo.dart: oh no | 52 zip zap zop |
| 53 ^""")); |
| 54 }); |
| 55 |
| 56 test("works for a point span at the end of the file with no trailing newline", |
| 57 () { |
| 58 file = new SourceFile("zip zap zop"); |
| 59 expect(file.location(11).pointSpan().highlight(), equals(""" |
| 58 zip zap zop | 60 zip zap zop |
| 59 ^""")); | 61 ^""")); |
| 60 }); | 62 }); |
| 61 | 63 |
| 62 test("works for a point span in an empty file", () { | 64 test("works for a point span in an empty file", () { |
| 63 expect(new SourceFile("").location(0).pointSpan().message("oh no"), | 65 expect(new SourceFile("").location(0).pointSpan().highlight(), |
| 64 equals(""" | 66 equals(""" |
| 65 line 1, column 1: oh no | |
| 66 | 67 |
| 67 ^""")); | 68 ^""")); |
| 68 }); | 69 }); |
| 69 | 70 |
| 70 test("works for a single-line file without a newline", () { | 71 test("works for a single-line file without a newline", () { |
| 71 expect(new SourceFile("foo bar").span(0, 7).message("oh no"), | 72 expect(new SourceFile("foo bar").span(0, 7).highlight(), |
| 72 equals(""" | 73 equals(""" |
| 73 line 1, column 1: oh no | |
| 74 foo bar | 74 foo bar |
| 75 ^^^^^^^""")); | 75 ^^^^^^^""")); |
| 76 }); | 76 }); |
| 77 | 77 |
| 78 test("emits tabs for tabs", () { |
| 79 expect(new SourceFile(" \t \t\tfoo bar").span(5, 8).highlight(), |
| 80 equals(""" |
| 81 \t \t\tfoo bar |
| 82 \t \t\t^^^""")); |
| 83 }); |
| 84 |
| 85 test("supports lines of preceding context", () { |
| 86 var span = new SourceSpanWithContext( |
| 87 new SourceLocation(5, line: 3, column: 5, sourceUrl: "foo.dart"), |
| 88 new SourceLocation(12, line: 3, column: 12, sourceUrl: "foo.dart"), |
| 89 "foo bar", |
| 90 "previous\nlines\n-----foo bar-----\nfollowing line\n"); |
| 91 |
| 92 expect(span.highlight(color: colors.YELLOW), equals(""" |
| 93 previous |
| 94 lines |
| 95 -----${colors.YELLOW}foo bar${colors.NONE}----- |
| 96 ${colors.YELLOW}^^^^^^^${colors.NONE}""")); |
| 97 }); |
| 98 |
| 78 group("colors", () { | 99 group("colors", () { |
| 79 test("doesn't colorize if color is false", () { | 100 test("doesn't colorize if color is false", () { |
| 80 expect(file.span(4, 7).message("oh no", color: false), equals(""" | 101 expect(file.span(4, 7).highlight(color: false), equals(""" |
| 81 line 1, column 5 of foo.dart: oh no | |
| 82 foo bar baz | 102 foo bar baz |
| 83 ^^^""")); | 103 ^^^""")); |
| 84 }); | 104 }); |
| 85 | 105 |
| 86 test("colorizes if color is true", () { | 106 test("colorizes if color is true", () { |
| 87 expect(file.span(4, 7).message("oh no", color: true), equals(""" | 107 expect(file.span(4, 7).highlight(color: true), equals(""" |
| 88 line 1, column 5 of foo.dart: oh no | |
| 89 foo ${colors.RED}bar${colors.NONE} baz | 108 foo ${colors.RED}bar${colors.NONE} baz |
| 90 ${colors.RED}^^^${colors.NONE}""")); | 109 ${colors.RED}^^^${colors.NONE}""")); |
| 91 }); | 110 }); |
| 92 | 111 |
| 93 test("uses the given color if it's passed", () { | 112 test("uses the given color if it's passed", () { |
| 94 expect(file.span(4, 7).message("oh no", color: colors.YELLOW), equals(""" | 113 expect(file.span(4, 7).highlight(color: colors.YELLOW), equals(""" |
| 95 line 1, column 5 of foo.dart: oh no | |
| 96 foo ${colors.YELLOW}bar${colors.NONE} baz | 114 foo ${colors.YELLOW}bar${colors.NONE} baz |
| 97 ${colors.YELLOW}^^^${colors.NONE}""")); | 115 ${colors.YELLOW}^^^${colors.NONE}""")); |
| 98 }); | 116 }); |
| 99 }); | 117 }); |
| 100 } | 118 } |
| OLD | NEW |