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

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

Issue 213833013: Add a string_scanner package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review 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/pubspec.yaml ('k') | pkg/string_scanner/test/string_scanner_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_format_test.dart
diff --git a/pkg/string_scanner/test/error_format_test.dart b/pkg/string_scanner/test/error_format_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..11873440050265c107d10b6b5ccec2130e3c5379
--- /dev/null
+++ b/pkg/string_scanner/test/error_format_test.dart
@@ -0,0 +1,110 @@
+// 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_format_test;
+
+import 'package:string_scanner/string_scanner.dart';
+import 'package:unittest/unittest.dart';
+
+void main() {
+ test('points to the first unconsumed character', () {
+ var scanner = new StringScanner('foo bar baz');
+ scanner.expect('foo ');
+ expect(() => scanner.expect('foo'), throwsFormattedError('''
+Expected "foo" on line 1, column 5.
+foo bar baz
+ ^'''));
+ });
+
+ test('prints the correct line', () {
+ var scanner = new StringScanner('foo bar baz\ndo re mi\nearth fire water');
+ scanner.expect('foo bar baz\ndo ');
+ expect(() => scanner.expect('foo'), throwsFormattedError('''
+Expected "foo" on line 2, column 4.
+do re mi
+ ^'''));
+ });
+
+ test('handles the beginning of the string correctly', () {
+ var scanner = new StringScanner('foo bar baz');
+ expect(() => scanner.expect('zap'), throwsFormattedError('''
+Expected "zap" on line 1, column 1.
+foo bar baz
+^'''));
+ });
+
+ test('handles the end of the string correctly', () {
+ var scanner = new StringScanner('foo bar baz');
+ scanner.expect('foo bar baz');
+ expect(() => scanner.expect('bang'), throwsFormattedError('''
+Expected "bang" on line 1, column 12.
+foo bar baz
+ ^'''));
+ });
+
+ test('handles an empty string correctly', () {
+ expect(() => new StringScanner('').expect('foo'), throwsFormattedError('''
+Expected "foo" on line 1, column 1.
+
+^'''));
+ });
+
+ group("expected name", () {
+ test("uses the provided name", () {
+ expect(() => new StringScanner('').expect('foo bar', name: 'zap'),
+ throwsFormattedError('''
+Expected zap on line 1, column 1.
+
+^'''));
+ });
+
+ test("escapes string quotes", () {
+ expect(() => new StringScanner('').expect('foo"bar'),
+ throwsFormattedError('''
+Expected "foo\\"bar" on line 1, column 1.
+
+^'''));
+ });
+
+ test("escapes string backslashes", () {
+ expect(() => new StringScanner('').expect('foo\\bar'),
+ throwsFormattedError('''
+Expected "foo\\\\bar" on line 1, column 1.
+
+^'''));
+ });
+
+ test("prints PERL-style regexps", () {
+ expect(() => new StringScanner('').expect(new RegExp(r'foo')),
+ throwsFormattedError('''
+Expected /foo/ on line 1, column 1.
+
+^'''));
+ });
+
+ test("escape regexp forward slashes", () {
+ expect(() => new StringScanner('').expect(new RegExp(r'foo/bar')),
+ throwsFormattedError('''
+Expected /foo\\/bar/ on line 1, column 1.
+
+^'''));
+ });
+
+ test("does not escape regexp backslashes", () {
+ expect(() => new StringScanner('').expect(new RegExp(r'foo\bar')),
+ throwsFormattedError('''
+Expected /foo\\bar/ on line 1, column 1.
+
+^'''));
+ });
+ });
+}
+
+Matcher throwsFormattedError(String format) {
+ return throwsA(predicate((error) {
+ expect(error, isFormatException);
+ expect(error.message, equals(format));
+ return true;
+ }));
+}
« no previous file with comments | « pkg/string_scanner/pubspec.yaml ('k') | pkg/string_scanner/test/string_scanner_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698