File: test_userinput.py

package info (click to toggle)
orange3 3.40.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,908 kB
  • sloc: python: 162,745; ansic: 622; makefile: 322; sh: 93; cpp: 77
file content (188 lines) | stat: -rw-r--r-- 6,623 bytes parent folder | download | duplicates (2)
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
import unittest
from unittest.mock import patch
from Orange.widgets.utils.userinput import (
    _get_points, numbers_from_list)


class TestPointsFromList(unittest.TestCase):
    @patch("Orange.widgets.utils.userinput._numbers_from_no_dots")
    @patch("Orange.widgets.utils.userinput._numbers_from_dots")
    def test_points_from_list(self, from_dots, no_dots):
        numbers_from_list("1 2 3", int)
        no_dots.assert_called()
        no_dots.reset_mock()
        from_dots.assert_not_called()

        numbers_from_list("5, 9", int)
        no_dots.assert_called()
        no_dots.reset_mock()
        from_dots.assert_not_called()

        numbers_from_list("5.13", int)
        no_dots.assert_called()
        no_dots.reset_mock()
        from_dots.assert_not_called()

        numbers_from_list("1 ... 3", int)
        no_dots.assert_not_called()
        from_dots.assert_called()
        from_dots.reset_mock()

        numbers_from_list("1, 2, 3...5", int)
        no_dots.assert_not_called()
        from_dots.assert_called()
        from_dots.reset_mock()

        numbers_from_list("...3, 4, 5", int)
        no_dots.assert_not_called()
        from_dots.assert_called()
        from_dots.reset_mock()

        numbers_from_list("3, 4, 5...", int)
        no_dots.assert_not_called()
        from_dots.assert_called()
        from_dots.reset_mock()

        numbers_from_list("3, 4, ...,5...", int)
        no_dots.assert_not_called()
        from_dots.assert_called()
        from_dots.reset_mock()

    def test_get_points(self):
        self.assertEqual(_get_points(["1", "2", "3"], int), (1, 2, 3))
        self.assertEqual(_get_points(["1", "2", "3"], float), (1, 2, 3))
        self.assertEqual(_get_points(["10.5", "2.25", "3"], float), (10.5, 2.25, 3))

        with self.assertRaisesRegex(ValueError, "3.3"):
            _get_points(["1", "2", "3.3", "4"], int)

        with self.assertRaisesRegex(ValueError, "asdf"):
            _get_points(["1", "2", "asdf", "4"], int)

    def test_numbers_from_no_dots(self):
        self.assertEqual(numbers_from_list("1 2 3", int), (1, 2, 3))
        self.assertEqual(numbers_from_list("1 2 3", float), (1, 2, 3))
        self.assertEqual(numbers_from_list("10.5 2.25 3", float), (2.25, 3, 10.5))

        with self.assertRaisesRegex(ValueError, "3.3"):
            numbers_from_list("1 2 3.3 4", int)

        with self.assertRaisesRegex(ValueError, "asdf"):
            numbers_from_list("1 2 asdf 4", int)

        with self.assertRaisesRegex(ValueError, "value must be at least 2"):
            numbers_from_list("1", int, 2)

        with self.assertRaisesRegex(ValueError, "value must be at most 2"):
            numbers_from_list("3", int, None, 2)

        with self.assertRaisesRegex(ValueError, "value must be between 2 and 3"):
            numbers_from_list("1 4 2 3", int, 2, 3)

    def test_numbers_from_dots(self):
        def check(minimum, maximum, tests, typ=int, enforce_range=True):
            for text, *expected in tests:
                if not expected:
                    with self.assertRaises(ValueError):
                        numbers_from_list(text, typ, minimum, maximum,
                                          enforce_range)
                else:
                    self.assertEqual(
                        numbers_from_list(text, typ, minimum, maximum,
                                          enforce_range),
                        expected[0],
                        f"for {text}")

        check(None, None, [
            ("1, 2, ..., 5", (1, 2, 3, 4, 5)),
            ("1, 2, 3, ..., 5, 6, 7", (1, 2, 3, 4, 5, 6, 7)),
            ("3, ..., 5, 6", (3, 4, 5, 6)),
            ("..., 5, 6", ),
            ("5, 6, ...", ),
            ("1, 2, 3, 4, 5, ...", ),
            ("1, ..., 5", ),
            ("1, 2, ..., 5, 6, ..., 8", )])

        # 5 to 10
        check(5, 10, [
            ("4, 5, ..., 8", ),
            ("5, 6, ..., 12", ),
            ("5, 6, ..., 9", (5, 6, 7, 8, 9)),
            ("6, 7, ..., 9", (6, 7, 8, 9)),
            ("6, 7, ..., 8, 9", (6, 7, 8, 9)),
            ("..., 8, 9", (5, 6, 7, 8, 9)),
            ("6, 7, ...", (6, 7, 8, 9, 10)),
            ("6, 7, 8, 9, ...", (6, 7, 8, 9, 10)),
            ("..., 8, 9", (5, 6, 7, 8, 9)),
            ])

        check(5, None, [
            ("4, 5, ..., 8", ),
            ("5, 6, ..., 12", (5, 6, 7, 8, 9, 10, 11, 12)),
            ("6, 7, ..., 9", (6, 7, 8, 9)),
            ("6, 7, ..., 8, 9", (6, 7, 8, 9)),
            ("..., 8, 9", (5, 6, 7, 8, 9)),
            ("6, 7, ...", )
            ])

        check(None, 10, [
            ("4, 5, ..., 8", (4, 5, 6, 7, 8)),
            ("5, 6, ..., 12", ),
            ("5, 6, ..., 9", (5, 6, 7, 8, 9)),
            ("6, 7, ..., 9", (6, 7, 8, 9)),
            ("..., 8, 9", ),
            ("6, 7, ...", (6, 7, 8, 9, 10)),
            ("6, 7, 8, 9, ...", (6, 7, 8, 9, 10)),
            ("..., 8, 9", )])

        check(5, 10, [
            ("5, 6..., 8", (5, 6, 7, 8)),
            ("5,6...,8", (5, 6, 7, 8)),
            ("5,6...8", (5, 6, 7, 8)),
            ("5,   6  ...  8", (5, 6, 7, 8)),
            ("5,   6  ...  8", (5, 6, 7, 8)),
            ("5    6  ...  ", (5, 6, 7, 8, 9, 10)),
            ("..., 7, 8", (5, 6, 7, 8)),
            ("..., 7, 8, ...", ),
            ("5, 6, ..., 7, 8, ...", ),
            ("5, 6, ..., 7, 8, ...", ),
            ("5  6  8, ...", ),
            ("5, 6, 8, ...", ),
            ("5, 6, ..., 8, 10", ),
            ("5, 7, ..., 8, 10", ),
            ("8, 7, 6, ...", ),
            ("5, 6, 7, ..., 7, 8", )])

        check(5, 10, [
            ("3, 4, 5, 6..., 8", (3, 4, 5, 6, 7, 8)),
            ("5, ..., 9, 11, 13", (5, 7, 9, 11, 13)),
            ("5, 6, ..., ", (5, 6, 7, 8, 9, 10)),
            ("..., 9, 10, 11", (5, 6, 7, 8, 9, 10, 11))],
            int, False)

        check(1, None, [
            ("5, 6..., 8", (5, 6, 7, 8)),
            ("..., 7, 9", (1, 3, 5, 7, 9)),
            ("..., 8, 10", (2, 4, 6, 8, 10)),
            ("..., 7, 10", (1, 4, 7, 10)),
            ("..., 6, 10", (2, 6, 10)),
            ("..., 5, 10", (5, 10)),
            ("..., 4, 10", (4, 10)),
        ])

        check(None, 10, [
            ("2, 4, ...", (2, 4, 6, 8, 10)),
            ("1, 3, ...", (1, 3, 5, 7, 9)),
            ("1, 4, ...", (1, 4, 7, 10)),
            ("1, 5, ...", (1, 5, 9)),
            ("1, 6, ...", (1, 6)),
        ])

        check(None, None, [
            ("1.3, 1.6, ..., 2.5", (1.3, 1.6, 1.9, 2.2, 2.5)),
            ("1.3, 1.6, ..., 2.55", )],
            float)


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