OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library matcher.core_matchers_test; |
| 6 |
| 7 import 'package:matcher/matcher.dart'; |
| 8 import 'package:test/test.dart' show test, group; |
| 9 |
| 10 import 'test_utils.dart'; |
| 11 |
| 12 void main() { |
| 13 test('isTrue', () { |
| 14 shouldPass(true, isTrue); |
| 15 shouldFail(false, isTrue, "Expected: true Actual: <false>"); |
| 16 }); |
| 17 |
| 18 test('isFalse', () { |
| 19 shouldPass(false, isFalse); |
| 20 shouldFail(10, isFalse, "Expected: false Actual: <10>"); |
| 21 shouldFail(true, isFalse, "Expected: false Actual: <true>"); |
| 22 }); |
| 23 |
| 24 test('isNull', () { |
| 25 shouldPass(null, isNull); |
| 26 shouldFail(false, isNull, "Expected: null Actual: <false>"); |
| 27 }); |
| 28 |
| 29 test('isNotNull', () { |
| 30 shouldPass(false, isNotNull); |
| 31 shouldFail(null, isNotNull, "Expected: not null Actual: <null>"); |
| 32 }); |
| 33 |
| 34 test('isNaN', () { |
| 35 shouldPass(double.NAN, isNaN); |
| 36 shouldFail(3.1, isNaN, "Expected: NaN Actual: <3.1>"); |
| 37 }); |
| 38 |
| 39 test('isNotNaN', () { |
| 40 shouldPass(3.1, isNotNaN); |
| 41 shouldFail(double.NAN, isNotNaN, "Expected: not NaN Actual: <NaN>"); |
| 42 }); |
| 43 |
| 44 test('same', () { |
| 45 var a = new Map(); |
| 46 var b = new Map(); |
| 47 shouldPass(a, same(a)); |
| 48 shouldFail(b, same(a), "Expected: same instance as {} Actual: {}"); |
| 49 }); |
| 50 |
| 51 test('equals', () { |
| 52 var a = new Map(); |
| 53 var b = new Map(); |
| 54 shouldPass(a, equals(a)); |
| 55 shouldPass(a, equals(b)); |
| 56 }); |
| 57 |
| 58 test('equals with a set', () { |
| 59 var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 60 var set1 = numbers.toSet(); |
| 61 numbers.shuffle(); |
| 62 var set2 = numbers.toSet(); |
| 63 |
| 64 shouldPass(set2, equals(set1)); |
| 65 shouldPass(numbers, equals(set1)); |
| 66 shouldFail([ |
| 67 1, |
| 68 2, |
| 69 3, |
| 70 4, |
| 71 5, |
| 72 6, |
| 73 7, |
| 74 8, |
| 75 9 |
| 76 ], equals(set1), matches(r"Expected: .*:\[1, 2, 3, 4, 5, 6, 7, 8, 9, 10\]" |
| 77 r" Actual: \[1, 2, 3, 4, 5, 6, 7, 8, 9\]" |
| 78 r" Which: does not contain 10")); |
| 79 shouldFail([ |
| 80 1, |
| 81 2, |
| 82 3, |
| 83 4, |
| 84 5, |
| 85 6, |
| 86 7, |
| 87 8, |
| 88 9, |
| 89 10, |
| 90 11 |
| 91 ], equals(set1), matches(r"Expected: .*:\[1, 2, 3, 4, 5, 6, 7, 8, 9, 10\]" |
| 92 r" Actual: \[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11\]" |
| 93 r" Which: larger than expected")); |
| 94 }); |
| 95 |
| 96 test('anything', () { |
| 97 var a = new Map(); |
| 98 shouldPass(0, anything); |
| 99 shouldPass(null, anything); |
| 100 shouldPass(a, anything); |
| 101 shouldFail(a, isNot(anything), "Expected: not anything Actual: {}"); |
| 102 }); |
| 103 |
| 104 test('returnsNormally', () { |
| 105 shouldPass(doesNotThrow, returnsNormally); |
| 106 shouldFail(doesThrow, returnsNormally, matches(r"Expected: return normally" |
| 107 r" Actual: <Closure(: \(\) => dynamic " |
| 108 r"from Function 'doesThrow': static\.)?>" |
| 109 r" Which: threw 'X'")); |
| 110 }); |
| 111 |
| 112 test('hasLength', () { |
| 113 var a = new Map(); |
| 114 var b = new List(); |
| 115 shouldPass(a, hasLength(0)); |
| 116 shouldPass(b, hasLength(0)); |
| 117 shouldPass('a', hasLength(1)); |
| 118 shouldFail(0, hasLength(0), |
| 119 "Expected: an object with length of <0> " |
| 120 "Actual: <0> " |
| 121 "Which: has no length property"); |
| 122 |
| 123 b.add(0); |
| 124 shouldPass(b, hasLength(1)); |
| 125 shouldFail(b, hasLength(2), "Expected: an object with length of <2> " |
| 126 "Actual: [0] " |
| 127 "Which: has length of <1>"); |
| 128 |
| 129 b.add(0); |
| 130 shouldFail(b, hasLength(1), "Expected: an object with length of <1> " |
| 131 "Actual: [0, 0] " |
| 132 "Which: has length of <2>"); |
| 133 shouldPass(b, hasLength(2)); |
| 134 }); |
| 135 |
| 136 test('scalar type mismatch', () { |
| 137 shouldFail('error', equals(5.1), "Expected: <5.1> " |
| 138 "Actual: 'error'"); |
| 139 }); |
| 140 |
| 141 test('nested type mismatch', () { |
| 142 shouldFail(['error'], equals([5.1]), "Expected: [5.1] " |
| 143 "Actual: ['error'] " |
| 144 "Which: was 'error' instead of <5.1> at location [0]"); |
| 145 }); |
| 146 |
| 147 test('doubly-nested type mismatch', () { |
| 148 shouldFail([['error']], equals([[5.1]]), "Expected: [[5.1]] " |
| 149 "Actual: [['error']] " |
| 150 "Which: was 'error' instead of <5.1> at location [0][0]"); |
| 151 }); |
| 152 |
| 153 test('doubly nested inequality', () { |
| 154 var actual1 = [['foo', 'bar'], ['foo'], 3, []]; |
| 155 var expected1 = [['foo', 'bar'], ['foo'], 4, []]; |
| 156 var reason1 = "Expected: [['foo', 'bar'], ['foo'], 4, []] " |
| 157 "Actual: [['foo', 'bar'], ['foo'], 3, []] " |
| 158 "Which: was <3> instead of <4> at location [2]"; |
| 159 |
| 160 var actual2 = [['foo', 'barry'], ['foo'], 4, []]; |
| 161 var expected2 = [['foo', 'bar'], ['foo'], 4, []]; |
| 162 var reason2 = "Expected: [['foo', 'bar'], ['foo'], 4, []] " |
| 163 "Actual: [['foo', 'barry'], ['foo'], 4, []] " |
| 164 "Which: was 'barry' instead of 'bar' at location [0][1]"; |
| 165 |
| 166 var actual3 = [['foo', 'bar'], ['foo'], 4, {'foo': 'bar'}]; |
| 167 var expected3 = [['foo', 'bar'], ['foo'], 4, {'foo': 'barry'}]; |
| 168 var reason3 = "Expected: [['foo', 'bar'], ['foo'], 4, {'foo': 'barry'}] " |
| 169 "Actual: [['foo', 'bar'], ['foo'], 4, {'foo': 'bar'}] " |
| 170 "Which: was 'bar' instead of 'barry' at location [3]['foo']"; |
| 171 |
| 172 shouldFail(actual1, equals(expected1), reason1); |
| 173 shouldFail(actual2, equals(expected2), reason2); |
| 174 shouldFail(actual3, equals(expected3), reason3); |
| 175 }); |
| 176 |
| 177 test('isInstanceOf', () { |
| 178 shouldFail(0, new isInstanceOf<String>(), |
| 179 "Expected: an instance of String Actual: <0>"); |
| 180 shouldPass('cow', new isInstanceOf<String>()); |
| 181 }); |
| 182 |
| 183 group('Predicate Matchers', () { |
| 184 test('isInstanceOf', () { |
| 185 shouldFail(0, predicate((x) => x is String, "an instance of String"), |
| 186 "Expected: an instance of String Actual: <0>"); |
| 187 shouldPass('cow', predicate((x) => x is String, "an instance of String")); |
| 188 }); |
| 189 }); |
| 190 |
| 191 test("Feature Matcher", () { |
| 192 var w = new Widget(); |
| 193 w.price = 10; |
| 194 shouldPass(w, new HasPrice(10)); |
| 195 shouldPass(w, new HasPrice(greaterThan(0))); |
| 196 shouldFail(w, new HasPrice(greaterThan(10)), |
| 197 "Expected: Widget with a price that is a value greater than <10> " |
| 198 "Actual: <Instance of 'Widget'> " |
| 199 "Which: has price with value <10> which is not " |
| 200 "a value greater than <10>"); |
| 201 }); |
| 202 } |
OLD | NEW |