OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library utf8_test; | |
6 import "package:expect/expect.dart"; | |
7 import 'dart:convert'; | |
8 import 'dart:codec'; | |
9 | |
10 String decode(List<int> bytes) => new Utf8Decoder().convert(bytes); | |
11 String decodeAllowMalformed(List<int> bytes) { | |
12 return new Utf8Decoder(allowMalformed: true).convert(bytes); | |
13 } | |
14 | |
15 String decode2(List<int> bytes) => UTF8.decode(bytes); | |
16 String decodeAllowMalformed2(List<int> bytes) { | |
17 return UTF8.decode(bytes, allowMalformed: true); | |
18 } | |
19 | |
20 String decode3(List<int> bytes) => new Utf8Codec().decode(bytes); | |
21 String decodeAllowMalformed3(List<int> bytes) { | |
22 return new Utf8Codec(allowMalformed: true).decode(bytes); | |
23 } | |
24 | |
25 String decode4(List<int> bytes) => new Utf8Codec().decoder.convert(bytes); | |
26 String decodeAllowMalformed4(List<int> bytes) { | |
27 return new Utf8Codec(allowMalformed: true).decoder.convert(bytes); | |
28 } | |
29 | |
30 final TESTS = [ | |
31 // Unfinished UTF-8 sequences. | |
32 [ 0xc3 ], | |
33 [ 0xE2, 0x82 ], | |
34 [ 0xF0, 0xA4, 0xAD ], | |
35 // Overlong encoding of euro-sign. | |
36 [ 0xF0, 0x82, 0x82, 0xAC ]]; | |
Lasse Reichstein Nielsen
2013/07/16 12:23:03
Test with "[0x80, 0x80]" too.
And decide what happ
floitsch
2013/07/16 14:25:24
I prefer making each one a separate error.
Otherwi
Lasse Reichstein Nielsen
2013/07/17 09:14:50
The point is that UTF-8 can be traversed both forw
| |
37 | |
38 main() { | |
39 var allTests = TESTS.expand((test) { | |
40 // Pairs of test and expected string output when malformed strings are | |
41 // allowed. Replacement character: 0xFFFD | |
42 return [[ test, "\u{FFFD}" ], | |
43 [ new List.from([0x61])..addAll(test), "a\u{FFFD}" ], | |
44 [ new List.from([0x61])..addAll(test)..add(0x61), "a\u{FFFD}a" ], | |
45 [ new List.from(test)..add(0x61), "\u{FFFD}a" ], | |
46 [ new List.from(test)..addAll(test), "\u{FFFD}\u{FFFD}" ], | |
47 [ new List.from(test)..add(0x61)..addAll(test), | |
48 "\u{FFFD}a\u{FFFD}" ], | |
49 [ new List.from([0xc3, 0xa5])..addAll(test), "å\u{FFFD}" ], | |
50 [ new List.from([0xc3, 0xa5])..addAll(test)..addAll([0xc3, 0xa5]), | |
51 "å\u{FFFD}å" ], | |
52 [ new List.from(test)..addAll([0xc3, 0xa5]), "\u{FFFD}å" ], | |
53 [ new List.from(test)..addAll([0xc3, 0xa5])..addAll(test), | |
54 "\u{FFFD}å\u{FFFD}" ]]; | |
55 }); | |
56 | |
57 for (var test in allTests) { | |
58 List<int> bytes = test[0]; | |
59 Expect.throws(() => decode(bytes), (e) => e is FormatException); | |
60 Expect.throws(() => decode2(bytes), (e) => e is FormatException); | |
61 Expect.throws(() => decode3(bytes), (e) => e is FormatException); | |
62 Expect.throws(() => decode4(bytes), (e) => e is FormatException); | |
63 | |
64 String expected = test[1]; | |
65 Expect.equals(expected, decodeAllowMalformed(bytes)); | |
66 Expect.equals(expected, decodeAllowMalformed2(bytes)); | |
67 Expect.equals(expected, decodeAllowMalformed3(bytes)); | |
68 Expect.equals(expected, decodeAllowMalformed4(bytes)); | |
69 } | |
70 } | |
OLD | NEW |