Chromium Code Reviews| 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 library js_ast.string_escape_test; | |
| 6 | |
| 7 import 'package:js_ast/js_ast.dart'; | |
| 8 import 'package:js_ast/src/characters.dart'; | |
| 9 import 'package:unittest/unittest.dart'; | |
| 10 | |
| 11 const int $LCURLY = $OPEN_CURLY_BRACKET; | |
| 12 const int $RCURLY = $CLOSE_CURLY_BRACKET; | |
| 13 | |
| 14 void main() { | |
| 15 check(input, expected, {ascii: false, utf8: false}) { | |
| 16 if (input is List) input = new String.fromCharCodes(input); | |
| 17 String actual = js.escapedString(input, ascii: ascii, utf8: utf8).value; | |
| 18 if (expected is List) { | |
| 19 expect(actual.codeUnits, expected); | |
| 20 } else { | |
| 21 expect(actual, expected); | |
| 22 } | |
| 23 } | |
| 24 | |
| 25 test('simple', () { | |
| 26 check('', [$DQ, $DQ]); | |
| 27 check('a', [$DQ, $a, $DQ]); | |
| 28 }); | |
| 29 | |
| 30 test('simple-escapes', () { | |
| 31 check([$BS], [$DQ, $BACKSLASH, $b, $DQ]); | |
| 32 check([$BS], [$DQ, $BACKSLASH, $b, $DQ], ascii: true); | |
| 33 check([$BS], [$DQ, $BACKSLASH, $b, $DQ], utf8: true); | |
| 34 | |
| 35 check([$LF], [$DQ, $BACKSLASH, $n, $DQ]); | |
| 36 check([$LF], [$DQ, $BACKSLASH, $n, $DQ], ascii: true); | |
| 37 check([$LF], [$DQ, $BACKSLASH, $n, $DQ], utf8: true); | |
| 38 | |
| 39 check([$FF], [$DQ, $FF, $DQ]); | |
| 40 check([$FF], [$DQ, $BACKSLASH, $f, $DQ], ascii: true); | |
| 41 check([$FF], [$DQ, $BACKSLASH, $f, $DQ], utf8: true); | |
| 42 | |
| 43 check([$CR], [$DQ, $BACKSLASH, $r, $DQ]); | |
| 44 check([$CR], [$DQ, $BACKSLASH, $r, $DQ], ascii: true); | |
| 45 check([$CR], [$DQ, $BACKSLASH, $r, $DQ], utf8: true); | |
| 46 | |
| 47 check([$TAB], [$DQ, $BACKSLASH, $t, $DQ]); | |
| 48 check([$TAB], [$DQ, $BACKSLASH, $t, $DQ], ascii: true); | |
| 49 check([$TAB], [$DQ, $BACKSLASH, $t, $DQ], utf8: true); | |
| 50 | |
| 51 check([$VTAB], [$DQ, $BACKSLASH, $v, $DQ]); | |
| 52 check([$VTAB], [$DQ, $BACKSLASH, $v, $DQ], ascii: true); | |
| 53 check([$VTAB], [$DQ, $BACKSLASH, $v, $DQ], utf8: true); | |
| 54 }); | |
| 55 | |
| 56 test('unnamed-control-codes-escapes', () { | |
| 57 check([0, 1, 2, 3], [$DQ, 0, 1, 2, 3, $DQ]); | |
| 58 check([0, 1, 2, 3], r'''"\x00\x01\x02\x03"''', ascii: true); | |
| 59 check([0, 1, 2, 3], [$DQ, 0, 1, 2, 3, $DQ], utf8: true); | |
| 60 }); | |
| 61 | |
| 62 | |
| 63 test('line-separator', () { | |
| 64 // Legacy escaper is broken. | |
| 65 // check([$LS], [$DQ, $BACKSLASH, $u, $2, $0, $2, $8, $DQ]); | |
| 66 check([$LS], [$DQ, $BACKSLASH, $u, $2, $0, $2, $8, $DQ], ascii: true); | |
| 67 check([$LS], [$DQ, $BACKSLASH, $u, $2, $0, $2, $8, $DQ], utf8: true); | |
| 68 }); | |
| 69 | |
| 70 test('page-separator', () { | |
| 71 // Legacy escaper is broken. | |
| 72 // check([$PS], [$DQ, $BACKSLASH, $u, $2, $0, $2, $9, $DQ]); | |
| 73 check([$PS], [$DQ, $BACKSLASH, $u, $2, $0, $2, $9, $DQ], ascii: true); | |
| 74 check([$PS], [$DQ, $BACKSLASH, $u, $2, $0, $2, $9, $DQ], utf8: true); | |
| 75 }); | |
| 76 | |
| 77 test('legacy-escaper-is-broken', () { | |
| 78 check([$LS], [$DQ, 0x2028, $DQ]); | |
| 79 check([$PS], [$DQ, 0x2029, $DQ]); | |
| 80 }); | |
| 81 | |
| 82 test('choose-quotes', () { | |
| 83 check('\'', [$DQ, $SQ, $DQ]); | |
| 84 check('"', [$SQ, $DQ, $SQ], ascii: true); | |
| 85 check("'", [$DQ, $SQ, $DQ], ascii: true); | |
| 86 // Legacy always double-quotes | |
| 87 check([$DQ, $DQ, $SQ], | |
| 88 [$DQ, $BACKSLASH, $DQ, $BACKSLASH, $DQ, $SQ, $DQ]); | |
| 89 // Using single quotes saves us one backslash: | |
| 90 check([$DQ, $DQ, $SQ], | |
| 91 [$SQ, $DQ, $DQ, $BACKSLASH, $SQ, $SQ], | |
| 92 ascii: true); | |
| 93 check([$DQ, $SQ, $SQ], | |
| 94 [$DQ, $BACKSLASH, $DQ, $SQ, $SQ, $DQ], | |
| 95 ascii: true); | |
| 96 }); | |
| 97 | |
| 98 test('u1234', () { | |
| 99 check('\u1234', [$DQ, 0x1234, $DQ]); | |
| 100 check('\u1234', [$DQ, $BACKSLASH, $u, $1, $2, $3, $4, $DQ], ascii: true); | |
| 101 check('\u1234', [$DQ, 0x1234, $DQ], utf8: true); | |
| 102 }); | |
| 103 | |
| 104 test('u12345', () { | |
| 105 check([0x12345], [$DQ, 55304, 57157, $DQ]); | |
| 106 // TODO: ES6 option: | |
| 107 //check([0x12345], | |
| 108 // [$DQ, $BACKSLASH, $u, $LCURLY, $1, $2, $3, $4, $5, $RCURLY, $DQ], | |
| 109 // ascii: true); | |
| 110 check([0x12345], r'''"\ud808\udf45"''', ascii: true); | |
| 111 check([0x12345], | |
| 112 [$DQ, $BACKSLASH, $u, $d, $8, $0, $8, | |
| 113 $BACKSLASH, $u, $d, $f, $4, $5, $DQ], | |
| 114 ascii: true); | |
| 115 check([0x12345], [$DQ, 55304, 57157, $DQ], utf8: true); | |
| 116 }); | |
| 117 | |
| 118 test('unpaired-surrogate', () { | |
| 119 // (0xD834, 0xDD1E) = 0x1D11E | |
| 120 // Strings containing unpaired surrogates must be encoded to prevent | |
| 121 // problems with the utf8 file-level encoding. | |
| 122 check([0xD834], [$DQ, 0xD834, $DQ]); // Legacy escapedString broken. | |
| 123 check([0xD834], [$DQ, $BACKSLASH, $u, $d, $8, $3, $4, $DQ], ascii: true); | |
| 124 check([0xD834], [$DQ, $BACKSLASH, $u, $d, $8, $3, $4, $DQ], utf8: true); | |
| 125 | |
| 126 check([0xDD1E], [$DQ, 0xDD1E, $DQ]); // Legacy escapedString broken. | |
| 127 check([0xDD1E], [$DQ, $BACKSLASH, $u, $d, $d, $1, $e, $DQ], ascii: true); | |
| 128 check([0xDD1E], [$DQ, $BACKSLASH, $u, $d, $d, $1, $e, $DQ], utf8: true); | |
| 129 | |
| 130 check([0xD834, $A], [$DQ, 0xD834, $A, $DQ]); // Legacy escapedString brok en. | |
|
Siggi Cherem (dart-lang)
2015/12/12 01:43:05
nit: long line
| |
| 131 check([0xD834, $A], | |
| 132 [$DQ, $BACKSLASH, $u, $d, $8, $3, $4, $A, $DQ], | |
| 133 ascii: true); | |
| 134 check([0xD834, $A], | |
| 135 [$DQ, $BACKSLASH, $u, $d, $8, $3, $4, $A, $DQ], | |
| 136 utf8: true); | |
| 137 | |
| 138 check([0xD834, 0xDD1E], [$DQ, 0xD834, 0xDD1E, $DQ]); // Legacy ok. | |
| 139 check([0xD834, 0xDD1E], | |
| 140 [$DQ, | |
| 141 $BACKSLASH, $u, $d, $8, $3, $4, | |
| 142 $BACKSLASH, $u, $d, $d, $1, $e, | |
| 143 $DQ], | |
| 144 ascii: true); | |
| 145 check([0xD834, 0xDD1E], r'''"\ud834\udd1e"''', ascii: true); | |
| 146 check([0xD834, 0xDD1E], [$DQ, 0xD834, 0xDD1E, $DQ], utf8: true); | |
| 147 }); | |
| 148 } | |
| OLD | NEW |