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