/
test_helpers.py
269 lines (212 loc) · 9.58 KB
/
test_helpers.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# -*- coding: utf-8 -*-
"""
Tests for helpers functions/classes.
"""
__author__ = 'Grzegorz Latuszek, Marcin Usielski'
__copyright__ = 'Copyright (C) 2018-2020, Nokia'
__email__ = 'grzegorz.latuszek@nokia.com, marcin.usielski@nokia.com'
import mock
import pytest
from moler.exceptions import WrongUsage
def test_instance_id_returns_id_in_hex_form_without_0x():
from moler.helpers import instance_id
from six.moves import builtins
# 0xf0a1 == 61601 decimal
with mock.patch.object(builtins, "id", return_value=61601):
instance = "moler object"
assert "0x" not in instance_id(instance)
assert instance_id(instance) == "f0a1"
def test_converterhelper_k():
from moler.util.converterhelper import ConverterHelper
converter = ConverterHelper.get_converter_helper()
bytes_value, value_in_units, unit = converter.to_bytes("2.5K")
assert 2560 == bytes_value
assert 2.5 == value_in_units
assert 'k' == unit
def test_converterhelper_m():
from moler.util.converterhelper import ConverterHelper
converter = ConverterHelper.get_converter_helper()
bytes_value, value_in_units, unit = converter.to_bytes(".3m", False)
assert 300000 == bytes_value
assert 0.3 == value_in_units
assert 'm' == unit
def test_converterhelper_wrong_unit():
from moler.util.converterhelper import ConverterHelper
converter = ConverterHelper.get_converter_helper()
with pytest.raises(ValueError):
converter.to_bytes("3UU", False)
def test_converterhelper_seconds():
from moler.util.converterhelper import ConverterHelper
converter = ConverterHelper.get_converter_helper()
value, value_in_units, unit = converter.to_seconds_str("3m")
assert 180 == value
assert 3 == value_in_units
assert 'm' == unit
def test_converterhelper_number_wrong_format():
from moler.util.converterhelper import ConverterHelper
converter = ConverterHelper.get_converter_helper()
with pytest.raises(ValueError):
converter.to_number(value="abc", raise_exception=True)
val = converter.to_number(value="abc", raise_exception=False)
assert val == 0
def test_converterhelper_number():
from moler.util.converterhelper import ConverterHelper
converter = ConverterHelper.get_converter_helper()
val = converter.to_number(value="1")
assert 1 == val
val = converter.to_number(value="0.1")
assert val == 0.1
def test_converterhelper_seconds_ms():
from moler.util.converterhelper import ConverterHelper
converter = ConverterHelper.get_converter_helper()
value = converter.to_seconds(0.408, "ms")
assert pytest.approx(0.000408, 0.000001) == value
def test_converterhelper_seconds_wrong_unit():
from moler.util.converterhelper import ConverterHelper
converter = ConverterHelper.get_converter_helper()
with pytest.raises(ValueError):
converter.to_seconds_str("3UU")
def test_copy_list():
from moler.helpers import copy_list
src = [1]
dst = copy_list(src, deep_copy=True)
assert src == dst
dst[0] = 2
assert src != dst
def test_copy_dict():
from moler.helpers import copy_dict
src = {'a': 1}
dst = copy_dict(src, deep_copy=True)
assert src == dst
dst['a'] = 2
assert src != dst
def test_regex_helper():
from moler.cmd import RegexHelper
regex_helper = RegexHelper()
assert regex_helper is not None
match = regex_helper.match(r"\d+(\D+)\d+", "111ABC222")
assert match is not None
assert match == regex_helper.get_match()
assert regex_helper.group(1) == "ABC"
def test_groups_at_regex_helper():
import re
from moler.cmd import RegexHelper
regex_helper = RegexHelper()
if regex_helper.search_compiled(re.compile(r"(\d+)_([A-Z]+)(\w+),(\d+)"), "111_ABCef,222"):
ones, uppers, lowers, twos = regex_helper.groups()
assert ones == '111'
assert uppers == 'ABC'
assert lowers == 'ef'
assert twos == '222'
def test_search_compiled_none():
from moler.cmd import RegexHelper
regex_helper = RegexHelper()
with pytest.raises(WrongUsage) as exc:
regex_helper.search_compiled(None, '123')
assert "search_compiled is None" in str(exc)
def test_match_compiled_none():
from moler.cmd import RegexHelper
regex_helper = RegexHelper()
with pytest.raises(WrongUsage) as exc:
regex_helper.match_compiled(None, '123')
assert "match_compiled is None" in str(exc)
def test_group_without_match_object():
from moler.cmd import RegexHelper
regex_helper = RegexHelper()
with pytest.raises(WrongUsage) as exc:
regex_helper.group(1)
assert "Nothing was matched before calling" in str(exc)
def test_groups_without_match_object():
from moler.cmd import RegexHelper
regex_helper = RegexHelper()
with pytest.raises(WrongUsage) as exc:
regex_helper.groups()
assert "Nothing was matched before calling" in str(exc)
def test_groupdict_without_match_object():
from moler.cmd import RegexHelper
regex_helper = RegexHelper()
with pytest.raises(WrongUsage) as exc:
regex_helper.groupdict()
assert "Nothing was matched before calling" in str(exc)
def test_all_chars_to_hex():
from moler.helpers import all_chars_to_hex
source = "a\n\rb" + chr(3) + chr(5)
expected_output = r"\x61\x0a\x0d\x62\x03\x05"
output = all_chars_to_hex(source=source)
assert output == expected_output
def test_non_printable_chars_to_hex():
from moler.helpers import non_printable_chars_to_hex
source = "a\n\rb" + chr(3) + chr(5)
expected_output = r"a\x0a\x0db\x03\x05"
output = non_printable_chars_to_hex(source=source)
assert output == expected_output
def test_removal_cursor_visibility_codes():
from moler.helpers import remove_cursor_visibility_codes
line = "\x1B[?25h\x1B[?25llogin\x1B[?25l :\x1B[?12h\x1B[?12l"
output = remove_cursor_visibility_codes(multiline=line)
assert "login :" == output
multiline = "\x1B[?25h\x1B[?25llogin\x1B[?25l :\x1B[?12h\x1B[?12l\n\x1B[?25h\x1B[?25l>"
output2 = remove_cursor_visibility_codes(multiline=multiline)
assert "login :\n>" == output2
def test_removal_fill_spaces_right_codes():
from moler.helpers import remove_fill_spaces_right_codes
full_line = "login:\x1B[300X\x1B[24X\x1B[300C\n"
multiline = "login:\x1B[300X\x1B[24X\x1B[300C\n\x1B[300X\x1B[300C\n"
incomplete_line = "login:\x1B[300X\x1B[300C"
output1 = remove_fill_spaces_right_codes(multiline=full_line)
assert "login:\n" == output1
output2 = remove_fill_spaces_right_codes(multiline=multiline)
assert "login:\n\n" == output2
output3 = remove_fill_spaces_right_codes(multiline=incomplete_line)
assert incomplete_line == output3 # no conversion since no newline
def test_removal_left_wrights_that_were_overwritten():
from moler.helpers import remove_overwritten_left_write
line = "\x1B[300X\x1B[300C\x1B[11;1H\x1B[?25h\x1B[?25l\x1B[HLast login:"
output = remove_overwritten_left_write(multiline=line)
assert "Last login:" == output
line2 = "\x1B[300X\x1B[300C\x1B[11;1H\x1B[?25h\x1B[?25l\x1B[H\x1B[32mLast login:"
output2 = remove_overwritten_left_write(multiline=line2)
assert "\x1B[32mLast login:" == output2
line3 = "\x1B[300X\x1B[300C\x1B[11;1H\x1B[?25h\x1B[?25l\x1B[H login:"
output3 = remove_overwritten_left_write(multiline=line3)
assert " login:" == output3
multiline = "\x1B[300X\x1B[300C\x1B[11;1H\x1B[?25h\x1B[?25l\x1B[H login:\nabc>\x1B[H\x1B[300X\nabc>\x1B[H password:"
output4 = remove_overwritten_left_write(multiline=multiline)
assert " login:\n\x1B[300X\n password:" == output4
def test_removal_text_formating_codes():
from moler.helpers import remove_text_formatting_codes
line = "\x1B[32muser-lab0@PLKR-SC5G-PC11 \x1B[33m~\x1B[m"
output = remove_text_formatting_codes(multiline=line)
assert "user-lab0@PLKR-SC5G-PC11 ~" == output
multiline = "\x1B[32muser-lab0@PLKR-SC5G-PC11 \x1B[33m~\x1B[m$ adb shell\n\x1B[32mmsmnile:/ #\x1B[m"
output2 = remove_text_formatting_codes(multiline=multiline)
assert "user-lab0@PLKR-SC5G-PC11 ~$ adb shell\nmsmnile:/ #" == output2
def test_removal_window_title_codes():
from moler.helpers import remove_window_title_codes
line = "\x1B]0;~\x07"
output = remove_window_title_codes(multiline=line)
assert "" == output
multiline = "\x1B]0;~\x07\n\x1B]2;~\x07"
output2 = remove_window_title_codes(multiline=multiline)
assert "\n" == output2
def test_convert_to_int():
from moler.helpers import convert_to_int, compare_objects
sample_input = {'KEY': [{'KEY1 ': {'contextInfoList': ['sample', '2', '4'],
'someIds': '0'}},
{'KEY2': {'contextInfoList': [],
'num': '20',
'poolId': '1',
'user': {
'contextType': 'sample',
'numContexts': '3',
'num': '4'}}}]}
expected_output = {'KEY': [{'KEY1 ': {'contextInfoList': ['sample', 2, 4],
'someIds': 0}},
{'KEY2': {'contextInfoList': [],
'num': 20,
'poolId': 1,
'user': {
'contextType': 'sample',
'numContexts': 3,
'num': 4}}}]}
assert not compare_objects(convert_to_int(sample_input), expected_output)