-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathtest_context.py
More file actions
467 lines (448 loc) · 15.3 KB
/
test_context.py
File metadata and controls
467 lines (448 loc) · 15.3 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# -*- coding: utf-8 -*-
import pytest
from mathics_scanner.errors import IncompleteSyntaxError
from .helper import check_evaluation
DEFAULT_CONTEXT_PATH = '{"System`", "Global`"}'
str_test_context_1 = """
BeginPackage["FeynCalc`"];
AppendTo[$ContextPath, "FeynCalc`Package`"];
Begin["`Package`"];(*Symbols to be shared between subpackages*)
sharedSymbol;
End[];
foo::usage = "";
bar::usage = "";(*-----------------------------------------*)
Begin["`MySubpackageA`Private`"];
intVarA::usage = "";
foo[x_] := (
Print["I can access sharedSymbol directly, since it is in ",
Context[sharedSymbol], " and not in ",
Context[intVarA]];
sharedSymbol = x;
x
);
End[];(*-----------------------------------------*)
Begin["`MySubpackageB`Private`"];
intVarB::usage = "";
bar[] := (
Print["I can access sharedSymbol directly, since it is in ",
Context[sharedSymbol], " and not in ",
Context[intVarB]];
sharedSymbol
);
End[];
EndPackage[];
"""
def test_context1():
expr = ""
for line in str_test_context_1.split("\n"):
if line in ("", "\n"):
continue
expr = expr + line
try:
print("expr=", expr)
check_evaluation(
expr, "Null", to_string_expr=False, to_string_expected=False
)
expr = ""
print(" OK")
except IncompleteSyntaxError:
continue
check_evaluation("foo[42]", "42", to_string_expr=False, to_string_expected=False)
check_evaluation("bar[]", "42", to_string_expr=False, to_string_expected=False)
@pytest.mark.parametrize(
("expr", "expected", "lst_messages", "msg"),
[
(
"""globalvarY = 37;""",
None,
None,
"set the value of a global symbol",
),
(
"""globalvarZ = 37;""",
None,
None,
"set the value of a global symbol",
),
(
"""BeginPackage["apackage`"];""",
None,
None,
"Start a context. Add it to the context path",
),
(
"""Minus::usage=" usage string set in the package for Minus";""",
None,
None,
"set the usage string for a protected symbol ->no error",
),
(
"""Minus::mymessage=" custom message string for Minus";""",
None,
None,
"set a custom message for a protected symbol ->no error",
),
(
"""Minus = pq;""",
None,
tuple(
[
"Symbol Minus is Protected.",
]
),
"try to set a value for a protected symbol ->error",
),
(
"""X::usage = "package variable";""",
None,
None,
"set the usage string for a package variable",
),
(
"""globalvarZ::usage = "a global variable";""",
None,
None,
"set the usage string for a global symbol",
),
(
"""globalvarZ = 57;""",
None,
None,
"reset the value of a global symbol",
),
("""B = 6;""", None, None, "Set a symbol value in the package context"),
(
"""Begin["`implementation`"];""",
None,
None,
"Start a context. Do not add it to the context path",
),
(
"""{Context[A], Context[B], Context[X], Context[globalvarY], Context[globalvarZ]}""",
"""{"apackage`implementation`", "apackage`", "apackage`", "apackage`implementation`", "apackage`"}""",
None,
None, # "context of the variables"
),
(
"""globalvarY::usage = "a global variable";""",
None,
None,
"set the usage string for a global symbol",
),
(
"""globalvarY = 97;""",
None,
None,
"reset the value of a global symbol",
),
(
"""Plus = PP;""",
None,
tuple(
[
"Symbol Plus is Protected.",
]
),
"try to set a value for a protected symbol ->error",
),
(
"""Plus::usage=" usage string set in the package for Plus";""",
None,
None,
"set the usage string for a protected symbol ->no error",
),
(
"""Plus::mymessage=" custom message string for Plus";""",
None,
None,
"set a custom message for a protected symbol ->no error",
),
("""A = 7;""", None, None, "Set a symbol value in the context"),
("""X = 9;""", None, None, "set the value of the package variable"),
("""End[];""", None, None, "go back to the previous context"),
(
"""{Context[A], Context[B], Context[X], Context[globalvarY], Context[globalvarZ]}""",
"""{"apackage`", "apackage`", "apackage`", "apackage`", "apackage`"}""",
None,
None, # "context of the variables in the package"
),
(
"""EndPackage[];""",
None,
None,
"go back to the previous context. Keep the context in the contextpath",
),
(
"""{Context[A], Context[B], Context[X], Context[globalvarY], Context[globalvarZ]}""",
"""{"apackage`", "apackage`", "apackage`", "apackage`", "apackage`"}""",
None,
None, # "context of the variables at global level"
),
("""A""", "A", None, "A is not in any context of the context path. "),
("""B""", "6", None, "B is in a context of the context path"),
("""Global`globalvarY""", "37", None, ""),
(
"""Global`globalvarY::usage""",
"Global`globalvarY::usage",
None,
"In WMA, the value would be set in the package",
),
("""Global`globalvarZ""", "37", None, "the value set inside the package"),
(
"""Global`globalvarZ::usage""",
"Global`globalvarZ::usage",
None,
"not affected by the package",
),
("""globalvarY""", "apackage`globalvarY", None, ""),
(
"""globalvarY::usage""",
"apackage`globalvarY::usage",
None,
"In WMA, the value would be set in the package",
),
("""globalvarZ""", "57", None, "the value set inside the package"),
(
"""globalvarZ::usage""",
'"a global variable"',
None,
"not affected by the package",
),
("""X""", "9", None, "X is in a context of the context path"),
(
"""X::usage""",
'"package variable"',
None,
"X is in a context of the context path",
),
(
"""apackage`implementation`A""",
"7",
None,
"get A using its fully qualified name",
),
("""apackage`B""", "6", None, "get B using its fully qualified name"),
(
"""Plus::usage""",
' " usage string set in the package for Plus" ',
None,
"custom usage for Plus",
),
(
"""Minus::usage""",
'" usage string set in the package for Minus"',
None,
"custom usage for Minus",
),
(
"""Plus::mymessage""",
'" custom message string for Plus"',
None,
"custom message for Plus",
),
(
"""Minus::mymessage""",
'" custom message string for Minus"',
None,
"custom message for Minus",
),
(None, None, None, None),
],
)
def test_context2(expr, expected, lst_messages, msg):
if expr is not None and expected is None:
expected = "System`Null"
if lst_messages is None:
lst_messages = tuple([])
check_evaluation(
expr,
expected,
failure_message=msg,
to_string_expr=False,
to_string_expected=False,
expected_messages=lst_messages,
hold_expected=True,
)
@pytest.mark.parametrize(
("expr", "expected", "lst_messages", "msg"),
[
(None, None, None, None),
(
"$Packages",
'{"ImportExport`","XML`","Internal`","System`","Global`"}',
None,
"initial value of $Packages",
),
("$ContextPath", DEFAULT_CONTEXT_PATH, None, "Default context path"),
('BeginPackage["MyPackage`", {"VectorAnalysis`"}]', '"MyPackage`"', None, None),
(
"$Packages",
'{"MyPackage`","VectorAnalysis`","ImportExport`","XML`","Internal`","System`","Global`"}',
None,
"Now, $Package is stored as available.",
),
("$Context", '"MyPackage`"', None, None),
(
"$ContextPath",
' {"MyPackage`", "VectorAnalysis`", "System`"}',
None,
"Context path now include MyPackage` and it needs...",
),
('Begin["`Private`"]', '"MyPackage`Private`"', None, None),
("$Context", '"MyPackage`Private`"', None, None),
("$ContextPath", '{"MyPackage`", "VectorAnalysis`", "System`"}', None, None),
("End[]", '"MyPackage`Private`"', None, None),
("$Context", '"MyPackage`"', None, None),
("$ContextPath", '{"MyPackage`", "VectorAnalysis`", "System`"}', None, None),
("EndPackage[]", "System`Null", None, None),
("$Context", '"Global`"', None, None),
(
"$ContextPath",
'{"MyPackage`", "VectorAnalysis`", "System`", "Global`"}',
None,
None,
),
("End[]", '"Global`"', ["No previous context defined."], None),
("EndPackage[]", "System`Null", ["No previous context defined."], None),
(
"$Packages",
'{"MyPackage`","VectorAnalysis`","ImportExport`","XML`","Internal`","System`","Global`"}',
None,
"Now, the package name is stored.",
),
(None, None, None, None),
(
"$Packages",
'{"ImportExport`","XML`","Internal`","System`","Global`"}',
None,
"After reset, the package are not there anymore.",
),
],
)
def test_context_with_need(expr, expected, lst_messages, msg):
if expr is not None and expected is None:
expected = "System`Null"
if lst_messages is None:
lst_messages = tuple([])
check_evaluation(
expr,
expected,
failure_message=msg,
to_string_expr=False,
to_string_expected=False,
expected_messages=lst_messages,
hold_expected=True,
)
@pytest.mark.xfail
@pytest.mark.parametrize(
("expr", "expected", "lst_messages", "msg"),
[
(None, None, None, None),
# The following two tests fail because we are not producing the message yet.
(
"BeginPackage[3]",
"BeginPackage[3]",
[
"Invalid context specified at position 1 in `BeginPackage[3,...]`. A context must consist of valid symbol names separated by and ending with `."
],
"numbers are not context names",
),
(
"BeginPackage[symb]",
"BeginPackage[symb]",
[
"Invalid context specified at position 1 in `BeginPackage[symb,...]`. A context must consist of valid symbol names separated by and ending with `."
],
"symbols are not context names",
),
(
'BeginPackage["P"]',
'BeginPackage["P"]',
[
"Invalid context specified at position 1 in `BeginPackage[P,...]`. A context must consist of valid symbol names separated by and ending with `."
],
"invalid name",
),
# This test fails, because Mathics3 implementation does not check for valid context names.
# TODO: Implement Internal`SymbolNameQ to check if a string is a valid symbol name.
# ('BeginPackage["a+b`", "nocontext"]', 'BeginPackage["a+b`", {"nocontext"}]', ['Invalid context specified at position 1 in `BeginPackage[a+b`,...]`. A context must consist of valid symbol names separated by and ending with `.'], "a valid context name should not have operators inside."),
(
'BeginPackage["P", "nocontext`"]',
'BeginPackage["P", "nocontext`"]',
[
"Invalid context specified at position 1 in `BeginPackage[P,...]`. A context must consist of valid symbol names separated by and ending with `."
],
"invalid name",
),
(
'BeginPackage["P`", 3]',
'BeginPackage["P`", 3]',
[
"Context or non-empty list of contexts expected at position 2 in `BeginPackage[P`, 3]`"
],
"numbers are not valid arguments for Needs",
),
(
'BeginPackage["P`", symb]',
'BeginPackage["P`", symb]',
[
"Context or non-empty list of contexts expected at position 2 in `BeginPackage[P`, symb]`"
],
"numbers are not needs",
),
# The following test fails because with the current implementation, Mathics3 still
# set the context, even if the needs field is not a valid name
# ('BeginPackage["P`", "nocontext"]', 'BeginPackage["P`", {"nocontext"}]', ['Context or non-empty list of contexts expected at position 2 in `BeginPackage[P`, nocontext]`'], "not a valid context name in needs."),
# The following two tests fail because we are not producing the message yet.
(
"Begin[3]",
"Begin[3]",
[
"Invalid context specified at position 1 in `Begin[3]`. A context must consist of valid symbol names separated by and ending with `."
],
"numbers are not context names",
),
(
"Begin[symb]",
"Begin[symb]",
[
"Invalid context specified at position 1 in `Begin[symb,...]`. A context must consist of valid symbol names separated by and ending with `."
],
"symbols are not context names",
),
(
'Begin["P"]',
'Begin["P"]',
[
"Invalid context specified at position 1 in `Begin[P,...]`. A context must consist of valid symbol names separated by and ending with `."
],
"invalid name",
),
(
"$Context",
'"Global`"',
None,
"Any of the previous calls change the context.",
),
(
"$ContextPath",
DEFAULT_CONTEXT_PATH,
None,
"Any of the previous calls change the context.",
),
],
)
def test_context_messages(expr, expected, lst_messages, msg):
if expr is not None and expected is None:
expected = "System`Null"
if lst_messages is None:
lst_messages = tuple([])
check_evaluation(
expr,
expected,
failure_message=msg,
to_string_expr=False,
to_string_expected=False,
expected_messages=lst_messages,
hold_expected=True,
)