|
1 | | -# import pytest |
2 | | -# from parser.ast import ( |
3 | | -# Program, Identifier, Literal, Assignment, BinaryOperation, |
4 | | -# FunctionDefinition, FunctionCall |
5 | | -# ) |
| 1 | +import pytest |
| 2 | +from parser.ast import ( |
| 3 | + Program, Identifier, Literal, Assignment, BinaryOperation, |
| 4 | + FunctionDefinition, FunctionCall |
| 5 | +) |
6 | 6 |
|
7 | | -# # Test Identifier Node |
8 | | -# def test_identifier_node(): |
9 | | -# ident = Identifier("my_var") |
10 | | -# assert ident.name == "my_var" |
11 | | -# assert str(ident) == "<Identifier:my_var>" |
| 7 | +# Test Identifier Node |
| 8 | +def test_identifier_node(): |
| 9 | + ident = Identifier("my_var") |
| 10 | + assert ident.name == "my_var" |
| 11 | + assert str(ident) == "<Identifier:my_var>" |
12 | 12 |
|
13 | | -# # Test Literal Node |
14 | | -# @pytest.mark.parametrize( |
15 | | -# "value, expected_str", |
16 | | -# [ |
17 | | -# (123, "<Literal:123>"), |
18 | | -# ("hello", "<Literal:hello>"), |
19 | | -# (True, "<Literal:True>"), |
20 | | -# (None, "<Literal:None>"), |
21 | | -# ] |
22 | | -# ) |
23 | | -# def test_literal_node(value, expected_str): |
24 | | -# literal = Literal(value) |
25 | | -# assert literal.value == value |
26 | | -# assert str(literal) == expected_str |
| 13 | +# Test Literal Node |
| 14 | +@pytest.mark.parametrize( |
| 15 | + "value, expected_str", |
| 16 | + [ |
| 17 | + (123, "<Literal:123>"), |
| 18 | + ("hello", "<Literal:hello>"), |
| 19 | + (True, "<Literal:True>"), |
| 20 | + (None, "<Literal:None>"), |
| 21 | + ] |
| 22 | +) |
| 23 | +def test_literal_node(value, expected_str): |
| 24 | + literal = Literal(value) |
| 25 | + assert literal.value == value |
| 26 | + assert str(literal) == expected_str |
27 | 27 |
|
28 | | -# # Test Assignment Node |
29 | | -# def test_assignment_node(): |
30 | | -# var = Identifier("x") |
31 | | -# val = Literal(10) |
32 | | -# assign = Assignment(var, val) |
33 | | -# assert assign.variable == var |
34 | | -# assert assign.value == val |
35 | | -# assert str(assign) == "<Assignment:<Identifier:x> = <Literal:10>>" |
| 28 | +# Test Assignment Node |
| 29 | +def test_assignment_node(): |
| 30 | + var = Identifier("x") |
| 31 | + val = Literal(10) |
| 32 | + assign = Assignment(var, val) |
| 33 | + assert assign.variable == var |
| 34 | + assert assign.value == val |
| 35 | + assert str(assign) == "<Assignment:<Identifier:x> = <Literal:10>>" |
36 | 36 |
|
37 | | -# # Test BinaryOperation Node |
38 | | -# def test_binary_operation_node(): |
39 | | -# left = Identifier("a") |
40 | | -# right = Literal(5) |
41 | | -# op = BinaryOperation(left, "+", right) |
42 | | -# assert op.left == left |
43 | | -# assert op.operator == "+" |
44 | | -# assert op.right == right |
45 | | -# assert str(op) == "<BinaryOp:<Identifier:a> + <Literal:5>>" |
| 37 | +# Test BinaryOperation Node |
| 38 | +def test_binary_operation_node(): |
| 39 | + left = Identifier("a") |
| 40 | + right = Literal(5) |
| 41 | + op = BinaryOperation(left, "+", right) |
| 42 | + assert op.left == left |
| 43 | + assert op.operator == "+" |
| 44 | + assert op.right == right |
| 45 | + assert str(op) == "<BinaryOp:<Identifier:a> + <Literal:5>>" |
46 | 46 |
|
47 | | -# # Test FunctionDefinition Node |
48 | | -# def test_function_definition_node(): |
49 | | -# name = Identifier("my_func") |
50 | | -# params = [Identifier("p1"), Identifier("p2")] |
51 | | -# body = [ |
52 | | -# Assignment(Identifier("local_var"), Literal(1)), |
53 | | -# BinaryOperation(Identifier("p1"), "+", Identifier("p2")) |
54 | | -# ] |
55 | | -# func_def = FunctionDefinition(name, params, body) |
56 | | -# assert func_def.name == name |
57 | | -# assert func_def.parameters == params |
58 | | -# assert func_def.body == body |
59 | | -# expected_str = ( |
60 | | -# "<FunctionDef:<Identifier:my_func>(<Identifier:p1>, <Identifier:p2>)>\n" |
61 | | -# " <Assignment:<Identifier:local_var> = <Literal:1>>\n" |
62 | | -# " <BinaryOp:<Identifier:p1> + <Identifier:p2>>" |
63 | | -# ) |
64 | | -# assert str(func_def) == expected_str |
| 47 | +# Test FunctionDefinition Node |
| 48 | +def test_function_definition_node(): |
| 49 | + name = Identifier("my_func") |
| 50 | + params = [Identifier("p1"), Identifier("p2")] |
| 51 | + body = [ |
| 52 | + Assignment(Identifier("local_var"), Literal(1)), |
| 53 | + BinaryOperation(Identifier("p1"), "+", Identifier("p2")) |
| 54 | + ] |
| 55 | + func_def = FunctionDefinition(name, params, body) |
| 56 | + assert func_def.name == name |
| 57 | + assert func_def.parameters == params |
| 58 | + assert func_def.body == body |
| 59 | + expected_str = ( |
| 60 | + "<FunctionDef:<Identifier:my_func>(<Identifier:p1>, <Identifier:p2>)>\n" |
| 61 | + " <Assignment:<Identifier:local_var> = <Literal:1>>\n" |
| 62 | + " <BinaryOp:<Identifier:p1> + <Identifier:p2>>" |
| 63 | + ) |
| 64 | + assert str(func_def) == expected_str |
65 | 65 |
|
66 | | -# def test_function_definition_no_params_no_body(): |
67 | | -# name = Identifier("empty_func") |
68 | | -# func_def = FunctionDefinition(name, None, None) |
69 | | -# assert func_def.name == name |
70 | | -# assert func_def.parameters == [] |
71 | | -# assert func_def.body == [] |
72 | | -# assert str(func_def) == "<FunctionDef:<Identifier:empty_func>()>\n" |
| 66 | +def test_function_definition_no_params_no_body(): |
| 67 | + name = Identifier("empty_func") |
| 68 | + func_def = FunctionDefinition(name, None, None) |
| 69 | + assert func_def.name == name |
| 70 | + assert func_def.parameters == [] |
| 71 | + assert func_def.body == [] |
| 72 | + assert str(func_def) == "<FunctionDef:<Identifier:empty_func>()>\n" |
73 | 73 |
|
74 | | -# # Test FunctionCall Node |
75 | | -# def test_function_call_node(): |
76 | | -# func = Identifier("call_me") |
77 | | -# args = [Literal(10), Identifier("arg2")] |
78 | | -# func_call = FunctionCall(func, args) |
79 | | -# assert func_call.function == func |
80 | | -# assert func_call.arguments == args |
81 | | -# assert str(func_call) == "<FunctionCall:<Identifier:call_me>(<Literal:10>, <Identifier:arg2>)>" |
| 74 | +# Test FunctionCall Node |
| 75 | +def test_function_call_node(): |
| 76 | + func = Identifier("call_me") |
| 77 | + args = [Literal(10), Identifier("arg2")] |
| 78 | + func_call = FunctionCall(func, args) |
| 79 | + assert func_call.function == func |
| 80 | + assert func_call.arguments == args |
| 81 | + assert str(func_call) == "<FunctionCall:<Identifier:call_me>(<Literal:10>, <Identifier:arg2>)>" |
82 | 82 |
|
83 | | -# def test_function_call_no_args(): |
84 | | -# func = Identifier("no_args_call") |
85 | | -# func_call = FunctionCall(func, None) |
86 | | -# assert func_call.function == func |
87 | | -# assert func_call.arguments == [] |
88 | | -# assert str(func_call) == "<FunctionCall:<Identifier:no_args_call>()>" |
| 83 | +def test_function_call_no_args(): |
| 84 | + func = Identifier("no_args_call") |
| 85 | + func_call = FunctionCall(func, None) |
| 86 | + assert func_call.function == func |
| 87 | + assert func_call.arguments == [] |
| 88 | + assert str(func_call) == "<FunctionCall:<Identifier:no_args_call>()>" |
89 | 89 |
|
90 | | -# # Test Program Node |
91 | | -# def test_program_node(): |
92 | | -# statements = [ |
93 | | -# Assignment(Identifier("a"), Literal(1)), |
94 | | -# FunctionCall(Identifier("print"), [Identifier("a")]) |
95 | | -# ] |
96 | | -# program = Program(statements) |
97 | | -# assert program.statements == statements |
98 | | -# expected_str = ( |
99 | | -# "<Program>\n" |
100 | | -# " <Assignment:<Identifier:a> = <Literal:1>>\n" |
101 | | -# " <FunctionCall:<Identifier:print>(<Identifier:a>)>" |
102 | | -# ) |
103 | | -# assert str(program) == expected_str |
| 90 | +# Test Program Node |
| 91 | +def test_program_node(): |
| 92 | + statements = [ |
| 93 | + Assignment(Identifier("a"), Literal(1)), |
| 94 | + FunctionCall(Identifier("print"), [Identifier("a")]) |
| 95 | + ] |
| 96 | + program = Program(statements) |
| 97 | + assert program.statements == statements |
| 98 | + expected_str = ( |
| 99 | + "<Program>\n" |
| 100 | + " <Assignment:<Identifier:a> = <Literal:1>>\n" |
| 101 | + " <FunctionCall:<Identifier:print>(<Identifier:a>)>" |
| 102 | + ) |
| 103 | + assert str(program) == expected_str |
104 | 104 |
|
105 | | -# def test_program_empty(): |
106 | | -# program = Program([]) |
107 | | -# assert program.statements == [] |
108 | | -# assert str(program) == "<Program>\n" |
| 105 | +def test_program_empty(): |
| 106 | + program = Program([]) |
| 107 | + assert program.statements == [] |
| 108 | + assert str(program) == "<Program>\n" |
109 | 109 |
|
110 | 110 |
|
0 commit comments