-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_strings.py
372 lines (306 loc) · 9.58 KB
/
test_strings.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""
Copyright 2018 Inmanta
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contact: code@inmanta.com
"""
from typing import Union
import pytest
import inmanta.compiler as compiler
from inmanta.ast import Namespace, NotFoundException
from inmanta.ast.variables import AttributeReference, Reference
from test_parser import parse_code
def test_multiline_string_interpolation(snippetcompiler):
snippetcompiler.setup_for_snippet(
"""
var = 42
str = \"\"\"var == {{ var }}\"\"\"
""",
)
(_, scopes) = compiler.do_compile()
root: Namespace = scopes.get_child("__config__")
assert root.lookup("str").get_value() == "var == 42"
def test_rstring(snippetcompiler):
snippetcompiler.setup_for_snippet(
"""
z=r"{{xxx}}"
entity X:
string a
end
# Force typecheck
X(a=z)
implement X using none
implementation none for X:
end
""",
)
compiler.do_compile()
def test_no_multiline_in_single_quoted_string_double_apostrophe(snippetcompiler):
snippetcompiler.setup_for_error(
"""
std::print(
"test
string"
)
""",
"Syntax error: Illegal character '\"' ({dir}/main.cf:3:3)",
)
def test_no_multiline_in_single_quoted_string_simple_apostrophe(snippetcompiler):
snippetcompiler.setup_for_error(
"""
std::print(
'test
string'
)
""",
"Syntax error: Illegal character ''' ({dir}/main.cf:3:3)",
)
def test_no_multiline_in_single_quoted_raw_string_double_apostrophe(snippetcompiler):
snippetcompiler.setup_for_error(
"""
std::print(
r"test
string"
)
""",
"Syntax error: Illegal character '\"' ({dir}/main.cf:3:4)",
)
def test_no_multiline_in_single_quoted_raw_string_simple_apostrophe(snippetcompiler):
snippetcompiler.setup_for_error(
"""
std::print(
r'test
string'
)
""",
"Syntax error: Illegal character ''' ({dir}/main.cf:3:4)",
)
def test_allowed_line_breaks(snippetcompiler):
snippetcompiler.setup_for_snippet(
r'''
test_string_1 = "TESTSTRING"
test_string_2 = "TEST\nSTR\rING"
test_string_3 = r"TESTSTRING"
test_string_4 = r"TEST\nSTR\rING"
test_string_5 = 'TESTSTRING'
test_string_6 = 'TEST\nSTR\rING'
test_string_7 = r'TESTSTRING'
test_string_8 = r'TEST\nSTR\rING'
test_string_9 = """TESTSTRING"""
test_string_10 = """TEST\nSTR\rING"""
test_string_11 = """TEST
STRING"""
std::print(test_string_1)
std::print(test_string_2)
std::print(test_string_3)
std::print(test_string_4)
std::print(test_string_5)
std::print(test_string_6)
std::print(test_string_7)
std::print(test_string_8)
std::print(test_string_9)
std::print(test_string_10)
std::print(test_string_11)
'''
)
compiler.do_compile()
def test_escaping_rules_single_vs_triple_quotes_2582(snippetcompiler, capsys):
"""
Check that new line characters are correctly interpreted in multi-line strings
"""
snippetcompiler.setup_for_snippet(
r'''
test_string_1 = """trippel hello\nworld"""
test_string_2 = "single hello\nworld"
std::print(test_string_1)
std::print(test_string_2)
'''
)
expected = r"""trippel hello
world
single hello
world
"""
compiler.do_compile()
out, err = capsys.readouterr()
assert expected == out
def test_fstring_float_formatting(snippetcompiler, capsys):
snippetcompiler.setup_for_snippet(
"""
arg = 12.23455
z=f"{arg:.4f}"
std::print(z)
""",
)
expected = "12.2346\n"
compiler.do_compile()
out, err = capsys.readouterr()
assert out == expected
@pytest.mark.parametrize(
"f_string,expected_output",
[
(r"f'{ arg }'", "123\n"),
(r"f'{arg}'", "123\n"),
(r"f'{arg}{arg}{arg}'", "123123123\n"),
(r"f'{arg:@>5}'", "@@123\n"),
(r"f'{arg:^5}'", " 123 \n"),
(r"f' { \t\narg \n } '", " 123 \n"),
],
)
def test_fstring_formatting(snippetcompiler, capsys, f_string, expected_output):
snippetcompiler.setup_for_snippet(
f"""
arg = 123
z={f_string}
std::print(z)
""",
)
compiler.do_compile()
out, err = capsys.readouterr()
assert out == expected_output
def test_fstring_expected_error(snippetcompiler, capsys):
with pytest.raises(NotFoundException):
snippetcompiler.setup_for_snippet(
"""
std::print(f"{unknown}")
""",
)
compiler.do_compile()
def test_fstring_relations(snippetcompiler, capsys):
snippetcompiler.setup_for_snippet(
"""
entity A:
end
entity B:
end
entity C:
int n_c = 3
end
implement A using std::none
implement B using std::none
implement C using std::none
A.b [1] -- B [1]
B.c [1] -- C [1]
a = A(b=b)
b = B(c=c)
c = C()
std::print(f"{ a .b . c . n_c }")
"""
)
compiler.do_compile()
out, err = capsys.readouterr()
expected_output = "3\n"
assert out == expected_output
def check_range(variable: Union[Reference, AttributeReference], start: int, end: int):
assert variable.location.start_char == start, f"{variable=} expected {start=} got {variable.location.start_char=}"
assert variable.location.end_char == end, f"{variable=} expected {end=} got {variable.location.end_char=}"
def test_fstring_numbering_logic():
"""
Check that variable ranges in f-strings are correctly computed
"""
statements = parse_code(
"""
# 10 20 30 40 50 60 70 80
# | | | | | | | |
std::print(f"---{s}{mm} - {sub.attr} - { padded } - { \tpadded.sub.attr }")
# | | | | |
# [-][--] [----] [------] [----] <--- expected ranges
"""
)
# Ranges are 1-indexed [start:end[
ranges = [
(len('std::print(f"---{s'), len('std::print(f"---{s}')),
(len('std::print(f"---{s}{m'), len('std::print(f"---{s}{mm}')),
(len('std::print(f"---{s}{mm} - {sub.a'), len('std::print(f"---{s}{mm} - {sub.attr}')),
(len('std::print(f"---{s}{mm} - {sub.attr} - { p'), len('std::print(f"---{s}{mm} - {sub.attr} - { padded ')),
(
len('std::print(f"---{s}{mm} - {sub.attr} - { padded } - { \tpadded.sub.a'),
len('std::print(f"---{s}{mm} - {sub.attr} - { padded } - { \tpadded.sub.attr '),
),
]
variables = statements[0].children[0]._variables
for var, range in zip(variables, ranges):
check_range(var, *range)
def test_fstring_numbering_logic_multiple_refs():
"""
Check that variable ranges in f-strings are correctly computed
"""
statements = parse_code(
"""
std::print(f"---{s}----{s}")
# | |
# [-] [-] <--- expected ranges
"""
)
# Ranges are 1-indexed [start:end[
ranges = [
(len('std::print(f"---{s'), len('std::print(f"---{s}')),
(len('std::print(f"---{s}----{s'), len('std::print(f"---{s}----{s}')),
]
variables = statements[0].children[0]._variables
for var, range in zip(variables, ranges):
check_range(var, *range)
def test_fstring_float_nested_formatting(snippetcompiler, capsys):
snippetcompiler.setup_for_snippet(
"""
width = 10
precision = 2
arg = 12.34567
z=f"result: {arg:{width}.{precision}f}"
std::print(z)
""",
)
expected = "result: 12.35\n"
compiler.do_compile()
out, err = capsys.readouterr()
assert out == expected
def test_fstring_double_brackets(snippetcompiler, capsys):
snippetcompiler.setup_for_snippet(
"""
z=f"not {{replaced}}"
std::print(z)
""",
)
expected = "not {replaced}\n"
compiler.do_compile()
out, err = capsys.readouterr()
assert out == expected
def test_fstring_numbering_logic_complex():
statements = parse_code(
"""
std::print(f"-{arg:{width}.{precision}}{other}-text-{a:{w}.{p}}-----{w}")
"""
)
# Ranges are 1-indexed [start:end[
ranges = [
(len('std::print(f"-{a'), len('std::print(f"-{arg:')),
(len('std::print(f"-{arg:{w'), len('std::print(f"-{arg:{width}')),
(len('std::print(f"-{arg:{width}.{p'), len('std::print(f"-{arg:{width}.{precision}')),
(len('std::print(f"-{arg:{width}.{precision}}{o'), len('std::print(f"-{arg:{width}.{precision}}{other}')),
(
len('std::print(f"-{arg:{width}.{precision}}{other}-text-{a'),
len('std::print(f"-{arg:{width}.{precision}}{other}-text-{a:'),
),
(
len('std::print(f"-{arg:{width}.{precision}}{other}-text-{a:{w'),
len('std::print(f"-{arg:{width}.{precision}}{other}-text-{a:{w}'),
),
(
len('std::print(f"-{arg:{width}.{precision}}{other}-text-{a:{w}.{p'),
len('std::print(f"-{arg:{width}.{precision}}{other}-text-{a:{w}.{p}'),
),
(
len('std::print(f"-{arg:{width}.{precision}}{other}-text-{a:{w}.{p}}-----{w'),
len('std::print(f"-{arg:{width}.{precision}}{other}-text-{a:{w}.{p}}-----{w}'),
),
]
variables = statements[0].children[0]._variables
for var, range in zip(variables, ranges):
check_range(var, *range)