Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/tutorius/tests/propertiestests.py
blob: 45ba2648b1ba31dd44574f49bb260a7470e0539d (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# 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 *
from sugar.tutorius.properties import *

# Helper function to test the wrong types on a property, given its type
def try_wrong_values(prop):
    if prop.type != "int":
        try:
            prop.set(3)
            assert False, "Able to insert int value in property of type %s"%prop.type
        except:
            pass
    
    if prop.type != "float":
        try:
            prop.set(1.1)
            assert False, "Able to insert float value in property of type %s"%prop.type
        except:
            pass
    
    if prop.type != "string":
        try:
            prop.set("Fake string")
            assert False, "Able to insert string value in property of type %s"%prop.type
        except:
            pass
        
    if prop.type != "array":
        try:
            prop.set([1, 2000, 3, 400])
            assert False, "Able to insert array value in property of type %s"%prop.type
        except:
            pass
    
    if prop.type != "color":
        try:
            prop.set([1,2,3])
            if prop.type != "array":
                assert False, "Able to insert color value in property of type %s"%prop.type
        except:
            pass
            
    if prop.type != "boolean":
        try:
            prop.set(True)
            if prop.type != "boolean":
                assert False, "Able to set boolean value in property of type %s"%prop.type
        except:
            pass

class BasePropertyTest(unittest.TestCase):
    def test_base_class(self):
        prop = TutoriusProperty()
        
        assert prop.value == None, "There should not be an initial value in the base property"
        
        assert prop.type == None, "There should be no type associated with the base property"
        
        assert prop.get_constraints() == [], "There should be no constraints on the base property"
        
        prop.set(2)
        
        assert prop.value == 2, "Unable to set a value on base class"
    
class TIntPropertyTest(unittest.TestCase):
    def test_int_property(self):
        prop = TIntProperty(22)
        
        assert prop.value == 22, "Could not set value on property via constructor"
        
        assert prop.type == "int", "Wrong type on int property : %s" % prop.type
        cons = prop.get_constraints()
        assert len(cons) == 2, "Not enough constraints on the int property"
        
        prop.set(12)
        
        assert prop.value == 12, "Could not set value"
        
    def test_wrong_values(self):
        prop = TIntProperty(33)
        
        # Try setting values of other types 
        try_wrong_values(prop)

    def test_limit_constructor(self):
        prop = TIntProperty(22, 0, 30)
        
        try:
            prop.set(-22)
            assert False, "Assigning an out-of-range value should trigger LowerLimitConstraint"
        except LowerLimitConstraintError:
            pass
        except Exception:
            assert False, "Wrong exception triggered by assignation"
        
        try:
            prop.set(222)
            assert False, "Assigning an out-of-range value should trigger UpperLimitConstraint"
        except UpperLimitConstraintError:
            pass
        except Exception:
            assert False, "Wrong exception triggered by assignation"
            
    def test_failing_constructor(self):
        try:
            prop = TIntProperty(100, 0, 20)
            assert False, "Creation of the property should fail."
        except UpperLimitConstraintError:
            pass
        except:
            assert False, "Wrong exception type on failed constructor"
            
        try:
            prop = TIntProperty(-100, 0, 20)
            assert False, "Creation of the property should fail."
        except LowerLimitConstraintError:
            pass
        except:
            assert False, "Wrong exception type on failed constructor"
    
class TFloatPropertyTest(unittest.TestCase):
    def test_float_property(self):
        prop = TFloatProperty(22)
        
        assert prop.value == 22, "Could not set value on property via constructor"
        
        assert prop.type == "float", "Wrong type on float property : %s" % prop.type
        cons = prop.get_constraints()
        assert len(cons) == 2, "Not enough constraints on the float property"
        
        prop.set(12)
        
        assert prop.value == 12, "Could not set value"
        
    def test_wrong_values(self):
        prop = TFloatProperty(33)
        # Try setting values of other types 
        try_wrong_values(prop)

    def test_limit_constructor(self):
        prop = TFloatProperty(22.4, 0.1, 30.5223)
        
        try:
            prop.set(-22.8)
            assert False, "Assigning an out-of-range value should trigger LowerLimitConstraint"
        except LowerLimitConstraintError:
            pass
        except Exception:
            assert False, "Wrong exception triggered by assignation"
        
        try:
            prop.set(222.2)
            assert False, "Assigning an out-of-range value should trigger UpperLimitConstraint"
        except UpperLimitConstraintError:
            pass
        except Exception:
            assert False, "Wrong exception triggered by assignation"
            
    def test_failing_constructor(self):
        try:
            prop = TFloatProperty(100, 0, 20)
            assert False, "Creation of the property should fail."
        except UpperLimitConstraintError:
            pass
        except:
            assert False, "Wrong exception type on failed constructor"
            
        try:
            prop = TFloatProperty(-100, 0, 20)
            assert False, "Creation of the property should fail."
        except LowerLimitConstraintError:
            pass
        except:
            assert False, "Wrong exception type on failed constructor"
    
class TStringPropertyTest(unittest.TestCase):
    def test_basic_string(self):
        prop = TStringProperty("Starter string")
        
        assert prop.value == "Starter string", "Could not set string value via constructor"
        
        assert prop.type == "string", "Wrong type for string property : %s" % prop.type
        
    def test_size_limit(self):
        prop = TStringProperty("Small", 10)
        
        try:
            prop.set("My string is too big!")
            assert False, "String should not set to longer than max size"
        except MaxSizeConstraintError:
            pass
        except:
            assert False, "Wrong exception type thrown"
    
    def test_wrong_values(self):
        prop = TStringProperty("Beginning")
        
        try_wrong_values(prop)

    def test_failing_constructor(self):
        try:
            prop = TStringProperty("This is normal", 5)
            assert False, "Creation of the property should fail."
        except MaxSizeConstraintError:
            pass
        except:
            assert False, "Wrong exception type on failed constructor"
    
class TArrayPropertyTest(unittest.TestCase):
    def test_basic_array(self):
        prop = TArrayProperty([1, 2, 3, 4])
        
        assert prop.value == [1,2,3,4], "Unable to set initial value via constructor"
        
        assert prop.type == "array", "Wrong type for array : %s"%prop.type
        
    def test_wrong_values(self):
        prop = TArrayProperty([1,2,3,4,5])
        
        try_wrong_values(prop)
        
    def test_size_limits(self):
        prop = TArrayProperty([1,2], None, 4)
        
        try:
            prop.set([1,2,4,5,6,7])
            assert False, "Maximum size limit constraint was not properly applied"
        except MaxSizeConstraintError:
            pass
        
        prop = TArrayProperty([1,2,3,4], 2)
        
        try:
            prop.set([1])
            assert False, "Minimum size limit constraint was not properly applied"
        except MinSizeConstraintError:
            pass
    
    def test_failing_constructor(self):
        try:
            prop = TArrayProperty([100, 0, 20], None, 2)
            assert False, "Creation of the property should fail."
        except MaxSizeConstraintError:
            pass
        try:
            prop = TArrayProperty([100, 0, 20], 4, None)
            assert False, "Creation of the property should fail."
        except MinSizeConstraintError:
            pass
    
class TColorPropertyTest(unittest.TestCase):
    def test_basic_color(self):
        prop = TColorProperty(20, 40, 60)
        
        assert prop.value == [20, 40, 60], "Could not set initial value with constructor"
        
        assert prop.type == "color", "Wrong type on color : %s"%prop.type
        
    def test_wrong_values(self):
        prop = TColorProperty(250, 250, 250)
        
        try_wrong_values(prop)
        
    def test_failing_constructor(self):
        try:
            prop = TColorProperty(0, "str", 0)
            assert False, "Creation of the property should fail."
        except ColorTypeError:
            pass
        except:
            assert False, "Wrong exception type on failed constructor"

class TBooleanPropertyTest(unittest.TestCase):
    def setUp(self):
        self.prop = TBooleanProperty(False)
    
    def test_basic_boolean(self):
        assert self.prop.value == False, "Could not set initial value via constructor"
        
        assert self.prop.type == "boolean", "Wrong type for TBooleanProperty : %s"%self.prop.type
        
        self.prop.set(True)
        
        assert self.prop.value == True, "Could not change the value via set"
        
        self.prop.set(False)
        
        assert self.prop.value == False, "Could not change the value via set"
        
    def test_wrong_types(self):
        try_wrong_values(self.prop)
        
    def test_failing_constructor(self):
        try:
            prop = TBooleanProperty(64)
            assert False, "Creation of the property should fail with non-boolean value"
        except BooleanConstraintError:
            pass
        except:
            assert False, "Wrong exception type on failed constructor"
    
class TEnumPropertyTest(unittest.TestCase):
    def setUp(self):
        self.prop = TEnumProperty("hello", [1, 2, "hello", "world", True, None])
    
    def test_basic_enum(self):
        assert self.prop.value == "hello", "Could not set initial value on property"
        
        assert self.prop.type == "enum", "Wrong type for TEnumProperty : %s"%self.prop.type
        
        self.prop.set(True)
        
        assert self.prop.value, "Could not change the value via set"
        
        try:
            self.prop.set(4)
            assert False, "Switched to a value outside the enum"
        except EnumConstraintError:
            pass
    
    def test_wrong_type(self):
        try_wrong_values(self.prop)
    
class TFilePropertyTest(unittest.TestCase):
    def setUp(self):
        self.prop = TFileProperty("propertiestests.py")
        
    def test_basic_file(self):
        assert self.prop.value == "propertiestests.py", "Could not set initial value"
        
        assert self.prop.type == "file", "Wrong type for TFileProperty : %s"%self.prop.type
        
        self.prop.set("run-tests.py")
        
        assert self.prop.value == "run-tests.py", "Could not change value"
        
        try:
            self.prop.set("unknown/file/on/disk.gif")
            assert False, "An exception should be thrown on unknown file"
        except FileConstraintError:
            pass

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