Test the iterator-based split methods. This is slightly different from `split` and `rsplit` in that it returns an iterator instead of a list. Moreover, the native `rsplit` and even `rsplit_byteset` report results in the identical order to `split` and `split_byteset`. Here `rsplit_it
()
| 374 | |
| 375 | |
| 376 | def test_unit_split_iterators(): |
| 377 | """ |
| 378 | Test the iterator-based split methods. |
| 379 | This is slightly different from `split` and `rsplit` in that it returns an iterator instead of a list. |
| 380 | Moreover, the native `rsplit` and even `rsplit_byteset` report results in the identical order to `split` |
| 381 | and `split_byteset`. Here `rsplit_iter` reports elements in the reverse order, compared to `split_iter`. |
| 382 | """ |
| 383 | native = "line1\nline2\nline3" |
| 384 | big = Str(native) |
| 385 | |
| 386 | # Splitting using a string |
| 387 | lines = list(sz.split_iter(big, "\n")) |
| 388 | assert lines == ["line1", "line2", "line3"] |
| 389 | |
| 390 | lines = list(sz.rsplit_iter(big, "\n")) |
| 391 | assert lines == ["line3", "line2", "line1"] |
| 392 | |
| 393 | lines = list(sz.split_iter(big, "\n", keepseparator=True)) |
| 394 | assert lines == ["line1\n", "line2\n", "line3"] |
| 395 | |
| 396 | lines = list(sz.rsplit_iter(big, "\n", keepseparator=True)) |
| 397 | assert lines == ["\nline3", "\nline2", "line1"] |
| 398 | |
| 399 | letters = list(sz.split_iter("a b c d")) |
| 400 | assert letters == ["a", "b", "c", "d"] |
| 401 | |
| 402 | # Splitting using character sets |
| 403 | letters = list(sz.split_byteset_iter("a-b_c-d", "-_")) |
| 404 | assert letters == ["a", "b", "c", "d"] |
| 405 | |
| 406 | letters = list(sz.rsplit_byteset_iter("a-b_c-d", "-_")) |
| 407 | assert letters == ["d", "c", "b", "a"] |
| 408 | |
| 409 | # Check for equivalence with native Python strings, including boundary conditions |
| 410 | assert native.split("line1") == list(big.split_iter("line1")) |
| 411 | assert native.split("line3") == list(big.split_iter("line3")) |
| 412 | assert native.split("\n", maxsplit=0) == list(big.split_iter("\n", maxsplit=0)) |
| 413 | assert native.split("\n", maxsplit=1) == list(big.split_iter("\n", maxsplit=1)) |
| 414 | assert native.split("\n", maxsplit=2) == list(big.split_iter("\n", maxsplit=2)) |
| 415 | assert native.split("\n", maxsplit=3) == list(big.split_iter("\n", maxsplit=3)) |
| 416 | assert native.split("\n", maxsplit=4) == list(big.split_iter("\n", maxsplit=4)) |
| 417 | |
| 418 | def rlist(seq): |
| 419 | seq = list(seq) |
| 420 | seq.reverse() |
| 421 | return seq |
| 422 | |
| 423 | # Check for equivalence with native Python strings in reverse order, including boundary conditions |
| 424 | assert native.rsplit("line1") == rlist(big.rsplit_iter("line1")) |
| 425 | assert native.rsplit("line3") == rlist(big.rsplit_iter("line3")) |
| 426 | assert native.rsplit("\n", maxsplit=0) == rlist(big.rsplit_iter("\n", maxsplit=0)) |
| 427 | assert native.rsplit("\n", maxsplit=1) == rlist(big.rsplit_iter("\n", maxsplit=1)) |
| 428 | assert native.rsplit("\n", maxsplit=2) == rlist(big.rsplit_iter("\n", maxsplit=2)) |
| 429 | assert native.rsplit("\n", maxsplit=3) == rlist(big.rsplit_iter("\n", maxsplit=3)) |
| 430 | assert native.rsplit("\n", maxsplit=4) == rlist(big.rsplit_iter("\n", maxsplit=4)) |
| 431 | |
| 432 | # If the passed separator is an empty string, the library must raise a `ValueError` |
| 433 | with pytest.raises(ValueError): |