| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 // Copyright (c) 2015, 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:analyzer/analyzer.dart'; | 
|  | 6 import 'package:unittest/unittest.dart'; | 
|  | 7 import 'package:unittest/src/util/string_literal_iterator.dart'; | 
|  | 8 | 
|  | 9 final _offset = "final str = ".length; | 
|  | 10 | 
|  | 11 void main() { | 
|  | 12   group("returns simple characters in", () { | 
|  | 13     test("a single simple string", () { | 
|  | 14       var iter = _parse('"abc"'); | 
|  | 15 | 
|  | 16       expect(iter.current, isNull); | 
|  | 17       expect(iter.offset, equals(_offset)); | 
|  | 18 | 
|  | 19       expect(iter.moveNext(), isTrue); | 
|  | 20       expect(iter.current, _isRune("a")); | 
|  | 21       expect(iter.offset, equals(_offset + 1)); | 
|  | 22 | 
|  | 23       expect(iter.moveNext(), isTrue); | 
|  | 24       expect(iter.current, _isRune("b")); | 
|  | 25       expect(iter.offset, equals(_offset + 2)); | 
|  | 26 | 
|  | 27       expect(iter.moveNext(), isTrue); | 
|  | 28       expect(iter.current, _isRune("c")); | 
|  | 29       expect(iter.offset, equals(_offset + 3)); | 
|  | 30 | 
|  | 31       expect(iter.moveNext(), isFalse); | 
|  | 32       expect(iter.current, isNull); | 
|  | 33       expect(iter.offset, equals(_offset + 4)); | 
|  | 34     }); | 
|  | 35 | 
|  | 36     test("a raw string", () { | 
|  | 37       var iter = _parse('r"abc"'); | 
|  | 38 | 
|  | 39       expect(iter.current, isNull); | 
|  | 40       expect(iter.offset, equals(_offset + 1)); | 
|  | 41 | 
|  | 42       expect(iter.moveNext(), isTrue); | 
|  | 43       expect(iter.current, _isRune("a")); | 
|  | 44       expect(iter.offset, equals(_offset + 2)); | 
|  | 45 | 
|  | 46       expect(iter.moveNext(), isTrue); | 
|  | 47       expect(iter.current, _isRune("b")); | 
|  | 48       expect(iter.offset, equals(_offset + 3)); | 
|  | 49 | 
|  | 50       expect(iter.moveNext(), isTrue); | 
|  | 51       expect(iter.current, _isRune("c")); | 
|  | 52       expect(iter.offset, equals(_offset + 4)); | 
|  | 53 | 
|  | 54       expect(iter.moveNext(), isFalse); | 
|  | 55       expect(iter.current, isNull); | 
|  | 56       expect(iter.offset, equals(_offset + 5)); | 
|  | 57     }); | 
|  | 58 | 
|  | 59     test("a multiline string", () { | 
|  | 60       var iter = _parse('"""ab\ncd"""'); | 
|  | 61 | 
|  | 62       expect(iter.current, isNull); | 
|  | 63       expect(iter.offset, equals(_offset + 2)); | 
|  | 64 | 
|  | 65       expect(iter.moveNext(), isTrue); | 
|  | 66       expect(iter.current, _isRune("a")); | 
|  | 67       expect(iter.offset, equals(_offset + 3)); | 
|  | 68 | 
|  | 69       expect(iter.moveNext(), isTrue); | 
|  | 70       expect(iter.current, _isRune("b")); | 
|  | 71       expect(iter.offset, equals(_offset + 4)); | 
|  | 72 | 
|  | 73       expect(iter.moveNext(), isTrue); | 
|  | 74       expect(iter.current, _isRune("\n")); | 
|  | 75       expect(iter.offset, equals(_offset + 5)); | 
|  | 76 | 
|  | 77       expect(iter.moveNext(), isTrue); | 
|  | 78       expect(iter.current, _isRune("c")); | 
|  | 79       expect(iter.offset, equals(_offset + 6)); | 
|  | 80 | 
|  | 81       expect(iter.moveNext(), isTrue); | 
|  | 82       expect(iter.current, _isRune("d")); | 
|  | 83       expect(iter.offset, equals(_offset + 7)); | 
|  | 84 | 
|  | 85       expect(iter.moveNext(), isFalse); | 
|  | 86       expect(iter.current, isNull); | 
|  | 87       expect(iter.offset, equals(_offset + 8)); | 
|  | 88     }); | 
|  | 89 | 
|  | 90     test("a raw multiline string", () { | 
|  | 91       var iter = _parse('r"""ab\ncd"""'); | 
|  | 92 | 
|  | 93       expect(iter.current, isNull); | 
|  | 94       expect(iter.offset, equals(_offset + 3)); | 
|  | 95 | 
|  | 96       expect(iter.moveNext(), isTrue); | 
|  | 97       expect(iter.current, _isRune("a")); | 
|  | 98       expect(iter.offset, equals(_offset + 4)); | 
|  | 99 | 
|  | 100       expect(iter.moveNext(), isTrue); | 
|  | 101       expect(iter.current, _isRune("b")); | 
|  | 102       expect(iter.offset, equals(_offset + 5)); | 
|  | 103 | 
|  | 104       expect(iter.moveNext(), isTrue); | 
|  | 105       expect(iter.current, _isRune("\n")); | 
|  | 106       expect(iter.offset, equals(_offset + 6)); | 
|  | 107 | 
|  | 108       expect(iter.moveNext(), isTrue); | 
|  | 109       expect(iter.current, _isRune("c")); | 
|  | 110       expect(iter.offset, equals(_offset + 7)); | 
|  | 111 | 
|  | 112       expect(iter.moveNext(), isTrue); | 
|  | 113       expect(iter.current, _isRune("d")); | 
|  | 114       expect(iter.offset, equals(_offset + 8)); | 
|  | 115 | 
|  | 116       expect(iter.moveNext(), isFalse); | 
|  | 117       expect(iter.current, isNull); | 
|  | 118       expect(iter.offset, equals(_offset + 9)); | 
|  | 119     }); | 
|  | 120 | 
|  | 121     test("adjacent strings", () { | 
|  | 122       var iter = _parse('"ab" r"cd" """ef\ngh"""'); | 
|  | 123 | 
|  | 124       expect(iter.current, isNull); | 
|  | 125       expect(iter.offset, equals(_offset)); | 
|  | 126 | 
|  | 127       expect(iter.moveNext(), isTrue); | 
|  | 128       expect(iter.current, _isRune("a")); | 
|  | 129       expect(iter.offset, equals(_offset + 1)); | 
|  | 130 | 
|  | 131       expect(iter.moveNext(), isTrue); | 
|  | 132       expect(iter.current, _isRune("b")); | 
|  | 133       expect(iter.offset, equals(_offset + 2)); | 
|  | 134 | 
|  | 135       expect(iter.moveNext(), isTrue); | 
|  | 136       expect(iter.current, _isRune("c")); | 
|  | 137       expect(iter.offset, equals(_offset + 7)); | 
|  | 138 | 
|  | 139       expect(iter.moveNext(), isTrue); | 
|  | 140       expect(iter.current, _isRune("d")); | 
|  | 141       expect(iter.offset, equals(_offset + 8)); | 
|  | 142 | 
|  | 143       expect(iter.moveNext(), isTrue); | 
|  | 144       expect(iter.current, _isRune("e")); | 
|  | 145       expect(iter.offset, equals(_offset + 14)); | 
|  | 146 | 
|  | 147       expect(iter.moveNext(), isTrue); | 
|  | 148       expect(iter.current, _isRune("f")); | 
|  | 149       expect(iter.offset, equals(_offset + 15)); | 
|  | 150 | 
|  | 151       expect(iter.moveNext(), isTrue); | 
|  | 152       expect(iter.current, _isRune("\n")); | 
|  | 153       expect(iter.offset, equals(_offset + 16)); | 
|  | 154 | 
|  | 155       expect(iter.moveNext(), isTrue); | 
|  | 156       expect(iter.current, _isRune("g")); | 
|  | 157       expect(iter.offset, equals(_offset + 17)); | 
|  | 158 | 
|  | 159       expect(iter.moveNext(), isTrue); | 
|  | 160       expect(iter.current, _isRune("h")); | 
|  | 161       expect(iter.offset, equals(_offset + 18)); | 
|  | 162 | 
|  | 163       expect(iter.moveNext(), isFalse); | 
|  | 164       expect(iter.current, isNull); | 
|  | 165       expect(iter.offset, equals(_offset + 19)); | 
|  | 166     }); | 
|  | 167   }); | 
|  | 168 | 
|  | 169   group("parses an escape sequence for", () { | 
|  | 170     test("a newline", () => _expectEscape(r"\n", "\n")); | 
|  | 171     test("a carriage return", () => _expectEscape(r"\r", "\r")); | 
|  | 172     test("a form feed", () => _expectEscape(r"\f", "\f")); | 
|  | 173     test("a backspace", () => _expectEscape(r"\b", "\b")); | 
|  | 174     test("a tab", () => _expectEscape(r"\t", "\t")); | 
|  | 175     test("a vertical tab", () => _expectEscape(r"\v", "\v")); | 
|  | 176     test("a quote", () => _expectEscape(r'\"', '"')); | 
|  | 177     test("a backslash", () => _expectEscape(r"\\", "\\")); | 
|  | 178 | 
|  | 179     test("a hex character", () { | 
|  | 180       _expectEscape(r"\x62", "b"); | 
|  | 181       _expectEscape(r"\x7A", "z"); | 
|  | 182       _expectEscape(r"\x7a", "z"); | 
|  | 183     }); | 
|  | 184 | 
|  | 185     test("a fixed-length unicode character", | 
|  | 186         () => _expectEscape(r"\u0062", "b")); | 
|  | 187 | 
|  | 188     test("a short variable-length unicode character", | 
|  | 189         () => _expectEscape(r"\u{62}", "b")); | 
|  | 190 | 
|  | 191     test("a long variable-length unicode character", | 
|  | 192         () => _expectEscape(r"\u{000062}", "b")); | 
|  | 193   }); | 
|  | 194 | 
|  | 195   group("throws an ArgumentError for", () { | 
|  | 196     test("interpolation", () { | 
|  | 197       expect(() => _parse(r'"$foo"'), throwsArgumentError); | 
|  | 198     }); | 
|  | 199 | 
|  | 200     test("interpolation in an adjacent string", () { | 
|  | 201       expect(() => _parse(r'"foo" "$bar" "baz"'), throwsArgumentError); | 
|  | 202     }); | 
|  | 203   }); | 
|  | 204 } | 
|  | 205 | 
|  | 206 /// Asserts that [escape] is parsed as [value]. | 
|  | 207 void _expectEscape(String escape, String value) { | 
|  | 208   var iter = _parse('"a${escape}b"'); | 
|  | 209 | 
|  | 210   expect(iter.current, isNull); | 
|  | 211   expect(iter.offset, equals(_offset)); | 
|  | 212 | 
|  | 213   expect(iter.moveNext(), isTrue); | 
|  | 214   expect(iter.current, _isRune("a")); | 
|  | 215   expect(iter.offset, equals(_offset + 1)); | 
|  | 216 | 
|  | 217   expect(iter.moveNext(), isTrue); | 
|  | 218   expect(iter.current, _isRune(value)); | 
|  | 219   expect(iter.offset, equals(_offset + 2)); | 
|  | 220 | 
|  | 221   expect(iter.moveNext(), isTrue); | 
|  | 222   expect(iter.current, _isRune("b")); | 
|  | 223   expect(iter.offset, equals(_offset + escape.length + 2)); | 
|  | 224 | 
|  | 225   expect(iter.moveNext(), isFalse); | 
|  | 226   expect(iter.current, isNull); | 
|  | 227   expect(iter.offset, equals(_offset + escape.length + 3)); | 
|  | 228 } | 
|  | 229 | 
|  | 230 /// Returns a matcher that asserts that the given rune is the rune for [char]. | 
|  | 231 Matcher _isRune(String char) { | 
|  | 232   return predicate((rune) { | 
|  | 233     return rune is int && new String.fromCharCode(rune) == char; | 
|  | 234   }, 'is the rune "$char"'); | 
|  | 235 } | 
|  | 236 | 
|  | 237 /// Parses [dart], which should be a string literal, into a | 
|  | 238 /// [StringLiteralIterator]. | 
|  | 239 StringLiteralIterator _parse(String dart) { | 
|  | 240   var literal = parseCompilationUnit("final str = $dart;") | 
|  | 241       .declarations.single.variables.variables.single.initializer; | 
|  | 242   return new StringLiteralIterator(literal); | 
|  | 243 } | 
| OLD | NEW | 
|---|