def test_parse_variable_statement_without_initialiser(self): self.assertEqual(self.parser.parse('var x, y = 5;'), ast.VariableDeclarationList( declarations=[ast.VariableDeclaration(identifier=ast.Identifier(name='x'), initialiser=None), ast.VariableDeclaration(identifier=ast.Identifier(name='y'), initialiser=ast.Literal(value=5))]))
def test_parse_function_expression(self): self.assertEqual(self.parser.parse('function (x, y) { return x + y; }'), ast.FunctionDefinition(parameters=[ast.Identifier(name='x'), ast.Identifier(name='y')], body=ast.Block( statements=[ast.ReturnStatement(expression=ast.BinaryOp(op='+', left_expression=ast.Identifier(name='x'), right_expression=ast.Identifier(name='y')))])))
def test_parse_compound_assignment(self): self.assertEqual(self.parser.parse('x /= 5 - 2'), ast.Assignment(op='/=', reference=ast.Identifier(name='x'), expression=ast.BinaryOp(op='-', left_expression=ast.Literal(value=5), right_expression=ast.Literal(value=2))))
def p_identifier(self, p): """identifier : ID""" p[0] = ast.Identifier(name=p[1])
def test_parse_prefix_op(self): self.assertEqual( self.parser.parse('++x'), ast.UnaryOp(op='++', expression=ast.Identifier(name='x')))