| OLD | NEW |
| 1 # Copyright (c) 2010-2015 Benjamin Peterson |
| 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 # of this software and associated documentation files (the "Software"), to deal |
| 5 # in the Software without restriction, including without limitation the rights |
| 6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 # copies of the Software, and to permit persons to whom the Software is |
| 8 # furnished to do so, subject to the following conditions: |
| 9 # |
| 10 # The above copyright notice and this permission notice shall be included in all |
| 11 # copies or substantial portions of the Software. |
| 12 # |
| 13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 # SOFTWARE. |
| 20 |
| 1 import operator | 21 import operator |
| 2 import sys | 22 import sys |
| 3 import types | 23 import types |
| 4 import unittest | 24 import unittest |
| 5 | 25 |
| 6 import py | 26 import py |
| 7 | 27 |
| 8 import six | 28 import six |
| 9 | 29 |
| 10 | 30 |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 def with_kw(*args, **kw): | 403 def with_kw(*args, **kw): |
| 384 record.append(kw["kw"]) | 404 record.append(kw["kw"]) |
| 385 return old(*args) | 405 return old(*args) |
| 386 old = getattr(MyDict, stock_method_name(name)) | 406 old = getattr(MyDict, stock_method_name(name)) |
| 387 monkeypatch.setattr(MyDict, stock_method_name(name), with_kw) | 407 monkeypatch.setattr(MyDict, stock_method_name(name), with_kw) |
| 388 meth(d, kw=42) | 408 meth(d, kw=42) |
| 389 assert record == [42] | 409 assert record == [42] |
| 390 monkeypatch.undo() | 410 monkeypatch.undo() |
| 391 | 411 |
| 392 | 412 |
| 393 @py.test.mark.skipif(sys.version_info[:2] < (2, 7), | 413 @py.test.mark.skipif("sys.version_info[:2] < (2, 7)", |
| 394 reason="view methods on dictionaries only available on 2.7+") | 414 reason="view methods on dictionaries only available on 2.7+") |
| 395 def test_dictionary_views(): | 415 def test_dictionary_views(): |
| 396 def stock_method_name(viewwhat): | 416 def stock_method_name(viewwhat): |
| 397 """Given a method suffix like "keys" or "values", return the name | 417 """Given a method suffix like "keys" or "values", return the name |
| 398 of the dict method that delivers those on the version of Python | 418 of the dict method that delivers those on the version of Python |
| 399 we're running in.""" | 419 we're running in.""" |
| 400 if six.PY3: | 420 if six.PY3: |
| 401 return viewwhat | 421 return viewwhat |
| 402 return 'view' + viewwhat | 422 return 'view' + viewwhat |
| 403 | 423 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 class X(object): | 469 class X(object): |
| 450 pass | 470 pass |
| 451 def f(self): | 471 def f(self): |
| 452 return self | 472 return self |
| 453 x = X() | 473 x = X() |
| 454 b = six.create_bound_method(f, x) | 474 b = six.create_bound_method(f, x) |
| 455 assert isinstance(b, types.MethodType) | 475 assert isinstance(b, types.MethodType) |
| 456 assert b() is x | 476 assert b() is x |
| 457 | 477 |
| 458 | 478 |
| 479 def test_create_unbound_method(): |
| 480 class X(object): |
| 481 pass |
| 482 |
| 483 def f(self): |
| 484 return self |
| 485 u = six.create_unbound_method(f, X) |
| 486 py.test.raises(TypeError, u) |
| 487 if six.PY2: |
| 488 assert isinstance(u, types.MethodType) |
| 489 x = X() |
| 490 assert f(x) is x |
| 491 |
| 492 |
| 459 if six.PY3: | 493 if six.PY3: |
| 460 | 494 |
| 461 def test_b(): | 495 def test_b(): |
| 462 data = six.b("\xff") | 496 data = six.b("\xff") |
| 463 assert isinstance(data, bytes) | 497 assert isinstance(data, bytes) |
| 464 assert len(data) == 1 | 498 assert len(data) == 1 |
| 465 assert data == bytes([255]) | 499 assert data == bytes([255]) |
| 466 | 500 |
| 467 | 501 |
| 468 def test_u(): | 502 def test_u(): |
| (...skipping 21 matching lines...) Expand all Loading... |
| 490 assert len(s) == 1 | 524 assert len(s) == 1 |
| 491 | 525 |
| 492 | 526 |
| 493 def test_unichr(): | 527 def test_unichr(): |
| 494 assert six.u("\u1234") == six.unichr(0x1234) | 528 assert six.u("\u1234") == six.unichr(0x1234) |
| 495 assert type(six.u("\u1234")) is type(six.unichr(0x1234)) | 529 assert type(six.u("\u1234")) is type(six.unichr(0x1234)) |
| 496 | 530 |
| 497 | 531 |
| 498 def test_int2byte(): | 532 def test_int2byte(): |
| 499 assert six.int2byte(3) == six.b("\x03") | 533 assert six.int2byte(3) == six.b("\x03") |
| 500 py.test.raises((OverflowError, ValueError), six.int2byte, 256) | 534 py.test.raises(Exception, six.int2byte, 256) |
| 501 | 535 |
| 502 | 536 |
| 503 def test_byte2int(): | 537 def test_byte2int(): |
| 504 assert six.byte2int(six.b("\x03")) == 3 | 538 assert six.byte2int(six.b("\x03")) == 3 |
| 505 assert six.byte2int(six.b("\x03\x04")) == 3 | 539 assert six.byte2int(six.b("\x03\x04")) == 3 |
| 506 py.test.raises(IndexError, six.byte2int, six.b("")) | 540 py.test.raises(IndexError, six.byte2int, six.b("")) |
| 507 | 541 |
| 508 | 542 |
| 509 def test_bytesindex(): | 543 def test_bytesindex(): |
| 510 assert six.indexbytes(six.b("hello"), 3) == ord("l") | 544 assert six.indexbytes(six.b("hello"), 3) == ord("l") |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 792 instance.ab = "foo" | 826 instance.ab = "foo" |
| 793 py.test.raises(AttributeError, setattr, instance, "a", "baz") | 827 py.test.raises(AttributeError, setattr, instance, "a", "baz") |
| 794 py.test.raises(AttributeError, setattr, instance, "b", "baz") | 828 py.test.raises(AttributeError, setattr, instance, "b", "baz") |
| 795 | 829 |
| 796 class MySlotsWeakref(object): | 830 class MySlotsWeakref(object): |
| 797 __slots__ = "__weakref__", | 831 __slots__ = "__weakref__", |
| 798 MySlotsWeakref = six.add_metaclass(Meta)(MySlotsWeakref) | 832 MySlotsWeakref = six.add_metaclass(Meta)(MySlotsWeakref) |
| 799 assert type(MySlotsWeakref) is Meta | 833 assert type(MySlotsWeakref) is Meta |
| 800 | 834 |
| 801 | 835 |
| 802 @py.test.mark.skipif("sys.version_info[:2] < (2, 7)") | 836 @py.test.mark.skipif("sys.version_info[:2] < (2, 7) or sys.version_info[:2] in (
(3, 0), (3, 1))") |
| 803 def test_assertCountEqual(): | 837 def test_assertCountEqual(): |
| 804 class TestAssertCountEqual(unittest.TestCase): | 838 class TestAssertCountEqual(unittest.TestCase): |
| 805 def test(self): | 839 def test(self): |
| 806 with self.assertRaises(AssertionError): | 840 with self.assertRaises(AssertionError): |
| 807 six.assertCountEqual(self, (1, 2), [3, 4, 5]) | 841 six.assertCountEqual(self, (1, 2), [3, 4, 5]) |
| 808 | 842 |
| 809 six.assertCountEqual(self, (1, 2), [2, 1]) | 843 six.assertCountEqual(self, (1, 2), [2, 1]) |
| 810 | 844 |
| 811 TestAssertCountEqual('test').test() | 845 TestAssertCountEqual('test').test() |
| 812 | 846 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 849 my_test = MyTest() | 883 my_test = MyTest() |
| 850 | 884 |
| 851 if six.PY2: | 885 if six.PY2: |
| 852 assert str(my_test) == six.b("hello") | 886 assert str(my_test) == six.b("hello") |
| 853 assert unicode(my_test) == six.u("hello") | 887 assert unicode(my_test) == six.u("hello") |
| 854 elif six.PY3: | 888 elif six.PY3: |
| 855 assert bytes(my_test) == six.b("hello") | 889 assert bytes(my_test) == six.b("hello") |
| 856 assert str(my_test) == six.u("hello") | 890 assert str(my_test) == six.u("hello") |
| 857 | 891 |
| 858 assert getattr(six.moves.builtins, 'bytes', str)(my_test) == six.b("hello") | 892 assert getattr(six.moves.builtins, 'bytes', str)(my_test) == six.b("hello") |
| OLD | NEW |