| OLD | NEW |
| 1 import operator | 1 import operator |
| 2 import sys | 2 import sys |
| 3 import types | 3 import types |
| 4 import unittest |
| 4 | 5 |
| 5 import py | 6 import py |
| 6 | 7 |
| 7 import six | 8 import six |
| 8 | 9 |
| 9 | 10 |
| 10 def test_add_doc(): | 11 def test_add_doc(): |
| 11 def f(): | 12 def f(): |
| 12 """Icky doc""" | 13 """Icky doc""" |
| 13 pass | 14 pass |
| (...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 def with_kw(*args, **kw): | 383 def with_kw(*args, **kw): |
| 383 record.append(kw["kw"]) | 384 record.append(kw["kw"]) |
| 384 return old(*args) | 385 return old(*args) |
| 385 old = getattr(MyDict, stock_method_name(name)) | 386 old = getattr(MyDict, stock_method_name(name)) |
| 386 monkeypatch.setattr(MyDict, stock_method_name(name), with_kw) | 387 monkeypatch.setattr(MyDict, stock_method_name(name), with_kw) |
| 387 meth(d, kw=42) | 388 meth(d, kw=42) |
| 388 assert record == [42] | 389 assert record == [42] |
| 389 monkeypatch.undo() | 390 monkeypatch.undo() |
| 390 | 391 |
| 391 | 392 |
| 393 @py.test.mark.skipif(sys.version_info[:2] < (2, 7), |
| 394 reason="view methods on dictionaries only available on 2.7+") |
| 395 def test_dictionary_views(): |
| 396 def stock_method_name(viewwhat): |
| 397 """Given a method suffix like "keys" or "values", return the name |
| 398 of the dict method that delivers those on the version of Python |
| 399 we're running in.""" |
| 400 if six.PY3: |
| 401 return viewwhat |
| 402 return 'view' + viewwhat |
| 403 |
| 404 d = dict(zip(range(10), (range(11, 20)))) |
| 405 for name in "keys", "values", "items": |
| 406 meth = getattr(six, "view" + name) |
| 407 view = meth(d) |
| 408 assert set(view) == set(getattr(d, name)()) |
| 409 |
| 410 |
| 392 def test_advance_iterator(): | 411 def test_advance_iterator(): |
| 393 assert six.next is six.advance_iterator | 412 assert six.next is six.advance_iterator |
| 394 l = [1, 2] | 413 l = [1, 2] |
| 395 it = iter(l) | 414 it = iter(l) |
| 396 assert six.next(it) == 1 | 415 assert six.next(it) == 1 |
| 397 assert six.next(it) == 2 | 416 assert six.next(it) == 2 |
| 398 py.test.raises(StopIteration, six.next, it) | 417 py.test.raises(StopIteration, six.next, it) |
| 399 py.test.raises(StopIteration, six.next, it) | 418 py.test.raises(StopIteration, six.next, it) |
| 400 | 419 |
| 401 | 420 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 563 try: | 582 try: |
| 564 six.reraise(tp, None, tb) | 583 six.reraise(tp, None, tb) |
| 565 except Exception: | 584 except Exception: |
| 566 tp2, value2, tb2 = sys.exc_info() | 585 tp2, value2, tb2 = sys.exc_info() |
| 567 assert tp2 is Exception | 586 assert tp2 is Exception |
| 568 assert value2 is not val | 587 assert value2 is not val |
| 569 assert isinstance(value2, Exception) | 588 assert isinstance(value2, Exception) |
| 570 assert tb is get_next(tb2) | 589 assert tb is get_next(tb2) |
| 571 | 590 |
| 572 | 591 |
| 592 def test_raise_from(): |
| 593 try: |
| 594 try: |
| 595 raise Exception("blah") |
| 596 except Exception: |
| 597 ctx = sys.exc_info()[1] |
| 598 f = Exception("foo") |
| 599 six.raise_from(f, None) |
| 600 except Exception: |
| 601 tp, val, tb = sys.exc_info() |
| 602 if sys.version_info[:2] > (3, 0): |
| 603 # We should have done a raise f from None equivalent. |
| 604 assert val.__cause__ is None |
| 605 assert val.__context__ is ctx |
| 606 if sys.version_info[:2] >= (3, 3): |
| 607 # And that should suppress the context on the exception. |
| 608 assert val.__suppress_context__ |
| 609 # For all versions the outer exception should have raised successfully. |
| 610 assert str(val) == "foo" |
| 611 |
| 612 |
| 573 def test_print_(): | 613 def test_print_(): |
| 574 save = sys.stdout | 614 save = sys.stdout |
| 575 out = sys.stdout = six.moves.StringIO() | 615 out = sys.stdout = six.moves.StringIO() |
| 576 try: | 616 try: |
| 577 six.print_("Hello,", "person!") | 617 six.print_("Hello,", "person!") |
| 578 finally: | 618 finally: |
| 579 sys.stdout = save | 619 sys.stdout = save |
| 580 assert out.getvalue() == "Hello, person!\n" | 620 assert out.getvalue() == "Hello, person!\n" |
| 581 out = six.StringIO() | 621 out = six.StringIO() |
| 582 six.print_("Hello,", "person!", file=out) | 622 six.print_("Hello,", "person!", file=out) |
| 583 assert out.getvalue() == "Hello, person!\n" | 623 assert out.getvalue() == "Hello, person!\n" |
| 584 out = six.StringIO() | 624 out = six.StringIO() |
| 585 six.print_("Hello,", "person!", file=out, end="") | 625 six.print_("Hello,", "person!", file=out, end="") |
| 586 assert out.getvalue() == "Hello, person!" | 626 assert out.getvalue() == "Hello, person!" |
| 587 out = six.StringIO() | 627 out = six.StringIO() |
| 588 six.print_("Hello,", "person!", file=out, sep="X") | 628 six.print_("Hello,", "person!", file=out, sep="X") |
| 589 assert out.getvalue() == "Hello,Xperson!\n" | 629 assert out.getvalue() == "Hello,Xperson!\n" |
| 590 out = six.StringIO() | 630 out = six.StringIO() |
| 591 six.print_(six.u("Hello,"), six.u("person!"), file=out) | 631 six.print_(six.u("Hello,"), six.u("person!"), file=out) |
| 592 result = out.getvalue() | 632 result = out.getvalue() |
| 593 assert isinstance(result, six.text_type) | 633 assert isinstance(result, six.text_type) |
| 594 assert result == six.u("Hello, person!\n") | 634 assert result == six.u("Hello, person!\n") |
| 595 six.print_("Hello", file=None) # This works. | 635 six.print_("Hello", file=None) # This works. |
| 596 out = six.StringIO() | 636 out = six.StringIO() |
| 597 six.print_(None, file=out) | 637 six.print_(None, file=out) |
| 598 assert out.getvalue() == "None\n" | 638 assert out.getvalue() == "None\n" |
| 639 class FlushableStringIO(six.StringIO): |
| 640 def __init__(self): |
| 641 six.StringIO.__init__(self) |
| 642 self.flushed = False |
| 643 def flush(self): |
| 644 self.flushed = True |
| 645 out = FlushableStringIO() |
| 646 six.print_("Hello", file=out) |
| 647 assert not out.flushed |
| 648 six.print_("Hello", file=out, flush=True) |
| 649 assert out.flushed |
| 599 | 650 |
| 600 | 651 |
| 601 @py.test.mark.skipif("sys.version_info[:2] >= (2, 6)") | 652 @py.test.mark.skipif("sys.version_info[:2] >= (2, 6)") |
| 602 def test_print_encoding(monkeypatch): | 653 def test_print_encoding(monkeypatch): |
| 603 # Fool the type checking in print_. | 654 # Fool the type checking in print_. |
| 604 monkeypatch.setattr(six, "file", six.BytesIO, raising=False) | 655 monkeypatch.setattr(six, "file", six.BytesIO, raising=False) |
| 605 out = six.BytesIO() | 656 out = six.BytesIO() |
| 606 out.encoding = "utf-8" | 657 out.encoding = "utf-8" |
| 607 out.errors = None | 658 out.errors = None |
| 608 six.print_(six.u("\u053c"), end="", file=out) | 659 six.print_(six.u("\u053c"), end="", file=out) |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 pass | 706 pass |
| 656 original_k = k | 707 original_k = k |
| 657 k = f(f(k)) | 708 k = f(f(k)) |
| 658 assert hasattr(k, '__wrapped__') | 709 assert hasattr(k, '__wrapped__') |
| 659 k = k.__wrapped__ | 710 k = k.__wrapped__ |
| 660 assert hasattr(k, '__wrapped__') | 711 assert hasattr(k, '__wrapped__') |
| 661 k = k.__wrapped__ | 712 k = k.__wrapped__ |
| 662 assert k is original_k | 713 assert k is original_k |
| 663 assert not hasattr(k, '__wrapped__') | 714 assert not hasattr(k, '__wrapped__') |
| 664 | 715 |
| 716 def f(g, assign, update): |
| 717 def w(): |
| 718 return 42 |
| 719 w.glue = {"foo" : "bar"} |
| 720 return six.wraps(g, assign, update)(w) |
| 721 k.glue = {"melon" : "egg"} |
| 722 k.turnip = 43 |
| 723 k = f(k, ["turnip"], ["glue"]) |
| 724 assert k.__name__ == "w" |
| 725 assert k.turnip == 43 |
| 726 assert k.glue == {"melon" : "egg", "foo" : "bar"} |
| 727 |
| 665 | 728 |
| 666 def test_add_metaclass(): | 729 def test_add_metaclass(): |
| 667 class Meta(type): | 730 class Meta(type): |
| 668 pass | 731 pass |
| 669 class X: | 732 class X: |
| 670 "success" | 733 "success" |
| 671 X = six.add_metaclass(Meta)(X) | 734 X = six.add_metaclass(Meta)(X) |
| 672 assert type(X) is Meta | 735 assert type(X) is Meta |
| 673 assert issubclass(X, object) | 736 assert issubclass(X, object) |
| 674 assert X.__module__ == __name__ | 737 assert X.__module__ == __name__ |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 assert MyStringSlots.__slots__ == "ab" | 790 assert MyStringSlots.__slots__ == "ab" |
| 728 instance = MyStringSlots() | 791 instance = MyStringSlots() |
| 729 instance.ab = "foo" | 792 instance.ab = "foo" |
| 730 py.test.raises(AttributeError, setattr, instance, "a", "baz") | 793 py.test.raises(AttributeError, setattr, instance, "a", "baz") |
| 731 py.test.raises(AttributeError, setattr, instance, "b", "baz") | 794 py.test.raises(AttributeError, setattr, instance, "b", "baz") |
| 732 | 795 |
| 733 class MySlotsWeakref(object): | 796 class MySlotsWeakref(object): |
| 734 __slots__ = "__weakref__", | 797 __slots__ = "__weakref__", |
| 735 MySlotsWeakref = six.add_metaclass(Meta)(MySlotsWeakref) | 798 MySlotsWeakref = six.add_metaclass(Meta)(MySlotsWeakref) |
| 736 assert type(MySlotsWeakref) is Meta | 799 assert type(MySlotsWeakref) is Meta |
| 800 |
| 801 |
| 802 @py.test.mark.skipif("sys.version_info[:2] < (2, 7)") |
| 803 def test_assertCountEqual(): |
| 804 class TestAssertCountEqual(unittest.TestCase): |
| 805 def test(self): |
| 806 with self.assertRaises(AssertionError): |
| 807 six.assertCountEqual(self, (1, 2), [3, 4, 5]) |
| 808 |
| 809 six.assertCountEqual(self, (1, 2), [2, 1]) |
| 810 |
| 811 TestAssertCountEqual('test').test() |
| 812 |
| 813 |
| 814 @py.test.mark.skipif("sys.version_info[:2] < (2, 7)") |
| 815 def test_assertRegex(): |
| 816 class TestAssertRegex(unittest.TestCase): |
| 817 def test(self): |
| 818 with self.assertRaises(AssertionError): |
| 819 six.assertRegex(self, 'test', r'^a') |
| 820 |
| 821 six.assertRegex(self, 'test', r'^t') |
| 822 |
| 823 TestAssertRegex('test').test() |
| 824 |
| 825 |
| 826 @py.test.mark.skipif("sys.version_info[:2] < (2, 7)") |
| 827 def test_assertRaisesRegex(): |
| 828 class TestAssertRaisesRegex(unittest.TestCase): |
| 829 def test(self): |
| 830 with six.assertRaisesRegex(self, AssertionError, '^Foo'): |
| 831 raise AssertionError('Foo') |
| 832 |
| 833 with self.assertRaises(AssertionError): |
| 834 with six.assertRaisesRegex(self, AssertionError, r'^Foo'): |
| 835 raise AssertionError('Bar') |
| 836 |
| 837 TestAssertRaisesRegex('test').test() |
| 838 |
| 839 |
| 840 def test_python_2_unicode_compatible(): |
| 841 @six.python_2_unicode_compatible |
| 842 class MyTest(object): |
| 843 def __str__(self): |
| 844 return six.u('hello') |
| 845 |
| 846 def __bytes__(self): |
| 847 return six.b('hello') |
| 848 |
| 849 my_test = MyTest() |
| 850 |
| 851 if six.PY2: |
| 852 assert str(my_test) == six.b("hello") |
| 853 assert unicode(my_test) == six.u("hello") |
| 854 elif six.PY3: |
| 855 assert bytes(my_test) == six.b("hello") |
| 856 assert str(my_test) == six.u("hello") |
| 857 |
| 858 assert getattr(six.moves.builtins, 'bytes', str)(my_test) == six.b("hello") |
| OLD | NEW |