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

Side by Side Diff: frog/tests/leg_only/src/StringEscapeTest.dart

Issue 9190038: Handle escapes in string literals. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add dependency on characters.dart to mini_parser.dart. Created 8 years, 11 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
ahe 2012/01/17 13:21:39 This comment is so last year.
Lasse Reichstein Nielsen 2012/01/18 08:22:34 Fixed.
2
ahe 2012/01/17 13:21:39 Extra line.
Lasse Reichstein Nielsen 2012/01/18 08:22:34 Cut.
3 // for details. All rights reserved. Use of this source code is governed by a
4 // BSD-style license that can be found in the LICENSE file.
5
6 // Test that string escapes work correctly.
ahe 2012/01/17 13:21:39 Move this test to language?
Lasse Reichstein Nielsen 2012/01/18 08:22:34 Let's keep it here until we can tests for characte
7
8 testSingleCharacterEscapes() {
9 List/*<String>*/ examples = [
10 "\b\f\n\r\t\v",
11 '\b\f\n\r\t\v',
12 """\b\f\n\r\t\v""",
13 '''\b\f\n\r\t\v''',
14 ];
15 List values = [8, 12, 10, 13, 9, 11];
16 for (String s in examples) {
17 Expect.equals(6, s.length);
18 for (int i = 0; i < 6; i++) {
19 Expect.equals(values[i], s.charCodeAt(i));
20 }
21 }
22 }
23
24 testXEscapes() {
25 var allBytes =
26 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" +
27 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
28 "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" +
29 "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" +
30 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" +
31 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f" +
32 "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" +
33 "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f" +
34 "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" +
35 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" +
36 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" +
37 "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" +
38 "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" +
39 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" +
40 "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" +
41 "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff";
42 Expect.equals(256, allBytes.length);
43 for (int i = 0; i < 256; i++) {
44 Expect.equals(i, allBytes.charCodeAt(i));
45 }
46 }
47
48 testUEscapes() {
49 List/*String*/ examples =
50 ["\u0000\u0001\u0022\u0027\u005c\u007f\u0080\u00ff" +
51 "\u0100\u1000\ud7ff\ue000\uffff",
52 '\u0000\u0001\u0022\u0027\u005c\u007f\u0080\u00ff' +
53 '\u0100\u1000\ud7ff\ue000\uffff',
54 """\u0000\u0001\u0022\u0027\u005c\u007f\u0080\u00ff""" +
55 """\u0100\u1000\ud7ff\ue000\uffff""",
56 '''\u0000\u0001\u0022\u0027\u005c\u007f\u0080\u00ff''' +
57 '''\u0100\u1000\ud7ff\ue000\uffff'''];
58 List/*<int>*/ values = [0, 1, 0x22, 0x27, 0x5c, 0x7f, 0x80, 0xff,
59 0x100, 0x1000, 0xd7ff, 0xe000, 0xffff];
60 for (String s in examples) {
61 Expect.equals(values.length, s.length);
62 for (int i = 0; i < values.length; i++) {
63 Expect.equals(values[i], s.charCodeAt(i));
64 }
65 }
66 // No characters above 0xffff until we support that.
67 var long =
68 "\u{0}\u{00}\u{000}\u{0000}\u{00000}\u{000000}\u{0000000}" +
69 "\u{1}\u{01}\u{001}\u{00001}" +
70 "\u{ffff}\u{0ffff}\u{00ffff}\u{000ffff}";
71 var longValues = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
72 0xffff, 0xffff, 0xffff, 0xffff];
73 Expect.equals(longValues.length, long.length);
74 for (int i = 0; i < longValues.length; i++) {
75 Expect.equals(longValues[i], long.charCodeAt(i));
76 }
77 }
78
79 testIdentityEscapes() {
80 // All non-control ASCII characters escaped, except those with special
81 // meaning: b, f, n, r, t, u, v, and x (replaced by \x00).
82 var asciiLiterals =
83 "\ \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\0\1\2\3\4\5\6\7\8\9\:\;\<\=\>" +
84 "\?\@\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z\[\\\]" +
85 "\^\_\`\a\x00\c\d\e\x00\g\h\i\j\k\l\m\x00\o\p\q\x00\s\x00\x00\x00" +
86 "\w\x00\y\z\{\|\}\~\";
87
88 Expect.equals(128 - 32, asciiLiterals.length);
89 for (int i = 32; i < 128; i++) {
90 int code = asciiLiterals.charCodeAt(i - 32);
91 if (code != 0) {
92 Expect.equals(i, code);
93 }
94 }
95 }
96
97
98 testQuotes() {
99 // The string [ "' ].
100 String bothQuotes = ' "' + "' ";
101 Expect.equals(bothQuotes, " \"' ");
102 Expect.equals(bothQuotes, ' "\' ');
103 Expect.equals(bothQuotes, """ "' """);
104 Expect.equals(bothQuotes, ''' "' ''');
105 Expect.equals(bothQuotes, @""" "' """);
106 Expect.equals(bothQuotes, @''' "' ''');
107 }
108
109
110 testRawStrings() {
111 String raw1 = @'\x00';
112 Expect.equals(4, raw1.length);
113 Expect.equals(0x5c, raw1.charCodeAt(0));
114 }
115
116 main() {
117 // Test \x??.
118 testXEscapes();
119 // Test \u???? and \u{?+}.
120 testUEscapes();
121 // Test \b, \f, \n, \r, \t, \v.
122 testSingleCharacterEscapes();
123 // Test all other single character (identity) escaeps.
124 testIdentityEscapes();
125 // Test that quotes are handled correctly.
126 testQuotes();
127 // Test that raw strings are raw.
128 testRawStrings();
129 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698