xonsh/tests/test_parser.py

3064 lines
61 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2015-04-02 19:42:38 -05:00
"""Tests the xonsh parser."""
2015-01-24 19:17:15 -06:00
import ast
2016-07-01 17:50:01 +03:00
import builtins
2016-08-28 15:13:03 -04:00
import textwrap
2016-08-20 13:08:56 -04:00
import itertools
2015-01-24 12:27:31 -06:00
2016-06-22 17:32:02 -04:00
import pytest
2015-01-24 12:27:31 -06:00
from xonsh.ast import AST, With, Pass, pdump, Str, Call
2015-01-24 12:27:31 -06:00
from xonsh.parser import Parser
2018-10-20 14:07:18 -06:00
from xonsh.parsers.base import eval_fstr_fields
2015-01-24 12:27:31 -06:00
from tools import VER_FULL, skip_if_lt_py36, nodes_equal
2015-01-24 20:19:10 -06:00
# a lot of col_offset data changed from Py v3.5.0 -> v3.5.1
2016-02-03 01:29:06 -05:00
INC_ATTRS = (3, 5, 1) <= VER_FULL
2018-08-30 09:18:49 -05:00
2016-07-01 17:50:01 +03:00
@pytest.fixture(autouse=True)
def xonsh_builtins_autouse(xonsh_builtins):
return xonsh_builtins
2018-08-30 09:18:49 -05:00
2016-09-02 19:57:25 -04:00
PARSER = Parser(lexer_optimize=False, yacc_optimize=False, yacc_debug=True)
2016-07-01 17:50:01 +03:00
2015-01-24 20:19:10 -06:00
2018-08-30 09:18:49 -05:00
def check_ast(inp, run=True, mode="eval", debug_level=0):
__tracebackhide__ = True
2015-01-24 20:19:10 -06:00
# expect a Python AST
2015-04-02 19:42:38 -05:00
exp = ast.parse(inp, mode=mode)
2015-01-24 20:19:10 -06:00
# observe something from xonsh
2017-07-22 15:35:38 -04:00
obs = PARSER.parse(inp, debug_level=debug_level)
2015-01-24 20:19:10 -06:00
# Check that they are equal
2016-07-01 17:50:01 +03:00
assert nodes_equal(exp, obs)
2015-01-24 20:19:10 -06:00
# round trip by running xonsh AST via Python
2015-01-25 19:43:23 -06:00
if run:
2018-08-30 09:18:49 -05:00
exec(compile(obs, "<test-ast>", mode))
2015-01-24 12:27:31 -06:00
2018-08-30 09:18:49 -05:00
def check_stmts(inp, run=True, mode="exec", debug_level=0):
__tracebackhide__ = True
2018-08-30 09:18:49 -05:00
if not inp.endswith("\n"):
inp += "\n"
2017-07-22 15:35:38 -04:00
check_ast(inp, run=run, mode=mode, debug_level=debug_level)
2015-01-25 18:22:43 -06:00
2018-08-30 09:18:49 -05:00
def check_xonsh_ast(xenv, inp, run=True, mode="eval", debug_level=0, return_obs=False):
__tracebackhide__ = True
2018-09-13 14:03:35 -04:00
builtins.__xonsh__.env = xenv
2016-08-14 18:52:39 -04:00
obs = PARSER.parse(inp, debug_level=debug_level)
2016-07-01 17:50:01 +03:00
if obs is None:
return # comment only
2018-08-30 09:18:49 -05:00
bytecode = compile(obs, "<test-xonsh-ast>", mode)
2016-07-01 17:50:01 +03:00
if run:
exec(bytecode)
2016-08-20 07:46:03 -04:00
return obs if return_obs else True
2015-07-29 23:58:25 +02:00
2018-08-30 09:18:49 -05:00
def check_xonsh(xenv, inp, run=True, mode="exec"):
__tracebackhide__ = True
2018-08-30 09:18:49 -05:00
if not inp.endswith("\n"):
inp += "\n"
2015-04-02 19:42:38 -05:00
check_xonsh_ast(xenv, inp, run=run, mode=mode)
2015-01-31 18:24:12 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 18:22:43 -06:00
#
# Tests
#
2015-01-24 12:27:31 -06:00
2015-01-27 21:52:47 -06:00
#
# expressions
#
2018-08-30 09:18:49 -05:00
2015-01-24 12:27:31 -06:00
def test_int_literal():
2018-08-30 09:18:49 -05:00
check_ast("42")
2017-10-09 13:36:34 +00:00
@skip_if_lt_py36
def test_int_literal_underscore():
2018-08-30 09:18:49 -05:00
check_ast("4_2")
2015-01-24 12:27:31 -06:00
2015-01-24 20:19:10 -06:00
def test_float_literal():
2018-08-30 09:18:49 -05:00
check_ast("42.0")
2017-10-09 13:36:34 +00:00
@skip_if_lt_py36
def test_float_literal_underscore():
2018-08-30 09:18:49 -05:00
check_ast("4_2.4_2")
2015-01-24 20:19:10 -06:00
2015-03-21 19:10:57 -04:00
def test_imag_literal():
2018-08-30 09:18:49 -05:00
check_ast("42j")
2015-03-21 19:10:57 -04:00
def test_float_imag_literal():
2018-08-30 09:18:49 -05:00
check_ast("42.0j")
2015-03-21 19:10:57 -04:00
def test_complex():
2018-08-30 09:18:49 -05:00
check_ast("42+84j")
2015-03-21 19:10:57 -04:00
2015-01-24 20:19:10 -06:00
def test_str_literal():
check_ast('"hello"')
2015-01-24 20:19:10 -06:00
2018-08-30 09:18:49 -05:00
2015-01-24 20:19:10 -06:00
def test_bytes_literal():
check_ast('b"hello"')
check_ast('B"hello"')
2018-08-30 09:18:49 -05:00
def test_raw_literal():
2018-10-01 23:01:00 -04:00
check_ast('r"hell\\o"')
check_ast('R"hell\\o"')
2018-08-30 09:18:49 -05:00
2017-07-22 15:40:04 -04:00
@skip_if_lt_py36
2017-07-17 13:04:53 -05:00
def test_f_literal():
2017-07-22 15:35:38 -04:00
check_ast('f"wakka{yo}yakka{42}"', run=False)
check_ast('F"{yo}"', run=False)
2017-07-17 13:04:53 -05:00
2018-08-30 09:18:49 -05:00
@skip_if_lt_py36
def test_f_env_var():
check_xonsh_ast({}, 'f"{$HOME}"', run=False)
check_xonsh_ast({}, "f'{$XONSH_DEBUG}'", run=False)
check_xonsh_ast({}, 'F"{$PATH} and {$XONSH_DEBUG}"', run=False)
2018-09-22 10:36:45 -04:00
@pytest.mark.parametrize(
"inp, exp",
[
2018-10-20 14:07:18 -06:00
('f"{}"', 'f"{}"'),
2018-09-22 10:36:45 -04:00
('f"$HOME"', 'f"$HOME"'),
2018-10-20 14:07:18 -06:00
('f"{0} - {1}"', 'f"{0} - {1}"'),
2018-10-20 14:12:09 -06:00
(
'f"{$HOME}"',
"f\"{__xonsh__.execer.eval(r'$HOME', glbs=globals(), locs=locals())}\"",
),
(
'f"{ $HOME }"',
"f\"{__xonsh__.execer.eval(r'$HOME ', glbs=globals(), locs=locals())}\"",
),
(
"f\"{'$HOME'}\"",
"f\"{__xonsh__.execer.eval(r'\\'$HOME\\'', glbs=globals(), locs=locals())}\"",
),
2018-09-22 10:36:45 -04:00
],
)
2018-10-20 14:07:18 -06:00
def test_eval_fstr_fields(inp, exp):
obs = eval_fstr_fields(inp, 'f"')
assert exp == obs
def test_raw_bytes_literal():
2018-10-01 23:01:00 -04:00
check_ast('br"hell\\o"')
check_ast('RB"hell\\o"')
check_ast('Br"hell\\o"')
check_ast('rB"hell\\o"')
2015-01-24 20:19:10 -06:00
2018-08-30 09:18:49 -05:00
2015-01-24 20:33:21 -06:00
def test_unary_plus():
2018-08-30 09:18:49 -05:00
check_ast("+1")
2015-01-24 20:33:21 -06:00
def test_unary_minus():
2018-08-30 09:18:49 -05:00
check_ast("-1")
2015-01-24 20:33:21 -06:00
def test_unary_invert():
2018-08-30 09:18:49 -05:00
check_ast("~1")
2015-01-24 20:33:21 -06:00
2015-01-24 20:46:05 -06:00
def test_binop_plus():
2018-08-30 09:18:49 -05:00
check_ast("42 + 65")
2015-01-24 20:46:05 -06:00
def test_binop_minus():
2018-08-30 09:18:49 -05:00
check_ast("42 - 65")
2015-01-24 20:46:05 -06:00
def test_binop_times():
2018-08-30 09:18:49 -05:00
check_ast("42 * 65")
2015-01-24 20:46:05 -06:00
2015-09-26 22:54:12 -04:00
def test_binop_matmult():
2018-08-30 09:18:49 -05:00
check_ast("x @ y", False)
2015-09-26 22:54:12 -04:00
2015-01-24 20:46:05 -06:00
def test_binop_div():
2018-08-30 09:18:49 -05:00
check_ast("42 / 65")
2015-01-24 20:46:05 -06:00
def test_binop_mod():
2018-08-30 09:18:49 -05:00
check_ast("42 % 65")
2015-01-24 20:46:05 -06:00
def test_binop_floordiv():
2018-08-30 09:18:49 -05:00
check_ast("42 // 65")
2015-01-24 20:46:05 -06:00
2015-01-24 21:56:22 -06:00
def test_binop_pow():
2018-08-30 09:18:49 -05:00
check_ast("2 ** 2")
2015-01-24 21:56:22 -06:00
2015-01-24 22:19:57 -06:00
def test_plus_pow():
2018-08-30 09:18:49 -05:00
check_ast("42 + 2 ** 2")
2015-01-24 20:46:05 -06:00
2015-01-24 22:19:57 -06:00
def test_plus_plus():
2018-08-30 09:18:49 -05:00
check_ast("42 + 65 + 6")
2015-01-24 20:33:21 -06:00
2015-01-24 22:19:57 -06:00
def test_plus_minus():
2018-08-30 09:18:49 -05:00
check_ast("42 + 65 - 6")
2015-01-24 20:33:21 -06:00
2015-01-24 22:19:57 -06:00
def test_minus_plus():
2018-08-30 09:18:49 -05:00
check_ast("42 - 65 + 6")
2015-01-24 20:33:21 -06:00
2015-01-24 22:19:57 -06:00
def test_minus_minus():
2018-08-30 09:18:49 -05:00
check_ast("42 - 65 - 6")
2015-01-24 22:19:57 -06:00
def test_minus_plus_minus():
2018-08-30 09:18:49 -05:00
check_ast("42 - 65 + 6 - 28")
2015-01-24 22:19:57 -06:00
def test_times_plus():
2018-08-30 09:18:49 -05:00
check_ast("42 * 65 + 6")
2015-01-24 22:19:57 -06:00
def test_plus_times():
2018-08-30 09:18:49 -05:00
check_ast("42 + 65 * 6")
2015-01-24 22:19:57 -06:00
def test_times_times():
2018-08-30 09:18:49 -05:00
check_ast("42 * 65 * 6")
2015-01-24 22:19:57 -06:00
def test_times_div():
2018-08-30 09:18:49 -05:00
check_ast("42 * 65 / 6")
2015-01-24 22:19:57 -06:00
def test_times_div_mod():
2018-08-30 09:18:49 -05:00
check_ast("42 * 65 / 6 % 28")
2015-01-24 22:19:57 -06:00
def test_times_div_mod_floor():
2018-08-30 09:18:49 -05:00
check_ast("42 * 65 / 6 % 28 // 13")
2015-01-24 22:19:57 -06:00
2015-01-24 22:27:36 -06:00
def test_str_str():
2018-08-30 09:18:49 -05:00
check_ast("\"hello\" 'mom'")
2015-01-24 22:27:36 -06:00
2016-02-02 00:19:37 -05:00
def test_str_str_str():
check_ast('"hello" \'mom\' "wow"')
2016-02-02 00:19:37 -05:00
2018-08-30 09:18:49 -05:00
2015-01-24 22:27:36 -06:00
def test_str_plus_str():
2018-08-30 09:18:49 -05:00
check_ast("\"hello\" + 'mom'")
2015-01-24 22:27:36 -06:00
def test_str_times_int():
check_ast('"hello" * 20')
2015-01-24 22:27:36 -06:00
2018-08-30 09:18:49 -05:00
2015-01-24 22:27:36 -06:00
def test_int_times_str():
check_ast('2*"hello"')
2015-01-24 22:19:57 -06:00
2018-08-30 09:18:49 -05:00
2015-01-24 22:43:01 -06:00
def test_group_plus_times():
2018-08-30 09:18:49 -05:00
check_ast("(42 + 65) * 20")
2015-01-24 22:43:01 -06:00
def test_plus_group_times():
2018-08-30 09:18:49 -05:00
check_ast("42 + (65 * 20)")
2015-01-24 22:43:01 -06:00
def test_group():
2018-08-30 09:18:49 -05:00
check_ast("(42)")
2015-01-24 22:43:01 -06:00
2015-01-24 23:02:15 -06:00
def test_lt():
2018-08-30 09:18:49 -05:00
check_ast("42 < 65")
2015-01-24 23:02:15 -06:00
def test_gt():
2018-08-30 09:18:49 -05:00
check_ast("42 > 65")
2015-01-24 23:02:15 -06:00
def test_eq():
2018-08-30 09:18:49 -05:00
check_ast("42 == 65")
2015-01-24 23:02:15 -06:00
def test_le():
2018-08-30 09:18:49 -05:00
check_ast("42 <= 65")
2015-01-24 23:02:15 -06:00
def test_ge():
2018-08-30 09:18:49 -05:00
check_ast("42 >= 65")
2015-01-24 23:02:15 -06:00
def test_ne():
2018-08-30 09:18:49 -05:00
check_ast("42 != 65")
2015-01-24 23:02:15 -06:00
def test_in():
check_ast('"4" in "65"')
2015-01-24 23:02:15 -06:00
2018-08-30 09:18:49 -05:00
2015-01-24 23:02:15 -06:00
def test_is():
2018-08-30 09:18:49 -05:00
check_ast("42 is 65")
2015-01-24 23:02:15 -06:00
def test_not_in():
check_ast('"4" not in "65"')
2015-01-24 23:02:15 -06:00
2018-08-30 09:18:49 -05:00
2015-01-24 23:02:15 -06:00
def test_is_not():
2018-08-30 09:18:49 -05:00
check_ast("42 is not 65")
2015-01-24 23:02:15 -06:00
def test_lt_lt():
2018-08-30 09:18:49 -05:00
check_ast("42 < 65 < 105")
2015-01-24 23:02:15 -06:00
def test_lt_lt_lt():
2018-08-30 09:18:49 -05:00
check_ast("42 < 65 < 105 < 77")
2015-01-24 23:02:15 -06:00
2015-01-24 23:20:49 -06:00
def test_not():
2018-08-30 09:18:49 -05:00
check_ast("not 0")
2015-01-24 23:20:49 -06:00
def test_or():
2018-08-30 09:18:49 -05:00
check_ast("1 or 0")
2015-01-24 23:20:49 -06:00
def test_or_or():
2018-08-30 09:18:49 -05:00
check_ast("1 or 0 or 42")
2015-01-24 23:20:49 -06:00
def test_and():
2018-08-30 09:18:49 -05:00
check_ast("1 and 0")
2015-01-24 23:20:49 -06:00
def test_and_and():
2018-08-30 09:18:49 -05:00
check_ast("1 and 0 and 2")
2015-01-24 23:20:49 -06:00
def test_and_or():
2018-08-30 09:18:49 -05:00
check_ast("1 and 0 or 2")
2015-01-24 23:20:49 -06:00
def test_or_and():
2018-08-30 09:18:49 -05:00
check_ast("1 or 0 and 2")
2015-01-24 23:20:49 -06:00
2015-01-24 23:54:32 -06:00
def test_group_and_and():
2018-08-30 09:18:49 -05:00
check_ast("(1 and 0) and 2")
2015-01-24 23:54:32 -06:00
def test_group_and_or():
2018-08-30 09:18:49 -05:00
check_ast("(1 and 0) or 2")
2015-01-24 23:54:32 -06:00
2015-01-27 20:49:11 -06:00
def test_if_else_expr():
2018-08-30 09:18:49 -05:00
check_ast("42 if True else 65")
2015-01-27 20:49:11 -06:00
2015-04-02 19:42:38 -05:00
def test_if_else_expr_expr():
2018-08-30 09:18:49 -05:00
check_ast("42+5 if 1 == 2 else 65-5")
2015-01-27 20:49:11 -06:00
2015-01-24 23:54:32 -06:00
def test_str_idx():
check_ast('"hello"[0]')
2015-01-24 23:54:32 -06:00
2018-08-30 09:18:49 -05:00
2015-01-24 23:54:32 -06:00
def test_str_slice():
check_ast('"hello"[0:3]')
2015-01-24 23:54:32 -06:00
2018-08-30 09:18:49 -05:00
2015-01-24 23:54:32 -06:00
def test_str_step():
check_ast('"hello"[0:3:1]')
2015-01-24 23:54:32 -06:00
2018-08-30 09:18:49 -05:00
2015-01-24 23:54:32 -06:00
def test_str_slice_all():
check_ast('"hello"[:]')
2015-01-24 23:54:32 -06:00
2018-08-30 09:18:49 -05:00
2015-01-24 23:54:32 -06:00
def test_str_slice_upper():
check_ast('"hello"[5:]')
2015-01-24 23:54:32 -06:00
2018-08-30 09:18:49 -05:00
2015-01-24 23:54:32 -06:00
def test_str_slice_lower():
check_ast('"hello"[:3]')
2015-01-24 23:54:32 -06:00
2018-08-30 09:18:49 -05:00
2015-01-24 23:54:32 -06:00
def test_str_slice_other():
check_ast('"hello"[::2]')
2015-01-24 23:54:32 -06:00
2018-08-30 09:18:49 -05:00
2015-01-24 23:54:32 -06:00
def test_str_slice_lower_other():
check_ast('"hello"[:3:2]')
2015-01-24 23:54:32 -06:00
2018-08-30 09:18:49 -05:00
2015-01-24 23:54:32 -06:00
def test_str_slice_upper_other():
check_ast('"hello"[3::2]')
2015-01-24 23:54:32 -06:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_2slice():
check_ast('"hello"[0:3,0:3]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_2step():
check_ast('"hello"[0:3:1,0:4:2]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_2slice_all():
check_ast('"hello"[:,:]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_2slice_upper():
check_ast('"hello"[5:,5:]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_2slice_lower():
check_ast('"hello"[:3,:3]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_2slice_lowerupper():
check_ast('"hello"[5:,:3]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_2slice_other():
check_ast('"hello"[::2,::2]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_2slice_lower_other():
check_ast('"hello"[:3:2,:3:2]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_2slice_upper_other():
check_ast('"hello"[3::2,3::2]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_3slice():
check_ast('"hello"[0:3,0:3,0:3]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_3step():
check_ast('"hello"[0:3:1,0:4:2,1:3:2]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_3slice_all():
check_ast('"hello"[:,:,:]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_3slice_upper():
check_ast('"hello"[5:,5:,5:]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_3slice_lower():
check_ast('"hello"[:3,:3,:3]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_3slice_lowerlowerupper():
check_ast('"hello"[:3,:3,:3]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_3slice_lowerupperlower():
check_ast('"hello"[:3,5:,:3]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_3slice_lowerupperupper():
check_ast('"hello"[:3,5:,5:]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_3slice_upperlowerlower():
check_ast('"hello"[5:,5:,:3]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_3slice_upperlowerupper():
check_ast('"hello"[5:,:3,5:]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_3slice_upperupperlower():
check_ast('"hello"[5:,5:,:3]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_3slice_other():
check_ast('"hello"[::2,::2,::2]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_3slice_lower_other():
check_ast('"hello"[:3:2,:3:2,:3:2]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-17 12:27:12 -04:00
def test_str_3slice_upper_other():
check_ast('"hello"[3::2,3::2,3::2]', False)
2016-03-17 12:27:12 -04:00
2018-08-30 09:18:49 -05:00
2016-03-18 12:20:13 -04:00
def test_str_slice_true():
check_ast('"hello"[0:3,True]', False)
2016-03-18 12:20:13 -04:00
2018-08-30 09:18:49 -05:00
2016-03-18 12:20:13 -04:00
def test_str_true_slice():
check_ast('"hello"[True,0:3]', False)
2016-03-18 12:20:13 -04:00
2018-08-30 09:18:49 -05:00
2015-01-25 00:20:47 -06:00
def test_list_empty():
2018-08-30 09:18:49 -05:00
check_ast("[]")
2015-01-25 00:20:47 -06:00
def test_list_one():
2018-08-30 09:18:49 -05:00
check_ast("[1]")
2015-01-25 00:20:47 -06:00
2015-01-25 00:58:25 -06:00
def test_list_one_comma():
2018-08-30 09:18:49 -05:00
check_ast("[1,]")
2015-01-25 00:58:25 -06:00
2015-01-25 01:10:46 -06:00
def test_list_two():
2018-08-30 09:18:49 -05:00
check_ast("[1, 42]")
2015-01-25 01:10:46 -06:00
2015-01-25 02:08:48 -06:00
def test_list_three():
2018-08-30 09:18:49 -05:00
check_ast("[1, 42, 65]")
2015-01-25 02:08:48 -06:00
2015-03-18 21:55:10 -05:00
def test_list_three_comma():
2018-08-30 09:18:49 -05:00
check_ast("[1, 42, 65,]")
2015-01-25 02:08:48 -06:00
2015-03-18 21:55:10 -05:00
def test_list_one_nested():
2018-08-30 09:18:49 -05:00
check_ast("[[1]]")
2015-03-18 21:55:10 -05:00
2015-03-21 19:04:30 -05:00
def test_list_list_four_nested():
2018-08-30 09:18:49 -05:00
check_ast("[[1], [2], [3], [4]]")
2015-03-21 19:04:30 -05:00
def test_list_tuple_three_nested():
2018-08-30 09:18:49 -05:00
check_ast("[(1,), (2,), (3,)]")
2015-03-21 19:04:30 -05:00
def test_list_set_tuple_three_nested():
2018-08-30 09:18:49 -05:00
check_ast("[{(1,)}, {(2,)}, {(3,)}]")
2015-03-21 19:04:30 -05:00
2015-03-18 21:55:10 -05:00
def test_list_tuple_one_nested():
2018-08-30 09:18:49 -05:00
check_ast("[(1,)]")
2015-03-18 21:55:10 -05:00
def test_tuple_tuple_one_nested():
2018-08-30 09:18:49 -05:00
check_ast("((1,),)")
2015-03-18 21:55:10 -05:00
def test_dict_list_one_nested():
2018-08-30 09:18:49 -05:00
check_ast("{1: [2]}")
2015-03-18 21:55:10 -05:00
2015-03-18 23:32:35 -05:00
def test_dict_list_one_nested_comma():
2018-08-30 09:18:49 -05:00
check_ast("{1: [2],}")
2015-03-18 23:32:35 -05:00
def test_dict_tuple_one_nested():
2018-08-30 09:18:49 -05:00
check_ast("{1: (2,)}")
2015-03-18 23:32:35 -05:00
def test_dict_tuple_one_nested_comma():
2018-08-30 09:18:49 -05:00
check_ast("{1: (2,),}")
2015-03-18 23:32:35 -05:00
2015-03-18 23:48:00 -05:00
def test_dict_list_two_nested():
2018-08-30 09:18:49 -05:00
check_ast("{1: [2], 3: [4]}")
2015-03-18 23:48:00 -05:00
def test_set_tuple_one_nested():
2018-08-30 09:18:49 -05:00
check_ast("{(1,)}")
2015-03-18 23:48:00 -05:00
def test_set_tuple_two_nested():
2018-08-30 09:18:49 -05:00
check_ast("{(1,), (2,)}")
2015-03-18 23:48:00 -05:00
2015-01-25 02:08:48 -06:00
def test_tuple_empty():
2018-08-30 09:18:49 -05:00
check_ast("()")
2015-01-25 02:08:48 -06:00
2015-01-25 11:19:11 -06:00
def test_tuple_one_bare():
2018-08-30 09:18:49 -05:00
check_ast("1,")
2015-01-25 02:08:48 -06:00
2015-01-25 11:19:11 -06:00
def test_tuple_two_bare():
2018-08-30 09:18:49 -05:00
check_ast("1, 42")
2015-01-25 11:19:11 -06:00
def test_tuple_three_bare():
2018-08-30 09:18:49 -05:00
check_ast("1, 42, 65")
2015-01-25 11:19:11 -06:00
def test_tuple_three_bare_comma():
2018-08-30 09:18:49 -05:00
check_ast("1, 42, 65,")
2015-01-25 11:19:11 -06:00
2015-01-25 02:08:48 -06:00
def test_tuple_one_comma():
2018-08-30 09:18:49 -05:00
check_ast("(1,)")
2015-01-25 02:08:48 -06:00
2015-01-25 11:19:11 -06:00
def test_tuple_two():
2018-08-30 09:18:49 -05:00
check_ast("(1, 42)")
2015-01-25 11:19:11 -06:00
def test_tuple_three():
2018-08-30 09:18:49 -05:00
check_ast("(1, 42, 65)")
2015-01-25 11:19:11 -06:00
2015-04-02 19:42:38 -05:00
def test_tuple_three_comma():
2018-08-30 09:18:49 -05:00
check_ast("(1, 42, 65,)")
2015-01-25 01:10:46 -06:00
2016-08-21 10:07:18 +08:00
def test_bare_tuple_of_tuples():
2018-08-30 09:18:49 -05:00
check_ast("(),")
check_ast("((),),(1,)")
check_ast("(),(),")
check_ast("[],")
check_ast("[],[]")
check_ast("[],()")
check_ast("(),[],")
check_ast("((),[()],)")
2016-08-21 10:07:18 +08:00
2015-01-25 11:34:40 -06:00
def test_set_one():
2018-08-30 09:18:49 -05:00
check_ast("{42}")
2015-01-25 15:05:38 -06:00
def test_set_one_comma():
2018-08-30 09:18:49 -05:00
check_ast("{42,}")
2015-01-25 15:05:38 -06:00
def test_set_two():
2018-08-30 09:18:49 -05:00
check_ast("{42, 65}")
2015-01-25 15:05:38 -06:00
def test_set_two_comma():
2018-08-30 09:18:49 -05:00
check_ast("{42, 65,}")
2015-01-25 15:05:38 -06:00
def test_set_three():
2018-08-30 09:18:49 -05:00
check_ast("{42, 65, 45}")
2015-01-25 15:05:38 -06:00
2015-01-25 15:35:06 -06:00
def test_dict_empty():
2018-08-30 09:18:49 -05:00
check_ast("{}")
2015-01-25 15:35:06 -06:00
def test_dict_one():
2018-08-30 09:18:49 -05:00
check_ast("{42: 65}")
2015-01-25 15:35:06 -06:00
def test_dict_one_comma():
2018-08-30 09:18:49 -05:00
check_ast("{42: 65,}")
2015-01-25 15:35:06 -06:00
def test_dict_two():
2018-08-30 09:18:49 -05:00
check_ast("{42: 65, 6: 28}")
2015-01-25 15:35:06 -06:00
def test_dict_two_comma():
2018-08-30 09:18:49 -05:00
check_ast("{42: 65, 6: 28,}")
2015-01-25 15:35:06 -06:00
def test_dict_three():
2018-08-30 09:18:49 -05:00
check_ast("{42: 65, 6: 28, 1: 2}")
2015-01-25 15:35:06 -06:00
2015-09-26 20:32:26 -04:00
def test_dict_from_dict_two_xy():
2016-06-22 18:51:56 -04:00
check_ast('{"x": 1, **{"y": 2}}')
2015-09-26 20:32:26 -04:00
2018-08-30 09:18:49 -05:00
2015-09-26 20:30:44 -04:00
def test_dict_from_dict_two_x_first():
2016-06-22 18:51:56 -04:00
check_ast('{"x": 1, **{"x": 2}}')
2015-09-26 20:30:44 -04:00
2018-08-30 09:18:49 -05:00
2015-09-26 20:30:44 -04:00
def test_dict_from_dict_two_x_second():
2016-06-22 18:51:56 -04:00
check_ast('{**{"x": 2}, "x": 1}')
2015-09-26 20:30:44 -04:00
2018-08-30 09:18:49 -05:00
2015-09-26 22:27:36 -04:00
def test_unpack_range_tuple():
2018-08-30 09:18:49 -05:00
check_stmts("*range(4),")
2015-09-26 22:27:36 -04:00
def test_unpack_range_tuple_4():
2018-08-30 09:18:49 -05:00
check_stmts("*range(4), 4")
2015-09-26 22:27:36 -04:00
def test_unpack_range_tuple_parens():
2018-08-30 09:18:49 -05:00
check_ast("(*range(4),)")
2015-09-26 22:27:36 -04:00
def test_unpack_range_tuple_parens_4():
2018-08-30 09:18:49 -05:00
check_ast("(*range(4), 4)")
2015-09-26 22:27:36 -04:00
def test_unpack_range_list():
2018-08-30 09:18:49 -05:00
check_ast("[*range(4)]")
2015-09-26 22:27:36 -04:00
2015-09-26 21:27:49 -04:00
def test_unpack_range_list_4():
2018-08-30 09:18:49 -05:00
check_ast("[*range(4), 4]")
2015-09-26 21:13:03 -04:00
def test_unpack_range_set():
2018-08-30 09:18:49 -05:00
check_ast("{*range(4)}")
2015-09-26 21:27:49 -04:00
def test_unpack_range_set_4():
2018-08-30 09:18:49 -05:00
check_ast("{*range(4), 4}")
2015-09-26 21:13:03 -04:00
2015-01-25 15:59:06 -06:00
def test_true():
2018-08-30 09:18:49 -05:00
check_ast("True")
2015-01-25 15:59:06 -06:00
def test_false():
2018-08-30 09:18:49 -05:00
check_ast("False")
2015-01-25 15:59:06 -06:00
def test_none():
2018-08-30 09:18:49 -05:00
check_ast("None")
2015-01-25 15:59:06 -06:00
def test_elipssis():
2018-08-30 09:18:49 -05:00
check_ast("...")
2015-01-25 15:59:06 -06:00
def test_not_implemented_name():
2018-08-30 09:18:49 -05:00
check_ast("NotImplemented")
2015-01-25 15:59:06 -06:00
2015-01-25 16:23:47 -06:00
def test_genexpr():
check_ast('(x for x in "mom")')
2015-01-25 16:23:47 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:07:10 -06:00
def test_genexpr_if():
check_ast('(x for x in "mom" if True)')
2015-01-25 17:07:10 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:07:10 -06:00
def test_genexpr_if_and():
check_ast('(x for x in "mom" if True and x == "m")')
2015-01-25 17:07:10 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:07:10 -06:00
def test_dbl_genexpr():
check_ast('(x+y for x in "mom" for y in "dad")')
2015-01-25 17:07:10 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:07:10 -06:00
def test_genexpr_if_genexpr():
check_ast('(x+y for x in "mom" if True for y in "dad")')
2015-01-25 16:23:47 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:17:34 -06:00
def test_genexpr_if_genexpr_if():
check_ast('(x+y for x in "mom" if True for y in "dad" if y == "d")')
2015-01-25 17:17:34 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:17:34 -06:00
def test_listcomp():
check_ast('[x for x in "mom"]')
2015-01-25 17:17:34 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:19:36 -06:00
def test_listcomp_if():
check_ast('[x for x in "mom" if True]')
2015-01-25 17:19:36 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:19:36 -06:00
def test_listcomp_if_and():
check_ast('[x for x in "mom" if True and x == "m"]')
2015-01-25 17:19:36 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:19:36 -06:00
def test_dbl_listcomp():
check_ast('[x+y for x in "mom" for y in "dad"]')
2015-01-25 17:19:36 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:19:36 -06:00
def test_listcomp_if_listcomp():
check_ast('[x+y for x in "mom" if True for y in "dad"]')
2015-01-25 17:19:36 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:19:36 -06:00
def test_listcomp_if_listcomp_if():
check_ast('[x+y for x in "mom" if True for y in "dad" if y == "d"]')
2015-01-25 15:59:06 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:25:10 -06:00
def test_setcomp():
check_ast('{x for x in "mom"}')
2015-01-25 15:59:06 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:25:10 -06:00
def test_setcomp_if():
check_ast('{x for x in "mom" if True}')
2015-01-25 17:25:10 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:25:10 -06:00
def test_setcomp_if_and():
check_ast('{x for x in "mom" if True and x == "m"}')
2015-01-25 17:25:10 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:25:10 -06:00
def test_dbl_setcomp():
check_ast('{x+y for x in "mom" for y in "dad"}')
2015-01-25 17:25:10 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:25:10 -06:00
def test_setcomp_if_setcomp():
check_ast('{x+y for x in "mom" if True for y in "dad"}')
2015-01-25 17:25:10 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:25:10 -06:00
def test_setcomp_if_setcomp_if():
check_ast('{x+y for x in "mom" if True for y in "dad" if y == "d"}')
2015-01-25 15:59:06 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:30:43 -06:00
def test_dictcomp():
check_ast('{x: x for x in "mom"}')
2015-01-25 17:30:43 -06:00
2018-08-30 09:18:49 -05:00
2015-04-16 19:07:24 -05:00
def test_dictcomp_unpack_parens():
check_ast('{k: v for (k, v) in {"x": 42}.items()}')
2015-04-16 19:07:24 -05:00
2018-08-30 09:18:49 -05:00
2015-04-16 19:07:24 -05:00
def test_dictcomp_unpack_no_parens():
check_ast('{k: v for k, v in {"x": 42}.items()}')
2015-04-16 19:07:24 -05:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:30:43 -06:00
def test_dictcomp_if():
check_ast('{x: x for x in "mom" if True}')
2015-01-25 17:30:43 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:30:43 -06:00
def test_dictcomp_if_and():
check_ast('{x: x for x in "mom" if True and x == "m"}')
2015-01-25 17:30:43 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:30:43 -06:00
def test_dbl_dictcomp():
check_ast('{x: y for x in "mom" for y in "dad"}')
2015-01-25 17:30:43 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:30:43 -06:00
def test_dictcomp_if_dictcomp():
check_ast('{x: y for x in "mom" if True for y in "dad"}')
2015-01-25 17:30:43 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 17:30:43 -06:00
def test_dictcomp_if_dictcomp_if():
check_ast('{x: y for x in "mom" if True for y in "dad" if y == "d"}')
2015-01-25 17:30:43 -06:00
2018-08-30 09:18:49 -05:00
2015-01-27 21:52:47 -06:00
def test_lambda():
2018-08-30 09:18:49 -05:00
check_ast("lambda: 42")
2015-01-27 21:52:47 -06:00
def test_lambda_x():
2018-08-30 09:18:49 -05:00
check_ast("lambda x: x")
2015-01-27 21:52:47 -06:00
def test_lambda_kwx():
2018-08-30 09:18:49 -05:00
check_ast("lambda x=42: x")
2015-01-27 21:52:47 -06:00
def test_lambda_x_y():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, y: x")
2015-01-27 21:52:47 -06:00
def test_lambda_x_y_z():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, y, z: x")
2015-01-27 21:52:47 -06:00
def test_lambda_x_kwy():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, y=42: x")
2015-01-27 21:52:47 -06:00
def test_lambda_kwx_kwy():
2018-08-30 09:18:49 -05:00
check_ast("lambda x=65, y=42: x")
2015-01-27 21:52:47 -06:00
def test_lambda_kwx_kwy_kwz():
2018-08-30 09:18:49 -05:00
check_ast("lambda x=65, y=42, z=1: x")
2015-01-27 21:52:47 -06:00
def test_lambda_x_comma():
2018-08-30 09:18:49 -05:00
check_ast("lambda x,: x")
2015-01-27 21:52:47 -06:00
def test_lambda_x_y_comma():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, y,: x")
2015-01-27 21:52:47 -06:00
def test_lambda_x_y_z_comma():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, y, z,: x")
2015-01-27 21:52:47 -06:00
def test_lambda_x_kwy_comma():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, y=42,: x")
2015-01-27 21:52:47 -06:00
def test_lambda_kwx_kwy_comma():
2018-08-30 09:18:49 -05:00
check_ast("lambda x=65, y=42,: x")
2015-01-27 21:52:47 -06:00
def test_lambda_kwx_kwy_kwz_comma():
2018-08-30 09:18:49 -05:00
check_ast("lambda x=65, y=42, z=1,: x")
2015-01-27 21:52:47 -06:00
2015-01-27 22:19:27 -06:00
def test_lambda_args():
2018-08-30 09:18:49 -05:00
check_ast("lambda *args: 42")
2015-01-27 22:19:27 -06:00
def test_lambda_args_x():
2018-08-30 09:18:49 -05:00
check_ast("lambda *args, x: 42")
2015-01-27 22:19:27 -06:00
def test_lambda_args_x_y():
2018-08-30 09:18:49 -05:00
check_ast("lambda *args, x, y: 42")
2015-01-27 22:19:27 -06:00
def test_lambda_args_x_kwy():
2018-08-30 09:18:49 -05:00
check_ast("lambda *args, x, y=10: 42")
2015-01-27 22:19:27 -06:00
def test_lambda_args_kwx_y():
2018-08-30 09:18:49 -05:00
check_ast("lambda *args, x=10, y: 42")
2015-01-27 22:19:27 -06:00
def test_lambda_args_kwx_kwy():
2018-08-30 09:18:49 -05:00
check_ast("lambda *args, x=42, y=65: 42")
2015-01-27 22:19:27 -06:00
2015-01-27 22:44:58 -06:00
def test_lambda_x_args():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, *args: 42")
2015-01-27 22:44:58 -06:00
2015-01-27 22:50:29 -06:00
def test_lambda_x_args_y():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, *args, y: 42")
2015-01-27 22:50:29 -06:00
def test_lambda_x_args_y_z():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, *args, y, z: 42")
2015-01-27 22:50:29 -06:00
def test_lambda_kwargs():
2018-08-30 09:18:49 -05:00
check_ast("lambda **kwargs: 42")
2015-01-27 22:50:29 -06:00
2015-01-27 23:40:19 -06:00
def test_lambda_x_kwargs():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, **kwargs: 42")
2015-01-27 23:40:19 -06:00
def test_lambda_x_y_kwargs():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, y, **kwargs: 42")
2015-01-27 23:40:19 -06:00
def test_lambda_x_kwy_kwargs():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, y=42, **kwargs: 42")
2015-01-27 23:40:19 -06:00
def test_lambda_args_kwargs():
2018-08-30 09:18:49 -05:00
check_ast("lambda *args, **kwargs: 42")
2015-01-27 23:40:19 -06:00
def test_lambda_x_args_kwargs():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, *args, **kwargs: 42")
2015-01-27 23:40:19 -06:00
def test_lambda_x_y_args_kwargs():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, y, *args, **kwargs: 42")
2015-01-27 23:40:19 -06:00
def test_lambda_kwx_args_kwargs():
2018-08-30 09:18:49 -05:00
check_ast("lambda x=10, *args, **kwargs: 42")
2015-01-27 23:40:19 -06:00
def test_lambda_x_kwy_args_kwargs():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, y=42, *args, **kwargs: 42")
2015-01-27 23:40:19 -06:00
def test_lambda_x_args_y_kwargs():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, *args, y, **kwargs: 42")
2015-01-27 23:40:19 -06:00
def test_lambda_x_args_kwy_kwargs():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, *args, y=42, **kwargs: 42")
2015-01-27 23:40:19 -06:00
def test_lambda_args_y_kwargs():
2018-08-30 09:18:49 -05:00
check_ast("lambda *args, y, **kwargs: 42")
2015-01-27 23:40:19 -06:00
2015-01-27 23:52:35 -06:00
def test_lambda_star_x():
2018-08-30 09:18:49 -05:00
check_ast("lambda *, x: 42")
2015-01-27 23:52:35 -06:00
def test_lambda_star_x_y():
2018-08-30 09:18:49 -05:00
check_ast("lambda *, x, y: 42")
2015-01-27 23:52:35 -06:00
def test_lambda_star_x_kwargs():
2018-08-30 09:18:49 -05:00
check_ast("lambda *, x, **kwargs: 42")
2015-01-27 23:52:35 -06:00
def test_lambda_star_kwx_kwargs():
2018-08-30 09:18:49 -05:00
check_ast("lambda *, x=42, **kwargs: 42")
2015-01-27 23:52:35 -06:00
def test_lambda_x_star_y():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, *, y: 42")
2015-01-27 23:52:35 -06:00
def test_lambda_x_y_star_z():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, y, *, z: 42")
2015-01-27 23:52:35 -06:00
def test_lambda_x_kwy_star_y():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, y=42, *, z: 42")
2015-01-27 23:52:35 -06:00
def test_lambda_x_kwy_star_kwy():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, y=42, *, z=65: 42")
2015-01-27 23:52:35 -06:00
def test_lambda_x_star_y_kwargs():
2018-08-30 09:18:49 -05:00
check_ast("lambda x, *, y, **kwargs: 42")
2015-01-27 23:52:35 -06:00
2015-01-28 01:17:25 -06:00
def test_call_range():
2018-08-30 09:18:49 -05:00
check_ast("range(6)")
2015-01-27 23:52:35 -06:00
2015-01-28 01:17:25 -06:00
def test_call_range_comma():
2018-08-30 09:18:49 -05:00
check_ast("range(6,)")
2015-01-27 23:52:35 -06:00
2015-01-28 01:17:25 -06:00
def test_call_range_x_y():
2018-08-30 09:18:49 -05:00
check_ast("range(6, 10)")
2015-01-27 23:52:35 -06:00
2015-01-28 01:17:25 -06:00
def test_call_range_x_y_comma():
2018-08-30 09:18:49 -05:00
check_ast("range(6, 10,)")
2015-01-28 01:17:25 -06:00
def test_call_range_x_y_z():
2018-08-30 09:18:49 -05:00
check_ast("range(6, 10, 2)")
2015-01-28 01:17:25 -06:00
def test_call_dict_kwx():
2018-08-30 09:18:49 -05:00
check_ast("dict(start=10)")
2015-01-28 01:17:25 -06:00
def test_call_dict_kwx_comma():
2018-08-30 09:18:49 -05:00
check_ast("dict(start=10,)")
2015-01-28 01:17:25 -06:00
def test_call_dict_kwx_kwy():
2018-08-30 09:18:49 -05:00
check_ast("dict(start=10, stop=42)")
2015-01-28 01:17:25 -06:00
def test_call_tuple_gen():
2018-08-30 09:18:49 -05:00
check_ast("tuple(x for x in [1, 2, 3])")
2015-01-28 01:17:25 -06:00
def test_call_tuple_genifs():
2018-08-30 09:18:49 -05:00
check_ast("tuple(x for x in [1, 2, 3] if x < 3)")
2015-01-27 23:40:19 -06:00
2015-01-28 01:45:29 -06:00
def test_call_range_star():
2018-08-30 09:18:49 -05:00
check_ast("range(*[1, 2, 3])")
2015-01-28 01:45:29 -06:00
def test_call_range_x_star():
2018-08-30 09:18:49 -05:00
check_ast("range(1, *[2, 3])")
2015-01-28 01:45:29 -06:00
def test_call_int():
check_ast('int(*["42"], base=8)')
2015-01-28 01:45:29 -06:00
2018-08-30 09:18:49 -05:00
2015-01-28 23:44:55 -06:00
def test_call_int_base_dict():
check_ast('int(*["42"], **{"base": 8})')
2015-01-28 23:44:55 -06:00
2018-08-30 09:18:49 -05:00
2015-01-28 23:44:55 -06:00
def test_call_dict_kwargs():
check_ast('dict(**{"base": 8})')
2015-01-28 23:44:55 -06:00
2018-08-30 09:18:49 -05:00
2016-06-23 14:46:15 +02:00
def test_call_list_many_star_args():
2018-08-30 09:18:49 -05:00
check_ast("min(*[1, 2], 3, *[4, 5])")
2015-09-24 19:27:19 -04:00
2016-06-23 14:46:15 +02:00
def test_call_list_many_starstar_args():
check_ast('dict(**{"a": 2}, v=3, **{"c": 5})')
2015-09-24 19:27:19 -04:00
2018-08-30 09:18:49 -05:00
2016-06-23 14:46:15 +02:00
def test_call_list_many_star_and_starstar_args():
check_ast('x(*[("a", 2)], *[("v", 3)], **{"c": 5})', False)
2015-09-24 19:27:19 -04:00
2018-08-30 09:18:49 -05:00
2015-01-28 23:44:55 -06:00
def test_call_alot():
2018-08-30 09:18:49 -05:00
check_ast("x(1, *args, **kwargs)", False)
2015-01-28 23:44:55 -06:00
def test_call_alot_next():
2018-08-30 09:18:49 -05:00
check_ast("x(x=1, *args, **kwargs)", False)
2015-01-28 23:44:55 -06:00
def test_call_alot_next_next():
2018-08-30 09:18:49 -05:00
check_ast("x(x=1, *args, y=42, **kwargs)", False)
2015-01-28 23:44:55 -06:00
2015-01-29 00:06:05 -06:00
def test_getattr():
2018-08-30 09:18:49 -05:00
check_ast("list.append")
2015-01-29 00:06:05 -06:00
def test_getattr_getattr():
2018-08-30 09:18:49 -05:00
check_ast("list.append.__str__")
2015-01-29 00:06:05 -06:00
2015-01-30 21:26:56 -06:00
def test_dict_tuple_key():
2018-08-30 09:18:49 -05:00
check_ast("{(42, 1): 65}")
2015-01-30 21:26:56 -06:00
def test_dict_tuple_key_get():
2018-08-30 09:18:49 -05:00
check_ast("{(42, 1): 65}[42, 1]")
2015-01-30 21:26:56 -06:00
def test_dict_tuple_key_get_3():
2018-08-30 09:18:49 -05:00
check_ast("{(42, 1, 3): 65}[42, 1, 3]")
2015-01-28 01:45:29 -06:00
2015-01-31 13:52:49 -06:00
def test_pipe_op():
2018-08-30 09:18:49 -05:00
check_ast("{42} | {65}")
2015-01-31 13:52:49 -06:00
def test_pipe_op_two():
2018-08-30 09:18:49 -05:00
check_ast("{42} | {65} | {1}")
2015-01-27 23:40:19 -06:00
2015-01-31 13:54:03 -06:00
def test_pipe_op_three():
2018-08-30 09:18:49 -05:00
check_ast("{42} | {65} | {1} | {7}")
2015-01-31 13:54:03 -06:00
2015-01-31 14:06:20 -06:00
def test_xor_op():
2018-08-30 09:18:49 -05:00
check_ast("{42} ^ {65}")
2015-01-31 14:06:20 -06:00
def test_xor_op_two():
2018-08-30 09:18:49 -05:00
check_ast("{42} ^ {65} ^ {1}")
2015-01-31 14:06:20 -06:00
def test_xor_op_three():
2018-08-30 09:18:49 -05:00
check_ast("{42} ^ {65} ^ {1} ^ {7}")
2015-01-31 14:06:20 -06:00
def test_xor_pipe():
2018-08-30 09:18:49 -05:00
check_ast("{42} ^ {65} | {1}")
2015-01-31 14:06:20 -06:00
def test_amp_op():
2018-08-30 09:18:49 -05:00
check_ast("{42} & {65}")
2015-01-31 14:06:20 -06:00
def test_amp_op_two():
2018-08-30 09:18:49 -05:00
check_ast("{42} & {65} & {1}")
2015-01-31 14:06:20 -06:00
def test_amp_op_three():
2018-08-30 09:18:49 -05:00
check_ast("{42} & {65} & {1} & {7}")
2015-01-31 14:06:20 -06:00
def test_lshift_op():
2018-08-30 09:18:49 -05:00
check_ast("42 << 65")
2015-01-31 14:06:20 -06:00
def test_lshift_op_two():
2018-08-30 09:18:49 -05:00
check_ast("42 << 65 << 1")
2015-01-31 14:06:20 -06:00
def test_lshift_op_three():
2018-08-30 09:18:49 -05:00
check_ast("42 << 65 << 1 << 7")
2015-01-31 14:06:20 -06:00
def test_rshift_op():
2018-08-30 09:18:49 -05:00
check_ast("42 >> 65")
2015-01-31 14:06:20 -06:00
def test_rshift_op_two():
2018-08-30 09:18:49 -05:00
check_ast("42 >> 65 >> 1")
2015-01-31 14:06:20 -06:00
def test_rshift_op_three():
2018-08-30 09:18:49 -05:00
check_ast("42 >> 65 >> 1 >> 7")
2015-01-31 14:06:20 -06:00
2015-01-27 21:52:47 -06:00
#
# statements
#
2018-08-30 09:18:49 -05:00
2015-01-25 18:22:43 -06:00
def test_equals():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42")
2015-01-25 18:22:43 -06:00
2015-01-25 18:34:30 -06:00
def test_equals_semi():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42;")
2015-01-25 18:34:30 -06:00
2015-03-26 00:51:31 -05:00
def test_x_y_equals_semi():
2018-08-30 09:18:49 -05:00
check_stmts("x = y = 42")
2015-03-26 00:51:31 -05:00
2015-01-25 18:34:30 -06:00
def test_equals_two():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; y = 65")
2015-01-25 18:34:30 -06:00
def test_equals_two_semi():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; y = 65;")
2015-01-25 18:34:30 -06:00
def test_equals_three():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; y = 65; z = 6")
2015-01-25 18:34:30 -06:00
def test_equals_three_semi():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; y = 65; z = 6;")
2015-01-25 18:34:30 -06:00
2015-01-25 18:55:33 -06:00
def test_plus_eq():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; x += 65")
2015-01-25 18:55:33 -06:00
def test_sub_eq():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; x -= 2")
2015-01-25 18:55:33 -06:00
def test_times_eq():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; x *= 2")
2015-01-25 18:55:33 -06:00
2015-09-26 22:54:12 -04:00
def test_matmult_eq():
2018-08-30 09:18:49 -05:00
check_stmts("x @= y", False)
2015-09-26 22:54:12 -04:00
2015-01-25 18:55:33 -06:00
def test_div_eq():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; x /= 2")
2015-01-25 18:55:33 -06:00
def test_floordiv_eq():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; x //= 2")
2015-01-25 18:55:33 -06:00
def test_pow_eq():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; x **= 2")
2015-01-25 18:55:33 -06:00
def test_mod_eq():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; x %= 2")
2015-01-25 18:55:33 -06:00
def test_xor_eq():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; x ^= 2")
2015-01-25 18:55:33 -06:00
def test_ampersand_eq():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; x &= 2")
2015-01-25 18:55:33 -06:00
def test_bitor_eq():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; x |= 2")
2015-01-25 18:55:33 -06:00
def test_lshift_eq():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; x <<= 2")
2015-01-25 18:55:33 -06:00
def test_rshift_eq():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; x >>= 2")
2015-01-25 18:55:33 -06:00
2015-03-25 23:53:15 -05:00
def test_bare_unpack():
2018-08-30 09:18:49 -05:00
check_stmts("x, y = 42, 65")
2015-03-25 23:53:15 -05:00
def test_lhand_group_unpack():
2018-08-30 09:18:49 -05:00
check_stmts("(x, y) = 42, 65")
2015-03-25 23:53:15 -05:00
def test_rhand_group_unpack():
2018-08-30 09:18:49 -05:00
check_stmts("x, y = (42, 65)")
2015-03-25 23:53:15 -05:00
def test_grouped_unpack():
2018-08-30 09:18:49 -05:00
check_stmts("(x, y) = (42, 65)")
2015-03-25 23:53:15 -05:00
2015-03-26 00:51:31 -05:00
def test_double_grouped_unpack():
2018-08-30 09:18:49 -05:00
check_stmts("(x, y) = (z, a) = (7, 8)")
2015-03-26 00:51:31 -05:00
def test_double_ungrouped_unpack():
2018-08-30 09:18:49 -05:00
check_stmts("x, y = z, a = 7, 8")
2015-03-26 00:51:31 -05:00
2015-01-30 22:52:26 -06:00
def test_stary_eq():
2018-08-30 09:18:49 -05:00
check_stmts("*y, = [1, 2, 3]")
2015-01-25 15:35:06 -06:00
2015-01-30 22:52:26 -06:00
def test_stary_x():
2018-08-30 09:18:49 -05:00
check_stmts("*y, x = [1, 2, 3]")
2015-07-29 23:58:25 +02:00
2015-01-31 00:49:53 -06:00
def test_tuple_x_stary():
2018-08-30 09:18:49 -05:00
check_stmts("(x, *y) = [1, 2, 3]")
2015-07-29 23:58:25 +02:00
2015-01-31 00:49:53 -06:00
def test_list_x_stary():
2018-08-30 09:18:49 -05:00
check_stmts("[x, *y] = [1, 2, 3]")
2015-01-31 00:49:53 -06:00
2015-01-31 11:34:18 -06:00
def test_bare_x_stary():
2018-08-30 09:18:49 -05:00
check_stmts("x, *y = [1, 2, 3]")
2015-01-31 11:34:18 -06:00
2015-01-31 11:43:15 -06:00
def test_bare_x_stary_z():
2018-08-30 09:18:49 -05:00
check_stmts("x, *y, z = [1, 2, 2, 3]")
2015-01-31 11:43:15 -06:00
2015-01-31 17:15:54 -06:00
def test_equals_list():
2018-08-30 09:18:49 -05:00
check_stmts("x = [42]; x[0] = 65")
2015-01-31 17:15:54 -06:00
def test_equals_dict():
2018-08-30 09:18:49 -05:00
check_stmts("x = {42: 65}; x[42] = 3")
2015-01-31 17:15:54 -06:00
def test_equals_attr():
2018-08-30 09:18:49 -05:00
check_stmts("class X(object):\n pass\nx = X()\nx.a = 65")
2015-01-31 17:15:54 -06:00
2018-11-02 19:18:37 -04:00
@skip_if_lt_py36
def test_equals_annotation():
check_stmts("x : int = 42")
def test_dict_keys():
check_stmts('x = {"x": 1}\nx.keys()')
2018-08-30 09:18:49 -05:00
2015-01-25 19:04:50 -06:00
def test_assert_msg():
check_stmts('assert True, "wow mom"')
2015-01-25 15:35:06 -06:00
2018-08-30 09:18:49 -05:00
2015-01-25 19:18:24 -06:00
def test_assert():
2018-08-30 09:18:49 -05:00
check_stmts("assert True")
2015-01-25 19:18:24 -06:00
def test_pass():
2018-08-30 09:18:49 -05:00
check_stmts("pass")
2015-01-25 19:18:24 -06:00
def test_del():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; del x")
2015-01-25 19:18:24 -06:00
2015-01-25 19:34:31 -06:00
def test_del_comma():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; del x,")
2015-01-25 19:34:31 -06:00
def test_del_two():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; y = 65; del x, y")
2015-01-25 19:34:31 -06:00
def test_del_two_comma():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; y = 65; del x, y,")
2015-01-25 19:34:31 -06:00
2017-02-25 00:50:56 -05:00
def test_del_with_parens():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42; y = 65; del (x, y)")
2017-02-25 00:50:56 -05:00
2015-01-25 19:43:23 -06:00
def test_raise():
2018-08-30 09:18:49 -05:00
check_stmts("raise", False)
2015-07-29 23:58:25 +02:00
2015-01-26 01:42:23 -06:00
def test_raise_x():
2018-08-30 09:18:49 -05:00
check_stmts("raise TypeError", False)
2015-07-29 23:58:25 +02:00
2015-01-26 01:42:23 -06:00
def test_raise_x_from():
2018-08-30 09:18:49 -05:00
check_stmts("raise TypeError from x", False)
2015-01-26 02:33:51 -06:00
def test_import_x():
2018-08-30 09:18:49 -05:00
check_stmts("import x", False)
2015-01-26 02:33:51 -06:00
def test_import_xy():
2018-08-30 09:18:49 -05:00
check_stmts("import x.y", False)
2015-01-26 02:33:51 -06:00
def test_import_xyz():
2018-08-30 09:18:49 -05:00
check_stmts("import x.y.z", False)
2015-01-26 02:33:51 -06:00
def test_from_x_import_y():
2018-08-30 09:18:49 -05:00
check_stmts("from x import y", False)
2015-07-29 23:58:25 +02:00
2015-01-26 02:33:51 -06:00
def test_from_dot_import_y():
2018-08-30 09:18:49 -05:00
check_stmts("from . import y", False)
2015-07-29 23:58:25 +02:00
2015-01-26 02:33:51 -06:00
def test_from_dotx_import_y():
2018-08-30 09:18:49 -05:00
check_stmts("from .x import y", False)
2015-07-29 23:58:25 +02:00
2015-01-26 02:33:51 -06:00
def test_from_dotdotx_import_y():
2018-08-30 09:18:49 -05:00
check_stmts("from ..x import y", False)
2015-01-26 02:33:51 -06:00
def test_from_dotdotdotx_import_y():
2018-08-30 09:18:49 -05:00
check_stmts("from ...x import y", False)
2015-01-26 02:33:51 -06:00
def test_from_dotdotdotdotx_import_y():
2018-08-30 09:18:49 -05:00
check_stmts("from ....x import y", False)
2015-01-26 02:33:51 -06:00
def test_from_import_x_y():
2018-08-30 09:18:49 -05:00
check_stmts("import x, y", False)
2015-01-26 02:33:51 -06:00
def test_from_import_x_y_z():
2018-08-30 09:18:49 -05:00
check_stmts("import x, y, z", False)
2015-01-26 02:33:51 -06:00
def test_from_dot_import_x_y():
2018-08-30 09:18:49 -05:00
check_stmts("from . import x, y", False)
2015-07-29 23:58:25 +02:00
2015-01-26 02:33:51 -06:00
def test_from_dot_import_x_y_z():
2018-08-30 09:18:49 -05:00
check_stmts("from . import x, y, z", False)
2015-01-26 02:33:51 -06:00
def test_from_dot_import_group_x_y():
2018-08-30 09:18:49 -05:00
check_stmts("from . import (x, y)", False)
2015-01-26 02:33:51 -06:00
def test_import_x_as_y():
2018-08-30 09:18:49 -05:00
check_stmts("import x as y", False)
2015-01-26 02:33:51 -06:00
def test_import_xy_as_z():
2018-08-30 09:18:49 -05:00
check_stmts("import x.y as z", False)
2015-01-26 02:33:51 -06:00
def test_import_x_y_as_z():
2018-08-30 09:18:49 -05:00
check_stmts("import x, y as z", False)
2015-01-26 02:33:51 -06:00
def test_import_x_as_y_z():
2018-08-30 09:18:49 -05:00
check_stmts("import x as y, z", False)
2015-01-26 02:33:51 -06:00
def test_import_x_as_y_z_as_a():
2018-08-30 09:18:49 -05:00
check_stmts("import x as y, z as a", False)
2015-01-26 02:33:51 -06:00
def test_from_dot_import_x_as_y():
2018-08-30 09:18:49 -05:00
check_stmts("from . import x as y", False)
2015-01-26 02:33:51 -06:00
2015-03-28 09:12:10 -05:00
def test_from_x_import_star():
2018-08-30 09:18:49 -05:00
check_stmts("from x import *", False)
2015-03-28 09:12:10 -05:00
def test_from_x_import_group_x_y_z():
check_stmts("from x import (x, y, z)", False)
def test_from_x_import_group_x_y_z_comma():
check_stmts("from x import (x, y, z,)", False)
2015-01-26 02:33:51 -06:00
def test_from_x_import_y_as_z():
2018-08-30 09:18:49 -05:00
check_stmts("from x import y as z", False)
2015-01-26 02:33:51 -06:00
def test_from_x_import_y_as_z_a_as_b():
2018-08-30 09:18:49 -05:00
check_stmts("from x import y as z, a as b", False)
2015-01-26 02:33:51 -06:00
def test_from_dotx_import_y_as_z_a_as_b_c_as_d():
2018-08-30 09:18:49 -05:00
check_stmts("from .x import y as z, a as b, c as d", False)
2015-01-26 02:33:51 -06:00
2015-01-26 21:43:20 -06:00
def test_continue():
2018-08-30 09:18:49 -05:00
check_stmts("continue", False)
2015-01-26 21:43:20 -06:00
def test_break():
2018-08-30 09:18:49 -05:00
check_stmts("break", False)
2015-01-26 21:43:20 -06:00
2015-01-29 00:14:16 -06:00
def test_global():
2018-08-30 09:18:49 -05:00
check_stmts("global x", False)
2015-01-29 00:14:16 -06:00
def test_global_xy():
2018-08-30 09:18:49 -05:00
check_stmts("global x, y", False)
2015-01-29 00:14:16 -06:00
def test_nonlocal_x():
2018-08-30 09:18:49 -05:00
check_stmts("nonlocal x", False)
2015-01-29 00:14:16 -06:00
def test_nonlocal_xy():
2018-08-30 09:18:49 -05:00
check_stmts("nonlocal x, y", False)
2015-01-29 00:14:16 -06:00
2015-01-26 21:43:20 -06:00
def test_yield():
2018-08-30 09:18:49 -05:00
check_stmts("yield", False)
2015-01-26 21:43:20 -06:00
def test_yield_x():
2018-08-30 09:18:49 -05:00
check_stmts("yield x", False)
2015-01-26 21:43:20 -06:00
def test_yield_x_comma():
2018-08-30 09:18:49 -05:00
check_stmts("yield x,", False)
2015-01-26 21:43:20 -06:00
def test_yield_x_y():
2018-08-30 09:18:49 -05:00
check_stmts("yield x, y", False)
2015-01-26 21:43:20 -06:00
def test_yield_from_x():
2018-08-30 09:18:49 -05:00
check_stmts("yield from x", False)
2015-01-26 21:43:20 -06:00
2015-01-26 21:49:04 -06:00
def test_return():
2018-08-30 09:18:49 -05:00
check_stmts("return", False)
2015-01-26 21:49:04 -06:00
def test_return_x():
2018-08-30 09:18:49 -05:00
check_stmts("return x", False)
2015-01-26 21:49:04 -06:00
def test_return_x_comma():
2018-08-30 09:18:49 -05:00
check_stmts("return x,", False)
2015-01-26 21:49:04 -06:00
def test_return_x_y():
2018-08-30 09:18:49 -05:00
check_stmts("return x, y", False)
2015-01-26 21:49:04 -06:00
2015-01-29 20:26:43 -06:00
def test_if_true():
2018-08-30 09:18:49 -05:00
check_stmts("if True:\n pass")
2015-01-26 21:43:20 -06:00
2015-01-29 21:48:29 -06:00
def test_if_true_twolines():
2018-08-30 09:18:49 -05:00
check_stmts("if True:\n pass\n pass")
2015-01-29 21:48:29 -06:00
2015-01-29 21:51:41 -06:00
def test_if_true_twolines_deindent():
2018-08-30 09:18:49 -05:00
check_stmts("if True:\n pass\n pass\npass")
2015-01-29 21:51:41 -06:00
2015-01-30 01:37:08 -06:00
def test_if_true_else():
2018-08-30 09:18:49 -05:00
check_stmts("if True:\n pass\nelse: \n pass")
2015-01-30 01:37:08 -06:00
def test_if_true_x():
2018-08-30 09:18:49 -05:00
check_stmts("if True:\n x = 42")
2015-01-30 01:37:08 -06:00
def test_if_switch():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42\nif x == 1:\n pass")
2015-01-30 01:37:08 -06:00
def test_if_switch_elif1_else():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42\nif x == 1:\n pass\n" "elif x == 2:\n pass\nelse:\n pass")
2015-01-30 01:37:08 -06:00
def test_if_switch_elif2_else():
2018-08-30 09:18:49 -05:00
check_stmts(
"x = 42\nif x == 1:\n pass\n"
"elif x == 2:\n pass\n"
"elif x == 3:\n pass\n"
"elif x == 4:\n pass\n"
"else:\n pass"
)
2015-01-30 01:37:08 -06:00
2015-01-30 02:15:18 -06:00
def test_if_nested():
2018-08-30 09:18:49 -05:00
check_stmts("x = 42\nif x == 1:\n pass\n if x == 4:\n pass")
2015-01-30 01:37:08 -06:00
2015-01-30 02:15:18 -06:00
def test_while():
2018-08-30 09:18:49 -05:00
check_stmts("while False:\n pass")
2015-01-30 01:37:08 -06:00
2015-01-30 02:16:47 -06:00
def test_while_else():
2018-08-30 09:18:49 -05:00
check_stmts("while False:\n pass\nelse:\n pass")
2015-01-30 02:16:47 -06:00
2015-01-30 18:41:51 -06:00
def test_for():
2018-08-30 09:18:49 -05:00
check_stmts("for x in range(6):\n pass")
2015-01-30 18:41:51 -06:00
def test_for_zip():
check_stmts('for x, y in zip(range(6), "123456"):\n pass')
2015-01-30 18:41:51 -06:00
2018-08-30 09:18:49 -05:00
2015-01-30 18:41:51 -06:00
def test_for_idx():
2018-08-30 09:18:49 -05:00
check_stmts("x = [42]\nfor x[0] in range(3):\n pass")
2015-01-30 18:41:51 -06:00
def test_for_zip_idx():
2018-08-30 09:18:49 -05:00
check_stmts('x = [42]\nfor x[0], y in zip(range(6), "123456"):\n' " pass")
2015-01-30 18:41:51 -06:00
def test_for_attr():
2018-08-30 09:18:49 -05:00
check_stmts("for x.a in range(3):\n pass", False)
2015-01-30 18:41:51 -06:00
def test_for_zip_attr():
check_stmts('for x.a, y in zip(range(6), "123456"):\n pass', False)
2015-01-30 18:41:51 -06:00
2018-08-30 09:18:49 -05:00
2015-01-30 18:41:51 -06:00
def test_for_else():
2018-08-30 09:18:49 -05:00
check_stmts("for x in range(6):\n pass\nelse: pass")
2015-01-30 01:37:08 -06:00
2015-09-27 00:30:49 -04:00
def test_async_for():
2016-06-22 18:51:56 -04:00
check_stmts("async def f():\n async for x in y:\n pass\n", False)
2015-09-27 00:30:49 -04:00
2018-08-30 09:18:49 -05:00
2015-01-30 18:55:12 -06:00
def test_with():
2018-08-30 09:18:49 -05:00
check_stmts("with x:\n pass", False)
2015-01-30 18:55:12 -06:00
def test_with_as():
2018-08-30 09:18:49 -05:00
check_stmts("with x as y:\n pass", False)
2015-01-30 18:55:12 -06:00
def test_with_xy():
2018-08-30 09:18:49 -05:00
check_stmts("with x, y:\n pass", False)
2015-01-30 18:55:12 -06:00
def test_with_x_as_y_z():
2018-08-30 09:18:49 -05:00
check_stmts("with x as y, z:\n pass", False)
2015-01-30 18:55:12 -06:00
def test_with_x_as_y_a_as_b():
2018-08-30 09:18:49 -05:00
check_stmts("with x as y, a as b:\n pass", False)
2015-01-30 18:55:12 -06:00
2016-02-02 21:19:56 -05:00
def test_with_in_func():
check_stmts("def f():\n with x:\n pass\n")
2016-02-02 21:19:56 -05:00
2018-08-30 09:18:49 -05:00
2015-09-27 00:24:40 -04:00
def test_async_with():
2016-06-22 18:51:56 -04:00
check_stmts("async def f():\n async with x as y:\n pass\n", False)
2015-09-27 00:24:40 -04:00
2018-08-30 09:18:49 -05:00
2015-01-30 19:09:02 -06:00
def test_try():
2018-08-30 09:18:49 -05:00
check_stmts("try:\n pass\nexcept:\n pass", False)
2015-01-30 19:09:02 -06:00
2015-01-30 19:19:44 -06:00
def test_try_except_t():
2018-08-30 09:18:49 -05:00
check_stmts("try:\n pass\nexcept TypeError:\n pass", False)
2015-01-30 19:19:44 -06:00
def test_try_except_t_as_e():
2018-08-30 09:18:49 -05:00
check_stmts("try:\n pass\nexcept TypeError as e:\n pass", False)
2015-01-30 19:19:44 -06:00
def test_try_except_t_u():
2018-08-30 09:18:49 -05:00
check_stmts("try:\n pass\nexcept (TypeError, SyntaxError):\n pass", False)
2015-01-30 19:19:44 -06:00
def test_try_except_t_u_as_e():
2018-08-30 09:18:49 -05:00
check_stmts("try:\n pass\nexcept (TypeError, SyntaxError) as e:\n pass", False)
2015-01-30 19:19:44 -06:00
def test_try_except_t_except_u():
2018-08-30 09:18:49 -05:00
check_stmts(
"try:\n pass\nexcept TypeError:\n pass\n" "except SyntaxError as f:\n pass",
False,
)
2015-01-30 19:19:44 -06:00
def test_try_except_else():
2018-08-30 09:18:49 -05:00
check_stmts("try:\n pass\nexcept:\n pass\nelse: pass", False)
2015-01-30 19:19:44 -06:00
def test_try_except_finally():
2018-08-30 09:18:49 -05:00
check_stmts("try:\n pass\nexcept:\n pass\nfinally: pass", False)
2015-01-30 19:19:44 -06:00
def test_try_except_else_finally():
2018-08-30 09:18:49 -05:00
check_stmts(
"try:\n pass\nexcept:\n pass\nelse:\n pass" "\nfinally: pass", False
)
2015-01-30 19:19:44 -06:00
def test_try_finally():
2018-08-30 09:18:49 -05:00
check_stmts("try:\n pass\nfinally: pass", False)
2015-01-30 19:19:44 -06:00
2015-01-30 19:33:02 -06:00
def test_func():
2018-08-30 09:18:49 -05:00
check_stmts("def f():\n pass")
2015-01-30 19:33:02 -06:00
def test_func_ret():
2018-08-30 09:18:49 -05:00
check_stmts("def f():\n return")
2015-01-30 19:33:02 -06:00
def test_func_ret_42():
2018-08-30 09:18:49 -05:00
check_stmts("def f():\n return 42")
2015-01-30 19:33:02 -06:00
def test_func_ret_42_65():
2018-08-30 09:18:49 -05:00
check_stmts("def f():\n return 42, 65")
2015-01-30 19:33:02 -06:00
def test_func_rarrow():
2018-08-30 09:18:49 -05:00
check_stmts("def f() -> int:\n pass")
2015-01-30 19:33:02 -06:00
2015-01-30 20:06:51 -06:00
def test_func_x():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x):\n return x")
2015-01-30 19:19:44 -06:00
2015-01-30 20:06:51 -06:00
def test_func_kwx():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x=42):\n return x")
2015-01-30 20:06:51 -06:00
def test_func_x_y():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, y):\n return x")
2015-01-30 20:06:51 -06:00
def test_func_x_y_z():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, y, z):\n return x")
2015-01-30 20:06:51 -06:00
def test_func_x_kwy():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, y=42):\n return x")
2015-01-30 20:06:51 -06:00
def test_func_kwx_kwy():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x=65, y=42):\n return x")
2015-01-30 20:06:51 -06:00
def test_func_kwx_kwy_kwz():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x=65, y=42, z=1):\n return x")
2015-01-30 20:06:51 -06:00
def test_func_x_comma():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x,):\n return x")
2015-01-30 20:06:51 -06:00
def test_func_x_y_comma():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, y,):\n return x")
2015-01-30 20:06:51 -06:00
def test_func_x_y_z_comma():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, y, z,):\n return x")
2015-01-30 20:06:51 -06:00
def test_func_x_kwy_comma():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, y=42,):\n return x")
2015-01-30 20:06:51 -06:00
def test_func_kwx_kwy_comma():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x=65, y=42,):\n return x")
2015-01-30 20:06:51 -06:00
def test_func_kwx_kwy_kwz_comma():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x=65, y=42, z=1,):\n return x")
2015-01-30 20:06:51 -06:00
def test_func_args():
2018-08-30 09:18:49 -05:00
check_stmts("def f(*args):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_args_x():
2018-08-30 09:18:49 -05:00
check_stmts("def f(*args, x):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_args_x_y():
2018-08-30 09:18:49 -05:00
check_stmts("def f(*args, x, y):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_args_x_kwy():
2018-08-30 09:18:49 -05:00
check_stmts("def f(*args, x, y=10):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_args_kwx_y():
2018-08-30 09:18:49 -05:00
check_stmts("def f(*args, x=10, y):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_args_kwx_kwy():
2018-08-30 09:18:49 -05:00
check_stmts("def f(*args, x=42, y=65):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_args():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, *args):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_args_y():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, *args, y):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_args_y_z():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, *args, y, z):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_kwargs():
2018-08-30 09:18:49 -05:00
check_stmts("def f(**kwargs):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_kwargs():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, **kwargs):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_y_kwargs():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, y, **kwargs):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_kwy_kwargs():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, y=42, **kwargs):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_args_kwargs():
2018-08-30 09:18:49 -05:00
check_stmts("def f(*args, **kwargs):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_args_kwargs():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, *args, **kwargs):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_y_args_kwargs():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, y, *args, **kwargs):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_kwx_args_kwargs():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x=10, *args, **kwargs):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_kwy_args_kwargs():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, y=42, *args, **kwargs):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_args_y_kwargs():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, *args, y, **kwargs):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_args_kwy_kwargs():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, *args, y=42, **kwargs):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_args_y_kwargs():
2018-08-30 09:18:49 -05:00
check_stmts("def f(*args, y, **kwargs):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_star_x():
2018-08-30 09:18:49 -05:00
check_stmts("def f(*, x):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_star_x_y():
2018-08-30 09:18:49 -05:00
check_stmts("def f(*, x, y):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_star_x_kwargs():
2018-08-30 09:18:49 -05:00
check_stmts("def f(*, x, **kwargs):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_star_kwx_kwargs():
2018-08-30 09:18:49 -05:00
check_stmts("def f(*, x=42, **kwargs):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_star_y():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, *, y):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_y_star_z():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, y, *, z):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_kwy_star_y():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, y=42, *, z):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_kwy_star_kwy():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, y=42, *, z=65):\n return 42")
2015-01-30 20:06:51 -06:00
def test_func_x_star_y_kwargs():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x, *, y, **kwargs):\n return 42")
2015-01-30 20:10:31 -06:00
2015-01-30 20:12:17 -06:00
def test_func_tx():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x:int):\n return x")
2015-01-30 20:12:17 -06:00
def test_func_txy():
2018-08-30 09:18:49 -05:00
check_stmts("def f(x:int, y:float=10.0):\n return x")
2015-01-30 20:12:17 -06:00
2015-01-30 20:30:45 -06:00
def test_class():
2018-08-30 09:18:49 -05:00
check_stmts("class X:\n pass")
2015-01-30 20:30:45 -06:00
def test_class_obj():
2018-08-30 09:18:49 -05:00
check_stmts("class X(object):\n pass")
2015-01-30 20:30:45 -06:00
def test_class_int_flt():
2018-08-30 09:18:49 -05:00
check_stmts("class X(int, object):\n pass")
2015-01-30 20:30:45 -06:00
2015-04-02 19:42:38 -05:00
def test_class_obj_kw():
2015-01-30 20:30:45 -06:00
# technically valid syntax, though it will fail to compile
2018-08-30 09:18:49 -05:00
check_stmts("class X(object=5):\n pass", False)
2015-01-30 20:30:45 -06:00
2015-01-30 21:12:50 -06:00
def test_decorator():
2018-08-30 09:18:49 -05:00
check_stmts("@g\ndef f():\n pass", False)
2015-01-30 21:12:50 -06:00
def test_decorator_2():
2018-08-30 09:18:49 -05:00
check_stmts("@h\n@g\ndef f():\n pass", False)
2015-01-30 21:12:50 -06:00
def test_decorator_call():
2018-08-30 09:18:49 -05:00
check_stmts("@g()\ndef f():\n pass", False)
2015-01-30 21:12:50 -06:00
def test_decorator_call_args():
2018-08-30 09:18:49 -05:00
check_stmts("@g(x, y=10)\ndef f():\n pass", False)
2015-01-30 21:12:50 -06:00
def test_decorator_dot_call_args():
2018-08-30 09:18:49 -05:00
check_stmts("@h.g(x, y=10)\ndef f():\n pass", False)
2015-01-30 21:12:50 -06:00
def test_decorator_dot_dot_call_args():
2018-08-30 09:18:49 -05:00
check_stmts("@i.h.g(x, y=10)\ndef f():\n pass", False)
2015-01-30 21:12:50 -06:00
2015-03-10 21:38:11 -05:00
def test_broken_prompt_func():
2018-08-30 09:18:49 -05:00
code = "def prompt():\n" " return '{user}'.format(\n" " user='me')\n"
check_stmts(code, False)
2015-03-10 21:38:11 -05:00
2018-08-30 09:18:49 -05:00
2015-04-02 19:42:38 -05:00
def test_class_with_methods():
2018-08-30 09:18:49 -05:00
code = (
"class Test:\n"
" def __init__(self):\n"
' self.msg("hello world")\n'
" def msg(self, m):\n"
" print(m)\n"
)
check_stmts(code, False)
2018-08-30 09:18:49 -05:00
def test_nested_functions():
2018-08-30 09:18:49 -05:00
code = (
"def test(x):\n"
" def test2(y):\n"
" return y+x\n"
" return test2\n"
)
check_stmts(code, False)
2018-08-30 09:18:49 -05:00
def test_function_blank_line():
2018-08-30 09:18:49 -05:00
code = (
"def foo():\n"
" ascii_art = [\n"
' "(╯°□°)╯︵ ┻━┻",\n'
2018-10-20 14:12:09 -06:00
r' "¯\\_(ツ)_/¯",'
"\n"
r' "┻━┻︵ \\(°□°)/ ︵ ┻━┻",'
"\n"
2018-08-30 09:18:49 -05:00
" ]\n"
"\n"
" import random\n"
" i = random.randint(0,len(ascii_art)) - 1\n"
' print(" Get to work!")\n'
" print(ascii_art[i])\n"
)
check_stmts(code, False)
2015-01-30 01:37:08 -06:00
2015-09-26 23:52:31 -04:00
def test_async_func():
2018-08-30 09:18:49 -05:00
check_stmts("async def f():\n pass\n")
2015-09-26 23:52:31 -04:00
def test_async_decorator():
2018-08-30 09:18:49 -05:00
check_stmts("@g\nasync def f():\n pass", False)
2015-09-26 23:52:31 -04:00
2015-09-27 01:09:28 -04:00
def test_async_await():
2016-06-22 18:51:56 -04:00
check_stmts("async def f():\n await fut\n", False)
2015-09-27 01:09:28 -04:00
2018-08-30 09:18:49 -05:00
2015-01-31 18:24:12 -06:00
#
# Xonsh specific syntax
#
def test_path_literal():
check_xonsh_ast({}, 'p"/foo"', False)
2016-12-08 13:56:45 +01:00
check_xonsh_ast({}, 'pr"/foo"', False)
check_xonsh_ast({}, 'rp"/foo"', False)
check_xonsh_ast({}, 'pR"/foo"', False)
check_xonsh_ast({}, 'Rp"/foo"', False)
2018-08-30 09:18:49 -05:00
def test_path_fstring_literal():
check_xonsh_ast({}, 'pf"/foo"', False)
check_xonsh_ast({}, 'fp"/foo"', False)
check_xonsh_ast({}, 'pF"/foo"', False)
check_xonsh_ast({}, 'Fp"/foo"', False)
check_xonsh_ast({}, 'pf"/foo{1+1}"', False)
check_xonsh_ast({}, 'fp"/foo{1+1}"', False)
check_xonsh_ast({}, 'pF"/foo{1+1}"', False)
check_xonsh_ast({}, 'Fp"/foo{1+1}"', False)
2016-07-01 12:43:32 +03:00
def test_dollar_name():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({"WAKKA": 42}, "$WAKKA")
2015-01-31 18:37:17 -06:00
2016-07-01 12:43:32 +03:00
def test_dollar_py():
2018-08-30 09:18:49 -05:00
check_xonsh({"WAKKA": 42}, 'x = "WAKKA"; y = ${x}')
2015-01-31 18:37:17 -06:00
2016-07-01 12:43:32 +03:00
def test_dollar_py_test():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({"WAKKA": 42}, '${None or "WAKKA"}')
2015-01-31 18:37:17 -06:00
2016-07-01 12:43:32 +03:00
def test_dollar_py_recursive_name():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({"WAKKA": 42, "JAWAKA": "WAKKA"}, "${$JAWAKA}")
2015-01-31 18:37:17 -06:00
2016-07-01 12:43:32 +03:00
def test_dollar_py_test_recursive_name():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({"WAKKA": 42, "JAWAKA": "WAKKA"}, "${None or $JAWAKA}")
2015-01-31 18:37:17 -06:00
2016-07-01 12:43:32 +03:00
def test_dollar_py_test_recursive_test():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({"WAKKA": 42, "JAWAKA": "WAKKA"}, '${${"JAWA" + $JAWAKA[-2:]}}')
2015-01-31 18:24:12 -06:00
2015-01-31 18:41:56 -06:00
def test_dollar_name_set():
2018-08-30 09:18:49 -05:00
check_xonsh({"WAKKA": 42}, "$WAKKA = 42")
2015-01-31 18:41:56 -06:00
def test_dollar_py_set():
2018-08-30 09:18:49 -05:00
check_xonsh({"WAKKA": 42}, 'x = "WAKKA"; ${x} = 65')
2015-01-31 18:41:56 -06:00
2015-01-31 19:13:08 -06:00
def test_dollar_sub():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls)", False)
2015-01-31 18:41:56 -06:00
2015-02-01 18:38:12 -06:00
def test_dollar_sub_space():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls )", False)
2015-02-01 18:38:12 -06:00
2015-02-01 17:53:53 -06:00
def test_ls_dot():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls .)", False)
2015-02-01 17:53:53 -06:00
2016-06-11 21:02:28 -04:00
def test_lambda_in_atparens():
2018-08-30 09:18:49 -05:00
check_xonsh_ast(
{}, '$(echo hello | @(lambda a, s=None: "hey!") foo bar baz)', False
)
2016-06-11 21:02:28 -04:00
2017-02-25 22:39:10 -05:00
def test_generator_in_atparens():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(echo @(i**2 for i in range(20)))", False)
2017-02-25 22:39:10 -05:00
def test_bare_tuple_in_atparens():
check_xonsh_ast({}, '$(echo @("a", 7))', False)
2018-08-30 09:18:49 -05:00
2016-06-11 21:02:28 -04:00
def test_nested_madness():
2018-08-30 09:18:49 -05:00
check_xonsh_ast(
{},
"$(@$(which echo) ls | @(lambda a, s=None: $(@(s.strip()) @(a[1]))) foo -la baz)",
False,
)
2016-06-11 21:02:28 -04:00
2018-09-17 17:52:40 -04:00
def test_atparens_intoken():
check_xonsh_ast({}, "![echo /x/@(y)/z]", False)
2018-09-17 18:23:20 -04:00
2015-02-01 19:01:02 -06:00
def test_ls_dot_nesting():
check_xonsh_ast({}, '$(ls @(None or "."))', False)
2015-02-01 19:01:02 -06:00
2018-08-30 09:18:49 -05:00
2016-07-01 12:43:32 +03:00
def test_ls_dot_nesting_var():
check_xonsh({}, 'x = "."; $(ls @(None or x))', False)
2018-08-30 09:18:49 -05:00
2015-02-01 19:32:20 -06:00
def test_ls_dot_str():
check_xonsh_ast({}, '$(ls ".")', False)
2015-01-31 18:41:56 -06:00
2018-08-30 09:18:49 -05:00
2015-02-01 21:38:58 -06:00
def test_ls_nest_ls():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls $(ls))", False)
2015-02-01 21:38:58 -06:00
def test_ls_nest_ls_dashl():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls $(ls) -l)", False)
2015-02-01 21:38:58 -06:00
2015-02-02 01:34:20 -06:00
def test_ls_envvar_strval():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({"WAKKA": "."}, "$(ls $WAKKA)", False)
2015-02-02 01:34:20 -06:00
def test_ls_envvar_listval():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({"WAKKA": [".", "."]}, "$(ls $WAKKA)", False)
2015-02-01 21:38:58 -06:00
def test_bang_sub():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "!(ls)", False)
2015-02-02 01:34:20 -06:00
def test_bang_sub_space():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "!(ls )", False)
def test_bang_ls_dot():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "!(ls .)", False)
def test_bang_ls_dot_nesting():
check_xonsh_ast({}, '!(ls @(None or "."))', False)
2018-08-30 09:18:49 -05:00
2016-07-01 12:43:32 +03:00
def test_bang_ls_dot_nesting_var():
check_xonsh({}, 'x = "."; !(ls @(None or x))', False)
2018-08-30 09:18:49 -05:00
def test_bang_ls_dot_str():
check_xonsh_ast({}, '!(ls ".")', False)
2018-08-30 09:18:49 -05:00
def test_bang_ls_nest_ls():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "!(ls $(ls))", False)
def test_bang_ls_nest_ls_dashl():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "!(ls $(ls) -l)", False)
def test_bang_ls_envvar_strval():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({"WAKKA": "."}, "!(ls $WAKKA)", False)
def test_bang_ls_envvar_listval():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({"WAKKA": [".", "."]}, "!(ls $WAKKA)", False)
2015-02-01 21:38:58 -06:00
2016-07-01 12:43:32 +03:00
def test_question():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "range?")
2016-07-01 12:43:32 +03:00
def test_dobquestion():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "range??")
2016-07-01 12:43:32 +03:00
def test_question_chain():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "range?.index?")
2016-07-01 12:43:32 +03:00
2015-02-02 03:07:08 -06:00
def test_ls_regex():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls `[Ff]+i*LE` -l)", False)
2015-02-02 03:07:08 -06:00
2015-02-23 00:33:11 -06:00
def test_backtick():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "print(`.*`)", False)
2015-02-23 00:33:11 -06:00
2016-05-19 19:23:42 -04:00
def test_ls_regex_octothorpe():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls `#[Ff]+i*LE` -l)", False)
2016-06-16 00:55:41 -04:00
def test_ls_explicitregex():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls r`[Ff]+i*LE` -l)", False)
2016-06-16 00:55:41 -04:00
def test_rbacktick():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "print(r`.*`)", False)
2016-06-16 00:55:41 -04:00
def test_ls_explicitregex_octothorpe():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls r`#[Ff]+i*LE` -l)", False)
2016-06-16 00:55:41 -04:00
def test_ls_glob():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls g`[Ff]+i*LE` -l)", False)
2016-06-16 00:55:41 -04:00
def test_gbacktick():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "print(g`.*`)", False)
2016-06-16 00:55:41 -04:00
def test_pbacktrick():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "print(p`.*`)", False)
def test_pgbacktick():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "print(pg`.*`)", False)
def test_prbacktick():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "print(pr`.*`)", False)
2016-06-16 00:55:41 -04:00
def test_ls_glob_octothorpe():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls g`#[Ff]+i*LE` -l)", False)
2016-06-16 00:55:41 -04:00
def test_ls_customsearch():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls @foo`[Ff]+i*LE` -l)", False)
2016-06-16 00:55:41 -04:00
def test_custombacktick():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "print(@foo`.*`)", False)
2016-06-16 00:55:41 -04:00
def test_ls_customsearch_octothorpe():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls @foo`#[Ff]+i*LE` -l)", False)
2016-05-19 19:23:42 -04:00
2016-05-20 19:46:46 -04:00
def test_injection():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$[@$(which python)]", False)
2016-05-20 19:46:46 -04:00
def test_rhs_nested_injection():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$[ls @$(dirname @$(which python))]", False)
2016-05-20 19:46:46 -04:00
2016-05-19 19:23:42 -04:00
def test_backtick_octothorpe():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "print(`#.*`)", False)
2016-05-19 19:23:42 -04:00
2015-02-24 20:43:41 -06:00
def test_uncaptured_sub():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$[ls]", False)
2015-02-23 00:33:11 -06:00
def test_hiddenobj_sub():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "![ls]", False)
2016-08-25 00:35:02 -04:00
def test_slash_envarv_echo():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "![echo $HOME/place]", False)
2016-08-25 00:35:02 -04:00
2016-09-24 17:00:30 -04:00
def test_echo_double_eq():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "![echo yo==yo]", False)
2016-09-24 17:00:30 -04:00
def test_bang_two_cmds_one_pipe():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "!(ls | grep wakka)", False)
def test_bang_three_cmds_two_pipes():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "!(ls | grep wakka | grep jawaka)", False)
def test_bang_one_cmd_write():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "!(ls > x.py)", False)
def test_bang_one_cmd_append():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "!(ls >> x.py)", False)
def test_bang_two_cmds_write():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "!(ls | grep wakka > x.py)", False)
def test_bang_two_cmds_append():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "!(ls | grep wakka >> x.py)", False)
def test_bang_cmd_background():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "!(emacs ugggh &)", False)
def test_bang_cmd_background_nospace():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "!(emacs ugggh&)", False)
def test_bang_git_quotes_no_space():
check_xonsh_ast({}, '![git commit -am "wakka"]', False)
2018-08-30 09:18:49 -05:00
def test_bang_git_quotes_space():
check_xonsh_ast({}, '![git commit -am "wakka jawaka"]', False)
2018-08-30 09:18:49 -05:00
2016-07-01 12:43:32 +03:00
def test_bang_git_two_quotes_space():
2018-08-30 09:18:49 -05:00
check_xonsh(
{},
'![git commit -am "wakka jawaka"]\n' '![git commit -am "flock jawaka"]\n',
False,
)
2016-07-01 12:43:32 +03:00
def test_bang_git_two_quotes_space_space():
2018-08-30 09:18:49 -05:00
check_xonsh(
{},
'![git commit -am "wakka jawaka" ]\n'
'![git commit -am "flock jawaka milwaka" ]\n',
False,
)
2016-07-01 12:43:32 +03:00
def test_bang_ls_quotes_3_space():
check_xonsh_ast({}, '![ls "wakka jawaka baraka"]', False)
2018-08-30 09:18:49 -05:00
2015-02-25 21:01:21 -06:00
def test_two_cmds_one_pipe():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls | grep wakka)", False)
2015-02-25 21:01:21 -06:00
def test_three_cmds_two_pipes():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls | grep wakka | grep jawaka)", False)
2015-02-25 21:01:21 -06:00
2016-04-01 01:52:41 -04:00
def test_two_cmds_one_and_brackets():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "![ls me] and ![grep wakka]", False)
2016-02-09 00:31:15 -05:00
def test_three_cmds_two_ands():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "![ls] and ![grep wakka] and ![grep jawaka]", False)
2016-02-09 00:31:15 -05:00
def test_two_cmds_one_doubleamps():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "![ls] && ![grep wakka]", False)
2016-02-09 00:31:15 -05:00
def test_three_cmds_two_doubleamps():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "![ls] && ![grep wakka] && ![grep jawaka]", False)
2016-02-09 00:31:15 -05:00
2016-02-09 00:47:48 -05:00
def test_two_cmds_one_or():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "![ls] or ![grep wakka]", False)
2016-02-09 00:47:48 -05:00
def test_three_cmds_two_ors():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "![ls] or ![grep wakka] or ![grep jawaka]", False)
2016-02-09 00:47:48 -05:00
def test_two_cmds_one_doublepipe():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "![ls] || ![grep wakka]", False)
2016-02-09 00:47:48 -05:00
def test_three_cmds_two_doublepipe():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "![ls] || ![grep wakka] || ![grep jawaka]", False)
2016-02-09 00:47:48 -05:00
def test_one_cmd_write():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls > x.py)", False)
def test_one_cmd_append():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls >> x.py)", False)
def test_two_cmds_write():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls | grep wakka > x.py)", False)
def test_two_cmds_append():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(ls | grep wakka >> x.py)", False)
2015-02-25 23:22:10 -06:00
def test_cmd_background():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(emacs ugggh &)", False)
2015-02-25 23:22:10 -06:00
def test_cmd_background_nospace():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "$(emacs ugggh&)", False)
2015-02-25 23:22:10 -06:00
2015-03-03 18:54:58 -06:00
def test_git_quotes_no_space():
check_xonsh_ast({}, '$[git commit -am "wakka"]', False)
2015-03-03 18:54:58 -06:00
2018-08-30 09:18:49 -05:00
2015-03-03 18:54:58 -06:00
def test_git_quotes_space():
check_xonsh_ast({}, '$[git commit -am "wakka jawaka"]', False)
2015-03-03 18:54:58 -06:00
2018-08-30 09:18:49 -05:00
2016-07-01 12:43:32 +03:00
def test_git_two_quotes_space():
2018-08-30 09:18:49 -05:00
check_xonsh(
{},
'$[git commit -am "wakka jawaka"]\n' '$[git commit -am "flock jawaka"]\n',
False,
)
2016-07-01 12:43:32 +03:00
def test_git_two_quotes_space_space():
2018-08-30 09:18:49 -05:00
check_xonsh(
{},
'$[git commit -am "wakka jawaka" ]\n'
'$[git commit -am "flock jawaka milwaka" ]\n',
False,
)
2016-07-01 12:43:32 +03:00
2015-03-03 19:57:18 -06:00
def test_ls_quotes_3_space():
check_xonsh_ast({}, '$[ls "wakka jawaka baraka"]', False)
2015-03-03 19:57:18 -06:00
2018-08-30 09:18:49 -05:00
2016-05-24 23:55:07 -04:00
def test_echo_comma():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "![echo ,]", False)
2016-05-24 23:55:07 -04:00
def test_echo_internal_comma():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "![echo 1,2]", False)
2016-05-24 23:55:07 -04:00
2015-03-07 23:02:49 -06:00
def test_comment_only():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "# hello")
2015-03-07 23:02:49 -06:00
2016-06-02 17:12:46 -04:00
def test_echo_slash_question():
2018-08-30 09:18:49 -05:00
check_xonsh_ast({}, "![echo /?]", False)
2016-06-02 17:12:46 -04:00
2017-02-24 22:39:36 -05:00
def test_bad_quotes():
with pytest.raises(SyntaxError):
check_xonsh_ast({}, '![echo """hello]', False)
2016-06-02 17:12:46 -04:00
2018-08-30 09:18:49 -05:00
2015-05-20 18:17:34 -04:00
def test_redirect():
2018-08-30 09:18:49 -05:00
assert check_xonsh_ast({}, "$[cat < input.txt]", False)
assert check_xonsh_ast({}, "$[< input.txt cat]", False)
@pytest.mark.parametrize(
"case",
[
"![(cat)]",
"![(cat;)]",
"![(cd path; ls; cd)]",
'![(echo "abc"; sleep 1; echo "def")]',
'![(echo "abc"; sleep 1; echo "def") | grep abc]',
"![(if True:\n ls\nelse:\n echo not true)]",
],
)
2017-02-25 16:45:33 -05:00
def test_use_subshell(case):
check_xonsh_ast({}, case, False, debug_level=0)
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize(
"case",
[
"$[cat < /path/to/input.txt]",
"$[(cat) < /path/to/input.txt]",
"$[< /path/to/input.txt cat]",
"![< /path/to/input.txt]",
"![< /path/to/input.txt > /path/to/output.txt]",
],
)
2017-02-25 16:45:33 -05:00
def test_redirect_abspath(case):
assert check_xonsh_ast({}, case, False)
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("case", ["", "o", "out", "1"])
2016-07-01 17:50:01 +03:00
def test_redirect_output(case):
assert check_xonsh_ast({}, '$[echo "test" {}> test.txt]'.format(case), False)
2018-08-30 09:18:49 -05:00
assert check_xonsh_ast(
{}, '$[< input.txt echo "test" {}> test.txt]'.format(case), False
)
assert check_xonsh_ast(
{}, '$[echo "test" {}> test.txt < input.txt]'.format(case), False
)
2016-07-01 17:50:01 +03:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("case", ["e", "err", "2"])
2016-07-01 17:50:01 +03:00
def test_redirect_error(case):
assert check_xonsh_ast({}, '$[echo "test" {}> test.txt]'.format(case), False)
2018-08-30 09:18:49 -05:00
assert check_xonsh_ast(
{}, '$[< input.txt echo "test" {}> test.txt]'.format(case), False
)
assert check_xonsh_ast(
{}, '$[echo "test" {}> test.txt < input.txt]'.format(case), False
)
2016-07-01 17:50:01 +03:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("case", ["a", "all", "&"])
2016-07-01 17:50:01 +03:00
def test_redirect_all(case):
assert check_xonsh_ast({}, '$[echo "test" {}> test.txt]'.format(case), False)
2018-08-30 09:18:49 -05:00
assert check_xonsh_ast(
{}, '$[< input.txt echo "test" {}> test.txt]'.format(case), False
)
assert check_xonsh_ast(
{}, '$[echo "test" {}> test.txt < input.txt]'.format(case), False
)
@pytest.mark.parametrize(
"r",
[
"e>o",
"e>out",
"err>o",
"2>1",
"e>1",
"err>1",
"2>out",
"2>o",
"err>&1",
"e>&1",
"2>&1",
],
)
@pytest.mark.parametrize("o", ["", "o", "out", "1"])
2016-07-01 17:50:01 +03:00
def test_redirect_error_to_output(r, o):
assert check_xonsh_ast({}, '$[echo "test" {} {}> test.txt]'.format(r, o), False)
2018-08-30 09:18:49 -05:00
assert check_xonsh_ast(
{}, '$[< input.txt echo "test" {} {}> test.txt]'.format(r, o), False
)
assert check_xonsh_ast(
{}, '$[echo "test" {} {}> test.txt < input.txt]'.format(r, o), False
)
@pytest.mark.parametrize(
"r",
[
"o>e",
"o>err",
"out>e",
"1>2",
"o>2",
"out>2",
"1>err",
"1>e",
"out>&2",
"o>&2",
"1>&2",
],
)
@pytest.mark.parametrize("e", ["e", "err", "2"])
2017-01-11 23:29:28 -05:00
def test_redirect_output_to_error(r, e):
assert check_xonsh_ast({}, '$[echo "test" {} {}> test.txt]'.format(r, e), False)
2018-08-30 09:18:49 -05:00
assert check_xonsh_ast(
{}, '$[< input.txt echo "test" {} {}> test.txt]'.format(r, e), False
)
assert check_xonsh_ast(
{}, '$[echo "test" {} {}> test.txt < input.txt]'.format(r, e), False
)
2017-01-11 23:29:28 -05:00
2016-08-14 18:52:39 -04:00
def test_macro_call_empty():
2018-08-30 09:18:49 -05:00
assert check_xonsh_ast({}, "f!()", False)
2016-08-20 07:46:03 -04:00
2016-08-20 13:08:56 -04:00
MACRO_ARGS = [
2018-08-30 09:18:49 -05:00
"x",
"True",
"None",
"import os",
"x=10",
'"oh no, mom"',
"...",
" ... ",
"if True:\n pass",
"{x: y}",
"{x: y, 42: 5}",
"{1, 2, 3,}",
"(x,y)",
"(x, y)",
"((x, y), z)",
"g()",
"range(10)",
"range(1, 10, 2)",
"()",
"{}",
"[]",
"[1, 2]",
"@(x)",
"!(ls -l)",
"![ls -l]",
"$(ls -l)",
"${x + y}",
"$[ls -l]",
"@$(which xonsh)",
2016-08-20 13:08:56 -04:00
]
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("s", MACRO_ARGS)
2016-08-20 13:11:48 -04:00
def test_macro_call_one_arg(s):
2018-08-30 09:18:49 -05:00
f = "f!({})".format(s)
2016-08-20 13:11:48 -04:00
tree = check_xonsh_ast({}, f, False, return_obs=True)
assert isinstance(tree, AST)
args = tree.body.args[1].elts
assert len(args) == 1
assert args[0].s == s.strip()
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("s,t", itertools.product(MACRO_ARGS[::2], MACRO_ARGS[1::2]))
2016-08-20 13:08:56 -04:00
def test_macro_call_two_args(s, t):
2018-08-30 09:18:49 -05:00
f = "f!({}, {})".format(s, t)
2016-08-20 13:08:56 -04:00
tree = check_xonsh_ast({}, f, False, return_obs=True)
assert isinstance(tree, AST)
args = tree.body.args[1].elts
assert len(args) == 2
assert args[0].s == s.strip()
assert args[1].s == t.strip()
2016-08-20 13:11:48 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize(
"s,t,u", itertools.product(MACRO_ARGS[::3], MACRO_ARGS[1::3], MACRO_ARGS[2::3])
)
2016-08-20 13:08:56 -04:00
def test_macro_call_three_args(s, t, u):
2018-08-30 09:18:49 -05:00
f = "f!({}, {}, {})".format(s, t, u)
2016-08-20 13:08:56 -04:00
tree = check_xonsh_ast({}, f, False, return_obs=True)
assert isinstance(tree, AST)
args = tree.body.args[1].elts
assert len(args) == 3
assert args[0].s == s.strip()
assert args[1].s == t.strip()
assert args[2].s == u.strip()
2016-08-20 18:48:55 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("s", MACRO_ARGS)
2016-08-20 18:48:55 -04:00
def test_macro_call_one_trailing(s):
2018-08-30 09:18:49 -05:00
f = "f!({0},)".format(s)
2016-08-20 18:48:55 -04:00
tree = check_xonsh_ast({}, f, False, return_obs=True)
assert isinstance(tree, AST)
args = tree.body.args[1].elts
assert len(args) == 1
assert args[0].s == s.strip()
2016-08-20 19:00:41 -04:00
2016-08-27 11:40:52 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("s", MACRO_ARGS)
2016-08-20 19:00:41 -04:00
def test_macro_call_one_trailing_space(s):
2018-08-30 09:18:49 -05:00
f = "f!( {0}, )".format(s)
2016-08-20 19:00:41 -04:00
tree = check_xonsh_ast({}, f, False, return_obs=True)
assert isinstance(tree, AST)
args = tree.body.args[1].elts
assert len(args) == 1
assert args[0].s == s.strip()
2016-08-27 16:46:32 -04:00
2018-08-30 09:18:49 -05:00
SUBPROC_MACRO_OC = [("!(", ")"), ("$(", ")"), ("![", "]"), ("$[", "]")]
2016-08-27 17:28:45 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("opener, closer", SUBPROC_MACRO_OC)
@pytest.mark.parametrize("body", ["echo!", "echo !", "echo ! "])
2016-08-27 17:38:57 -04:00
def test_empty_subprocbang(opener, closer, body):
tree = check_xonsh_ast({}, opener + body + closer, False, return_obs=True)
assert isinstance(tree, AST)
cmd = tree.body.args[0].elts
assert len(cmd) == 2
2018-08-30 09:18:49 -05:00
assert cmd[1].s == ""
2016-08-27 16:46:32 -04:00
2016-08-27 17:28:45 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("opener, closer", SUBPROC_MACRO_OC)
@pytest.mark.parametrize("body", ["echo!x", "echo !x", "echo !x", "echo ! x"])
2016-08-27 17:38:57 -04:00
def test_single_subprocbang(opener, closer, body):
tree = check_xonsh_ast({}, opener + body + closer, False, return_obs=True)
assert isinstance(tree, AST)
cmd = tree.body.args[0].elts
assert len(cmd) == 2
2018-08-30 09:18:49 -05:00
assert cmd[1].s == "x"
2016-08-27 17:28:45 -04:00
2016-08-27 18:12:09 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("opener, closer", SUBPROC_MACRO_OC)
@pytest.mark.parametrize(
"body", ["echo -n!x", "echo -n!x", "echo -n !x", "echo -n ! x"]
)
2016-08-27 18:12:09 -04:00
def test_arg_single_subprocbang(opener, closer, body):
tree = check_xonsh_ast({}, opener + body + closer, False, return_obs=True)
assert isinstance(tree, AST)
cmd = tree.body.args[0].elts
assert len(cmd) == 3
2018-08-30 09:18:49 -05:00
assert cmd[2].s == "x"
2016-08-27 18:12:09 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("opener, closer", SUBPROC_MACRO_OC)
@pytest.mark.parametrize("ipener, iloser", [("$(", ")"), ("@$(", ")"), ("$[", "]")])
@pytest.mark.parametrize(
"body", ["echo -n!x", "echo -n!x", "echo -n !x", "echo -n ! x"]
)
2016-08-30 21:04:03 -04:00
def test_arg_single_subprocbang_nested(opener, closer, ipener, iloser, body):
2018-08-30 09:18:49 -05:00
code = opener + "echo " + ipener + body + iloser + closer
2016-08-30 21:04:03 -04:00
tree = check_xonsh_ast({}, opener + body + closer, False, return_obs=True)
assert isinstance(tree, AST)
cmd = tree.body.args[0].elts
assert len(cmd) == 3
2018-08-30 09:18:49 -05:00
assert cmd[2].s == "x"
@pytest.mark.parametrize("opener, closer", SUBPROC_MACRO_OC)
@pytest.mark.parametrize(
"body",
[
"echo!x + y",
"echo !x + y",
"echo !x + y",
"echo ! x + y",
"timeit! bang! and more",
"timeit! recurse() and more",
"timeit! recurse[] and more",
"timeit! recurse!() and more",
"timeit! recurse![] and more",
"timeit! recurse$() and more",
"timeit! recurse$[] and more",
"timeit! recurse!() and more",
"timeit!!!!",
"timeit! (!)",
"timeit! [!]",
"timeit!!(ls)",
'timeit!"!)"',
],
)
2016-08-27 18:12:09 -04:00
def test_many_subprocbang(opener, closer, body):
2016-08-28 15:13:03 -04:00
tree = check_xonsh_ast({}, opener + body + closer, False, return_obs=True)
2016-08-27 18:12:09 -04:00
assert isinstance(tree, AST)
cmd = tree.body.args[0].elts
assert len(cmd) == 2
2018-08-30 09:18:49 -05:00
assert cmd[1].s == body.partition("!")[-1].strip()
2016-08-27 18:12:09 -04:00
2016-08-28 15:13:03 -04:00
2016-08-28 15:30:44 -04:00
WITH_BANG_RAWSUITES = [
2018-08-30 09:18:49 -05:00
"pass\n",
"x = 42\ny = 12\n",
2016-08-28 15:30:44 -04:00
'export PATH="yo:momma"\necho $PATH\n',
2018-08-30 09:18:49 -05:00
("with q as t:\n" " v = 10\n" "\n"),
(
"with q as t:\n"
" v = 10\n"
"\n"
"for x in range(6):\n"
" if True:\n"
" pass\n"
" else:\n"
" ls -l\n"
"\n"
"a = 42\n"
),
]
@pytest.mark.parametrize("body", WITH_BANG_RAWSUITES)
2016-08-28 15:13:03 -04:00
def test_withbang_single_suite(body):
2018-08-30 09:18:49 -05:00
code = "with! x:\n{}".format(textwrap.indent(body, " "))
tree = check_xonsh_ast({}, code, False, return_obs=True, mode="exec")
2016-08-28 15:30:44 -04:00
assert isinstance(tree, AST)
wither = tree.body[0]
assert isinstance(wither, With)
assert len(wither.body) == 1
assert isinstance(wither.body[0], Pass)
assert len(wither.items) == 1
item = wither.items[0]
s = item.context_expr.args[1].s
assert s == body
2016-08-29 01:10:57 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("body", WITH_BANG_RAWSUITES)
2016-08-28 15:30:44 -04:00
def test_withbang_as_single_suite(body):
2018-08-30 09:18:49 -05:00
code = "with! x as y:\n{}".format(textwrap.indent(body, " "))
tree = check_xonsh_ast({}, code, False, return_obs=True, mode="exec")
2016-08-28 15:13:03 -04:00
assert isinstance(tree, AST)
wither = tree.body[0]
assert isinstance(wither, With)
assert len(wither.body) == 1
assert isinstance(wither.body[0], Pass)
assert len(wither.items) == 1
item = wither.items[0]
2018-08-30 09:18:49 -05:00
assert item.optional_vars.id == "y"
2016-08-28 15:13:03 -04:00
s = item.context_expr.args[1].s
assert s == body
2016-08-28 16:06:01 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("body", WITH_BANG_RAWSUITES)
2016-08-29 01:10:57 -04:00
def test_withbang_single_suite_trailing(body):
2018-08-30 09:18:49 -05:00
code = "with! x:\n{}\nprint(x)\n".format(textwrap.indent(body, " "))
tree = check_xonsh_ast(
{},
code,
False,
return_obs=True,
mode="exec",
# debug_level=100
)
2016-08-29 01:10:57 -04:00
assert isinstance(tree, AST)
wither = tree.body[0]
assert isinstance(wither, With)
assert len(wither.body) == 1
assert isinstance(wither.body[0], Pass)
assert len(wither.items) == 1
item = wither.items[0]
s = item.context_expr.args[1].s
2018-08-30 09:18:49 -05:00
assert s == body + "\n"
2016-08-29 01:10:57 -04:00
2016-08-28 16:06:01 -04:00
WITH_BANG_RAWSIMPLE = [
2018-08-30 09:18:49 -05:00
"pass",
"x = 42; y = 12",
2016-08-28 16:06:01 -04:00
'export PATH="yo:momma"; echo $PATH',
2018-08-30 09:18:49 -05:00
"[1,\n 2,\n 3]",
]
2016-08-28 16:06:01 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("body", WITH_BANG_RAWSIMPLE)
2016-08-28 16:06:01 -04:00
def test_withbang_single_simple(body):
2018-08-30 09:18:49 -05:00
code = "with! x: {}\n".format(body)
tree = check_xonsh_ast({}, code, False, return_obs=True, mode="exec")
2016-08-28 16:06:01 -04:00
assert isinstance(tree, AST)
wither = tree.body[0]
assert isinstance(wither, With)
assert len(wither.body) == 1
assert isinstance(wither.body[0], Pass)
assert len(wither.items) == 1
item = wither.items[0]
s = item.context_expr.args[1].s
assert s == body
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("body", WITH_BANG_RAWSIMPLE)
def test_withbang_single_simple_opt(body):
2018-08-30 09:18:49 -05:00
code = "with! x as y: {}\n".format(body)
tree = check_xonsh_ast({}, code, False, return_obs=True, mode="exec")
2016-08-28 16:06:01 -04:00
assert isinstance(tree, AST)
wither = tree.body[0]
assert isinstance(wither, With)
assert len(wither.body) == 1
assert isinstance(wither.body[0], Pass)
assert len(wither.items) == 1
item = wither.items[0]
2018-08-30 09:18:49 -05:00
assert item.optional_vars.id == "y"
2016-08-28 16:06:01 -04:00
s = item.context_expr.args[1].s
assert s == body
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("body", WITH_BANG_RAWSUITES)
2016-08-28 16:17:51 -04:00
def test_withbang_as_many_suite(body):
2018-08-30 09:18:49 -05:00
code = "with! x as a, y as b, z as c:\n{}"
code = code.format(textwrap.indent(body, " "))
tree = check_xonsh_ast({}, code, False, return_obs=True, mode="exec")
2016-08-28 16:17:51 -04:00
assert isinstance(tree, AST)
wither = tree.body[0]
assert isinstance(wither, With)
assert len(wither.body) == 1
assert isinstance(wither.body[0], Pass)
assert len(wither.items) == 3
2018-08-30 09:18:49 -05:00
for i, targ in enumerate("abc"):
2016-08-28 16:17:51 -04:00
item = wither.items[i]
assert item.optional_vars.id == targ
s = item.context_expr.args[1].s
assert s == body
def test_subproc_raw_str_literal():
tree = check_xonsh_ast({}, "!(echo '$foo')", run=False, return_obs=True)
assert isinstance(tree, AST)
subproc = tree.body
assert isinstance(subproc.args[0].elts[1], Call)
assert subproc.args[0].elts[1].func.attr == "expand_path"
tree = check_xonsh_ast({}, "!(echo r'$foo')", run=False, return_obs=True)
assert isinstance(tree, AST)
subproc = tree.body
assert isinstance(subproc.args[0].elts[1], Str)
assert subproc.args[0].elts[1].s == "$foo"
2016-08-19 15:43:26 -04:00
# test invalid expressions
2018-08-30 09:18:49 -05:00
2016-08-19 15:43:26 -04:00
def test_syntax_error_del_literal():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("del 7")
2016-08-19 15:43:26 -04:00
def test_syntax_error_del_constant():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("del True")
2016-08-19 15:43:26 -04:00
def test_syntax_error_del_emptytuple():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("del ()")
2016-08-19 15:43:26 -04:00
def test_syntax_error_del_call():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("del foo()")
2016-08-19 15:43:26 -04:00
def test_syntax_error_del_lambda():
with pytest.raises(SyntaxError):
PARSER.parse('del lambda x: "yay"')
2018-08-30 09:18:49 -05:00
2016-08-19 15:43:26 -04:00
def test_syntax_error_del_ifexp():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("del x if y else z")
@pytest.mark.parametrize(
"exp",
[
"[i for i in foo]",
"{i for i in foo}",
"(i for i in foo)",
"{k:v for k,v in d.items()}",
],
)
2016-08-19 16:07:08 -04:00
def test_syntax_error_del_comps(exp):
2016-08-19 15:43:26 -04:00
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("del {}".format(exp))
2016-08-19 16:07:08 -04:00
2016-08-19 15:43:26 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("exp", ["x + y", "x and y", "-x"])
2016-08-19 16:07:08 -04:00
def test_syntax_error_del_ops(exp):
2016-08-19 15:43:26 -04:00
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("del {}".format(exp))
2016-08-19 16:07:08 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("exp", ["x > y", "x > y == z"])
2016-08-19 16:07:08 -04:00
def test_syntax_error_del_cmp(exp):
2016-08-19 15:43:26 -04:00
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("del {}".format(exp))
2016-08-19 15:43:26 -04:00
def test_syntax_error_lonely_del():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("del")
2016-08-19 15:43:26 -04:00
def test_syntax_error_assign_literal():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("7 = x")
2016-08-19 15:43:26 -04:00
def test_syntax_error_assign_constant():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("True = 8")
2016-08-19 15:43:26 -04:00
def test_syntax_error_assign_emptytuple():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("() = x")
2016-08-19 15:43:26 -04:00
def test_syntax_error_assign_call():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("foo() = x")
2016-08-19 15:43:26 -04:00
def test_syntax_error_assign_lambda():
with pytest.raises(SyntaxError):
PARSER.parse('lambda x: "yay" = y')
2018-08-30 09:18:49 -05:00
2016-08-19 15:43:26 -04:00
def test_syntax_error_assign_ifexp():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("x if y else z = 8")
@pytest.mark.parametrize(
"exp",
[
"[i for i in foo]",
"{i for i in foo}",
"(i for i in foo)",
"{k:v for k,v in d.items()}",
],
)
2016-08-19 16:07:08 -04:00
def test_syntax_error_assign_comps(exp):
2016-08-19 15:43:26 -04:00
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("{} = z".format(exp))
2016-08-19 16:07:08 -04:00
2016-08-19 15:43:26 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("exp", ["x + y", "x and y", "-x"])
2016-08-19 16:07:08 -04:00
def test_syntax_error_assign_ops(exp):
2016-08-19 15:43:26 -04:00
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("{} = z".format(exp))
2016-08-19 16:07:08 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("exp", ["x > y", "x > y == z"])
2016-08-19 16:07:08 -04:00
def test_syntax_error_assign_cmp(exp):
2016-08-19 15:43:26 -04:00
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("{} = a".format(exp))
2016-08-19 16:07:08 -04:00
2016-08-19 15:43:26 -04:00
def test_syntax_error_augassign_literal():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("7 += x")
2016-08-19 15:43:26 -04:00
def test_syntax_error_augassign_constant():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("True += 8")
2016-08-19 15:43:26 -04:00
def test_syntax_error_augassign_emptytuple():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("() += x")
2016-08-19 15:43:26 -04:00
def test_syntax_error_augassign_call():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("foo() += x")
2016-08-19 15:43:26 -04:00
def test_syntax_error_augassign_lambda():
with pytest.raises(SyntaxError):
PARSER.parse('lambda x: "yay" += y')
2018-08-30 09:18:49 -05:00
2016-08-19 15:43:26 -04:00
def test_syntax_error_augassign_ifexp():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("x if y else z += 8")
@pytest.mark.parametrize(
"exp",
[
"[i for i in foo]",
"{i for i in foo}",
"(i for i in foo)",
"{k:v for k,v in d.items()}",
],
)
2016-08-19 16:07:08 -04:00
def test_syntax_error_augassign_comps(exp):
2016-08-19 15:43:26 -04:00
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("{} += z".format(exp))
2016-08-19 15:43:26 -04:00
2016-08-19 16:07:08 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("exp", ["x + y", "x and y", "-x"])
2016-08-19 16:07:08 -04:00
def test_syntax_error_augassign_ops(exp):
2016-08-19 15:43:26 -04:00
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("{} += z".format(exp))
2016-08-19 16:07:08 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("exp", ["x > y", "x > y +=+= z"])
2016-08-19 16:07:08 -04:00
def test_syntax_error_augassign_cmp(exp):
2016-08-19 15:43:26 -04:00
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("{} += a".format(exp))
2017-12-06 22:12:50 -05:00
def test_syntax_error_bar_kwonlyargs():
with pytest.raises(SyntaxError):
2018-08-30 09:18:49 -05:00
PARSER.parse("def spam(*):\n pass\n", mode="exec")