File: test_lib_validators.py

package info (click to toggle)
python-reportlab 3.3.0-2%2Bdeb9u1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 7,172 kB
  • sloc: python: 71,880; ansic: 19,093; xml: 1,494; makefile: 416; java: 193; sh: 100
file content (164 lines) | stat: -rw-r--r-- 4,864 bytes parent folder | download | duplicates (5)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""Tests (incomplete) for the reportlab.lib.validators module.
"""
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, printLocation
setOutDir(__name__)
import unittest
from reportlab.lib import colors
from reportlab.lib import validators

class ValidatorTestCase(unittest.TestCase):
    "Test validating functions."

    def test0(self):
        "Test isBoolean validator."

        msg = "Validation failed for 'boolean' %s!"

        booleans = [0, 1, 'yes','no','true','false']
        badbooleans = ['a',3,-1,()]
        isBoolean = validators.isBoolean
        for b in booleans:
            assert isBoolean(b) == 1, msg % str(b)
        for b in badbooleans:
            assert isBoolean(b) == 0, msg % str(b)


    def test1(self):
        "Test isNumber validator."

        msg = 'Validation failed for number %s!'

        numbers = [0, 1, 2, -1, -2, 0.0, 0.1, -0.1]
        badNumbers = ['aaa',(1,1),(1+1j),colors]
        isNumber = validators.isNumber
        isListOfNumbers = validators.isListOfNumbers
        for n in numbers:
            assert isNumber(n) == 1, msg % str(n)
        for n in badNumbers:
            assert isNumber(n) == 0, msg % str(n)

        msg = 'Validation failed for numbers %s!'

        assert isListOfNumbers(numbers) == 1, msg % str(numbers)
        assert isListOfNumbers(badNumbers) == 0, msg % str(badNumbers)
        assert isListOfNumbers(numbers+[colors]) == 0, msg % str(numbers+[colors])


    def test2(self):
        "Test isNumberOrNone validator."

        msg = 'Validation failed for number %s!'

        numbers = [None, 0, 1, 2, -1, -2, 0.0, 0.1, -0.1] #, 2L, -2L]
        isNumberOrNone = validators.isNumberOrNone
        for n in numbers:
            assert isNumberOrNone(n) == 1, msg % str(n)


    def test4(self):
        "Test isString validator."

        msg = 'Validation failed for string %s!'

        strings = ['', '\n', '  ', 'foo', '""']
        badStrings = [1,2.0,None,('a','b')]
        isString = validators.isString
        isListOfStrings = validators.isListOfStrings
        for s in strings:
            assert isString(s) == 1, msg % str(s)
        for s in badStrings:
            assert isString(s) == 0, msg % str(s)

        msg = 'Validation failed for strings %s!'

        assert isListOfStrings(strings) == 1, msg % str(strings)
        assert isListOfStrings(badStrings) == 0, msg % str(badStrings)
        assert isListOfStrings(strings+[1]) == 0, msg % str(strings+[1])


    def test5(self):
        "Test isTextAnchor validator."

        msg = 'Validation failed for text anchor %s!'

        strings = ['start', 'middle', 'end']
        isTextAnchor = validators.isTextAnchor
        for s in strings:
            assert isTextAnchor(s) == 1, msg % s

        """
        def isListOfNumbersOrNone(x):
        def isListOfShapes(x):
        def isListOfStrings(x):
        def isListOfStringsOrNone(x):
        def isTransform(x):
        def isColor(x):
        def isColorOrNone(x):
        def isValidChild(x):
        class OneOf:
        class SequenceOf:
        """


    def test6(self):
        "Test OneOf validator."

        msg = 'Validation failed for OneOf %s!'

        choices = ('clockwise', 'anticlockwise')
        OneOf = validators.OneOf(choices)
        for c in choices:
            assert OneOf(c) == 1, msg % c
        for c in ('a', 'b', 'c'):
            assert OneOf(c) == 0, msg % c

        OneOf = validators.OneOf('clockwise', 'anticlockwise')
        for c in choices:
            assert OneOf(c) == 1, msg % c
        for c in ('a', 'b', 'c'):
            assert OneOf(c) == 0, msg % c

        try:
            validators.OneOf(choices,'bongo')
            raise AssertionError("OneOf failed to detect bad arguments")
        except ValueError:
            pass


    def test7(self):
        "Test isInt validator"

        msg = 'Validation failed for isInt %s!'

        isInt = validators.isInt
        for c in (1,2,-3,0,'-4','4'):
            assert isInt(c), msg % str(c)

        for c in (1.2,0.0,-3.0,'-4.0','4.4','AAAA'):
            assert not isInt(c), msg % str(c)


    def test8(self):
        "test Sequence of validator"

        msg = 'Validation failed for SequenceOf %s!'

        v=validators.SequenceOf(validators.OneOf(('eps','pdf','png','gif','jpg','tif')),lo=1,hi=3,emptyOK=0)
        for c in (['png'],('eps',),('eps','pdf')):
            assert v(c), msg % str(c)
        v._lo = 2
        for c in ([],(),('eps'),('eps','pdf','a'),['eps','pdf','png','gif']):
            assert not v(c), msg % str(c)
        v._emptyOK=1
        for c in ([],(),('eps','pdf')):
            assert v(c), msg % str(c)


def makeSuite():
    return makeSuiteForClasses(ValidatorTestCase)


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