Index: tests/lib/convert/json_utf8_chunk_test.dart |
diff --git a/tests/lib/convert/json_utf8_chunk_test.dart b/tests/lib/convert/json_utf8_chunk_test.dart |
index efa8593df5cadcc3f129917846790b113fee2117..fda36aba40b648a54a0e7dfb132a9b8e3e879265 100644 |
--- a/tests/lib/convert/json_utf8_chunk_test.dart |
+++ b/tests/lib/convert/json_utf8_chunk_test.dart |
@@ -32,8 +32,8 @@ void jsonParse(testName, check, action, [bool allowMalformed = false]) { |
var value = values[0]; |
check(value); |
}); |
- var decoderSink = JSON.decoder.startChunkedConversion(sink) |
- .asUtf8Sink(allowMalformed); |
+ var decoderSink = |
+ JSON.decoder.startChunkedConversion(sink).asUtf8Sink(allowMalformed); |
try { |
action(decoderSink); |
} on FormatException catch (e, s) { |
@@ -46,11 +46,11 @@ void testStrings() { |
// String literal containing characters, all escape types, |
// and a number of UTF-8 encoded characters. |
var s = r'"abc\f\ndef\r\t\b\"\/\\\u0001\u9999\uffff' |
- '\x7f\xc2\x80\xdf\xbf\xe0\xa0\x80\xef\xbf\xbf' |
- '\xf0\x90\x80\x80\xf4\x8f\xbf\xbf"'; // UTF-8. |
+ '\x7f\xc2\x80\xdf\xbf\xe0\xa0\x80\xef\xbf\xbf' |
+ '\xf0\x90\x80\x80\xf4\x8f\xbf\xbf"'; // UTF-8. |
var expected = "abc\f\ndef\r\t\b\"\/\\\u0001\u9999\uffff" |
- "\x7f\x80\u07ff\u0800\uffff" |
- "\u{10000}\u{10ffff}"; |
+ "\x7f\x80\u07ff\u0800\uffff" |
+ "\u{10000}\u{10ffff}"; |
for (var i = 1; i < s.length - 1; i++) { |
var s1 = s.substring(0, i); |
var s2 = s.substring(i); |
@@ -77,10 +77,17 @@ void testStrings() { |
} |
void testNumbers() { |
- for (var number in ["-0.12e-12", "-34.12E+12", "0.0e0", "9.9E9", "0", "9" |
- "1234.56789123456701418035663664340972900390625", |
- "1.2345678912345671e-14", |
- "99999999999999999999"]) { |
+ for (var number in [ |
+ "-0.12e-12", |
+ "-34.12E+12", |
+ "0.0e0", |
+ "9.9E9", |
+ "0", |
+ "9" |
+ "1234.56789123456701418035663664340972900390625", |
+ "1.2345678912345671e-14", |
+ "99999999999999999999" |
+ ]) { |
var expected = num.parse(number); |
for (int i = 1; i < number.length - 1; i++) { |
var p1 = number.substring(0, i); |
@@ -157,58 +164,62 @@ void testAll() { |
Expect.fail("Expected map, found ${o.runtimeType}"); |
} |
} |
+ |
for (var i = 1; i < s.length - 1; i++) { |
- var s1 = s.substring(0, i); |
- var s2 = s.substring(i); |
- jsonParse("$s1|$s2-$i", check, (sink) { |
- sink.add(s1.codeUnits); |
- sink.add(s2.codeUnits); |
- sink.close(); |
- }); |
- jsonParse("$s1|$s2-$i-slice", check, (sink) { |
- sink.addSlice(s.codeUnits, 0, i, false); |
- sink.addSlice(s.codeUnits, i, s.length, true); |
- }); |
- for (var j = i; j < s.length - 1; j++) { |
- var s2a = s.substring(i, j); |
- var s2b = s.substring(j); |
- jsonParse("$s1|$s2a|$s2b-$i-$j", check, (sink) { |
+ var s1 = s.substring(0, i); |
+ var s2 = s.substring(i); |
+ jsonParse("$s1|$s2-$i", check, (sink) { |
sink.add(s1.codeUnits); |
- sink.add(s2a.codeUnits); |
- sink.add(s2b.codeUnits); |
+ sink.add(s2.codeUnits); |
sink.close(); |
}); |
+ jsonParse("$s1|$s2-$i-slice", check, (sink) { |
+ sink.addSlice(s.codeUnits, 0, i, false); |
+ sink.addSlice(s.codeUnits, i, s.length, true); |
+ }); |
+ for (var j = i; j < s.length - 1; j++) { |
+ var s2a = s.substring(i, j); |
+ var s2b = s.substring(j); |
+ jsonParse("$s1|$s2a|$s2b-$i-$j", check, (sink) { |
+ sink.add(s1.codeUnits); |
+ sink.add(s2a.codeUnits); |
+ sink.add(s2b.codeUnits); |
+ sink.close(); |
+ }); |
+ } |
} |
} |
-} |
- |
// Check that [codes] decode to [expect] when allowing malformed UTF-8, |
// and throws otherwise. |
void jsonMalformedTest(name, expect, codes) { |
// Helper method. |
void test(name, expect, action(sink)) { |
var tag = "Malform:$name-$expect"; |
- { // Allowing malformed, expect [expect] |
+ { |
+ // Allowing malformed, expect [expect] |
var sink = new ChunkedConversionSink.withCallback((values) { |
var value = values[0]; |
Expect.equals(expect, value, tag); |
}); |
- var decoderSink = JSON.decoder.startChunkedConversion(sink) |
- .asUtf8Sink(true); |
+ var decoderSink = |
+ JSON.decoder.startChunkedConversion(sink).asUtf8Sink(true); |
try { |
action(decoderSink); |
} catch (e, s) { |
Expect.fail("Unexpected throw ($tag): $e\n$s"); |
} |
} |
- { // Not allowing malformed, expect throw. |
+ { |
+ // Not allowing malformed, expect throw. |
var sink = new ChunkedConversionSink.withCallback((values) { |
Expect.fail(tag); |
}); |
- var decoderSink = JSON.decoder.startChunkedConversion(sink) |
- .asUtf8Sink(false); |
- Expect.throws(() { action(decoderSink); }, null, tag); |
+ var decoderSink = |
+ JSON.decoder.startChunkedConversion(sink).asUtf8Sink(false); |
+ Expect.throws(() { |
+ action(decoderSink); |
+ }, null, tag); |
} |
} |
@@ -242,9 +253,11 @@ void jsonThrows(String name, String codeString) { |
var sink = new ChunkedConversionSink.withCallback((values) { |
Expect.fail(tag); |
}); |
- var decoderSink = JSON.decoder.startChunkedConversion(sink) |
- .asUtf8Sink(true); |
- Expect.throws(() { action(decoderSink); }, null, tag); |
+ var decoderSink = |
+ JSON.decoder.startChunkedConversion(sink).asUtf8Sink(true); |
+ Expect.throws(() { |
+ action(decoderSink); |
+ }, null, tag); |
} |
var codes = codeString.codeUnits; |
@@ -272,145 +285,143 @@ void jsonThrows(String name, String codeString) { |
// Malformed UTF-8 encodings. |
void testMalformed() { |
// Overlong encodings. |
- jsonMalformedTest("overlong-0-2", "@\uFFFD@", |
- [0x22, 0x40, 0xc0, 0x80, 0x40, 0x22]); |
- jsonMalformedTest("overlong-0-3", "@\uFFFD@", |
- [0x22, 0x40, 0xe0, 0x80, 0x80, 0x40, 0x22]); |
+ jsonMalformedTest( |
+ "overlong-0-2", "@\uFFFD@", [0x22, 0x40, 0xc0, 0x80, 0x40, 0x22]); |
+ jsonMalformedTest( |
+ "overlong-0-3", "@\uFFFD@", [0x22, 0x40, 0xe0, 0x80, 0x80, 0x40, 0x22]); |
jsonMalformedTest("overlong-0-4", "@\uFFFD@", |
- [0x22, 0x40, 0xf0, 0x80, 0x80, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xf0, 0x80, 0x80, 0x80, 0x40, 0x22]); |
- jsonMalformedTest("overlong-7f-2", "@\uFFFD@", |
- [0x22, 0x40, 0xc1, 0xbf, 0x40, 0x22]); |
- jsonMalformedTest("overlong-7f-3", "@\uFFFD@", |
- [0x22, 0x40, 0xe0, 0x81, 0xbf, 0x40, 0x22]); |
+ jsonMalformedTest( |
+ "overlong-7f-2", "@\uFFFD@", [0x22, 0x40, 0xc1, 0xbf, 0x40, 0x22]); |
+ jsonMalformedTest( |
+ "overlong-7f-3", "@\uFFFD@", [0x22, 0x40, 0xe0, 0x81, 0xbf, 0x40, 0x22]); |
jsonMalformedTest("overlong-7f-4", "@\uFFFD@", |
- [0x22, 0x40, 0xf0, 0x80, 0x81, 0xbf, 0x40, 0x22]); |
+ [0x22, 0x40, 0xf0, 0x80, 0x81, 0xbf, 0x40, 0x22]); |
- jsonMalformedTest("overlong-80-3", "@\uFFFD@", |
- [0x22, 0x40, 0xe0, 0x82, 0x80, 0x40, 0x22]); |
+ jsonMalformedTest( |
+ "overlong-80-3", "@\uFFFD@", [0x22, 0x40, 0xe0, 0x82, 0x80, 0x40, 0x22]); |
jsonMalformedTest("overlong-80-4", "@\uFFFD@", |
- [0x22, 0x40, 0xf0, 0x80, 0x82, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xf0, 0x80, 0x82, 0x80, 0x40, 0x22]); |
- jsonMalformedTest("overlong-7ff-3", "@\uFFFD@", |
- [0x22, 0x40, 0xe0, 0x9f, 0xbf, 0x40, 0x22]); |
+ jsonMalformedTest( |
+ "overlong-7ff-3", "@\uFFFD@", [0x22, 0x40, 0xe0, 0x9f, 0xbf, 0x40, 0x22]); |
jsonMalformedTest("overlong-7ff-4", "@\uFFFD@", |
- [0x22, 0x40, 0xf0, 0x80, 0x9f, 0xbf, 0x40, 0x22]); |
+ [0x22, 0x40, 0xf0, 0x80, 0x9f, 0xbf, 0x40, 0x22]); |
jsonMalformedTest("overlong-800-4", "@\uFFFD@", |
- [0x22, 0x40, 0xf0, 0x80, 0xa0, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xf0, 0x80, 0xa0, 0x80, 0x40, 0x22]); |
jsonMalformedTest("overlong-ffff-4", "@\uFFFD@", |
- [0x22, 0x40, 0xf0, 0x8f, 0xbf, 0xbf, 0x40, 0x22]); |
+ [0x22, 0x40, 0xf0, 0x8f, 0xbf, 0xbf, 0x40, 0x22]); |
// Unterminated multibyte sequences. |
- jsonMalformedTest("unterminated-2-normal", "@\uFFFD@", |
- [0x22, 0x40, 0xc0, 0x40, 0x22]); |
+ jsonMalformedTest( |
+ "unterminated-2-normal", "@\uFFFD@", [0x22, 0x40, 0xc0, 0x40, 0x22]); |
jsonMalformedTest("unterminated-3-normal", "@\uFFFD@", |
- [0x22, 0x40, 0xe0, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xe0, 0x80, 0x40, 0x22]); |
jsonMalformedTest("unterminated-4-normal", "@\uFFFD@", |
- [0x22, 0x40, 0xf0, 0x80, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xf0, 0x80, 0x80, 0x40, 0x22]); |
jsonMalformedTest("unterminated-2-multi", "@\uFFFD\x80@", |
- [0x22, 0x40, 0xc0, 0xc2, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xc0, 0xc2, 0x80, 0x40, 0x22]); |
jsonMalformedTest("unterminated-3-multi", "@\uFFFD\x80@", |
- [0x22, 0x40, 0xe0, 0x80, 0xc2, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xe0, 0x80, 0xc2, 0x80, 0x40, 0x22]); |
jsonMalformedTest("unterminated-4-multi", "@\uFFFD\x80@", |
- [0x22, 0x40, 0xf0, 0x80, 0x80, 0xc2, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xf0, 0x80, 0x80, 0xc2, 0x80, 0x40, 0x22]); |
jsonMalformedTest("unterminated-2-escape", "@\uFFFD\n@", |
- [0x22, 0x40, 0xc0, 0x5c, 0x6e, 0x40, 0x22]); |
+ [0x22, 0x40, 0xc0, 0x5c, 0x6e, 0x40, 0x22]); |
jsonMalformedTest("unterminated-3-escape", "@\uFFFD\n@", |
- [0x22, 0x40, 0xe0, 0x80, 0x5c, 0x6e, 0x40, 0x22]); |
+ [0x22, 0x40, 0xe0, 0x80, 0x5c, 0x6e, 0x40, 0x22]); |
jsonMalformedTest("unterminated-4-escape", "@\uFFFD\n@", |
- [0x22, 0x40, 0xf0, 0x80, 0x80, 0x5c, 0x6e, 0x40, 0x22]); |
+ [0x22, 0x40, 0xf0, 0x80, 0x80, 0x5c, 0x6e, 0x40, 0x22]); |
- jsonMalformedTest("unterminated-2-end", "@\uFFFD", |
- [0x22, 0x40, 0xc0, 0x22]); |
+ jsonMalformedTest("unterminated-2-end", "@\uFFFD", [0x22, 0x40, 0xc0, 0x22]); |
- jsonMalformedTest("unterminated-3-end", "@\uFFFD", |
- [0x22, 0x40, 0xe0, 0x80, 0x22]); |
+ jsonMalformedTest( |
+ "unterminated-3-end", "@\uFFFD", [0x22, 0x40, 0xe0, 0x80, 0x22]); |
- jsonMalformedTest("unterminated-4-end", "@\uFFFD", |
- [0x22, 0x40, 0xf0, 0x80, 0x80, 0x22]); |
+ jsonMalformedTest( |
+ "unterminated-4-end", "@\uFFFD", [0x22, 0x40, 0xf0, 0x80, 0x80, 0x22]); |
// Unexpected continuation byte |
// - after a normal character. |
- jsonMalformedTest("continuation-normal", "@\uFFFD@", |
- [0x22, 0x40, 0x80, 0x40, 0x22]); |
+ jsonMalformedTest( |
+ "continuation-normal", "@\uFFFD@", [0x22, 0x40, 0x80, 0x40, 0x22]); |
// - after a valid continuation byte. |
jsonMalformedTest("continuation-continuation-2", "@\x80\uFFFD@", |
- [0x22, 0x40, 0xc2, 0x80, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xc2, 0x80, 0x80, 0x40, 0x22]); |
jsonMalformedTest("continuation-continuation-3", "@\u0800\uFFFD@", |
- [0x22, 0x40, 0xe0, 0xa0, 0x80, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xe0, 0xa0, 0x80, 0x80, 0x40, 0x22]); |
jsonMalformedTest("continuation-continuation-4", "@\u{10000}\uFFFD@", |
- [0x22, 0x40, 0xf0, 0x90, 0x80, 0x80, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xf0, 0x90, 0x80, 0x80, 0x80, 0x40, 0x22]); |
// - after another invalid continuation byte |
jsonMalformedTest("continuation-twice", "@\uFFFD\uFFFD\uFFFD@", |
- [0x22, 0x40, 0x80, 0x80, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0x80, 0x80, 0x80, 0x40, 0x22]); |
// - at start. |
- jsonMalformedTest("continuation-start", "\uFFFD@", |
- [0x22, 0x80, 0x40, 0x22]); |
+ jsonMalformedTest("continuation-start", "\uFFFD@", [0x22, 0x80, 0x40, 0x22]); |
// Unexpected leading byte where continuation byte expected. |
- jsonMalformedTest("leading-2", "@\uFFFD\x80@", |
- [0x22, 0x40, 0xc0, 0xc2, 0x80, 0x40, 0x22]); |
+ jsonMalformedTest( |
+ "leading-2", "@\uFFFD\x80@", [0x22, 0x40, 0xc0, 0xc2, 0x80, 0x40, 0x22]); |
jsonMalformedTest("leading-3-1", "@\uFFFD\x80@", |
- [0x22, 0x40, 0xe0, 0xc2, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xe0, 0xc2, 0x80, 0x40, 0x22]); |
jsonMalformedTest("leading-3-2", "@\uFFFD\x80@", |
- [0x22, 0x40, 0xe0, 0x80, 0xc2, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xe0, 0x80, 0xc2, 0x80, 0x40, 0x22]); |
jsonMalformedTest("leading-4-1", "@\uFFFD\x80@", |
- [0x22, 0x40, 0xf0, 0xc2, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xf0, 0xc2, 0x80, 0x40, 0x22]); |
jsonMalformedTest("leading-4-2", "@\uFFFD\x80@", |
- [0x22, 0x40, 0xf0, 0x80, 0xc2, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xf0, 0x80, 0xc2, 0x80, 0x40, 0x22]); |
jsonMalformedTest("leading-4-3", "@\uFFFD\x80@", |
- [0x22, 0x40, 0xf0, 0x80, 0x80, 0xc2, 0x80, 0x40, 0x22]); |
+ [0x22, 0x40, 0xf0, 0x80, 0x80, 0xc2, 0x80, 0x40, 0x22]); |
// Overlong encodings of ASCII outside of strings always fail. |
// Use Latin-1 strings as argument since most chars are correct, |
// pass string.codeUnits to decoder as UTF-8. |
- jsonThrows("number-1", "\xc0\xab0.0e-0"); // '-' is 0x2b => \xc0\xab |
- jsonThrows("number-2", "-\xc0\xb0.0e-0"); // '0' is 0x30 => \xc0\xb0 |
- jsonThrows("number-3", "-0\xc0\xae0e-0"); // '.' is 0x2e => \xc0\xae |
+ jsonThrows("number-1", "\xc0\xab0.0e-0"); // '-' is 0x2b => \xc0\xab |
+ jsonThrows("number-2", "-\xc0\xb0.0e-0"); // '0' is 0x30 => \xc0\xb0 |
+ jsonThrows("number-3", "-0\xc0\xae0e-0"); // '.' is 0x2e => \xc0\xae |
jsonThrows("number-4", "-0.\xc0\xb0e-0"); |
- jsonThrows("number-5", "-0.0\xc1\xa5-0"); // 'e' is 0x65 => \xc1\xa5 |
+ jsonThrows("number-5", "-0.0\xc1\xa5-0"); // 'e' is 0x65 => \xc1\xa5 |
jsonThrows("number-6", "-0.0e\xc0\xab0"); |
jsonThrows("number-7", "-0.0e-\xc0\xb0"); |
- jsonThrows("true-1", "\xc1\xb4rue"); // 't' is 0x74 |
- jsonThrows("true-2", "t\xc1\xb2ue"); // 'r' is 0x72 |
- jsonThrows("true-3", "tr\xc1\xb5e"); // 'u' is 0x75 |
- jsonThrows("true-4", "tru\xc1\xa5"); // 'e' is 0x65 |
+ jsonThrows("true-1", "\xc1\xb4rue"); // 't' is 0x74 |
+ jsonThrows("true-2", "t\xc1\xb2ue"); // 'r' is 0x72 |
+ jsonThrows("true-3", "tr\xc1\xb5e"); // 'u' is 0x75 |
+ jsonThrows("true-4", "tru\xc1\xa5"); // 'e' is 0x65 |
- jsonThrows("false-1", "\xc1\xa6alse"); // 'f' is 0x66 |
- jsonThrows("false-2", "f\xc1\xa1lse"); // 'a' is 0x61 |
- jsonThrows("false-3", "fa\xc1\xacse"); // 'l' is 0x6c |
- jsonThrows("false-4", "fal\xc1\xb3e"); // 's' is 0x73 |
- jsonThrows("false-5", "fals\xc1\xa5"); // 'e' is 0x65 |
+ jsonThrows("false-1", "\xc1\xa6alse"); // 'f' is 0x66 |
+ jsonThrows("false-2", "f\xc1\xa1lse"); // 'a' is 0x61 |
+ jsonThrows("false-3", "fa\xc1\xacse"); // 'l' is 0x6c |
+ jsonThrows("false-4", "fal\xc1\xb3e"); // 's' is 0x73 |
+ jsonThrows("false-5", "fals\xc1\xa5"); // 'e' is 0x65 |
- jsonThrows("null-1", "\xc1\xaeull"); // 'n' is 0x6e |
- jsonThrows("null-2", "n\xc1\xb5ll"); // 'u' is 0x75 |
- jsonThrows("null-3", "nu\xc1\xacl"); // 'l' is 0x6c |
- jsonThrows("null-4", "nul\xc1\xac"); // 'l' is 0x6c |
+ jsonThrows("null-1", "\xc1\xaeull"); // 'n' is 0x6e |
+ jsonThrows("null-2", "n\xc1\xb5ll"); // 'u' is 0x75 |
+ jsonThrows("null-3", "nu\xc1\xacl"); // 'l' is 0x6c |
+ jsonThrows("null-4", "nul\xc1\xac"); // 'l' is 0x6c |
- jsonThrows("array-1", "\xc1\x9b0,0]"); // '[' is 0x5b |
- jsonThrows("array-2", "[0,0\xc1\x9d"); // ']' is 0x5d |
- jsonThrows("array-2", "[0\xc0\xac0]"); // ',' is 0x2c |
+ jsonThrows("array-1", "\xc1\x9b0,0]"); // '[' is 0x5b |
+ jsonThrows("array-2", "[0,0\xc1\x9d"); // ']' is 0x5d |
+ jsonThrows("array-2", "[0\xc0\xac0]"); // ',' is 0x2c |
- jsonThrows("object-1", '\xc1\xbb"x":0}'); // '{' is 0x7b |
- jsonThrows("object-2", '{"x":0\xc1\xbd'); // '}' is 0x7d |
- jsonThrows("object-2", '{"x\xc0\xba0}'); // ':' is 0x3a |
+ jsonThrows("object-1", '\xc1\xbb"x":0}'); // '{' is 0x7b |
+ jsonThrows("object-2", '{"x":0\xc1\xbd'); // '}' is 0x7d |
+ jsonThrows("object-2", '{"x\xc0\xba0}'); // ':' is 0x3a |
- jsonThrows("string-1", '\xc0\xa2x"'); // '"' is 0x22 |
- jsonThrows("string-1", '"x\xc0\xa2'); // Unterminated string. |
+ jsonThrows("string-1", '\xc0\xa2x"'); // '"' is 0x22 |
+ jsonThrows("string-1", '"x\xc0\xa2'); // Unterminated string. |
- jsonThrows("whitespace-1", "\xc0\xa01"); // ' ' is 0x20 |
+ jsonThrows("whitespace-1", "\xc0\xa01"); // ' ' is 0x20 |
} |
void testUnicodeTests() { |
@@ -421,7 +432,7 @@ void testUnicodeTests() { |
if (bytes.length > 100) step = bytes.length ~/ 13; |
for (int i = 1; i < bytes.length - 1; i += step) { |
jsonTest("$string:$i", string, (sink) { |
- sink.add([0x22]); // Double-quote. |
+ sink.add([0x22]); // Double-quote. |
sink.add(bytes.sublist(0, i)); |
sink.add(bytes.sublist(i)); |
sink.add([0x22]); |