Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(903)

Unified Diff: pkg/string_scanner/test/error_test.dart

Issue 222843003: Convert shelf to use the string_scanner package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/string_scanner/test/error_format_test.dart ('k') | pkg/string_scanner/test/expect_error_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/string_scanner/test/error_test.dart
diff --git a/pkg/string_scanner/test/error_test.dart b/pkg/string_scanner/test/error_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..6432197b3a0792ed080c3af3078bc4d477d625b2
--- /dev/null
+++ b/pkg/string_scanner/test/error_test.dart
@@ -0,0 +1,168 @@
+// 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.
+
+library string_scanner.error_test;
+
+import 'package:string_scanner/string_scanner.dart';
+import 'package:unittest/unittest.dart';
+
+import 'utils.dart';
+
+void main() {
+ test('defaults to the last match', () {
+ var scanner = new StringScanner('foo bar baz');
+ scanner.expect('foo ');
+ scanner.expect('bar');
+ expect(() => scanner.error('oh no!'), throwsFormattedError('''
+Error on line 1, column 5: oh no!
+foo bar baz
+ ^^^'''));
+ });
+
+ group("with match", () {
+ test('supports an earlier match', () {
+ var scanner = new StringScanner('foo bar baz');
+ scanner.expect('foo ');
+ var match = scanner.lastMatch;
+ scanner.expect('bar');
+ expect(() => scanner.error('oh no!', match: match),
+ throwsFormattedError('''
+Error on line 1, column 1: oh no!
+foo bar baz
+^^^^'''));
+ });
+
+ test('supports a match on a previous line', () {
+ var scanner = new StringScanner('foo bar baz\ndo re mi\nearth fire water');
+ scanner.expect('foo bar baz\ndo ');
+ scanner.expect('re');
+ var match = scanner.lastMatch;
+ scanner.expect(' mi\nearth ');
+ expect(() => scanner.error('oh no!', match: match),
+ throwsFormattedError('''
+Error on line 2, column 4: oh no!
+do re mi
+ ^^'''));
+ });
+
+ test('supports a multiline match', () {
+ var scanner = new StringScanner('foo bar baz\ndo re mi\nearth fire water');
+ scanner.expect('foo bar ');
+ scanner.expect('baz\ndo');
+ var match = scanner.lastMatch;
+ scanner.expect(' re mi');
+ expect(() => scanner.error('oh no!', match: match),
+ throwsFormattedError('''
+Error on line 1, column 9: oh no!
+foo bar baz
+ ^^^'''));
+ });
+
+ test('supports a match after position', () {
+ var scanner = new StringScanner('foo bar baz');
+ scanner.expect('foo ');
+ scanner.expect('bar');
+ var match = scanner.lastMatch;
+ scanner.position = 0;
+ expect(() => scanner.error('oh no!', match: match),
+ throwsFormattedError('''
+Error on line 1, column 5: oh no!
+foo bar baz
+ ^^^'''));
+ });
+ });
+
+ group("with position and/or length", () {
+ test('defaults to length 1', () {
+ var scanner = new StringScanner('foo bar baz');
+ scanner.expect('foo ');
+ expect(() => scanner.error('oh no!', position: 1),
+ throwsFormattedError('''
+Error on line 1, column 2: oh no!
+foo bar baz
+ ^'''));
+ });
+
+ test('defaults to the current position', () {
+ var scanner = new StringScanner('foo bar baz');
+ scanner.expect('foo ');
+ expect(() => scanner.error('oh no!', length: 3),
+ throwsFormattedError('''
+Error on line 1, column 5: oh no!
+foo bar baz
+ ^^^'''));
+ });
+
+ test('supports an earlier position', () {
+ var scanner = new StringScanner('foo bar baz');
+ scanner.expect('foo ');
+ expect(() => scanner.error('oh no!', position: 1, length: 2),
+ throwsFormattedError('''
+Error on line 1, column 2: oh no!
+foo bar baz
+ ^^'''));
+ });
+
+ test('supports a position on a previous line', () {
+ var scanner = new StringScanner('foo bar baz\ndo re mi\nearth fire water');
+ scanner.expect('foo bar baz\ndo re mi\nearth');
+ expect(() => scanner.error('oh no!', position: 15, length: 2),
+ throwsFormattedError('''
+Error on line 2, column 4: oh no!
+do re mi
+ ^^'''));
+ });
+
+ test('supports a multiline length', () {
+ var scanner = new StringScanner('foo bar baz\ndo re mi\nearth fire water');
+ scanner.expect('foo bar baz\ndo re mi\nearth');
+ expect(() => scanner.error('oh no!', position: 8, length: 8),
+ throwsFormattedError('''
+Error on line 1, column 9: oh no!
+foo bar baz
+ ^^^'''));
+ });
+
+ test('supports a position after the current one', () {
+ var scanner = new StringScanner('foo bar baz');
+ expect(() => scanner.error('oh no!', position: 4, length: 3),
+ throwsFormattedError('''
+Error on line 1, column 5: oh no!
+foo bar baz
+ ^^^'''));
+ });
+ });
+
+ group("argument errors", () {
+ var scanner;
+ setUp(() {
+ scanner = new StringScanner('foo bar baz');
+ scanner.scan('foo');
+ });
+
+ test("if match is passed with position", () {
+ expect(
+ () => scanner.error("oh no!", match: scanner.lastMatch, position: 1),
+ throwsArgumentError);
+ });
+
+ test("if match is passed with length", () {
+ expect(
+ () => scanner.error("oh no!", match: scanner.lastMatch, length: 1),
+ throwsArgumentError);
+ });
+
+ test("if position is negative", () {
+ expect(() => scanner.error("oh no!", position: -1), throwsArgumentError);
+ });
+
+ test("if position is outside the string", () {
+ expect(() => scanner.error("oh no!", position: 100), throwsArgumentError);
+ });
+
+ test("if length is zero", () {
+ expect(() => scanner.error("oh no!", length: 0), throwsArgumentError);
+ });
+ });
+}
« no previous file with comments | « pkg/string_scanner/test/error_format_test.dart ('k') | pkg/string_scanner/test/expect_error_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698