Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/constraintstests.py
blob: b7b0a47fb0f0cb5998de61d0f40054288e359e46 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# Copyright (C) 2009, Tutorius.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import unittest

from sugar.tutorius.constraints import *

class ConstraintTest(unittest.TestCase):
    def test_base_class(self):
        cons = Constraint()
        try:
            cons.validate(1)
            assert False, "Base class should throw an assertion"
        except NotImplementedError:
            pass

class ValueConstraintTest(unittest.TestCase):
    def test_limit_set(self):
        cons = ValueConstraint(12)
        
        assert cons.limit == 12
        
class UpperLimitConstraintTest(unittest.TestCase):
    def test_empty_constraint(self):
        cons = UpperLimitConstraint(None)
        try:
            cons.validate(20)
        except UpperLimitConstraintError:
            assert False, "Empty contraint should not raise an exception"
    
    def test_validate(self):
        cons = UpperLimitConstraint(10)
        
        try:
            cons.validate(20)
            assert False, "Validation of UpperLimit(10) on 20 should raise an exception"
        except UpperLimitConstraintError:
            pass
        
        try:
            cons.validate(5)
        except UpperLimitConstraintError:
            assert True, "Validation of UpperLimit(10) on 5 should not raise an exception"
    
class LowerLimitConstraintTest(unittest.TestCase):
    def test_empty_constraint(self):
        cons = LowerLimitConstraint(None)
        try:
            cons.validate(20)
        except LowerLimitConstraintError:
            assert False, "Empty contraint should not raise an exception"
    
    def test_validate(self):
        cons = LowerLimitConstraint(10)
        
        try:
            cons.validate(5)
            assert False, "Validation of LowerLimit(10) on 5 should raise an exception"
        except LowerLimitConstraintError:
            pass
        
        try:
            cons.validate(20)
        except LowerLimitConstraintError:
            assert True, "Validation of LowerLimit(10) on 20 should not raise an exception"

class MaxSizeConstraintTest(unittest.TestCase):
    def test_empty_constraint(self):
        cons = MaxSizeConstraint(None)
        try:
            cons.validate(20)
        except MaxSizeConstraintError:
            assert False, "Empty contraint should not raise an exception"
    
    def test_validate(self):
        cons = MaxSizeConstraint(10)
        
        try:
            cons.validate(range(0, 20))
            assert False, "Validation of MaxSizeConstraint(10) on list of length 20 should raise an exception"
        except MaxSizeConstraintError:
            pass
        
        try:
            cons.validate(range(0,5))
        except MaxSizeConstraintError:
            assert True, "Validation of MaxSizeConstraint(10) on list of length 5 should not raise an exception"
    
class MinSizeConstraintTest(unittest.TestCase):
    def test_empty_constraint(self):
        cons = MinSizeConstraint(None)
        try:
            cons.validate(20)
        except MinSizeConstraintError:
            assert False, "Empty contraint should not raise an exception"
    
    def test_validate(self):
        cons = MinSizeConstraint(10)
        
        try:
            cons.validate(range(0, 5))
            assert False, "Validation of MinSizeConstraint(10) on list of length 20 should raise an exception"
        except MinSizeConstraintError:
            pass
        
        try:
            cons.validate(range(0,20))
        except MinSizeConstraintError:
            assert True, "Validation of MinSizeConstraint(10) on list of length 5 should not raise an exception"

class ColorConstraintTest(unittest.TestCase):
    def test_validate(self):
        cons = ColorConstraint()
        
        try:
            cons.validate([0, 0])
            assert False, "ColorConstraint on list of length 2 should raise an exception"
        except ColorArraySizeError:
            pass
        except ColorConstraintError:
            assert False, "ColorConstraint threw the wrong type of error"
        
        try:
            cons.validate([0, 0, "str"])
            assert False, "ColorConstraint on with non-integers values should raise an exception"
        except ColorTypeError:
            pass
        except ColorConstraintError:
            assert False, "ColorConstraint threw the wrong type of error"
        
        try:
            cons.validate([0, "str", 0])
            assert False, "ColorConstraint on with non-integers values should raise an exception"
        except ColorTypeError:
            pass
        except ColorConstraintError:
            assert False, "ColorConstraint threw the wrong type of error"
        
        try:
            cons.validate(["str", 0, 0])
            assert False, "ColorConstraint on with non-integers values should raise an exception"
        except ColorTypeError:
            pass
        except ColorConstraintError:
            assert False, "ColorConstraint threw the wrong type of error"
        
        try:
            cons.validate([1, 2, 300])
            assert False, "ColorConstraint on with non-integers values should raise an exception"
        except ColorValueError:
            pass
        except ColorConstraintError:
            assert False, "ColorConstraint threw the wrong type of error"
            
        try:
            cons.validate([1, -100, 30])
            assert False, "ColorConstraint on with non-integers values should raise an exception"
        except ColorValueError:
            pass
        except ColorConstraintError:
            assert False, "ColorConstraint threw the wrong type of error"
        
        try:
            cons.validate([999999, 2, 300])
            assert False, "ColorConstraint on with non-integers values should raise an exception"
        except ColorValueError:
            pass
        except ColorConstraintError:
            assert False, "ColorConstraint threw the wrong type of error"
        
        try:
            cons.validate([12, 23, 34])
        except LowerLimitConstraintError:
            assert True, "ColorConstraint on expected input should not raise an exception"

class BooleanConstraintTest(unittest.TestCase):
    def test_validate(self):
        cons = BooleanConstraint()
        
        cons.validate(True)
        cons.validate(False)
        
        try:
            cons.validate(18)
            assert False, "Setting integer on constraint should raise an error"
        except BooleanConstraintError:
            pass
        except:
            assert False, "Wrong exception type raised when setting wrong type"

class EnumConstraintTest(unittest.TestCase):
    def test_validate(self):
        cons = EnumConstraint([1,2,3,7,8,9, "ex"])
        
        cons.validate(8)
        
        cons.validate("ex")
        
        try:
            cons.validate(4)
            assert False, "There should be an exception on setting a value out of the enum"
        except EnumConstraintError:
            pass
        except:
            assert False, "Wrong exception type thrown"
            
class FileConstraintTest(unittest.TestCase):
    def test_validate(self):
        cons = FileConstraint()
        
        cons.validate("run-tests.py")
        
        try:
            cons.validate("unknown/file.py")
            assert False, "Non-existing file check should throw an exception"
        except FileConstraintError:
            pass

if __name__ == "__main__":
    unittest.main()