예제 #1
0
    def test_1_2_float_token_number(self):
        token_input = "begin 2.0; end"
        ast_output = """{"P_expression_number" :["float",2.0]}"""

        with captured_output() as (out, err):
            x, _, _ = calc.parse(token_input)
            self.assertEqual(repr(x.block.blocks.block.casual_statement.operation.expression), ast_output)
        
        self.assertEqual(out.getvalue(), '')
예제 #2
0
    def test_2_3_unary_operations(self):
        token_suite = {
            "begin exp(2); end" : "exp",
            "begin log(2); end" : "log",
            "begin sqrt(2); end" : "sqrt"
            }

        for expected_input, expected_output in token_suite.items():
            with captured_output() as (out, err):
                x, _, _ = calc.parse(expected_input)
                self.assertEqual(x.block.blocks.block.casual_statement.operation.expression.op, expected_output)
                self.assertEqual(out.getvalue(), '')
예제 #3
0
    def test_2_3_dynamic_types(self):
        token_suite = {
            "begin float i = 2.0; print(i); end" : "float",
            "begin int i = 2; print(i); end" : "int",
            "begin boolean i = True; print(i); end" : "boolean",
            "begin boolean i = False; print(i); end" : "boolean"
            }

        for expected_input, expected_output in token_suite.items():
            with captured_output() as (out, err):
                x, _, _ = calc.parse(expected_input)
                self.assertEqual(x.block.blocks.block.casual_statement.operation.typeo, expected_output)
예제 #4
0
    def test_3_1_power(self):
        token_suite = {
            "begin 2 ** 4; end" : "**",
            "begin 3**3; end" : "**",
            "begin 2 **3; end" : "**",
            "begin 2** 3; end" : "**"
            }

        for expected_input, expected_output in token_suite.items():
            with captured_output() as (out, err):
                x, _, _ = calc.parse(expected_input)
                self.assertEqual(x.block.blocks.block.casual_statement.operation.expression.op, expected_output)
                self.assertEqual(out.getvalue(), '')
예제 #5
0
    def test_2_2_cos(self):
        token_suite = {
            "begin cos(2); end" : "cos",
            "begin cos2; end" : "cos",
            "begin cos(2.0); end" : "cos",
            "begin cos 2; end" : "cos"
            }

        for expected_input, expected_output in token_suite.items():
            with captured_output() as (out, err):
                x, _, _ = calc.parse(expected_input)
                self.assertEqual(x.block.blocks.block.casual_statement.operation.expression.op, expected_output)
                self.assertEqual(out.getvalue(), '')
예제 #6
0
    def test_4_2_assign_statement(self):
        token_type_suite = {
            "begin int i = 20; i = 21; print(i); end" : "int",
            "begin int i = 2; i = 3; end" : "int",
            "begin int abecadlo = 220; abecadlo = 221; end" : "int",
            }
            
        values_suite = {
            "begin int i = 2; i = 3; print(i); end" : "3\n",
            "begin float i = 2.0; i = 3.0; print(i); end" : "3.0\n",
            "begin boolean i = True; i = False; print(i); end" : "False\n",
            }

        for expected_input, expected_output in token_type_suite.items():
            with captured_output() as (out, err):
                x, _, _ = calc.parse(expected_input)
                self.assertEqual(x.block.blocks.block.casual_statement.operation.typeo, expected_output)

        for expected_input, expected_output in values_suite.items():
            with captured_output() as (out, err):
                x, _, _ = calc.parse(expected_input)
                self.assertEqual(out.getvalue(), expected_output)
예제 #7
0
    def test_2_2_float_types(self):
        token_suite = {
            "begin float i = 2.0; print(i); end" : "float",
            "begin float i =2.0; print(i); end" : "float",
            "begin float i=2.0; print(i); end" : "float",
            "begin float  i=2.0; print(i); end" : "float",
            "begin float i= 2.0; print(i); end" : "float"
            }

        for expected_input, expected_output in token_suite.items():
            with captured_output() as (out, err):
                x, _, _ = calc.parse(expected_input)
                self.assertEqual(x.block.blocks.block.casual_statement.operation.typeo, expected_output)
                self.assertEqual(out.getvalue(), '2.0\n')
예제 #8
0
    def test_3_3_binary_operations(self):
        token_suite = {
            "begin 2 - 4; end" : "-",
            "begin 2- 4; end" : "-",
            "begin 2 + -4; end" : "+",
            "begin 2 * 4; end" : "*",
            "begin 2* 4; end" : "*",
            "begin 2 *4; end" : "*",
            "begin 2 / 4; end" : "/",
            "begin 2/ 4; end" : "/",
            "begin 2 /4; end" : "/",
            }

        for expected_input, expected_output in token_suite.items():
            with captured_output() as (out, err):
                x, _, _ = calc.parse(expected_input)
                self.assertEqual(x.block.blocks.block.casual_statement.operation.expression.op, expected_output)
                self.assertEqual(out.getvalue(), '')
예제 #9
0
파일: test_parse.py 프로젝트: minamorl/calc
def test_parse2():
    expr = "1+2+3+4+5"
    tokens = Tokenizer().tokenize(expr)

    assert (((((0, 1), 2), 3), 4), 5) == parse(None, tokens)
예제 #10
0
 def test_3_1_save_the_evaluation_for_later(self):
     with captured_output() as (out, err):
         x, _, expressions = calc.parse("begin 2 + 3; end")
         self.assertTrue(repr(ast.P_expression_binop(_, ast.P_expression_number(_, 'int', 2), '+', ast.P_expression_number(_, 'int', 3))) in expressions.keys())
예제 #11
0
파일: test_parse.py 프로젝트: minamorl/calc
def test_parse5():
    expr = "3+2-(-(15+3))"
    tokens = Tokenizer().tokenize(expr)
    print(parse(None, tokens))

    assert calc(parse(None, tokens)) == 23
예제 #12
0
    def test_1_2_ast_complex_addition(self):
        token_input = "begin int i = 2; define add(int a) = a + 5; add(i); end"
        ast_repr = ('' +
            '{"P_start" :[' +
                '{"P_block" :[' +
                    '{"P_blocks" :[' +
                        '{"P_block" :[' +
                            '"None",' +
                            '{"P_casual_statements" :[' +
                                '{"P_statement_assign" :[' +
                                    '"int",' +
                                    '"i",' +
                                    '{"P_statement_expr" :[' +
                                        '{"P_expression_number" :[' +
                                            '"int",' +
                                            '2]}' +
                                        ']}' +
                                    ']}' +
                                ']}' +
                            ']},' +
                            '{"P_blocks" :[' +
                                '{"P_block" :[' +
                                    '"None",' +
                                    '{"P_casual_statements" :[' +
                                        '{"P_define" :[' +
                                            '"add",' +
                                            '{"P_params" :[' +
                                                '"int",' +
                                                '"a",' +
                                                '"None"' +
                                            ']},' +
                                            '{"P_statement_expr" :[' +
                                                '{"P_expression_binop" :[' +
                                                    '{"P_expression_name" :[' +
                                                        '"a"' +
                                                    ']},' +
                                                    '"+",' +
                                                    '{"P_expression_number" :[' +
                                                        '"int",' +
                                                        '5' +
                                                    ']}' +
                                                ']}' +
                                            ']}' +
                                        ']}' +
                                    ']}' +
                                ']},' +
                                '{"P_blocks" :[' +
                                    '{"P_block" :[' +
                                        '"None",' +
                                        '{"P_casual_statements" :[' +
                                            '{"P_statement_expr" :[' +
                                                '{"P_call_function" :[' +
                                                    '"add",' +
                                                    '{"P_statements" :[' +
                                                        '{"P_statement_expr" :[' +
                                                            '{"P_expression_name" :[' +
                                                                '"i"' +
                                                            ']}' +
                                                        ']},' +
                                                        '"None"' +
                                                    ']}' +
                                                ']}' +
                                            ']}' +
                                        ']}' +
                                    ']},' +
                                    '"None"' +
                                ']}' +
                            ']}' +
                        ']},' +
                        '"None"' +
                    ']}' +
                ']}' +
            '')

        with captured_output() as (out, err):
            x, _, _ = calc.parse(token_input)
            self.assertEqual(repr(x), ast_repr)
예제 #13
0
    def test_1_1_ast_simple_addition(self):
        token_input = "begin print(2+2); end"
        pass_function = lambda : "dummy function"
        ast_output = ast.P_start(
            # block
            ast.P_block(
                # blocks
                ast.P_blocks(
                    # block
                    ast.P_block(
                        # blocks
                        None,
                        # casual_statements
                        ast.P_casual_statements(
                            # operation
                            ast.P_print(
                                # fun
                                ast.P_statement_expr(
                                    # fun
                                    pass_function(),
                                    # expression
                                    ast.P_expression_binop(
                                        # fun
                                        pass_function(),
                                        # expr1
                                        ast.P_expression_number(
                                            # fun
                                            pass_function(),
                                            # typeo
                                            "int",
                                            # val
                                            2
                                        ),
                                        # op
                                        "+",
                                        # expr2
                                        ast.P_expression_number(
                                            # fun
                                            pass_function(),
                                            # typeo
                                            "int",
                                            # val
                                            2
                                        )
                                    )
                                )
                            )
                        )
                    ),
                    # blocks
                    None
                ),
                # casual_statements
                None
            )
        )
        ast_repr = """{"P_start" :[{"P_block" :[{"P_blocks" :[{"P_block" :["None",{"P_casual_statements" :[{"P_print"}]}]},"None"]},"None"]}]}"""

        with captured_output() as (out, err):
            x, _, _ = calc.parse(token_input)
            self.assertEqual(repr(x), repr(ast_output))
            self.assertEqual(repr(x), ast_repr)
예제 #14
0
def assertThose(testcase, text_input, expect):
    with captured_output() as (out, err):
        x, _, _ = calc.parse(text_input)
    return testcase.assertEqual(out.getvalue(), expect)
예제 #15
0
파일: test_parse.py 프로젝트: minamorl/calc
def test_parse3():
    expr = "1+2+3+4+-5"
    tokens = Tokenizer().tokenize(expr)

    assert (((((0, 1), 2), 3), 4), -5) == parse(None, tokens)
    assert calc(parse(None, tokens)) == 5
예제 #16
0
 def test_2_1_omit_declarations(self):
     with captured_output() as (out, err):
         x, used_names, _ = calc.parse("begin int thisNameShouldNotExist = 1; print(2); end")
         self.assertTrue("thisNameShouldNotExist" not in used_names)
예제 #17
0
파일: test_parse.py 프로젝트: minamorl/calc
def test_parse4():
    expr = "1+(2+3)+((-4)+-5)"
    tokens = Tokenizer().tokenize(expr)

    assert ((((0, -4), 0), -5), (((0, 2), 3), (0, 1))) == parse(None, tokens)
    assert calc(parse(None, tokens)) == -3
예제 #18
0
 def test_2_2_dont_omit(self):
     with captured_output() as (out, err):
         x, used_names, _ = calc.parse("begin int thisNameShouldExist = 1; print(thisNameShouldExist); end")
         self.assertTrue("thisNameShouldExist" in used_names)
예제 #19
0
파일: test_parse.py 프로젝트: minamorl/calc
def test_parse1():
    expr = "1"
    tokens = Tokenizer().tokenize(expr)

    assert (0, 1) == parse(None, tokens)
예제 #20
0
 def test_parse(self):
     res = calc.parse('3+4')
     self.assertEqual(res,[3,operator.add,4])