| 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 = <String>[]; |
| 20 var controller = new StreamController<String>(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 = <String>[]; |
| 33 var controller = new StreamController<String>(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 List<List<int>> results; |
| 71 var sink; |
| 72 setUp(() { |
| 73 results = []; |
| 74 var controller = new StreamController<List<int>>(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, isEmpty); |
| 106 |
| 107 sink.add("0"); |
| 108 expect(results, equals([[]])); |
| 109 |
| 110 sink.add(""); |
| 111 expect(results, equals([[]])); |
| 112 |
| 113 sink.add("0"); |
| 114 expect(results, equals([[], [0x00]])); |
| 115 |
| 116 sink.add(""); |
| 117 expect(results, equals([[], [0x00]])); |
| 118 }); |
| 119 |
| 120 test("rejects odd length detected in close()", () { |
| 121 sink.add("1ab23"); |
| 122 expect(results, equals([[0x1a, 0xb2]])); |
| 123 expect(() => sink.close(), throwsFormatException); |
| 124 }); |
| 125 |
| 126 test("rejects odd length detected in addSlice()", () { |
| 127 sink.addSlice("1ab23cd", 0, 5, false); |
| 128 expect(results, equals([[0x1a, 0xb2]])); |
| 129 |
| 130 expect(() => sink.addSlice("1ab23cd", 5, 7, true), |
| 131 throwsFormatException); |
| 132 }); |
| 133 }); |
| 134 |
| 135 group("rejects non-hex character", () { |
| 136 for (var char in |
| 137 ["g", "G", "/", ":", "@", "`", "\x00", "\u0141", "\u{10041}"]) { |
| 138 test('"$char"', () { |
| 139 expect(() => hex.decode("a$char"), throwsFormatException); |
| 140 expect(() => hex.decode("${char}a"), throwsFormatException); |
| 141 |
| 142 var sink = hex.decoder.startChunkedConversion( |
| 143 new StreamController(sync: true)); |
| 144 expect(() => sink.add(char), throwsFormatException); |
| 145 }); |
| 146 } |
| 147 }); |
| 148 |
| 149 test("rejects odd length detected in convert()", () { |
| 150 expect(() => hex.decode("1ab23cd"), throwsFormatException); |
| 151 }); |
| 152 }); |
| 153 } |
| OLD | NEW |