forked from pixee/codemodder-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_format_string_parser.py
More file actions
93 lines (82 loc) · 3.5 KB
/
test_format_string_parser.py
File metadata and controls
93 lines (82 loc) · 3.5 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from typing import cast
import libcst as cst
from codemodder.utils.format_string_parser import (
PrintfStringExpression,
expressions_from_replacements,
extract_mapping_key,
parse_formatted_string,
parse_formatted_string_raw,
)
class TestFormatStringParser:
def test_parse_string_raw(self):
string = "1 %s 3 %(key)d 5"
assert len(parse_formatted_string_raw(string)) == 5
def test_key_extraction(self):
dict_key = "%(key)s"
no_key = "%s"
assert extract_mapping_key(dict_key) == "key"
assert extract_mapping_key(no_key) is None
def test_parsing_multiple_parts_mix_expressions(self):
first = cst.parse_expression("'some %s'")
first = cast(cst.SimpleString, first)
second = cst.parse_expression("name")
second = cast(cst.FormattedString, second)
third = cst.parse_expression("'another %s'")
third = cast(cst.SimpleString, third)
keys = cst.parse_expression("(1,2,3,)")
keys = cast(cst.Tuple, keys)
parsed_keys = expressions_from_replacements(keys)
all_parts = [first, second, third]
parsed = parse_formatted_string(all_parts, parsed_keys)
assert parsed and len(parsed) == 5
def test_parsing_multiple_parts_values(self):
first = cst.parse_expression("'some %(name)s'")
first = cast(cst.SimpleString, first)
second = cst.parse_expression("f' and %(phone)d'")
second = cast(cst.FormattedString, second)
all_parts = [first, *second.parts]
key_dict: dict[str | cst.BaseExpression, cst.BaseExpression] = {
"name": cst.parse_expression("name"),
"phone": cst.parse_expression("phone"),
}
parsed = parse_formatted_string(all_parts, key_dict)
assert parsed is not None
values = [p.value for p in parsed]
assert values == ["some ", "%(name)s", " and ", "%(phone)d"]
def test_single_key_to_expression(self):
first = cst.parse_expression("'%d'")
first = cast(cst.SimpleString, first)
keys = cst.parse_expression("1")
parsed_keys = expressions_from_replacements(keys)
parsed = parse_formatted_string([first], parsed_keys)
assert parsed
for p in parsed:
assert isinstance(p, PrintfStringExpression)
assert isinstance(p.key, int)
assert p.expression == parsed_keys[p.key]
def test_tuple_key_to_expression(self):
first = cst.parse_expression("'%d%d%d'")
first = cast(cst.SimpleString, first)
keys = cst.parse_expression("(1,2,3,)")
keys = cast(cst.Tuple, keys)
parsed_keys = expressions_from_replacements(keys)
parsed = parse_formatted_string([first], parsed_keys)
assert parsed
for p in parsed:
assert isinstance(p, PrintfStringExpression)
assert isinstance(p.key, int)
assert p.expression == parsed_keys[p.key]
def test_dict_key_to_expression(self):
first = cst.parse_expression("'%(one)d%(two)d%(three)d'")
first = cast(cst.SimpleString, first)
keys: dict[str | cst.BaseExpression, cst.BaseExpression] = {
"one": cst.Integer("1"),
"two": cst.Integer("2"),
"three": cst.Integer("3"),
}
parsed = parse_formatted_string([first], keys)
assert parsed
for p in parsed:
assert isinstance(p, PrintfStringExpression)
assert isinstance(p.key, str)
assert p.expression == keys[p.key]