|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 | + |
| 5 | +import 'package:http_parser/src/scan.dart'; |
| 6 | +import 'package:string_scanner/string_scanner.dart'; |
| 7 | +import 'package:test/test.dart'; |
| 8 | + |
| 9 | +void main() { |
| 10 | + group('expectQuotedString', () { |
| 11 | + test('no open quote', () { |
| 12 | + final scanner = StringScanner('test"'); |
| 13 | + expect( |
| 14 | + () => expectQuotedString(scanner), |
| 15 | + throwsA(isA<StringScannerException>() |
| 16 | + .having((e) => e.offset, 'offset', 0) |
| 17 | + .having((e) => e.message, 'message', 'expected quoted string.') |
| 18 | + .having((e) => e.source, 'source', 'test"'))); |
| 19 | + expect(scanner.isDone, isFalse); |
| 20 | + expect(scanner.lastMatch, null); |
| 21 | + expect(scanner.position, 0); |
| 22 | + }); |
| 23 | + |
| 24 | + test('no close quote', () { |
| 25 | + final scanner = StringScanner('"test'); |
| 26 | + expect( |
| 27 | + () => expectQuotedString(scanner), |
| 28 | + throwsA(isA<StringScannerException>() |
| 29 | + .having((e) => e.offset, 'offset', 0) |
| 30 | + .having((e) => e.message, 'message', 'expected quoted string.') |
| 31 | + .having((e) => e.source, 'source', '"test'))); |
| 32 | + expect(scanner.isDone, isFalse); |
| 33 | + expect(scanner.lastMatch, null); |
| 34 | + expect(scanner.position, 0); |
| 35 | + }); |
| 36 | + |
| 37 | + test('simple quoted', () { |
| 38 | + final scanner = StringScanner('"test"'); |
| 39 | + expect(expectQuotedString(scanner), 'test'); |
| 40 | + expect(scanner.isDone, isTrue); |
| 41 | + expect(scanner.lastMatch?.group(0), '"test"'); |
| 42 | + expect(scanner.position, 6); |
| 43 | + }); |
| 44 | + |
| 45 | + test(r'escaped \', () { |
| 46 | + final scanner = StringScanner(r'"escaped: \\"'); |
| 47 | + expect(expectQuotedString(scanner), r'escaped: \'); |
| 48 | + expect(scanner.isDone, isTrue); |
| 49 | + expect(scanner.lastMatch?.group(0), r'"escaped: \\"'); |
| 50 | + expect(scanner.position, 13); |
| 51 | + }); |
| 52 | + |
| 53 | + test(r'bare \', () { |
| 54 | + final scanner = StringScanner(r'"bare: \"'); |
| 55 | + expect( |
| 56 | + () => expectQuotedString(scanner), |
| 57 | + throwsA(isA<StringScannerException>() |
| 58 | + .having((e) => e.offset, 'offset', 0) |
| 59 | + .having((e) => e.message, 'message', 'expected quoted string.') |
| 60 | + .having((e) => e.source, 'source', r'"bare: \"'))); |
| 61 | + expect(scanner.isDone, isFalse); |
| 62 | + expect(scanner.lastMatch, null); |
| 63 | + expect(scanner.position, 0); |
| 64 | + }); |
| 65 | + |
| 66 | + test(r'escaped "', () { |
| 67 | + final scanner = StringScanner(r'"escaped: \""'); |
| 68 | + expect(expectQuotedString(scanner), r'escaped: "'); |
| 69 | + expect(scanner.isDone, isTrue); |
| 70 | + expect(scanner.lastMatch?.group(0), r'"escaped: \""'); |
| 71 | + expect(scanner.position, 13); |
| 72 | + }); |
| 73 | + }); |
| 74 | +} |
0 commit comments