| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library matcher.pretty_print_test; | |
| 6 | |
| 7 import 'dart:collection'; | 5 import 'dart:collection'; |
| 8 | 6 |
| 9 import 'package:matcher/matcher.dart'; | 7 import 'package:matcher/matcher.dart'; |
| 10 import 'package:matcher/src/pretty_print.dart'; | 8 import 'package:matcher/src/pretty_print.dart'; |
| 11 import 'package:test/test.dart' show group, test, expect; | 9 import 'package:test/test.dart' show group, test, expect; |
| 12 | 10 |
| 13 class DefaultToString {} | 11 class DefaultToString {} |
| 14 | 12 |
| 15 class CustomToString { | 13 class CustomToString { |
| 16 String toString() => "string representation"; | 14 String toString() => "string representation"; |
| 17 } | 15 } |
| 18 | 16 |
| 19 class _PrivateName { | 17 class _PrivateName { |
| 20 String toString() => "string representation"; | 18 String toString() => "string representation"; |
| 21 } | 19 } |
| 22 | 20 |
| 23 class _PrivateNameIterable extends IterableMixin { | 21 class _PrivateNameIterable extends IterableMixin { |
| 24 Iterator get iterator => [1, 2, 3].iterator; | 22 Iterator get iterator => [1, 2, 3].iterator; |
| 25 } | 23 } |
| 26 | 24 |
| 27 void main() { | 25 void main() { |
| 28 test('with primitive objects', () { | 26 test('with primitive objects', () { |
| 29 expect(prettyPrint(12), equals('<12>')); | 27 expect(prettyPrint(12), equals('<12>')); |
| 30 expect(prettyPrint(12.13), equals('<12.13>')); | 28 expect(prettyPrint(12.13), equals('<12.13>')); |
| 31 expect(prettyPrint(true), equals('<true>')); | 29 expect(prettyPrint(true), equals('<true>')); |
| 32 expect(prettyPrint(null), equals('<null>')); | 30 expect(prettyPrint(null), equals('<null>')); |
| 33 expect(prettyPrint(() => 12), matches(r'<Closure(: \(\) => dynamic)?>')); | 31 expect(prettyPrint(() => 12), matches(r'<Closure.*>')); |
| 34 }); | 32 }); |
| 35 | 33 |
| 36 group('with a string', () { | 34 group('with a string', () { |
| 37 test('containing simple characters', () { | 35 test('containing simple characters', () { |
| 38 expect(prettyPrint('foo'), equals("'foo'")); | 36 expect(prettyPrint('foo'), equals("'foo'")); |
| 39 }); | 37 }); |
| 40 | 38 |
| 41 test('containing newlines', () { | 39 test('containing newlines', () { |
| 42 expect(prettyPrint('foo\nbar\nbaz'), equals("'foo\\n'\n" | 40 expect( |
| 43 " 'bar\\n'\n" | 41 prettyPrint('foo\nbar\nbaz'), |
| 44 " 'baz'")); | 42 equals("'foo\\n'\n" |
| 43 " 'bar\\n'\n" |
| 44 " 'baz'")); |
| 45 }); | 45 }); |
| 46 | 46 |
| 47 test('containing escapable characters', () { | 47 test('containing escapable characters', () { |
| 48 expect( | 48 expect(prettyPrint("foo\rbar\tbaz'qux\v"), |
| 49 prettyPrint("foo\rbar\tbaz'qux\v"), equals(r"'foo\rbar\tbaz\'qux\v'"))
; | 49 equals(r"'foo\rbar\tbaz\'qux\v'")); |
| 50 }); | 50 }); |
| 51 }); | 51 }); |
| 52 | 52 |
| 53 group('with an iterable', () { | 53 group('with an iterable', () { |
| 54 test('containing primitive objects', () { | 54 test('containing primitive objects', () { |
| 55 expect(prettyPrint([1, true, 'foo']), equals("[1, true, 'foo']")); | 55 expect(prettyPrint([1, true, 'foo']), equals("[1, true, 'foo']")); |
| 56 }); | 56 }); |
| 57 | 57 |
| 58 test('containing a multiline string', () { | 58 test('containing a multiline string', () { |
| 59 expect(prettyPrint(['foo', 'bar\nbaz\nbip', 'qux']), equals("[\n" | 59 expect( |
| 60 " 'foo',\n" | 60 prettyPrint(['foo', 'bar\nbaz\nbip', 'qux']), |
| 61 " 'bar\\n'\n" | 61 equals("[\n" |
| 62 " 'baz\\n'\n" | 62 " 'foo',\n" |
| 63 " 'bip',\n" | 63 " 'bar\\n'\n" |
| 64 " 'qux'\n" | 64 " 'baz\\n'\n" |
| 65 "]")); | 65 " 'bip',\n" |
| 66 " 'qux'\n" |
| 67 "]")); |
| 66 }); | 68 }); |
| 67 | 69 |
| 68 test('containing a matcher', () { | 70 test('containing a matcher', () { |
| 69 expect(prettyPrint(['foo', endsWith('qux')]), | 71 expect(prettyPrint(['foo', endsWith('qux')]), |
| 70 equals("['foo', <a string ending with 'qux'>]")); | 72 equals("['foo', <a string ending with 'qux'>]")); |
| 71 }); | 73 }); |
| 72 | 74 |
| 73 test("that's under maxLineLength", () { | 75 test("that's under maxLineLength", () { |
| 74 expect(prettyPrint([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxLineLength: 30), | 76 expect(prettyPrint([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxLineLength: 30), |
| 75 equals("[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]")); | 77 equals("[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]")); |
| 76 }); | 78 }); |
| 77 | 79 |
| 78 test("that's over maxLineLength", () { | 80 test("that's over maxLineLength", () { |
| 79 expect(prettyPrint([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxLineLength: 29), | 81 expect( |
| 82 prettyPrint([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxLineLength: 29), |
| 80 equals("[\n" | 83 equals("[\n" |
| 81 " 0,\n" | 84 " 0,\n" |
| 82 " 1,\n" | 85 " 1,\n" |
| 83 " 2,\n" | 86 " 2,\n" |
| 84 " 3,\n" | 87 " 3,\n" |
| 85 " 4,\n" | 88 " 4,\n" |
| 86 " 5,\n" | 89 " 5,\n" |
| 87 " 6,\n" | 90 " 6,\n" |
| 88 " 7,\n" | 91 " 7,\n" |
| 89 " 8,\n" | 92 " 8,\n" |
| 90 " 9\n" | 93 " 9\n" |
| 91 "]")); | 94 "]")); |
| 92 }); | 95 }); |
| 93 | 96 |
| 94 test("factors indentation into maxLineLength", () { | 97 test("factors indentation into maxLineLength", () { |
| 95 expect(prettyPrint(["foo\nbar", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],], | 98 expect( |
| 96 maxLineLength: 30), equals("[\n" | 99 prettyPrint([ |
| 97 " 'foo\\n'\n" | 100 "foo\nbar", |
| 98 " 'bar',\n" | 101 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], |
| 99 " [\n" | 102 ], maxLineLength: 30), |
| 100 " 0,\n" | 103 equals("[\n" |
| 101 " 1,\n" | 104 " 'foo\\n'\n" |
| 102 " 2,\n" | 105 " 'bar',\n" |
| 103 " 3,\n" | 106 " [\n" |
| 104 " 4,\n" | 107 " 0,\n" |
| 105 " 5,\n" | 108 " 1,\n" |
| 106 " 6,\n" | 109 " 2,\n" |
| 107 " 7,\n" | 110 " 3,\n" |
| 108 " 8,\n" | 111 " 4,\n" |
| 109 " 9\n" | 112 " 5,\n" |
| 110 " ]\n" | 113 " 6,\n" |
| 111 "]")); | 114 " 7,\n" |
| 115 " 8,\n" |
| 116 " 9\n" |
| 117 " ]\n" |
| 118 "]")); |
| 112 }); | 119 }); |
| 113 | 120 |
| 114 test("that's under maxItems", () { | 121 test("that's under maxItems", () { |
| 115 expect(prettyPrint([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxItems: 10), | 122 expect(prettyPrint([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxItems: 10), |
| 116 equals("[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]")); | 123 equals("[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]")); |
| 117 }); | 124 }); |
| 118 | 125 |
| 119 test("that's over maxItems", () { | 126 test("that's over maxItems", () { |
| 120 expect(prettyPrint([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxItems: 9), | 127 expect(prettyPrint([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxItems: 9), |
| 121 equals("[0, 1, 2, 3, 4, 5, 6, 7, ...]")); | 128 equals("[0, 1, 2, 3, 4, 5, 6, 7, ...]")); |
| 122 }); | 129 }); |
| 123 | 130 |
| 124 test("that's recursive", () { | 131 test("that's recursive", () { |
| 125 var list = [1, 2, 3]; | 132 var list = <dynamic>[1, 2, 3]; |
| 126 list.add(list); | 133 list.add(list); |
| 127 expect(prettyPrint(list), equals("[1, 2, 3, (recursive)]")); | 134 expect(prettyPrint(list), equals("[1, 2, 3, (recursive)]")); |
| 128 }); | 135 }); |
| 129 }); | 136 }); |
| 130 | 137 |
| 131 group("with a map", () { | 138 group("with a map", () { |
| 132 test('containing primitive objects', () { | 139 test('containing primitive objects', () { |
| 133 expect(prettyPrint({'foo': 1, 'bar': true}), | 140 expect(prettyPrint({'foo': 1, 'bar': true}), |
| 134 equals("{'foo': 1, 'bar': true}")); | 141 equals("{'foo': 1, 'bar': true}")); |
| 135 }); | 142 }); |
| 136 | 143 |
| 137 test('containing a multiline string key', () { | 144 test('containing a multiline string key', () { |
| 138 expect(prettyPrint({'foo\nbar': 1, 'bar': true}), equals("{\n" | 145 expect( |
| 139 " 'foo\\n'\n" | 146 prettyPrint({'foo\nbar': 1, 'bar': true}), |
| 140 " 'bar': 1,\n" | 147 equals("{\n" |
| 141 " 'bar': true\n" | 148 " 'foo\\n'\n" |
| 142 "}")); | 149 " 'bar': 1,\n" |
| 150 " 'bar': true\n" |
| 151 "}")); |
| 143 }); | 152 }); |
| 144 | 153 |
| 145 test('containing a multiline string value', () { | 154 test('containing a multiline string value', () { |
| 146 expect(prettyPrint({'foo': 'bar\nbaz', 'qux': true}), equals("{\n" | 155 expect( |
| 147 " 'foo': 'bar\\n'\n" | 156 prettyPrint({'foo': 'bar\nbaz', 'qux': true}), |
| 148 " 'baz',\n" | 157 equals("{\n" |
| 149 " 'qux': true\n" | 158 " 'foo': 'bar\\n'\n" |
| 150 "}")); | 159 " 'baz',\n" |
| 160 " 'qux': true\n" |
| 161 "}")); |
| 151 }); | 162 }); |
| 152 | 163 |
| 153 test('containing a multiline string key/value pair', () { | 164 test('containing a multiline string key/value pair', () { |
| 154 expect(prettyPrint({'foo\nbar': 'baz\nqux'}), equals("{\n" | 165 expect( |
| 155 " 'foo\\n'\n" | 166 prettyPrint({'foo\nbar': 'baz\nqux'}), |
| 156 " 'bar': 'baz\\n'\n" | 167 equals("{\n" |
| 157 " 'qux'\n" | 168 " 'foo\\n'\n" |
| 158 "}")); | 169 " 'bar': 'baz\\n'\n" |
| 170 " 'qux'\n" |
| 171 "}")); |
| 159 }); | 172 }); |
| 160 | 173 |
| 161 test('containing a matcher key', () { | 174 test('containing a matcher key', () { |
| 162 expect(prettyPrint({endsWith('bar'): 'qux'}), | 175 expect(prettyPrint({endsWith('bar'): 'qux'}), |
| 163 equals("{<a string ending with 'bar'>: 'qux'}")); | 176 equals("{<a string ending with 'bar'>: 'qux'}")); |
| 164 }); | 177 }); |
| 165 | 178 |
| 166 test('containing a matcher value', () { | 179 test('containing a matcher value', () { |
| 167 expect(prettyPrint({'foo': endsWith('qux')}), | 180 expect(prettyPrint({'foo': endsWith('qux')}), |
| 168 equals("{'foo': <a string ending with 'qux'>}")); | 181 equals("{'foo': <a string ending with 'qux'>}")); |
| 169 }); | 182 }); |
| 170 | 183 |
| 171 test("that's under maxLineLength", () { | 184 test("that's under maxLineLength", () { |
| 172 expect(prettyPrint({'0': 1, '2': 3, '4': 5, '6': 7}, maxLineLength: 32), | 185 expect(prettyPrint({'0': 1, '2': 3, '4': 5, '6': 7}, maxLineLength: 32), |
| 173 equals("{'0': 1, '2': 3, '4': 5, '6': 7}")); | 186 equals("{'0': 1, '2': 3, '4': 5, '6': 7}")); |
| 174 }); | 187 }); |
| 175 | 188 |
| 176 test("that's over maxLineLength", () { | 189 test("that's over maxLineLength", () { |
| 177 expect(prettyPrint({'0': 1, '2': 3, '4': 5, '6': 7}, maxLineLength: 31), | 190 expect( |
| 191 prettyPrint({'0': 1, '2': 3, '4': 5, '6': 7}, maxLineLength: 31), |
| 178 equals("{\n" | 192 equals("{\n" |
| 179 " '0': 1,\n" | 193 " '0': 1,\n" |
| 180 " '2': 3,\n" | 194 " '2': 3,\n" |
| 181 " '4': 5,\n" | 195 " '4': 5,\n" |
| 182 " '6': 7\n" | 196 " '6': 7\n" |
| 183 "}")); | 197 "}")); |
| 184 }); | 198 }); |
| 185 | 199 |
| 186 test("factors indentation into maxLineLength", () { | 200 test("factors indentation into maxLineLength", () { |
| 187 expect(prettyPrint(["foo\nbar", {'0': 1, '2': 3, '4': 5, '6': 7}], | 201 expect( |
| 188 maxLineLength: 32), equals("[\n" | 202 prettyPrint([ |
| 189 " 'foo\\n'\n" | 203 "foo\nbar", |
| 190 " 'bar',\n" | 204 {'0': 1, '2': 3, '4': 5, '6': 7} |
| 191 " {\n" | 205 ], maxLineLength: 32), |
| 192 " '0': 1,\n" | 206 equals("[\n" |
| 193 " '2': 3,\n" | 207 " 'foo\\n'\n" |
| 194 " '4': 5,\n" | 208 " 'bar',\n" |
| 195 " '6': 7\n" | 209 " {\n" |
| 196 " }\n" | 210 " '0': 1,\n" |
| 197 "]")); | 211 " '2': 3,\n" |
| 212 " '4': 5,\n" |
| 213 " '6': 7\n" |
| 214 " }\n" |
| 215 "]")); |
| 198 }); | 216 }); |
| 199 | 217 |
| 200 test("that's under maxItems", () { | 218 test("that's under maxItems", () { |
| 201 expect(prettyPrint({'0': 1, '2': 3, '4': 5, '6': 7}, maxItems: 4), | 219 expect(prettyPrint({'0': 1, '2': 3, '4': 5, '6': 7}, maxItems: 4), |
| 202 equals("{'0': 1, '2': 3, '4': 5, '6': 7}")); | 220 equals("{'0': 1, '2': 3, '4': 5, '6': 7}")); |
| 203 }); | 221 }); |
| 204 | 222 |
| 205 test("that's over maxItems", () { | 223 test("that's over maxItems", () { |
| 206 expect(prettyPrint({'0': 1, '2': 3, '4': 5, '6': 7}, maxItems: 3), | 224 expect(prettyPrint({'0': 1, '2': 3, '4': 5, '6': 7}, maxItems: 3), |
| 207 equals("{'0': 1, '2': 3, ...}")); | 225 equals("{'0': 1, '2': 3, ...}")); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 228 test("that's not a list", () { | 246 test("that's not a list", () { |
| 229 expect(prettyPrint([1, 2, 3, 4].map((n) => n * 2)), | 247 expect(prettyPrint([1, 2, 3, 4].map((n) => n * 2)), |
| 230 equals("MappedListIterable:[2, 4, 6, 8]")); | 248 equals("MappedListIterable:[2, 4, 6, 8]")); |
| 231 }); | 249 }); |
| 232 | 250 |
| 233 test("that's not a list and has a private name", () { | 251 test("that's not a list and has a private name", () { |
| 234 expect(prettyPrint(new _PrivateNameIterable()), equals("?:[1, 2, 3]")); | 252 expect(prettyPrint(new _PrivateNameIterable()), equals("?:[1, 2, 3]")); |
| 235 }); | 253 }); |
| 236 }); | 254 }); |
| 237 } | 255 } |
| OLD | NEW |