Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import 'dart:async'; | |
| 6 | |
| 7 import 'package:convert/convert.dart'; | |
| 8 import 'package:test/test.dart'; | |
| 9 | |
| 10 void main() { | |
| 11 group("encoder", () { | |
| 12 test("converts byte arrays to hex", () { | |
| 13 expect(hex.encode([0x1a, 0xb2, 0x3c, 0xd4]), equals("1ab23cd4")); | |
| 14 expect(hex.encode([0x00, 0x01, 0xfe, 0xff]), equals("0001feff")); | |
| 15 }); | |
| 16 | |
| 17 group("with chunked conversion", () { | |
| 18 test("converts byte arrays to hex", () { | |
| 19 var results = []; | |
| 20 var controller = new StreamController(sync: true); | |
| 21 controller.stream.listen(results.add); | |
| 22 var sink = hex.encoder.startChunkedConversion(controller.sink); | |
| 23 | |
| 24 sink.add([0x1a, 0xb2, 0x3c, 0xd4]); | |
| 25 expect(results, equals(["1ab23cd4"])); | |
| 26 | |
| 27 sink.add([0x00, 0x01, 0xfe, 0xff]); | |
| 28 expect(results, equals(["1ab23cd4", "0001feff"])); | |
| 29 }); | |
| 30 | |
| 31 test("handles empty and single-byte lists", () { | |
| 32 var results = []; | |
| 33 var controller = new StreamController(sync: true); | |
| 34 controller.stream.listen(results.add); | |
| 35 var sink = hex.encoder.startChunkedConversion(controller.sink); | |
| 36 | |
| 37 sink.add([]); | |
| 38 expect(results, equals([""])); | |
| 39 | |
| 40 sink.add([0x00]); | |
| 41 expect(results, equals(["", "00"])); | |
| 42 | |
| 43 sink.add([]); | |
| 44 expect(results, equals(["", "00", ""])); | |
| 45 }); | |
| 46 }); | |
| 47 | |
| 48 test("rejects non-bytes", () { | |
| 49 expect(() => hex.encode([0x100]), throwsFormatException); | |
| 50 | |
| 51 var sink = hex.encoder.startChunkedConversion( | |
| 52 new StreamController(sync: true)); | |
| 53 expect(() => sink.add([0x100]), throwsFormatException); | |
| 54 }); | |
| 55 }); | |
| 56 | |
| 57 group("decoder", () { | |
| 58 test("converts hex to byte arrays", () { | |
| 59 expect(hex.decode("1ab23cd4"), equals([0x1a, 0xb2, 0x3c, 0xd4])); | |
| 60 expect(hex.decode("0001feff"), equals([0x00, 0x01, 0xfe, 0xff])); | |
| 61 }); | |
| 62 | |
| 63 test("supports uppercase letters", () { | |
| 64 expect(hex.decode("0123456789ABCDEFabcdef"), equals([ | |
| 65 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef | |
| 66 ])); | |
| 67 }); | |
| 68 | |
| 69 group("with chunked conversion", () { | |
| 70 var results; | |
| 71 var sink; | |
| 72 setUp(() { | |
| 73 results = []; | |
| 74 var controller = new StreamController(sync: true); | |
| 75 controller.stream.listen(results.add); | |
| 76 sink = hex.decoder.startChunkedConversion(controller.sink); | |
| 77 }); | |
| 78 | |
| 79 test("converts hex to byte arrays", () { | |
| 80 sink.add("1ab23cd4"); | |
| 81 expect(results, equals([[0x1a, 0xb2, 0x3c, 0xd4]])); | |
| 82 | |
| 83 sink.add("0001feff"); | |
| 84 expect(results, | |
| 85 equals([[0x1a, 0xb2, 0x3c, 0xd4], [0x00, 0x01, 0xfe, 0xff]])); | |
| 86 }); | |
| 87 | |
| 88 test("supports trailing digits split across chunks", () { | |
| 89 sink.add("1ab23"); | |
| 90 expect(results, equals([[0x1a, 0xb2]])); | |
| 91 | |
| 92 sink.add("cd"); | |
| 93 expect(results, equals([[0x1a, 0xb2], [0x3c]])); | |
| 94 | |
| 95 sink.add("40001"); | |
| 96 expect(results, equals([[0x1a, 0xb2], [0x3c], [0xd4, 0x00, 0x01]])); | |
| 97 | |
| 98 sink.add("feff"); | |
| 99 expect(results, | |
| 100 equals([[0x1a, 0xb2], [0x3c], [0xd4, 0x00, 0x01], [0xfe, 0xff]])); | |
| 101 }); | |
| 102 | |
| 103 test("supports empty strings", () { | |
| 104 sink.add(""); | |
| 105 expect(results, equals([[]])); | |
| 106 | |
| 107 sink.add("00"); | |
| 108 expect(results, equals([[], [0x00]])); | |
| 109 | |
| 110 sink.add(""); | |
| 111 expect(results, equals([[], [0x00], []])); | |
| 112 }); | |
| 113 | |
| 114 test("rejects odd length detected in close()", () { | |
| 115 sink.add("1ab23"); | |
| 116 expect(results, equals([[0x1a, 0xb2]])); | |
| 117 expect(() => sink.close(), throwsFormatException); | |
| 118 }); | |
| 119 | |
| 120 test("rejects odd length detected in addSlice()", () { | |
| 121 sink.addSlice("1ab23cd", 0, 5, false); | |
| 122 expect(results, equals([[0x1a, 0xb2]])); | |
| 123 | |
| 124 expect(() => sink.addSlice("1ab23cd", 5, 7, true), | |
| 125 throwsFormatException); | |
| 126 }); | |
| 127 }); | |
| 128 | |
| 129 group("rejects non-hex character", () { | |
| 130 for (var char in | |
| 131 ["g", "G", "/", ":", "@", "`", "\x00", "\u0141", "\u{10041}"]) { | |
| 132 test('"$char"', () { | |
| 133 expect(() => hex.decode("a$char"), throwsFormatException); | |
| 134 | |
| 135 var sink = hex.decoder.startChunkedConversion( | |
| 136 new StreamController(sync: true)); | |
| 137 expect(() => sink.add("a$char"), throwsFormatException); | |
|
Lasse Reichstein Nielsen
2015/09/24 08:07:02
Also try them as first digit and as the single dig
nweiz
2015/09/24 23:23:42
Done.
| |
| 138 }); | |
| 139 } | |
| 140 }); | |
| 141 | |
| 142 test("rejects odd characters detected in convert()", () { | |
|
Lasse Reichstein Nielsen
2015/09/24 08:07:02
odd characters -> odd length
nweiz
2015/09/24 23:23:42
Done.
| |
| 143 expect(() => hex.decode("1ab23cd"), throwsFormatException); | |
| 144 }); | |
| 145 }); | |
| 146 } | |
| OLD | NEW |