xonsh/tests/test_parser.py

212 lines
4.2 KiB
Python
Raw Normal View History

2015-01-24 12:27:31 -06:00
"""Tests the xonsh lexer."""
from __future__ import unicode_literals, print_function
import os
import sys
2015-01-24 19:17:15 -06:00
import ast
2015-01-24 12:27:31 -06:00
from collections import Sequence
from pprint import pprint, pformat
sys.path.insert(0, os.path.abspath('..')) # FIXME
import nose
from nose.tools import assert_equal
from ply.lex import LexToken
from xonsh.parser import Parser
2015-01-24 20:19:10 -06:00
PARSER = None
DEBUG_LEVEL = 0
def setup():
# only setup one parser
global PARSER
PARSER = Parser(lexer_optimize=False, yacc_optimize=False, yacc_debug=True)
2015-01-24 19:17:15 -06:00
def nodes_equal(x, y):
if type(x) != type(y):
return False
2015-01-24 20:19:10 -06:00
if isinstance(x, ast.Expr):
if x.lineno != y.lineno:
return False
if x.col_offset != y.col_offset:
return False
2015-01-24 19:17:15 -06:00
for (xname, xval), (yname, yval) in zip(ast.iter_fields(x),
ast.iter_fields(y)):
if xname != yname:
return False
if xval != yval:
return False
for xchild, ychild in zip(ast.iter_child_nodes(x),
ast.iter_child_nodes(y)):
if not nodes_equal(xchild, ychild):
return False
return True
def assert_nodes_equal(x, y):
if nodes_equal(x, y):
return True
assert_equal(ast.dump(x), ast.dump(y))
def check_ast(input):
2015-01-24 20:19:10 -06:00
# expect a Python AST
2015-01-24 19:17:15 -06:00
exp = ast.parse(input)
2015-01-24 20:19:10 -06:00
# observe something from xonsh
obs = PARSER.parse(input, debug_level=DEBUG_LEVEL)
# Check that they are equal
2015-01-24 19:17:15 -06:00
assert_nodes_equal(exp, obs)
2015-01-24 20:19:10 -06:00
# round trip by running xonsh AST via Python
exec(compile(obs, '<test>', 'exec'))
2015-01-24 12:27:31 -06:00
def test_int_literal():
2015-01-24 19:17:15 -06:00
yield check_ast, '42'
2015-01-24 12:27:31 -06:00
2015-01-24 20:19:10 -06:00
def test_float_literal():
yield check_ast, '42.0'
def test_str_literal():
yield check_ast, '"hello"'
def test_bytes_literal():
yield check_ast, 'b"hello"'
2015-01-24 20:33:21 -06:00
def test_unary_plus():
yield check_ast, '+1'
def test_unary_minus():
yield check_ast, '-1'
def test_unary_invert():
yield check_ast, '~1'
2015-01-24 20:46:05 -06:00
def test_binop_plus():
yield check_ast, '42 + 65'
def test_binop_minus():
yield check_ast, '42 - 65'
def test_binop_times():
yield check_ast, '42 * 65'
def test_binop_div():
yield check_ast, '42 / 65'
def test_binop_mod():
yield check_ast, '42 % 65'
def test_binop_floordiv():
yield check_ast, '42 // 65'
2015-01-24 21:56:22 -06:00
def test_binop_pow():
yield check_ast, '2 ** 2'
2015-01-24 22:19:57 -06:00
def test_plus_pow():
yield 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():
yield 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():
yield 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():
yield 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():
yield check_ast, '42 - 65 - 6'
def test_minus_plus_minus():
yield check_ast, '42 - 65 + 6 - 28'
def test_times_plus():
yield check_ast, '42 * 65 + 6'
def test_plus_times():
yield check_ast, '42 + 65 * 6'
def test_times_times():
yield check_ast, '42 * 65 * 6'
def test_times_div():
yield check_ast, '42 * 65 / 6'
def test_times_div_mod():
yield check_ast, '42 * 65 / 6 % 28'
def test_times_div_mod_floor():
yield check_ast, '42 * 65 / 6 % 28 // 13'
2015-01-24 22:27:36 -06:00
def test_str_str():
yield check_ast, '"hello" \'mom\''
def test_str_plus_str():
yield check_ast, '"hello" + \'mom\''
def test_str_times_int():
yield check_ast, '"hello" * 20'
def test_int_times_str():
yield check_ast, '2*"hello"'
2015-01-24 22:19:57 -06:00
2015-01-24 22:43:01 -06:00
def test_group_plus_times():
yield check_ast, '(42 + 65) * 20'
def test_plus_group_times():
yield check_ast, '42 + (65 * 20)'
def test_group():
yield check_ast, '(42)'
2015-01-24 23:02:15 -06:00
def test_lt():
yield check_ast, '42 < 65'
def test_lt():
yield check_ast, '42 < 65'
def test_gt():
yield check_ast, '42 > 65'
def test_eq():
yield check_ast, '42 == 65'
def test_lt():
yield check_ast, '42 < 65'
def test_le():
yield check_ast, '42 <= 65'
def test_ge():
yield check_ast, '42 >= 65'
def test_ne():
yield check_ast, '42 != 65'
def test_in():
yield check_ast, '"4" in "65"'
def test_is():
yield check_ast, '42 is 65'
def test_not_in():
yield check_ast, '"4" not in "65"'
def test_is_not():
yield check_ast, '42 is not 65'
def test_lt_lt():
yield check_ast, '42 < 65 < 105'
def test_lt_lt_lt():
yield check_ast, '42 < 65 < 105 < 77'
2015-01-24 22:19:57 -06:00
2015-01-24 12:27:31 -06:00
if __name__ == '__main__':
2015-01-24 22:19:57 -06:00
nose.runmodule()