BoundedValidatorTest.py 3.03 KB
Newer Older
1
2
3
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4
5
#   NScD Oak Ridge National Laboratory, European Spallation Source,
#   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6
# SPDX - License - Identifier: GPL - 3.0 +
7
import unittest
8
import testhelpers
9
from mantid.kernel import FloatBoundedValidator, IntBoundedValidator
10

11

12
class BoundedValidatorTest(unittest.TestCase):
13
14

    def test_construction_does_not_raise_error_when_both_are_floats(self):
15
        testhelpers.assertRaisesNothing(self, FloatBoundedValidator, 1.0, 2.0)
16

17
    def test_construction_with_Exclusive_bounds_with_floats(self):
Samuel Jackson's avatar
Samuel Jackson committed
18
        testhelpers.assertRaisesNothing(self, FloatBoundedValidator, 1.0, 2.0, True)
19

20
21
22
    def test_constructor_sets_both_boundary_values_correctly(self):
        validator = FloatBoundedValidator(1.3, 2.6)
        self.assertTrue(validator.hasLower())
23
        self.assertEqual(validator.lower(), 1.3)
24
        self.assertTrue(validator.hasUpper())
25
        self.assertEqual(validator.upper(), 2.6)
26
27
        self.assertFalse(validator.isLowerExclusive())
        self.assertFalse(validator.isUpperExclusive())
28

29
    def test_constructor_sets_both_Exclusive_boundary_values_correctly(self):
Samuel Jackson's avatar
Samuel Jackson committed
30
31
        validator = FloatBoundedValidator(1.3, 2.6, True)
        self.assertTrue(validator.hasLower())
32
        self.assertEqual(validator.lower(), 1.3)
Samuel Jackson's avatar
Samuel Jackson committed
33
        self.assertTrue(validator.hasUpper())
34
        self.assertEqual(validator.upper(), 2.6)
35
36
        self.assertTrue(validator.isLowerExclusive())
        self.assertTrue(validator.isUpperExclusive())
37
38

    def test_construction_does_not_raise_error_when_both_are_ints(self):
39
        testhelpers.assertRaisesNothing(self, IntBoundedValidator, 1, 20)
40

41
    def test_construction_with_Exclusive_bounds_with_ints(self):
Samuel Jackson's avatar
Samuel Jackson committed
42
        testhelpers.assertRaisesNothing(self, IntBoundedValidator, 1, 20, True)
43

44
45
46
    def test_lower_only_keyword_in_constructor(self):
        validator = FloatBoundedValidator(lower=2.5)
        self.assertTrue(validator.hasLower())
47
        self.assertEqual(validator.lower(), 2.5)
48
49
50
51
52
53
        self.assertFalse(validator.hasUpper())

    def test_upper_only_keyword_in_constructor(self):
        validator = FloatBoundedValidator(upper=5.5)
        self.assertFalse(validator.hasLower())
        self.assertTrue(validator.hasUpper())
54
        self.assertEqual(validator.upper(), 5.5)
55

56
    def test_construction_with_lower_sets_only_lower(self):
57
58
59
        validator = FloatBoundedValidator()
        lower = 1.4
        validator.setLower(lower)
60
        self.assertEqual(validator.hasLower(), True)
61
        self.assertFalse(validator.hasUpper())
62
        self.assertEqual(validator.lower(), lower)
63
64

    def test_construction_with_upper_sets_only_upper(self):
65
66
67
        validator = FloatBoundedValidator()
        upper = 5.4
        validator.setUpper(upper)
68
69
70
        self.assertEqual(validator.hasUpper(), True)
        self.assertEqual(validator.hasLower(), False)
        self.assertEqual(validator.upper(), upper)
71
72
73

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