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