File: test_widgetbase_tpc.py

package info (click to toggle)
python-reportlab 3.1.8-3%2Bdeb8u1
  • links: PTS
  • area: main
  • in suites: jessie
  • size: 6,996 kB
  • ctags: 9,483
  • sloc: python: 69,727; ansic: 19,108; xml: 1,494; makefile: 416; java: 193; sh: 100
file content (136 lines) | stat: -rw-r--r-- 2,997 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#Copyright ReportLab Europe Ltd. 2000-2012
#see license.txt for license details
"""
Tests for TypedPropertyCollection class.
"""
__version__='''$Id$'''
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, printLocation
setOutDir(__name__)
import os, sys, copy
from os.path import join, basename, splitext
import unittest
from reportlab.graphics.widgetbase import PropHolder, TypedPropertyCollection
from reportlab.lib.attrmap import AttrMap, AttrMapValue
from reportlab.lib.validators import isNumber


TPC = TypedPropertyCollection


class PH(PropHolder):
    _attrMap = AttrMap(
        a = AttrMapValue(isNumber),
        b = AttrMapValue(isNumber)
        )


class APH(PH):
    def __init__(self):
        self.a = 1


class BPH(APH):
    def __init__(self):
        APH.__init__(self)

    def __getattr__(self,name):
        if name=='b': return -1
        raise AttributeError


class TPCTestCase(unittest.TestCase):
    "Test TypedPropertyCollection class."

    def test0(self):
        "Test setting an invalid collective attribute."

        t = TPC(PH)
        try:
            t.c = 42
        except AttributeError:
            pass


    def test1(self):
        "Test setting a valid collective attribute."

        t = TPC(PH)
        t.a = 42
        assert t.a == 42


    def test2(self):
        "Test setting a valid collective attribute with an invalid value."

        t = TPC(PH)
        try:
            t.a = 'fourty-two'
        except AttributeError:
            pass


    def test3(self):
        "Test setting a valid collective attribute with a convertible invalid value."

        t = TPC(PH)
        t.a = '42'
        assert t.a == '42' # Or should it rather be an integer?


    def test4(self):
        "Test accessing an unset collective attribute."

        t = TPC(PH)
        try:
            t.a
        except AttributeError:
            pass


    def test5(self):
        "Test overwriting a collective attribute in one slot."

        t = TPC(PH)
        t.a = 42
        t[0].a = 4242
        assert t[0].a == 4242


    def test6(self):
        "Test overwriting a one slot attribute with a collective one."

        t = TPC(PH)
        t[0].a = 4242
        t.a = 42
        assert t[0].a == 4242


    def test7(self):
        "Test to ensure we can handle classes with __getattr__ methods"

        a=TypedPropertyCollection(APH)
        b=TypedPropertyCollection(BPH)

        a.a=3
        b.a=4
        try:
            a.b
            assert 1, "Shouldn't be able to see a.b"
        except AttributeError:
            pass
        a.b=0
        assert a.b==0, "Wrong value for "+str(a.b)
        assert b.b==-1, "This should call __getattr__ special"
        b.b=0
        assert a[0].b==0
        assert b[0].b==-1, "Class __getattr__ should return -1"


def makeSuite():
    return makeSuiteForClasses(TPCTestCase)


#noruntests
if __name__ == "__main__":
    unittest.TextTestRunner().run(makeSuite())
    printLocation()