|
7 | 7 | use Smoren\ArrayView\Exceptions\SizeError; |
8 | 8 | use Smoren\ArrayView\Exceptions\ValueError; |
9 | 9 | use Smoren\ArrayView\Selectors\MaskSelector; |
| 10 | +use Smoren\ArrayView\Selectors\SliceSelector; |
10 | 11 | use Smoren\ArrayView\Views\ArrayView; |
11 | 12 |
|
12 | 13 | class ErrorsTest extends \Codeception\Test\Unit |
@@ -127,6 +128,64 @@ public function testWriteByMaskSizeError(array $source, array $boolMask) |
127 | 128 | $view[new MaskSelector($boolMask)] = $boolMask; |
128 | 129 | } |
129 | 130 |
|
| 131 | + /** |
| 132 | + * @dataProvider dataProviderForInvalidSlice |
| 133 | + */ |
| 134 | + public function testInvalidSliceRead(array $source, string $slice) |
| 135 | + { |
| 136 | + $view = ArrayView::toView($source); |
| 137 | + |
| 138 | + $this->expectException(IndexError::class); |
| 139 | + $this->expectExceptionMessage("Step cannot be 0."); |
| 140 | + |
| 141 | + $_ = $view[new SliceSelector($slice)]; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @dataProvider dataProviderForInvalidSlice |
| 146 | + */ |
| 147 | + public function testInvalidSliceWrite(array $source, string $slice) |
| 148 | + { |
| 149 | + $view = ArrayView::toView($source); |
| 150 | + |
| 151 | + $this->expectException(IndexError::class); |
| 152 | + $this->expectExceptionMessage("Step cannot be 0."); |
| 153 | + |
| 154 | + $view[new SliceSelector($slice)] = [1, 2, 3]; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @dataProvider dataProviderForApplyWithSizeError |
| 159 | + */ |
| 160 | + public function testApplyWithSizeError(array $source, callable $viewGetter, callable $mapper, array $toApplyWith) |
| 161 | + { |
| 162 | + $view = ArrayView::toView($source); |
| 163 | + |
| 164 | + $sourceSize = \count($source); |
| 165 | + $argSize = \count($toApplyWith); |
| 166 | + |
| 167 | + $this->expectException(SizeError::class); |
| 168 | + $this->expectExceptionMessage("Length of values array not equal to view length ({$argSize} != {$sourceSize})."); |
| 169 | + |
| 170 | + $view->applyWith($toApplyWith, $mapper); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @dataProvider dataProviderForWriteSizeError |
| 175 | + */ |
| 176 | + public function testWriteSizeError(array $source, callable $viewGetter, array $toWrite) |
| 177 | + { |
| 178 | + $view = ArrayView::toView($source); |
| 179 | + |
| 180 | + $sourceSize = \count($source); |
| 181 | + $argSize = \count($toWrite); |
| 182 | + |
| 183 | + $this->expectException(SizeError::class); |
| 184 | + $this->expectExceptionMessage("Length of values array not equal to view length ({$argSize} != {$sourceSize})."); |
| 185 | + |
| 186 | + $view[':'] = $toWrite; |
| 187 | + } |
| 188 | + |
130 | 189 | /** |
131 | 190 | * @dataProvider dataProviderForNonSequentialError |
132 | 191 | */ |
@@ -177,6 +236,97 @@ public function dataProviderForBadSizeMask(): array |
177 | 236 | ]; |
178 | 237 | } |
179 | 238 |
|
| 239 | + public function dataProviderForInvalidSlice(): array |
| 240 | + { |
| 241 | + return [ |
| 242 | + [[], '::0'], |
| 243 | + [[], '0:0:0'], |
| 244 | + [[], '0:1:0'], |
| 245 | + [[], '0::0'], |
| 246 | + [[], ':-1:0'], |
| 247 | + [[], '1:-1:0'], |
| 248 | + [[1], '::0'], |
| 249 | + [[1], '0:0:0'], |
| 250 | + [[1], '0:1:0'], |
| 251 | + [[1], '0::0'], |
| 252 | + [[1], ':-1:0'], |
| 253 | + [[1], '1:-1:0'], |
| 254 | + [[1, 2, 3], '::0'], |
| 255 | + [[1, 2, 3], '0:0:0'], |
| 256 | + [[1, 2, 3], '0:1:0'], |
| 257 | + [[1, 2, 3], '0::0'], |
| 258 | + [[1, 2, 3], ':-1:0'], |
| 259 | + [[1, 2, 3], '1:-1:0'], |
| 260 | + ]; |
| 261 | + } |
| 262 | + |
| 263 | + public function dataProviderForApplyWithSizeError(): array |
| 264 | + { |
| 265 | + return [ |
| 266 | + [ |
| 267 | + [], |
| 268 | + fn (array &$source) => ArrayView::toView($source), |
| 269 | + fn (int $item) => $item, |
| 270 | + [1], |
| 271 | + ], |
| 272 | + [ |
| 273 | + [1], |
| 274 | + fn (array &$source) => ArrayView::toView($source), |
| 275 | + fn (int $item) => $item, |
| 276 | + [], |
| 277 | + ], |
| 278 | + [ |
| 279 | + [1], |
| 280 | + fn (array &$source) => ArrayView::toView($source), |
| 281 | + fn (int $item) => $item, |
| 282 | + [1, 2], |
| 283 | + ], |
| 284 | + [ |
| 285 | + [1, 2, 3], |
| 286 | + fn (array &$source) => ArrayView::toView($source), |
| 287 | + fn (int $item) => $item, |
| 288 | + [1, 2], |
| 289 | + ], |
| 290 | + [ |
| 291 | + [1, 2, 3], |
| 292 | + fn (array &$source) => ArrayView::toView($source), |
| 293 | + fn (int $item) => $item, |
| 294 | + [1, 2, 3, 4, 5], |
| 295 | + ], |
| 296 | + ]; |
| 297 | + } |
| 298 | + |
| 299 | + public function dataProviderForWriteSizeError(): array |
| 300 | + { |
| 301 | + return [ |
| 302 | + [ |
| 303 | + [], |
| 304 | + fn (array &$source) => ArrayView::toView($source), |
| 305 | + [1], |
| 306 | + ], |
| 307 | + [ |
| 308 | + [1], |
| 309 | + fn (array &$source) => ArrayView::toView($source), |
| 310 | + [], |
| 311 | + ], |
| 312 | + [ |
| 313 | + [1], |
| 314 | + fn (array &$source) => ArrayView::toView($source), |
| 315 | + [1, 2], |
| 316 | + ], |
| 317 | + [ |
| 318 | + [1, 2, 3], |
| 319 | + fn (array &$source) => ArrayView::toView($source), |
| 320 | + [1, 2], |
| 321 | + ], |
| 322 | + [ |
| 323 | + [1, 2, 3], |
| 324 | + fn (array &$source) => ArrayView::toView($source), |
| 325 | + [1, 2, 3, 4, 5], |
| 326 | + ], |
| 327 | + ]; |
| 328 | + } |
| 329 | + |
180 | 330 | public function dataProviderForNonSequentialError(): array |
181 | 331 | { |
182 | 332 | return [ |
|
0 commit comments