-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_parser.py
58 lines (50 loc) · 2.3 KB
/
test_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pytest
from prompt_server.custom_parser import function_parser, _get_indentation_type, _extract_function_header
import ast
@pytest.mark.parametrize(
"input_function, expected_leading",
[
("def hello():\n\tprint('hello')", ""),
("def hello():\n print('hello')", ""),
("\tdef hello():\n\t\tprint('hello')", "\t"),
(" def hello():\n print('hello')", " "),
]
)
def test_different_indentations(input_function, expected_leading):
(header, body, leading, indentation_type) = function_parser(input_function)
assert header == 'def hello():'
assert body == expected_leading + indentation_type + "print('hello')"
assert leading == expected_leading
@pytest.mark.parametrize(
"input_function, expected_type",
[
("def hello():\n\tprint('hello')", "\t"),
("def hello():\n print('hello')", " "),
("\tdef hello():\n\t\tprint('hello')", "\t\t"),
(" def hello():\n print('hello')", " "),
]
)
def test_get_indentation_type(input_function, expected_type):
assert _get_indentation_type(input_function) == expected_type
normal_string = """def different_indentations(input_function, expected_leading):
header, body, leading, indentation_type = function_parser(input_function)
assert header == "def hello():"
assert body == expected_leading + indentation_type + "print('hello')"
assert leading == expected_leading
"""
def test_extract_header():
parsed_body = ast.parse(normal_string).body[0]
unparsed_body = ast.unparse(parsed_body)
print("Unparsed body: ", unparsed_body)
assert _extract_function_header(parsed_body) == "def different_indentations(input_function, expected_leading):"
test_string = """def test_different_indentations(input_function, expected_leading):
header, body, leading, indentation_type = function_parser(input_function)
assert header == "def hello():"
assert body == expected_leading + indentation_type + "print('hello')"
assert leading == expected_leading
"""
def test_extract_header():
parsed_body = ast.parse(test_string).body[0]
unparsed_body = ast.unparse(parsed_body)
print("Unparsed body: ", unparsed_body)
assert _extract_function_header(parsed_body) == "def test_different_indentations(input_function, expected_leading):"