OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 utf8_test; | |
6 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
7 import 'dart:convert'; | 6 import 'dart:convert'; |
8 | 7 |
9 String decode(List<int> bytes) => new Utf8Decoder().convert(bytes); | 8 String decode(List<int> bytes) { |
10 String decodeAllowMalformed(List<int> bytes) { | 9 StringBuffer buffer = new StringBuffer(); |
11 return new Utf8Decoder(allowMalformed: true).convert(bytes); | 10 ChunkedConversionSink stringSink = |
| 11 new StringConversionSink.fromStringSink(buffer); |
| 12 var byteSink = new Utf8Decoder().startChunkedConversion(stringSink); |
| 13 bytes.forEach((byte) { byteSink.add([byte]); }); |
| 14 byteSink.close(); |
| 15 return buffer.toString(); |
12 } | 16 } |
13 | 17 |
14 String decode2(List<int> bytes) => UTF8.decode(bytes); | 18 String decodeAllowMalformed(List<int> bytes) { |
15 String decodeAllowMalformed2(List<int> bytes) { | 19 StringBuffer buffer = new StringBuffer(); |
16 return UTF8.decode(bytes, allowMalformed: true); | 20 ChunkedConversionSink stringSink = |
| 21 new StringConversionSink.fromStringSink(buffer); |
| 22 var decoder = new Utf8Decoder(allowMalformed: true); |
| 23 var byteSink = decoder.startChunkedConversion(stringSink); |
| 24 bytes.forEach((byte) { byteSink.add([byte]); }); |
| 25 byteSink.close(); |
| 26 return buffer.toString(); |
17 } | 27 } |
18 | 28 |
19 String decode3(List<int> bytes) => new Utf8Codec().decode(bytes); | |
20 String decodeAllowMalformed3(List<int> bytes) { | |
21 return new Utf8Codec(allowMalformed: true).decode(bytes); | |
22 } | |
23 | |
24 String decode4(List<int> bytes) => new Utf8Codec().decoder.convert(bytes); | |
25 String decodeAllowMalformed4(List<int> bytes) { | |
26 return new Utf8Codec(allowMalformed: true).decoder.convert(bytes); | |
27 } | |
28 | 29 |
29 final TESTS = [ | 30 final TESTS = [ |
30 // Unfinished UTF-8 sequences. | 31 // Unfinished UTF-8 sequences. |
31 [ 0xc3 ], | 32 [ 0xc3 ], |
32 [ 0xE2, 0x82 ], | 33 [ 0xE2, 0x82 ], |
33 [ 0xF0, 0xA4, 0xAD ], | 34 [ 0xF0, 0xA4, 0xAD ], |
34 // Overlong encoding of euro-sign. | 35 // Overlong encoding of euro-sign. |
35 [ 0xF0, 0x82, 0x82, 0xAC ], | 36 [ 0xF0, 0x82, 0x82, 0xAC ], |
36 // Other overlong/unfinished sequences. | 37 // Other overlong/unfinished sequences. |
37 [ 0xC0 ], | 38 [ 0xC0 ], |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 var allTests2 = TESTS2.map((test) { | 115 var allTests2 = TESTS2.map((test) { |
115 // Pairs of test and expected string output when malformed strings are | 116 // Pairs of test and expected string output when malformed strings are |
116 // allowed. Replacement character: U+FFFD | 117 // allowed. Replacement character: U+FFFD |
117 String expected = test[1].replaceAll("X", "\u{FFFD}"); | 118 String expected = test[1].replaceAll("X", "\u{FFFD}"); |
118 return [test[0], expected]; | 119 return [test[0], expected]; |
119 }); | 120 }); |
120 | 121 |
121 for (var test in []..addAll(allTests)..addAll(allTests2)) { | 122 for (var test in []..addAll(allTests)..addAll(allTests2)) { |
122 List<int> bytes = test[0]; | 123 List<int> bytes = test[0]; |
123 Expect.throws(() => decode(bytes), (e) => e is FormatException); | 124 Expect.throws(() => decode(bytes), (e) => e is FormatException); |
124 Expect.throws(() => decode2(bytes), (e) => e is FormatException); | |
125 Expect.throws(() => decode3(bytes), (e) => e is FormatException); | |
126 Expect.throws(() => decode4(bytes), (e) => e is FormatException); | |
127 | 125 |
128 String expected = test[1]; | 126 String expected = test[1]; |
129 Expect.equals(expected, decodeAllowMalformed(bytes)); | 127 Expect.equals(expected, decodeAllowMalformed(bytes)); |
130 Expect.equals(expected, decodeAllowMalformed2(bytes)); | |
131 Expect.equals(expected, decodeAllowMalformed3(bytes)); | |
132 Expect.equals(expected, decodeAllowMalformed4(bytes)); | |
133 } | 128 } |
134 } | 129 } |
OLD | NEW |