forked from robotframework/robotframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_escaping.py
More file actions
200 lines (168 loc) · 7.4 KB
/
Copy pathtest_escaping.py
File metadata and controls
200 lines (168 loc) · 7.4 KB
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
import unittest
from robot.utils.asserts import assert_equal
from robot.utils.escaping import escape, unescape, split_from_equals
def assert_unescape(inp, exp):
assert_equal(unescape(inp), exp, repr(inp))
class TestUnEscape(unittest.TestCase):
def test_no_backslash(self):
for inp in ['no escapes', '', 42]:
assert_unescape(inp, inp)
def test_single_backslash(self):
for inp, exp in [('\\', ''),
('\\ ', ' '),
('\\ ', ' '),
('a\\', 'a'),
('\\a', 'a'),
('\\-', u'-'),
(u'\\\xe4', u'\xe4'),
('\\0', '0'),
('a\\b\\c\\d', 'abcd')]:
assert_unescape(inp, exp)
def test_multiple_backslash(self):
for inp, exp in [('\\\\', '\\'),
('\\\\\\', '\\'),
('\\\\\\\\', '\\\\'),
('\\\\\\\\\\', '\\\\'),
('x\\\\x', 'x\\x'),
('x\\\\\\x', 'x\\x'),
('x\\\\\\\\x', 'x\\\\x')]:
assert_unescape(inp, exp)
def test_newline(self):
for inp, exp in [('\\n', '\n'),
('\\\\n', '\\n'),
('\\\\\\n', '\\\n'),
('\\n ', '\n '),
('\\\\n ', '\\n '),
('\\\\\\n ', '\\\n '),
('\\nx', '\nx'),
('\\\\nx', '\\nx'),
('\\\\\\nx', '\\\nx'),
('\\n x', '\n x'),
('\\\\n x', '\\n x'),
('\\\\\\n x', '\\\n x')]:
assert_unescape(inp, exp)
def test_carriage_return(self):
for inp, exp in [('\\r', '\r'),
('\\\\r', '\\r'),
('\\\\\\r', '\\\r'),
('\\r ', '\r '),
('\\\\r ', '\\r '),
('\\\\\\r ', '\\\r '),
('\\rx', '\rx'),
('\\\\rx', '\\rx'),
('\\\\\\rx', '\\\rx'),
('\\r x', '\r x'),
('\\\\r x', '\\r x'),
('\\\\\\r x', '\\\r x')]:
assert_unescape(inp, exp)
def test_tab(self):
for inp, exp in [('\\t', '\t'),
('\\\\t', '\\t'),
('\\\\\\t', '\\\t'),
('\\t ', '\t '),
('\\\\t ', '\\t '),
('\\\\\\t ', '\\\t '),
('\\tx', '\tx'),
('\\\\tx', '\\tx'),
('\\\\\\tx', '\\\tx'),
('\\t x', '\t x'),
('\\\\t x', '\\t x'),
('\\\\\\t x', '\\\t x')]:
assert_unescape(inp, exp)
def test_invalid_x(self):
for inp in r'\x \xxx xx\xxx \x0 \x0g \X00 \x-1 \x+1'.split():
assert_unescape(inp, inp.replace('\\', ''))
def test_valid_x(self):
for inp, exp in [(r'\x00', u'\x00'),
(r'\xab\xBA', u'\xab\xba'),
(r'\xe4iti', u'\xe4iti')]:
assert_unescape(inp, exp)
def test_invalid_u(self):
for inp in r'''\u
\ukekkonen
b\uu
\u0
\u123
\u123x
\u-123
\u+123
\u1.23'''.split():
assert_unescape(inp, inp.replace('\\', ''))
def test_valid_u(self):
for inp, exp in [(r'\u0000', u'\x00'),
(r'\uABba', u'\uabba'),
(r'\u00e4iti', u'\xe4iti')]:
assert_unescape(inp, exp)
def test_invalid_U(self):
for inp in r'''\U
\Ukekkonen
b\Uu
\U0
\U1234567
\U1234567x
\U-1234567
\U+1234567
\U1.234567'''.split():
assert_unescape(inp, inp.replace('\\', ''))
def test_valid_U(self):
for inp, exp in [(r'\U00000000', u'\x00'),
(r'\U0000ABba', u'\uabba'),
(r'\U0001f3e9', u'\U0001f3e9'),
(r'\U0010FFFF', u'\U0010ffff'),
(r'\U000000e4iti', u'\xe4iti')]:
assert_unescape(inp, exp)
def test_U_above_valid_range(self):
assert_unescape(r'\U00110000', 'U00110000')
assert_unescape(r'\U12345678', 'U12345678')
assert_unescape(r'\UffffFFFF', 'UffffFFFF')
class TestEscape(unittest.TestCase):
def test_escape(self):
for inp, exp in [('nothing to escape', 'nothing to escape'),
('still nothing $ @', 'still nothing $ @' ),
('1 backslash to 2: \\', '1 backslash to 2: \\\\'),
('3 bs to 6: \\\\\\', '3 bs to 6: \\\\\\\\\\\\'),
('\\' * 1000, '\\' * 2000 ),
('${notvar}', '\\${notvar}'),
('@{notvar}', '\\@{notvar}'),
('${nv} ${nv} @{nv}', '\\${nv} \\${nv} \\@{nv}'),
('\\${already esc}', '\\\\\\${already esc}'),
('\\${ae} \\\\@{ae} \\\\\\@{ae}',
'\\\\\\${ae} \\\\\\\\\\@{ae} \\\\\\\\\\\\\\@{ae}'),
('%{reserved}', '\\%{reserved}'),
('&{reserved}', '\\&{reserved}'),
('*{reserved}', '\\*{reserved}'),
('x{notreserved}', 'x{notreserved}'),
('named=arg', 'named\\=arg')]:
assert_equal(escape(inp), exp, inp)
def test_escape_control_words(self):
for inp in ['ELSE', 'ELSE IF', 'AND']:
assert_equal(escape(inp), '\\' + inp)
assert_equal(escape(inp.lower()), inp.lower())
assert_equal(escape('other' + inp), 'other' + inp)
assert_equal(escape(inp + ' '), inp + ' ')
class TestSplitFromEquals(unittest.TestCase):
def test_basics(self):
for inp in 'foo=bar', '=', 'split=from=first', '===':
self._test(inp, *inp.split('=', 1))
def test_escaped(self):
self._test(r'a\=b=c', r'a\=b', 'c')
self._test(r'\=====', r'\=', '===')
self._test(r'\=\\\=\\=', r'\=\\\=\\', '')
def test_no_unescaped_equal(self):
for inp in '', 'xxx', r'\=', r'\\\=', r'\\\\\=\\\\\\\=\\\\\\\\\=':
self._test(inp, inp, None)
def test_no_split_in_variable(self):
self._test(r'${a=b}', '${a=b}', None)
self._test(r'=${a=b}', '', '${a=b}')
self._test(r'${a=b}=', '${a=b}', '')
self._test(r'\=${a=b}', r'\=${a=b}', None)
self._test(r'${a=b}=${c=d}', '${a=b}', '${c=d}')
self._test(r'${a=b}\=${c=d}', r'${a=b}\=${c=d}', None)
self._test(r'${a=b}${c=d}${e=f}\=${g=h}=${i=j}',
r'${a=b}${c=d}${e=f}\=${g=h}', '${i=j}')
def test_broken_variable(self):
self._test('${foo=bar', '${foo', 'bar')
def _test(self, inp, *exp):
assert_equal(split_from_equals(inp), exp)
if __name__ == '__main__':
unittest.main()