Files
sdk/pkg/source_span/test/file_message_test.dart
T
nweiz@google.com 22d2778e00 Extract out a source_span package from source_maps.
This is just the first step; future CLs will add support for the new API to
various packages currently using the old one.

BUG=19930
R=sigmund@google.com

Review URL: https://codereview.chromium.org//381363002

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@38360 260f80e4-7a28-3924-810f-c04153c831b5
2014-07-18 00:51:40 +00:00

101 lines
2.7 KiB
Dart

// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:unittest/unittest.dart';
import 'package:source_span/source_span.dart';
import 'package:source_span/src/colors.dart' as colors;
main() {
var file;
setUp(() {
file = new SourceFile("""
foo bar baz
whiz bang boom
zip zap zop
""", url: "foo.dart");
});
test("points to the span in the source", () {
expect(file.span(4, 7).message("oh no"), equals("""
line 1, column 5 of foo.dart: oh no
foo bar baz
^^^"""));
});
test("gracefully handles a missing source URL", () {
var span = new SourceFile("foo bar baz").span(4, 7);
expect(span.message("oh no"), equals("""
line 1, column 5: oh no
foo bar baz
^^^"""));
});
test("highlights the first line of a multiline span", () {
expect(file.span(4, 20).message("oh no"), equals("""
line 1, column 5 of foo.dart: oh no
foo bar baz
^^^^^^^^"""));
});
test("works for a point span", () {
expect(file.location(4).pointSpan().message("oh no"), equals("""
line 1, column 5 of foo.dart: oh no
foo bar baz
^"""));
});
test("works for a point span at the end of a line", () {
expect(file.location(11).pointSpan().message("oh no"), equals("""
line 1, column 12 of foo.dart: oh no
foo bar baz
^"""));
});
test("works for a point span at the end of the file", () {
expect(file.location(38).pointSpan().message("oh no"), equals("""
line 3, column 12 of foo.dart: oh no
zip zap zop
^"""));
});
test("works for a point span in an empty file", () {
expect(new SourceFile("").location(0).pointSpan().message("oh no"),
equals("""
line 1, column 1: oh no
^"""));
});
test("works for a single-line file without a newline", () {
expect(new SourceFile("foo bar").span(0, 7).message("oh no"),
equals("""
line 1, column 1: oh no
foo bar
^^^^^^^"""));
});
group("colors", () {
test("doesn't colorize if color is false", () {
expect(file.span(4, 7).message("oh no", color: false), equals("""
line 1, column 5 of foo.dart: oh no
foo bar baz
^^^"""));
});
test("colorizes if color is true", () {
expect(file.span(4, 7).message("oh no", color: true), equals("""
line 1, column 5 of foo.dart: oh no
foo ${colors.RED}bar${colors.NONE} baz
${colors.RED}^^^${colors.NONE}"""));
});
test("uses the given color if it's passed", () {
expect(file.span(4, 7).message("oh no", color: colors.YELLOW), equals("""
line 1, column 5 of foo.dart: oh no
foo ${colors.YELLOW}bar${colors.NONE} baz
${colors.YELLOW}^^^${colors.NONE}"""));
});
});
}