|
| 1 | +import 'package:fluent_assertions/src/iterable/iterable_matchers.dart'; |
| 2 | +import 'package:test/test.dart'; |
| 3 | + |
| 4 | +extension IterableObjectAssertions on Iterable<Object> { |
| 5 | + /// Asserts that every value in [Iterable] is of type [E]. |
| 6 | + /// [E] - Expected type. |
| 7 | + void shouldAllBeInstanceOf<E>() { |
| 8 | + expect( |
| 9 | + this, |
| 10 | + everyElement((e) => e is E), |
| 11 | + reason: 'Expected all elements to be instances of $E, but some were not.', |
| 12 | + ); |
| 13 | + } |
| 14 | + |
| 15 | + /// Asserts that any value in [Iterable] is of type [E]. |
| 16 | + /// [E] - Expected type. |
| 17 | + void shouldAnyBeInstanceOf<E>() { |
| 18 | + expect( |
| 19 | + this, |
| 20 | + anyElement((e) => e is E), |
| 21 | + reason: |
| 22 | + 'Expected at least one element to be an instance of $E, but none were.', |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + /// Asserts that every value in [Iterable] is not of type [E]. |
| 27 | + /// [E] - Expected type. |
| 28 | + void shouldNoneBeInstanceOf<E>() { |
| 29 | + expect( |
| 30 | + this, |
| 31 | + everyElement((e) => e is! E), |
| 32 | + reason: 'Expected no elements to be instances of $E, but some were.', |
| 33 | + ); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +extension IterableAssertions<T> on Iterable<T> { |
| 38 | + /// Asserts that [Iterable] has [expectedSize] elements |
| 39 | + void shouldHaveSize(int expectedSize) { |
| 40 | + expect( |
| 41 | + this, |
| 42 | + hasLength(expectedSize), |
| 43 | + reason: |
| 44 | + 'Expected iterable to have $expectedSize elements, but found $length.', |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + /// Asserts that [Iterable] is empty. |
| 49 | + void shouldBeEmpty() { |
| 50 | + expect( |
| 51 | + this, |
| 52 | + isEmpty, |
| 53 | + reason: 'Expected iterable to be empty, but it was not.', |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /// Asserts that [Iterable] is not empty. |
| 58 | + void shouldNotBeEmpty() { |
| 59 | + expect( |
| 60 | + this, |
| 61 | + isNot(isEmpty), |
| 62 | + reason: 'Expected iterable to not be empty, but it was.', |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /// Asserts that [Iterable] has 1 element. |
| 67 | + void shouldHaveSingleItem() { |
| 68 | + expect( |
| 69 | + this, |
| 70 | + hasLength(1), |
| 71 | + reason: 'Expected iterable to have exactly one item, but has $length.', |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /// Asserts that [Iterable] contains [expected]. |
| 76 | + void shouldContain(T? expected) { |
| 77 | + expect( |
| 78 | + this, |
| 79 | + contains(expected), |
| 80 | + reason: 'Expected iterable to contain $expected, but it did not.', |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + /// Asserts that [Iterable] does not contain [expected]. |
| 85 | + void shouldNotContain(T? expected) { |
| 86 | + expect( |
| 87 | + this, |
| 88 | + isNot(contains(expected)), |
| 89 | + reason: 'Expected iterable to not contain $expected, but it did.', |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + /// Asserts that [Iterable] contains any of [expected]. |
| 94 | + void shouldContainAny(Iterable<T> expected) { |
| 95 | + expect( |
| 96 | + this, |
| 97 | + containsAny(expected.map(equals).toList()), |
| 98 | + reason: |
| 99 | + 'Expected at least one element from $expected to be present in $this.', |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + /// Asserts that [Iterable] contains none of [expected]. |
| 104 | + void shouldContainNone(Iterable<T> expected) { |
| 105 | + expect( |
| 106 | + this, |
| 107 | + isNot(containsAny(expected.map(equals).toList())), |
| 108 | + reason: |
| 109 | + 'Expected iterable to contain none of $expected, but some were found.', |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + /// Asserts that [Iterable] contains an element matching every value in |
| 114 | + /// [expected] in any order, and may contain additional values. |
| 115 | + void shouldContainAll(Iterable<T> expected) { |
| 116 | + expect( |
| 117 | + this, |
| 118 | + containsAll(expected), |
| 119 | + reason: |
| 120 | + 'Expected iterable to contain all of $expected, but some were missing.', |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + /// Asserts that [Iterable] contains an element matching every value in |
| 125 | + /// [expected] in the same order, but may contain additional values |
| 126 | + /// interleaved throughout. |
| 127 | + void shouldContainAllInOrder(Iterable<T> expected) { |
| 128 | + expect( |
| 129 | + this, |
| 130 | + containsAllInOrder(expected), |
| 131 | + reason: |
| 132 | + 'Expected iterable to contain all of $expected in order, but it did not.', |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + /// Asserts that [Iterable] contains an element matching the given [predicate]. |
| 137 | + void shouldContainAnyThat(bool Function(T argument) predicate) { |
| 138 | + expect( |
| 139 | + this, |
| 140 | + anyElement(predicate), |
| 141 | + reason: |
| 142 | + 'Expected iterable to contain at least one element matching the predicate, but none did.', |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + /// Asserts that all [Iterable] elements match [predicate]. |
| 147 | + void shouldContainAllThat(bool Function(T argument) predicate) { |
| 148 | + expect( |
| 149 | + this, |
| 150 | + everyElement(predicate), |
| 151 | + reason: |
| 152 | + 'Expected every element in iterable to match the predicate, but some did not.', |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + /// Asserts that none [Iterable] elements match [predicate]. |
| 157 | + void shouldContainNoneThat(bool Function(T argument) predicate) { |
| 158 | + expect( |
| 159 | + this, |
| 160 | + everyElement(isNot(predicate)), |
| 161 | + reason: |
| 162 | + 'Expected no elements in iterable to match the predicate, but some did.', |
| 163 | + ); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +extension IterableStringAssertions on Iterable<String> { |
| 168 | + /// Asserts that [Iterable] contains [expected] case-insensitively. |
| 169 | + void shouldContainIgnoringCase(String expected) { |
| 170 | + expect( |
| 171 | + this, |
| 172 | + containsAll([equalsIgnoringCase(expected)]), |
| 173 | + reason: |
| 174 | + 'Expected iterable to contain "$expected" ignoring case, but it did not.', |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + /// Asserts that [Iterable] contains [expected] case-insensitively. |
| 179 | + void shouldNotContainIgnoringCase(String expected) { |
| 180 | + expect( |
| 181 | + this, |
| 182 | + isNot(containsAll([equalsIgnoringCase(expected)])), |
| 183 | + reason: |
| 184 | + 'Expected iterable to not contain "$expected" ignoring case, but it did.', |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + /// Asserts that [Iterable] contains any of [expected] case-insensitively. |
| 189 | + void shouldContainAnyIgnoringCase(Iterable<String> expected) { |
| 190 | + expect( |
| 191 | + this, |
| 192 | + containsAny(expected.map((e) => equalsIgnoringCase(e)).toList()), |
| 193 | + reason: |
| 194 | + 'Expected iterable to contain any of $expected ignoring case, but none were found.', |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + /// Asserts that [Iterable] contains none of [expected] case-insensitively. |
| 199 | + void shouldContainNoneIgnoringCase(Iterable<String> expected) { |
| 200 | + expect( |
| 201 | + this, |
| 202 | + isNot(containsAny(expected.map((e) => equalsIgnoringCase(e)).toList())), |
| 203 | + reason: |
| 204 | + 'Expected iterable to contain none of $expected ignoring case, but some were found.', |
| 205 | + ); |
| 206 | + } |
| 207 | + |
| 208 | + /// Asserts that [Iterable] contains an element matching every value in |
| 209 | + /// [expected] in any order, case-insensitively, and may contain additional |
| 210 | + /// values. |
| 211 | + void shouldContainAllIgnoringCase(Iterable<String> expected) { |
| 212 | + expect( |
| 213 | + this, |
| 214 | + containsAll(expected.map((e) => equalsIgnoringCase(e))), |
| 215 | + reason: |
| 216 | + 'Expected iterable to contain all of $expected ignoring case, but some were missing.', |
| 217 | + ); |
| 218 | + } |
| 219 | + |
| 220 | + /// Asserts that [Iterable] contains an element matching every value in |
| 221 | + /// [expected] in the same order and case-insensitively but may contain |
| 222 | + /// additional values interleaved throughout. |
| 223 | + void shouldContainAllInOrderIgnoringCase(Iterable<String> expected) { |
| 224 | + expect( |
| 225 | + this, |
| 226 | + containsAllInOrder(expected.map((e) => equalsIgnoringCase(e))), |
| 227 | + reason: |
| 228 | + 'Expected iterable to contain all of $expected in order ignoring case, but it did not.', |
| 229 | + ); |
| 230 | + } |
| 231 | +} |
0 commit comments