# 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 SizeConstraintTest(unittest.TestCase): def test_empty_constraint(self): cons = SizeConstraint(None) try: cons.validate(20) except SizeConstraintError: assert False, "Empty contraint should not raise an exception" def test_validate(self): cons = SizeConstraint(10) try: cons.validate(range(0, 20)) assert False, "Validation of SizeLimit(10) on list of length 20 should raise an exception" except SizeConstraintError: pass try: cons.validate(range(0,5)) except SizeConstraintError: assert True, "Validation of SizeLimit(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()