-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
189 lines (165 loc) · 5.01 KB
/
tests.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
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
import json
from io import StringIO
from textwrap import dedent
import pytest
import yaml
from hypothesis import given
from hypothesis.strategies import text
from simple_rofi_menu import import_menu_from_config, Menu, MenuGroup, MenuItem, run_selected_item
@pytest.fixture
def valid_yaml_config_file():
menu_string = """\
separator: "----"
index_start: 1
index_format: "{item_index}: {item_name}"
numbered: True
groups:
- items:
- name: Lock screen
command: "i3 lock"
- name: Logout
command: i3-msg exit
- items:
- name: Switch user
command: gdmflexiserver
- name: Reboot
command: reboot
- name: Shutdown
command: init 0"""
# Because our code will load a file, we use a StringIO
# instead of a string in case of a regression between
# yaml.load() and yaml.loads()
return yaml.load(StringIO(dedent(menu_string)))
@pytest.fixture
def valid_json_config_file():
menu_string = """\
{
"separator": "----",
"index_start": 1,
"index_format": "{item_index}: {item_name}",
"numbered": true,
"groups": [
{
"items": [
{
"name": "Lock screen",
"command": "i3lock"
},
{
"name": "Logout",
"command": "i3-msg exit"
}
]
},
{
"items": [
{
"name": "Switch user",
"command": "gdmflexiserver"
},
{
"name": "Reboot",
"command": "reboot"
},
{
"name": "Shutdown",
"command": "init 0"
}
]
}
]
}"""
# Because our code will load a file, we use a StringIO
# instead of a string in case of a regression between
# json.load() and json.loads()
return json.load(StringIO(dedent(menu_string)))
@pytest.fixture(params=['valid_yaml_config_file', 'valid_json_config_file'])
def valid_config_file_metafixture(request):
"""
Parametrizes test with multiple fixtures
"""
return request.getfuncargvalue(request.param)
class TestEquality:
@pytest.mark.parametrize('first_menu, second_menu, equal', [
(Menu(), Menu(), True),
(Menu(MenuGroup()), Menu(), False),
(Menu(MenuGroup()), Menu(MenuGroup()), True),
(Menu(MenuGroup(MenuItem('lmjg', 'lkjg'))), Menu(MenuGroup()), False),
(Menu(MenuGroup(MenuItem('lmjg', 'lkjg'))), Menu(MenuGroup(MenuItem('lmjg', 'lkjg'))), True),
(Menu(numbered=True), Menu(), False),
(Menu(separator=""), Menu(), False),
(Menu(index_start=3), Menu(), False),
(Menu(index_format=""), Menu(), False),
])
def test_Menu(self, first_menu, second_menu, equal):
assert (first_menu == second_menu) == equal
assert (second_menu == first_menu) == equal
def test_import_menu_from_config(valid_config_file_metafixture):
menu = import_menu_from_config(valid_config_file_metafixture)
expected_menu = Menu(
MenuGroup(
MenuItem(
name='Lock screen',
command='i3lock'
),
MenuItem(
name='Logout',
command='i3-msg exit'
)
),
MenuGroup(
MenuItem(
name='Switch user',
command='gdmflexiserver'
),
MenuItem(
name='Reboot',
command='reboot'
),
MenuItem(
name='Shutdown',
command='init 0'
)
),
numbered=True,
index_format="{item_index}: {item_name}",
index_start=1,
separator='----',
)
assert menu == expected_menu
def test_menu_str(valid_config_file_metafixture):
menu = import_menu_from_config(valid_config_file_metafixture)
assert str(menu) == dedent("""\
1: Lock screen
2: Logout
----
3: Switch user
4: Reboot
5: Shutdown""")
@pytest.mark.parametrize('sys_argv', [
['test', 'bad_argument'],
['test', 'multiple', 'bad', 'arguments'],
['test'], # No arguments
])
def test_invalid_run_menu(sys_argv):
menu = Menu()
with pytest.raises(KeyError):
run_selected_item(menu, sys_argv, dry_run=True)
@given(text())
def test_successful_run_menu(item_name):
menu = Menu(
MenuGroup(
MenuItem(
name=item_name, command="whatever"
),
),
MenuGroup(
MenuItem(
name="whatever_1", command="whatever"
),
MenuItem(
name="whatever_2", command="whatever"
),
),
)
run_selected_item(menu, ['test', item_name], dry_run=True)