| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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.iterable_matchers_test; | 5 library matcher.iterable_matchers_test; |
| 6 | 6 |
| 7 import 'package:matcher/matcher.dart'; | 7 import 'package:matcher/matcher.dart'; |
| 8 import 'package:unittest/unittest.dart' show test, group; | 8 import 'package:unittest/unittest.dart' show test, group; |
| 9 | 9 |
| 10 import 'test_utils.dart'; | 10 import 'test_utils.dart'; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 test('isIn', () { | 41 test('isIn', () { |
| 42 var d = [1, 2]; | 42 var d = [1, 2]; |
| 43 shouldPass(1, isIn(d)); | 43 shouldPass(1, isIn(d)); |
| 44 shouldFail(0, isIn(d), "Expected: is in [1, 2] Actual: <0>"); | 44 shouldFail(0, isIn(d), "Expected: is in [1, 2] Actual: <0>"); |
| 45 }); | 45 }); |
| 46 | 46 |
| 47 test('everyElement', () { | 47 test('everyElement', () { |
| 48 var d = [1, 2]; | 48 var d = [1, 2]; |
| 49 var e = [1, 1, 1]; | 49 var e = [1, 1, 1]; |
| 50 shouldFail(d, everyElement(1), | 50 shouldFail(d, everyElement(1), "Expected: every element(<1>) " |
| 51 "Expected: every element(<1>) " | |
| 52 "Actual: [1, 2] " | 51 "Actual: [1, 2] " |
| 53 "Which: has value <2> which doesn't match <1> at index 1"); | 52 "Which: has value <2> which doesn't match <1> at index 1"); |
| 54 shouldPass(e, everyElement(1)); | 53 shouldPass(e, everyElement(1)); |
| 55 }); | 54 }); |
| 56 | 55 |
| 57 test('nested everyElement', () { | 56 test('nested everyElement', () { |
| 58 var d = [['foo', 'bar'], ['foo'], []]; | 57 var d = [['foo', 'bar'], ['foo'], []]; |
| 59 var e = [['foo', 'bar'], ['foo'], 3, []]; | 58 var e = [['foo', 'bar'], ['foo'], 3, []]; |
| 60 shouldPass(d, everyElement(anyOf(isEmpty, contains('foo')))); | 59 shouldPass(d, everyElement(anyOf(isEmpty, contains('foo')))); |
| 61 shouldFail(d, everyElement(everyElement(equals('foo'))), | 60 shouldFail(d, everyElement(everyElement(equals('foo'))), |
| 62 "Expected: every element(every element('foo')) " | 61 "Expected: every element(every element('foo')) " |
| 63 "Actual: [['foo', 'bar'], ['foo'], []] " | 62 "Actual: [['foo', 'bar'], ['foo'], []] " |
| 64 "Which: has value ['foo', 'bar'] which has value 'bar' " | 63 "Which: has value ['foo', 'bar'] which has value 'bar' " |
| 65 "which is different. Expected: foo Actual: bar ^ " | 64 "which is different. Expected: foo Actual: bar ^ " |
| 66 "Differ at offset 0 at index 1 at index 0"); | 65 "Differ at offset 0 at index 1 at index 0"); |
| 67 shouldFail(d, everyElement(allOf(hasLength(greaterThan(0)), | 66 shouldFail(d, |
| 68 contains('foo'))), | 67 everyElement(allOf(hasLength(greaterThan(0)), contains('foo'))), |
| 69 "Expected: every element((an object with length of a value " | 68 "Expected: every element((an object with length of a value " |
| 70 "greater than <0> and contains 'foo')) " | 69 "greater than <0> and contains 'foo')) " |
| 71 "Actual: [['foo', 'bar'], ['foo'], []] " | 70 "Actual: [['foo', 'bar'], ['foo'], []] " |
| 72 "Which: has value [] which has length of <0> at index 2"); | 71 "Which: has value [] which has length of <0> at index 2"); |
| 73 shouldFail(d, everyElement(allOf(contains('foo'), | 72 shouldFail(d, |
| 74 hasLength(greaterThan(0)))), | 73 everyElement(allOf(contains('foo'), hasLength(greaterThan(0)))), |
| 75 "Expected: every element((contains 'foo' and " | 74 "Expected: every element((contains 'foo' and " |
| 76 "an object with length of a value greater than <0>)) " | 75 "an object with length of a value greater than <0>)) " |
| 77 "Actual: [['foo', 'bar'], ['foo'], []] " | 76 "Actual: [['foo', 'bar'], ['foo'], []] " |
| 78 "Which: has value [] which doesn't match (contains 'foo' and " | 77 "Which: has value [] which doesn't match (contains 'foo' and " |
| 79 "an object with length of a value greater than <0>) at index 2"); | 78 "an object with length of a value greater than <0>) at index 2"); |
| 80 shouldFail(e, everyElement(allOf(contains('foo'), | 79 shouldFail(e, |
| 81 hasLength(greaterThan(0)))), | 80 everyElement(allOf(contains('foo'), hasLength(greaterThan(0)))), |
| 82 "Expected: every element((contains 'foo' and an object with " | 81 "Expected: every element((contains 'foo' and an object with " |
| 83 "length of a value greater than <0>)) " | 82 "length of a value greater than <0>)) " |
| 84 "Actual: [['foo', 'bar'], ['foo'], 3, []] " | 83 "Actual: [['foo', 'bar'], ['foo'], 3, []] " |
| 85 "Which: has value <3> which is not a string, map or iterable " | 84 "Which: has value <3> which is not a string, map or iterable " |
| 86 "at index 2"); | 85 "at index 2"); |
| 87 }); | 86 }); |
| 88 | 87 |
| 89 test('anyElement', () { | 88 test('anyElement', () { |
| 90 var d = [1, 2]; | 89 var d = [1, 2]; |
| 91 var e = [1, 1, 1]; | 90 var e = [1, 1, 1]; |
| 92 shouldPass(d, anyElement(2)); | 91 shouldPass(d, anyElement(2)); |
| 93 shouldFail(e, anyElement(2), | 92 shouldFail( |
| 94 "Expected: some element <2> Actual: [1, 1, 1]"); | 93 e, anyElement(2), "Expected: some element <2> Actual: [1, 1, 1]"); |
| 95 }); | 94 }); |
| 96 | 95 |
| 97 test('orderedEquals', () { | 96 test('orderedEquals', () { |
| 98 shouldPass([null], orderedEquals([null])); | 97 shouldPass([null], orderedEquals([null])); |
| 99 var d = [1, 2]; | 98 var d = [1, 2]; |
| 100 shouldPass(d, orderedEquals([1, 2])); | 99 shouldPass(d, orderedEquals([1, 2])); |
| 101 shouldFail(d, orderedEquals([2, 1]), | 100 shouldFail(d, orderedEquals([2, 1]), "Expected: equals [2, 1] ordered " |
| 102 "Expected: equals [2, 1] ordered " | |
| 103 "Actual: [1, 2] " | 101 "Actual: [1, 2] " |
| 104 "Which: was <1> instead of <2> at location [0]"); | 102 "Which: was <1> instead of <2> at location [0]"); |
| 105 }); | 103 }); |
| 106 | 104 |
| 107 test('unorderedEquals', () { | 105 test('unorderedEquals', () { |
| 108 var d = [1, 2]; | 106 var d = [1, 2]; |
| 109 shouldPass(d, unorderedEquals([2, 1])); | 107 shouldPass(d, unorderedEquals([2, 1])); |
| 110 shouldFail(d, unorderedEquals([1]), | 108 shouldFail(d, unorderedEquals([1]), "Expected: equals [1] unordered " |
| 111 "Expected: equals [1] unordered " | |
| 112 "Actual: [1, 2] " | 109 "Actual: [1, 2] " |
| 113 "Which: has too many elements (2 > 1)"); | 110 "Which: has too many elements (2 > 1)"); |
| 114 shouldFail(d, unorderedEquals([3, 2, 1]), | 111 shouldFail(d, unorderedEquals([3, 2, 1]), |
| 115 "Expected: equals [3, 2, 1] unordered " | 112 "Expected: equals [3, 2, 1] unordered " |
| 116 "Actual: [1, 2] " | 113 "Actual: [1, 2] " |
| 117 "Which: has too few elements (2 < 3)"); | 114 "Which: has too few elements (2 < 3)"); |
| 118 shouldFail(d, unorderedEquals([3, 1]), | 115 shouldFail(d, unorderedEquals([3, 1]), "Expected: equals [3, 1] unordered " |
| 119 "Expected: equals [3, 1] unordered " | |
| 120 "Actual: [1, 2] " | 116 "Actual: [1, 2] " |
| 121 "Which: has no match for <3> at index 0"); | 117 "Which: has no match for <3> at index 0"); |
| 122 }); | 118 }); |
| 123 | 119 |
| 124 test('unorderedMatchess', () { | 120 test('unorderedMatchess', () { |
| 125 var d = [1, 2]; | 121 var d = [1, 2]; |
| 126 shouldPass(d, unorderedMatches([2, 1])); | 122 shouldPass(d, unorderedMatches([2, 1])); |
| 127 shouldPass(d, unorderedMatches([greaterThan(1), greaterThan(0)])); | 123 shouldPass(d, unorderedMatches([greaterThan(1), greaterThan(0)])); |
| 128 shouldFail(d, unorderedMatches([greaterThan(0)]), | 124 shouldFail(d, unorderedMatches([greaterThan(0)]), |
| 129 "Expected: matches [a value greater than <0>] unordered " | 125 "Expected: matches [a value greater than <0>] unordered " |
| 130 "Actual: [1, 2] " | 126 "Actual: [1, 2] " |
| 131 "Which: has too many elements (2 > 1)"); | 127 "Which: has too many elements (2 > 1)"); |
| 132 shouldFail(d, unorderedMatches([3, 2, 1]), | 128 shouldFail(d, unorderedMatches([3, 2, 1]), |
| 133 "Expected: matches [<3>, <2>, <1>] unordered " | 129 "Expected: matches [<3>, <2>, <1>] unordered " |
| 134 "Actual: [1, 2] " | 130 "Actual: [1, 2] " |
| 135 "Which: has too few elements (2 < 3)"); | 131 "Which: has too few elements (2 < 3)"); |
| 136 shouldFail(d, unorderedMatches([3, 1]), | 132 shouldFail(d, unorderedMatches([3, 1]), |
| 137 "Expected: matches [<3>, <1>] unordered " | 133 "Expected: matches [<3>, <1>] unordered " |
| 138 "Actual: [1, 2] " | 134 "Actual: [1, 2] " |
| 139 "Which: has no match for <3> at index 0"); | 135 "Which: has no match for <3> at index 0"); |
| 140 shouldFail(d, unorderedMatches([greaterThan(3), greaterThan(0)]), | 136 shouldFail(d, unorderedMatches([greaterThan(3), greaterThan(0)]), |
| 141 "Expected: matches [a value greater than <3>, a value greater than " | 137 "Expected: matches [a value greater than <3>, a value greater than " |
| 142 "<0>] unordered " | 138 "<0>] unordered " |
| 143 "Actual: [1, 2] " | 139 "Actual: [1, 2] " |
| 144 "Which: has no match for a value greater than <3> at index 0"); | 140 "Which: has no match for a value greater than <3> at index 0"); |
| 145 }); | 141 }); |
| 146 | 142 |
| 147 test('pairwise compare', () { | 143 test('pairwise compare', () { |
| 148 var c = [1, 2]; | 144 var c = [1, 2]; |
| 149 var d = [1, 2, 3]; | 145 var d = [1, 2, 3]; |
| 150 var e = [1, 4, 9]; | 146 var e = [1, 4, 9]; |
| 151 shouldFail('x', pairwiseCompare(e, (e,a) => a <= e, | 147 shouldFail('x', pairwiseCompare(e, (e, a) => a <= e, "less than or equal"), |
| 152 "less than or equal"), | |
| 153 "Expected: pairwise less than or equal [1, 4, 9] " | 148 "Expected: pairwise less than or equal [1, 4, 9] " |
| 154 "Actual: 'x' " | 149 "Actual: 'x' " |
| 155 "Which: is not an Iterable"); | 150 "Which: is not an Iterable"); |
| 156 shouldFail(c, pairwiseCompare(e, (e,a) => a <= e, "less than or equal"), | 151 shouldFail(c, pairwiseCompare(e, (e, a) => a <= e, "less than or equal"), |
| 157 "Expected: pairwise less than or equal [1, 4, 9] " | 152 "Expected: pairwise less than or equal [1, 4, 9] " |
| 158 "Actual: [1, 2] " | 153 "Actual: [1, 2] " |
| 159 "Which: has length 2 instead of 3"); | 154 "Which: has length 2 instead of 3"); |
| 160 shouldPass(d, pairwiseCompare(e, (e,a) => a <= e, "less than or equal")); | 155 shouldPass(d, pairwiseCompare(e, (e, a) => a <= e, "less than or equal")); |
| 161 shouldFail(d, pairwiseCompare(e, (e,a) => a < e, "less than"), | 156 shouldFail(d, pairwiseCompare(e, (e, a) => a < e, "less than"), |
| 162 "Expected: pairwise less than [1, 4, 9] " | 157 "Expected: pairwise less than [1, 4, 9] " |
| 163 "Actual: [1, 2, 3] " | 158 "Actual: [1, 2, 3] " |
| 164 "Which: has <1> which is not less than <1> at index 0"); | 159 "Which: has <1> which is not less than <1> at index 0"); |
| 165 shouldPass(d, pairwiseCompare(e, (e,a) => a * a == e, "square root of")); | 160 shouldPass(d, pairwiseCompare(e, (e, a) => a * a == e, "square root of")); |
| 166 shouldFail(d, pairwiseCompare(e, (e,a) => a + a == e, "double"), | 161 shouldFail(d, pairwiseCompare(e, (e, a) => a + a == e, "double"), |
| 167 "Expected: pairwise double [1, 4, 9] " | 162 "Expected: pairwise double [1, 4, 9] " |
| 168 "Actual: [1, 2, 3] " | 163 "Actual: [1, 2, 3] " |
| 169 "Which: has <1> which is not double <1> at index 0"); | 164 "Which: has <1> which is not double <1> at index 0"); |
| 170 }); | 165 }); |
| 171 } | 166 } |
| OLD | NEW |