-
-
Notifications
You must be signed in to change notification settings - Fork 559
/
test_select.py
47 lines (37 loc) · 1.73 KB
/
test_select.py
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
import sys
import unittest
import lxml.html
class SelectTest(unittest.TestCase):
@staticmethod
def _evaluate_select(options, multiple=False):
options = ''.join('<option' + (' selected="selected"' if selected else '') + '>' + option + '</option>'
for option, selected in options)
string = '<title>test</title><form><select%s>%s</select></form>' % \
(' multiple="multiple"' if multiple else '', options)
return lxml.html.fromstring(string).find('.//select').value
def test_single_select_value_no_options(self):
self.assertEqual(
self._evaluate_select([]),
None)
def test_single_select_value_no_selected_option(self):
# If no option is selected, the HTML5 specification requires the first option to get selected.
self.assertEqual(
self._evaluate_select([('a', False), ('b', False)]),
'a')
def test_single_select_value_multiple_selected_options(self):
# If multiple options are selected, the proposed HTML 5.1 specification
# requires all but the last selected options to get deselected.
self.assertEqual(
self._evaluate_select([('a', True), ('b', True)]),
'b')
def test_multiple_select_value_no_selected_option(self):
self.assertEqual(
self._evaluate_select([('a', False), ('b', False)], multiple=True),
set())
def test_multiple_select_value_multiple_selected_options(self):
self.assertEqual(
self._evaluate_select([('a', True), ('b', True)], multiple=True),
{'a', 'b'})
def test_suite():
loader = unittest.TestLoader()
return loader.loadTestsFromModule(sys.modules[__name__])