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

Side by Side Diff: pkg/unittest/test/pretty_print_test.dart

Issue 14600029: Add pretty-printing to unittest and more thoroughly print values. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes. Created 7 years, 7 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013, 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:collection';
6
7 import 'package:unittest/src/pretty_print.dart';
8 import 'package:unittest/unittest.dart';
9
10 class DefaultToString {}
11
12 class CustomToString {
13 String toString() => "string representation";
14 }
15
16 class _PrivateName {
17 String toString() => "string representation";
18 }
19
20 class _PrivateNameIterable extends IterableMixin {
21 Iterator get iterator => [1, 2, 3].iterator;
22 }
23
24 void main() {
25 test('with primitive objects', () {
26 expect(prettyPrint(12), equals('<12>'));
27 expect(prettyPrint(12.13), equals('<12.13>'));
28 expect(prettyPrint(true), equals('<true>'));
29 expect(prettyPrint(null), equals('<null>'));
30 expect(prettyPrint(() => 12), equals('<Closure: (dynamic) => dynamic>'));
31 });
32
33 test('with an object with a default [toString]', () {
34 expect(prettyPrint(new DefaultToString()),
35 equals("<Instance of 'DefaultToString'>"));
36 });
37
38 test('with an object with a custom [toString]', () {
39 expect(prettyPrint(new CustomToString()),
40 equals('CustomToString:<string representation>'));
41 });
42
43 test('with an object with a custom [toString] and a private name', () {
44 expect(prettyPrint(new _PrivateName()),
45 equals('Unknown:<string representation>'));
46 });
47
48 group('with a string', () {
49 test('containing simple characters', () {
50 expect(prettyPrint('foo'), equals("'foo'"));
51 });
52
53 test('containing newlines', () {
54 expect(prettyPrint('foo\nbar\nbaz'), equals(
55 "'foo\\n'\n"
56 " 'bar\\n'\n"
57 " 'baz'"));
58 });
59
60 test('containing escapable characters', () {
61 expect(prettyPrint("foo\rbar\tbaz'qux"),
62 equals("'foo\\rbar\\tbaz\\'qux'"));
63 });
64 });
65
66 group('with an iterable', () {
67 test('containing primitive objects', () {
68 expect(prettyPrint([1, true, 'foo']), equals("[1, true, 'foo']"));
69 });
70
71 test('containing a multiline string', () {
72 expect(prettyPrint(['foo', 'bar\nbaz\nbip', 'qux']), equals("[\n"
73 " 'foo',\n"
74 " 'bar\\n'\n"
75 " 'baz\\n'\n"
76 " 'bip',\n"
77 " 'qux'\n"
78 "]"));
79 });
80
81 test("that's under maxLineLength", () {
82 expect(prettyPrint([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxLineLength: 30),
83 equals("[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"));
84 });
85
86 test("that's over maxLineLength", () {
87 expect(prettyPrint([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxLineLength: 29),
88 equals("[\n"
89 " 0,\n"
90 " 1,\n"
91 " 2,\n"
92 " 3,\n"
93 " 4,\n"
94 " 5,\n"
95 " 6,\n"
96 " 7,\n"
97 " 8,\n"
98 " 9\n"
99 "]"));
100 });
101
102 test("factors indentation into maxLineLength", () {
103 expect(prettyPrint([
104 "foo\nbar",
105 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
106 ], maxLineLength: 30), equals("[\n"
107 " 'foo\\n'\n"
108 " 'bar',\n"
109 " [\n"
110 " 0,\n"
111 " 1,\n"
112 " 2,\n"
113 " 3,\n"
114 " 4,\n"
115 " 5,\n"
116 " 6,\n"
117 " 7,\n"
118 " 8,\n"
119 " 9\n"
120 " ]\n"
121 "]"));
122 });
123
124 test("that's under maxItems", () {
125 expect(prettyPrint([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxItems: 10),
126 equals("[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"));
127 });
128
129 test("that's over maxItems", () {
130 expect(prettyPrint([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxItems: 9),
131 equals("[0, 1, 2, 3, 4, 5, 6, 7, ...]"));
132 });
133
134 test("that's not a list", () {
135 expect(prettyPrint([1, 2, 3, 4].map((n) => n * 2)),
136 equals("MappedListIterable:[2, 4, 6, 8]"));
137 });
138
139 test("that's not a list and has a private name", () {
140 expect(prettyPrint(new _PrivateNameIterable()),
141 equals("Unknown:[1, 2, 3]"));
142 });
143
144 test("that's recursive", () {
145 var list = [1, 2, 3];
146 list.add(list);
147 expect(prettyPrint(list), equals("[1, 2, 3, (recursive)]"));
148 });
149 });
150
151 group("with a map", () {
152 test('containing primitive objects', () {
153 expect(prettyPrint({'foo': 1, 'bar': true}),
154 equals("{'foo': 1, 'bar': true}"));
155 });
156
157 test('containing a multiline string key', () {
158 expect(prettyPrint({'foo\nbar': 1, 'bar': true}), equals("{\n"
159 " 'foo\\n'\n"
160 " 'bar': 1,\n"
161 " 'bar': true\n"
162 "}"));
163 });
164
165 test('containing a multiline string value', () {
166 expect(prettyPrint({'foo': 'bar\nbaz', 'qux': true}), equals("{\n"
167 " 'foo': 'bar\\n'\n"
168 " 'baz',\n"
169 " 'qux': true\n"
170 "}"));
171 });
172
173 test('containing a multiline string key/value pair', () {
174 expect(prettyPrint({'foo\nbar': 'baz\nqux'}), equals("{\n"
175 " 'foo\\n'\n"
176 " 'bar': 'baz\\n'\n"
177 " 'qux'\n"
178 "}"));
179 });
180
181 test("that's under maxLineLength", () {
182 expect(prettyPrint({'0': 1, '2': 3, '4': 5, '6': 7}, maxLineLength: 32),
183 equals("{'0': 1, '2': 3, '4': 5, '6': 7}"));
184 });
185
186 test("that's over maxLineLength", () {
187 expect(prettyPrint({'0': 1, '2': 3, '4': 5, '6': 7}, maxLineLength: 31),
188 equals("{\n"
189 " '0': 1,\n"
190 " '2': 3,\n"
191 " '4': 5,\n"
192 " '6': 7\n"
193 "}"));
194 });
195
196 test("factors indentation into maxLineLength", () {
197 expect(prettyPrint(["foo\nbar", {'0': 1, '2': 3, '4': 5, '6': 7}],
198 maxLineLength: 32),
199 equals("[\n"
200 " 'foo\\n'\n"
201 " 'bar',\n"
202 " {\n"
203 " '0': 1,\n"
204 " '2': 3,\n"
205 " '4': 5,\n"
206 " '6': 7\n"
207 " }\n"
208 "]"));
209 });
210
211 test("that's under maxItems", () {
212 expect(prettyPrint({'0': 1, '2': 3, '4': 5, '6': 7}, maxItems: 4),
213 equals("{'0': 1, '2': 3, '4': 5, '6': 7}"));
214 });
215
216 test("that's over maxItems", () {
217 expect(prettyPrint({'0': 1, '2': 3, '4': 5, '6': 7}, maxItems: 3),
218 equals("{'0': 1, '2': 3, ...}"));
219 });
220 });
221 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698