|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +"""Test __repr__ output""" |
| 4 | + |
| 5 | +import System |
| 6 | +import pytest |
| 7 | +from Python.Test import ReprTest |
| 8 | + |
| 9 | +def test_basic(): |
| 10 | + """Test Point class which implements both ToString and __repr__ without inheritance""" |
| 11 | + ob = ReprTest.Point(1,2) |
| 12 | + # point implements ToString() and __repr__() |
| 13 | + assert ob.__repr__() == "Point(1,2)" |
| 14 | + assert str(ob) == "Python.Test.ReprTest+Point: X=1, Y=2" |
| 15 | + |
| 16 | +def test_system_string(): |
| 17 | + """Test system string""" |
| 18 | + ob = System.String("hello") |
| 19 | + assert str(ob) == "hello" |
| 20 | + assert "<System.String object at " in ob.__repr__() |
| 21 | + |
| 22 | +def test_str_only(): |
| 23 | + """Test class implementing ToString() but not __repr__()""" |
| 24 | + ob = ReprTest.Bar() |
| 25 | + assert str(ob) == "I implement ToString() but not __repr__()!" |
| 26 | + assert "<Python.Test.Bar object at " in ob.__repr__() |
| 27 | + |
| 28 | +def test_hierarchy1(): |
| 29 | + """Test inheritance heirarchy with base & middle class implementing ToString""" |
| 30 | + ob1 = ReprTest.BazBase() |
| 31 | + assert str(ob1) == "Base class implementing ToString()!" |
| 32 | + assert "<Python.Test.BazBase object at " in ob1.__repr__() |
| 33 | + |
| 34 | + ob2 = ReprTest.BazMiddle() |
| 35 | + assert str(ob2) == "Middle class implementing ToString()!" |
| 36 | + assert "<Python.Test.BazMiddle object at " in ob2.__repr__() |
| 37 | + |
| 38 | + ob3 = ReprTest.Baz() |
| 39 | + assert str(ob3) == "Middle class implementing ToString()!" |
| 40 | + assert "<Python.Test.Baz object at " in ob3.__repr__() |
| 41 | + |
| 42 | +def bad_tostring(): |
| 43 | + """Test ToString that can't be used by str()""" |
| 44 | + ob = ReprTest.Quux() |
| 45 | + assert str(ob) == "Python.Test.ReprTest+Quux" |
| 46 | + assert "<Python.Test.Quux object at " in ob.__repr__() |
| 47 | + |
| 48 | +def bad_repr(): |
| 49 | + """Test incorrect implementation of repr""" |
| 50 | + ob1 = ReprTest.QuuzBase() |
| 51 | + assert str(ob1) == "Python.Test.ReprTest+QuuzBase" |
| 52 | + assert "<Python.Test.QuuzBase object at " in ob.__repr__() |
| 53 | + |
| 54 | + ob2 = ReprTest.Quuz() |
| 55 | + assert str(ob2) == "Python.Test.ReprTest+Quuz" |
| 56 | + assert "<Python.Test.Quuz object at " in ob.__repr__() |
| 57 | + |
| 58 | + ob3 = ReprTest.Corge() |
| 59 | + with pytest.raises(Exception): |
| 60 | + str(ob3) |
| 61 | + with pytest.raises(Exception): |
| 62 | + ob3.__repr__() |
| 63 | + |
| 64 | + ob4 = ReprTest.Grault() |
| 65 | + with pytest.raises(Exception): |
| 66 | + str(ob4) |
| 67 | + with pytest.raises(Exception): |
| 68 | + ob4.__repr__() |
0 commit comments