Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: test/hex_test.dart

Issue 1364613002: Add a hexadecimal codec. (Closed) Base URL: git@github.com:dart-lang/convert.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« lib/src/hex/encoder.dart ('K') | « pubspec.yaml ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 test("does chunked conversion", () {
18 var results = [];
19 var controller = new StreamController(sync: true);
20 controller.stream.listen(results.add);
21 var sink = hex.encoder.startChunkedConversion(controller.sink);
22
23 sink.add([0x1a, 0xb2, 0x3c, 0xd4]);
24 expect(results, equals(["1ab23cd4"]));
Lasse Reichstein Nielsen 2015/09/23 08:32:04 Don't expect that the input is immediately output.
nweiz 2015/09/23 22:04:21 That would be a valid implementation, but these ar
25
26 sink.add([0x00, 0x01, 0xfe, 0xff]);
27 expect(results, equals(["1ab23cd4", "0001feff"]));
Lasse Reichstein Nielsen 2015/09/23 08:32:05 Test adding an empty list and a single-element lis
nweiz 2015/09/23 22:04:21 Done.
28 });
29
30 test("rejects non-bytes", () {
31 expect(() => hex.encode([0x100]), throwsRangeError);
32
33 var sink = hex.encoder.startChunkedConversion(
34 new StreamController(sync: true));
35 expect(() => sink.add([0x100]), throwsRangeError);
36 });
37 });
38
39 group("decoder", () {
40 test("converts hex to byte arrays", () {
41 expect(hex.decode("1ab23cd4"), equals([0x1a, 0xb2, 0x3c, 0xd4]));
42 expect(hex.decode("0001feff"), equals([0x00, 0x01, 0xfe, 0xff]));
43 });
44
45 test("supports uppercase letters", () {
46 expect(hex.decode("1Ab23cD4"), equals([0x1a, 0xb2, 0x3c, 0xd4]));
Lasse Reichstein Nielsen 2015/09/23 08:32:04 Check all of them. No reason not to: "0123456789AB
nweiz 2015/09/23 22:04:21 Done.
47 });
48
49 group("with chunked conversion", () {
50 var results;
51 var sink;
52 setUp(() {
53 results = [];
54 var controller = new StreamController(sync: true);
Lasse Reichstein Nielsen 2015/09/23 08:32:04 Hmmm. Not really a proper use of a sync stream con
55 controller.stream.listen(results.add);
56 sink = hex.decoder.startChunkedConversion(controller.sink);
57 });
58
Lasse Reichstein Nielsen 2015/09/23 08:32:05 Again, don't compare intermediate results, only ch
59 test("converts hex to byte arrays", () {
60 sink.add("1ab23cd4");
61 expect(results, equals([[0x1a, 0xb2, 0x3c, 0xd4]]));
62
63 sink.add("0001feff");
64 expect(results, equals([[0x1a, 0xb2, 0x3c, 0xd4], [0x00, 0x01, 0xfe, 0xf f]]));
Lasse Reichstein Nielsen 2015/09/23 08:32:04 long line.
nweiz 2015/09/23 22:04:21 Done.
65 });
66
67 test("supports trailing digits split across chunks", () {
68 sink.add("1ab23");
69 expect(results, equals([[0x1a, 0xb2]]));
70
71 sink.add("cd");
72 expect(results, equals([[0x1a, 0xb2], [0x3c]]));
73
74 sink.add("40001");
75 expect(results, equals([[0x1a, 0xb2], [0x3c], [0xd4, 0x00, 0x01]]));
76
77 sink.add("feff");
78 expect(results,
79 equals([[0x1a, 0xb2], [0x3c], [0xd4, 0x00, 0x01], [0xfe, 0xff]]));
80 });
Lasse Reichstein Nielsen 2015/09/23 08:32:04 Also check empty lists. In similar cases I have m
nweiz 2015/09/23 22:04:21 Done.
81
82 test("rejects odd characters detected in close()", () {
Lasse Reichstein Nielsen 2015/09/23 08:32:04 odd characters -> odd length Ditto below.
nweiz 2015/09/23 22:04:21 Done.
83 sink.add("1ab23");
84 expect(results, equals([[0x1a, 0xb2]]));
85 expect(() => sink.close(), throwsFormatException);
86 });
87
88 test("rejects odd characters detected in addSlice()", () {
89 sink.addSlice("1ab23cd", 0, 5, false);
90 expect(results, equals([[0x1a, 0xb2]]));
91
92 expect(() => sink.addSlice("1ab23cd", 5, 7, true), throwsFormatException );
Lasse Reichstein Nielsen 2015/09/23 08:32:04 Long line.
nweiz 2015/09/23 22:04:21 Done.
93 });
94 });
95
96 test("rejects non-hex characters", () {
97 expect(() => hex.decode("az"), throwsFormatException);
Lasse Reichstein Nielsen 2015/09/23 08:32:04 Also check the characters: "/" ":" "@" "["
nweiz 2015/09/23 22:04:21 Done, although I replaced "{" and "[" with "g" and
Lasse Reichstein Nielsen 2015/09/24 08:07:01 Does it show that I'm working on BASE64 :)
98
99 var sink = hex.decoder.startChunkedConversion(
100 new StreamController(sync: true));
101 expect(() => sink.add("az"), throwsFormatException);
102 });
103
104 test("rejects odd characters detected in convert()", () {
105 expect(() => hex.decode("1ab23cd"), throwsFormatException);
106 });
107 });
108 }
OLDNEW
« lib/src/hex/encoder.dart ('K') | « pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698