|
1 |
| -# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
| 1 | +# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 2 | # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
3 | 3 | #
|
4 | 4 | # The Universal Permissive License (UPL), Version 1.0
|
|
37 | 37 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
38 | 38 | # SOFTWARE.
|
39 | 39 |
|
| 40 | +import unittest |
40 | 41 |
|
41 | 42 | def assert_raises(err, fn, *args, **kwargs):
|
42 | 43 | raised = False
|
@@ -534,3 +535,89 @@ def test_strip_bytes():
|
534 | 535 | assert b'abc'.strip(b'ac') == b'b'
|
535 | 536 | assert b'abc'.lstrip(b'ac') == b'bc'
|
536 | 537 | assert b'abc'.rstrip(b'ac') == b'ab'
|
| 538 | + |
| 539 | +class BaseTestSplit: |
| 540 | + |
| 541 | + def test_string_error(self): |
| 542 | + self.assertRaises(TypeError, self.type2test(b'a b').split, ' ') |
| 543 | + self.assertRaises(TypeError, self.type2test(b'a b').rsplit, ' ') |
| 544 | + |
| 545 | + def test_int_error(self): |
| 546 | + self.assertRaises(TypeError, self.type2test(b'a b').split, 32) |
| 547 | + self.assertRaises(TypeError, self.type2test(b'a b').rsplit, 32) |
| 548 | + |
| 549 | + def test_split_unicodewhitespace(self): |
| 550 | + for b in (b'a\x1Cb', b'a\x1Db', b'a\x1Eb', b'a\x1Fb'): |
| 551 | + b = self.type2test(b) |
| 552 | + self.assertEqual(b.split(), [b]) |
| 553 | + b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F") |
| 554 | + self.assertEqual(b.split(), [b'\x1c\x1d\x1e\x1f']) |
| 555 | + |
| 556 | + def test_rsplit_unicodewhitespace(self): |
| 557 | + b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F") |
| 558 | + self.assertEqual(b.rsplit(), [b'\x1c\x1d\x1e\x1f']) |
| 559 | + |
| 560 | + def test_memoryview(self): |
| 561 | + self.assertEqual(self.type2test(b'a b').split(memoryview(b' ')), [b'a', b'b']) |
| 562 | + self.assertEqual(self.type2test(b'c d').rsplit(memoryview(b' ')), [b'c', b'd']) |
| 563 | + |
| 564 | + def test_split(self): |
| 565 | + self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').split(), [b'ahoj', b'jak', b'se', b'mas']) |
| 566 | + self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').rsplit(), [b'ahoj', b'jak', b'se', b'mas']) |
| 567 | + |
| 568 | + def test_maxsplit(self): |
| 569 | + self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').split(maxsplit=1), [b'ahoj', b'jak\tse\nmas']) |
| 570 | + self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').rsplit(maxsplit=1), [b'ahoj jak\tse', b'mas']) |
| 571 | + |
| 572 | + def test_maxsplit_zero(self): |
| 573 | + self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').split(maxsplit=0), [b'ahoj jak\tse\nmas']) |
| 574 | + self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').rsplit(maxsplit=0), [b'ahoj jak\tse\nmas']) |
| 575 | + |
| 576 | + def test_maxsplit_negative(self): |
| 577 | + self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').split(maxsplit=-10), [b'ahoj', b'jak', b'se', b'mas']) |
| 578 | + self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').rsplit(maxsplit=-10), [b'ahoj', b'jak', b'se', b'mas']) |
| 579 | + |
| 580 | + def test_separator(self): |
| 581 | + self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').split(b' '), [b'ahoj', b'jak\tse\nmas']) |
| 582 | + self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').rsplit(b' '), [b'ahoj', b'jak\tse\nmas']) |
| 583 | + |
| 584 | + def test_empty(self): |
| 585 | + self.assertEqual(self.type2test(b'').split(), []) |
| 586 | + self.assertEqual(self.type2test(b'').rsplit(), []) |
| 587 | + |
| 588 | + def test_empty_delim(self): |
| 589 | + self.assertEqual(self.type2test(b'').split(b' '), [b'']) |
| 590 | + self.assertEqual(self.type2test(b'').rsplit(b' '), [b'']) |
| 591 | + |
| 592 | + def test_empty_separator(self): |
| 593 | + self.assertRaises(ValueError, self.type2test(b'a b').split, b'') |
| 594 | + self.assertRaises(ValueError, self.type2test(b'a b').rsplit, b'') |
| 595 | + |
| 596 | + def test_indexable_object(self): |
| 597 | + |
| 598 | + class MyIndexable(object): |
| 599 | + def __init__(self, value): |
| 600 | + self.value = value |
| 601 | + def __index__(self): |
| 602 | + return self.value |
| 603 | + |
| 604 | + self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').split(maxsplit=MyIndexable(1)), [b'ahoj', b'jak\tse\nmas']) |
| 605 | + self.assertEqual(self.type2test(b'ahoj jak\tse\nmas').rsplit(maxsplit=MyIndexable(1)), [b'ahoj jak\tse', b'mas']) |
| 606 | + |
| 607 | +class BytesSplitTest(BaseTestSplit, unittest.TestCase): |
| 608 | + type2test = bytes |
| 609 | + |
| 610 | +class ByteArraySplitTest(BaseTestSplit, unittest.TestCase): |
| 611 | + type2test = bytearray |
| 612 | + |
| 613 | +def test_add_mv_to_bytes(): |
| 614 | + b = b'hello ' |
| 615 | + mv = memoryview(b'world') |
| 616 | + b += mv |
| 617 | + assert b == b'hello world' |
| 618 | + |
| 619 | +def test_add_mv_to_bytearray(): |
| 620 | + ba = bytearray(b'hello ') |
| 621 | + mv = memoryview(b'world') |
| 622 | + ba += mv |
| 623 | + assert ba == b'hello world' |
0 commit comments