File: test_contexts.py

package info (click to toggle)
xonsh 0.13.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,024 kB
  • sloc: python: 46,350; makefile: 136; sh: 41; xml: 17
file content (312 lines) | stat: -rw-r--r-- 9,230 bytes parent folder | download
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
"""Tests xonsh contexts."""
from textwrap import dedent

from xonsh.contexts import Block, Functor

#
# helpers
#

X1_WITH = "x = 1\n" "with! Block() as b:\n"
SIMPLE_WITH = "with! Block() as b:\n"
FUNC_WITH = (
    "x = 1\n"
    "def func():\n"
    "    y = 1\n"
    "    with! Block() as b:\n"
    "{body}"
    "    y += 1\n"
    "    return b\n"
    "x += 1\n"
    "rtn = func()\n"
    "x += 1\n"
)

FUNC_OBSG = {"x": 3}
FUNC_OBSL = {"y": 1}


def norm_body(body):
    if not isinstance(body, str):
        body = "\n".join(body)
    body = dedent(body)
    body = body.splitlines()
    return body


def block_checks_glb(name, glbs, body, obs=None):
    block = glbs[name]
    obs = obs or {}
    for k, v in obs.items():
        assert v == glbs[k]
    body = norm_body(body)
    assert body == block.lines
    assert glbs is block.glbs
    assert block.locs is None


def block_checks_func(name, glbs, body, obsg=None, obsl=None):
    block = glbs[name]
    obsg = obsg or {}
    for k, v in obsg.items():
        assert v == glbs[k]
    body = norm_body(body)
    assert body == block.lines
    assert glbs is block.glbs
    # local context tests
    locs = block.locs
    assert locs is not None
    obsl = obsl or {}
    for k, v in obsl.items():
        assert v == locs[k]


#
# Block tests
#


def test_block_noexec(xonsh_execer_exec):
    s = "x = 1\n" "with! Block():\n" "    x += 42\n"
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    assert 1 == glbs["x"]


def test_block_oneline(xonsh_execer_exec):
    body = "    x += 42\n"
    s = X1_WITH + body
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("b", glbs, body, {"x": 1})


def test_block_manylines(xonsh_execer_exec):
    body = "    ![echo wow mom]\n" "# bad place for a comment\n" "    x += 42"
    s = X1_WITH + body
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("b", glbs, body, {"x": 1})


def test_block_leading_comment(xonsh_execer_exec):
    # leading comments do not show up in block lines
    body = "    # I am a leading comment\n" "    x += 42\n"
    s = X1_WITH + body
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("b", glbs, ["    x += 42"], {"x": 1})


def test_block_trailing_comment(xonsh_execer_exec):
    # trailing comments show up in block lines
    body = "    x += 42\n" "    # I am a trailing comment\n"
    s = X1_WITH + body
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("b", glbs, body, {"x": 1})


def test_block_trailing_line_continuation(xonsh_execer_exec):
    body = "    x += \\\n" "         42\n"
    s = X1_WITH + body
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("b", glbs, body, {"x": 1})


def test_block_trailing_close_paren(xonsh_execer_exec):
    body = '    x += int("42"\n' "             )\n"
    s = X1_WITH + body
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("b", glbs, body, {"x": 1})


def test_block_trailing_close_many(xonsh_execer_exec):
    body = (
        '    x = {None: [int("42"\n'
        "                    )\n"
        "                ]\n"
        "         }\n"
    )
    s = SIMPLE_WITH + body
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("b", glbs, body)


def test_block_trailing_triple_string(xonsh_execer_exec):
    body = '    x = """This\n' "is\n" '"probably"\n' "'not' what I meant.\n" '"""\n'
    s = SIMPLE_WITH + body
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("b", glbs, body)


def test_block_func_oneline(xonsh_execer_exec):
    body = "        x += 42\n"
    s = FUNC_WITH.format(body=body)
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_func("rtn", glbs, body, FUNC_OBSG, FUNC_OBSL)


def test_block_func_manylines(xonsh_execer_exec):
    body = "        ![echo wow mom]\n" "# bad place for a comment\n" "        x += 42\n"
    s = FUNC_WITH.format(body=body)
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_func("rtn", glbs, body, FUNC_OBSG, FUNC_OBSL)


def test_block_func_leading_comment(xonsh_execer_exec):
    # leading comments do not show up in block lines
    body = "        # I am a leading comment\n" "        x += 42\n"
    s = FUNC_WITH.format(body=body)
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_func("rtn", glbs, "        x += 42\n", FUNC_OBSG, FUNC_OBSL)


def test_block_func_trailing_comment(xonsh_execer_exec):
    # trailing comments show up in block lines
    body = "        x += 42\n" "        # I am a trailing comment\n"
    s = FUNC_WITH.format(body=body)
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_func("rtn", glbs, body, FUNC_OBSG, FUNC_OBSL)


def test_blockfunc__trailing_line_continuation(xonsh_execer_exec):
    body = "        x += \\\n" "             42\n"
    s = FUNC_WITH.format(body=body)
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_func("rtn", glbs, body, FUNC_OBSG, FUNC_OBSL)


def test_block_func_trailing_close_paren(xonsh_execer_exec):
    body = '        x += int("42"\n' "                 )\n"
    s = FUNC_WITH.format(body=body)
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_func("rtn", glbs, body, FUNC_OBSG, FUNC_OBSL)


def test_block_func_trailing_close_many(xonsh_execer_exec):
    body = (
        '        x = {None: [int("42"\n'
        "                        )\n"
        "                    ]\n"
        "             }\n"
    )
    s = FUNC_WITH.format(body=body)
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_func("rtn", glbs, body, FUNC_OBSG, FUNC_OBSL)


def test_block_func_trailing_triple_string(xonsh_execer_exec):
    body = '        x = """This\n' "is\n" '"probably"\n' "'not' what I meant.\n" '"""\n'
    s = FUNC_WITH.format(body=body)
    glbs = {"Block": Block}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_func("rtn", glbs, body, FUNC_OBSG, FUNC_OBSL)


#
# Functor tests
#

X2_WITH = "{var} = 1\n" "with! Functor() as f:\n" "{body}" "{var} += 1\n" "{calls}\n"


def test_functor_oneline_onecall_class(xonsh_execer_exec):
    body = "    global y\n" "    y += 42\n"
    calls = "f()"
    s = X2_WITH.format(body=body, calls=calls, var="y")
    glbs = {"Functor": Functor}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("f", glbs, body, {"y": 44})


def test_functor_oneline_onecall_func(xonsh_execer_exec):
    body = "    global z\n" "    z += 42\n"
    calls = "f.func()"
    s = X2_WITH.format(body=body, calls=calls, var="z")
    glbs = {"Functor": Functor}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("f", glbs, body, {"z": 44})


def test_functor_oneline_onecall_both(xonsh_execer_exec):
    body = "    global x\n" "    x += 42\n"
    calls = "f()\nf.func()"
    s = X2_WITH.format(body=body, calls=calls, var="x")
    glbs = {"Functor": Functor}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("f", glbs, body, {"x": 86})


XA_WITH = "x = [1]\n" "with! Functor() as f:\n" "{body}" "x.append(2)\n" "{calls}\n"


def test_functor_oneline_append(xonsh_execer_exec):
    body = "    x.append(3)\n"
    calls = "f()\n"
    s = XA_WITH.format(body=body, calls=calls)
    glbs = {"Functor": Functor}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("f", glbs, body, {"x": [1, 2, 3]})


def test_functor_return(xonsh_execer_exec):
    body = "    x = 42"
    t = "res = 0\n" 'with! Functor(rtn="x") as f:\n' "{body}\n" "res = f()\n"
    s = t.format(body=body)
    glbs = {"Functor": Functor}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("f", glbs, body, {"res": 42})


def test_functor_args(xonsh_execer_exec):
    body = "    x = 42 + a"
    t = (
        "res = 0\n"
        'with! Functor(args=("a",), rtn="x") as f:\n'
        "{body}\n"
        "res = f(2)\n"
    )
    s = t.format(body=body)
    glbs = {"Functor": Functor}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("f", glbs, body, {"res": 44})


def test_functor_kwargs(xonsh_execer_exec):
    body = "    x = 42 + a + b"
    t = (
        "res = 0\n"
        'with! Functor(kwargs={{"a": 1, "b": 12}}, rtn="x") as f:\n'
        "{body}\n"
        "res = f(b=6)\n"
    )
    s = t.format(body=body)
    glbs = {"Functor": Functor}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("f", glbs, body, {"res": 49})


def test_functor_fullsig(xonsh_execer_exec):
    body = "    x = 42 + a + b + c"
    t = (
        "res = 0\n"
        'with! Functor(args=("c",), kwargs={{"a": 1, "b": 12}}, rtn="x") as f:\n'
        "{body}\n"
        "res = f(55)\n"
    )
    s = t.format(body=body)
    glbs = {"Functor": Functor}
    xonsh_execer_exec(s, glbs=glbs, locs=None)
    block_checks_glb("f", glbs, body, {"res": 110})