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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# Copyright 2024 Huawei Cloud Computing Technology Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from runner import (
compute_value,
get_tokens,
handle01,
replace_value,
undefine,
)
from hypothesis import given
from hypothesis.strategies import text
import re
def test_get_tokens():
magic_string = "cmakedefine"
match, is_01 = get_tokens(
f"# \t \t {magic_string} \t TOKEN_KEY \t\t TOKEN_VALUE \t\n", magic_string
)
assert match
assert not is_01
groups = match.groups()
assert len(groups) == 6
assert groups[1] == magic_string
assert groups[3] == "TOKEN_KEY"
assert groups[5] == "TOKEN_VALUE \t"
match, _ = get_tokens("#cmakedefine TOKEN_KEY TOKEN_VALUE", magic_string)
assert match
groups = match.groups()
assert groups[1] == magic_string
assert groups[3] == "TOKEN_KEY"
assert groups[5] == "TOKEN_VALUE"
match, _ = get_tokens("#cmakedefine TOKEN_KEY @TOKEN_VALUE@", magic_string)
assert match
groups = match.groups()
assert len(groups) == 6
assert groups[1] == magic_string
assert groups[3] == "TOKEN_KEY"
assert groups[5] == "@TOKEN_VALUE@"
match, _ = get_tokens("#cmakedefine TOKEN_KEY ${TOKEN_VALUE}", magic_string)
assert match
groups = match.groups()
assert len(groups) == 6
assert groups[1] == magic_string
assert groups[3] == "TOKEN_KEY"
assert groups[5] == "${TOKEN_VALUE}"
match, _ = get_tokens("#cmakedefine TOKEN_KEY", magic_string)
assert match
groups = match.groups()
assert len(groups) == 6
assert groups[1] == magic_string
assert groups[3] == "TOKEN_KEY"
assert groups[5] == ""
match, _ = get_tokens("#differnt_magic_string TOKEN_KEY", magic_string)
assert not match
match, is_01 = get_tokens(f"#{magic_string}01 TOKEN_KEY", magic_string)
assert match
assert is_01
match, _ = get_tokens(f'#cmakedefine FOO "@FOO@"', "cmakedefine")
groups = match.groups()
assert groups[5] == '"@FOO@"'
def test_handle_01():
line = "#cmakedefine01 FOO"
tokens, _ = get_tokens(line, "cmakedefine")
assert handle01(line, tokens, True) == "#define FOO 1"
assert handle01(line, tokens, False) == "#define FOO 0"
def test_undefine():
lines = [
"#cmakedefine FOO",
"#cmakedefine FOO foo",
"#cmakedefine FOO ${FOO}",
"#cmakedefine FOO @FOO@",
]
for line in lines:
tokens, _ = get_tokens(line, "cmakedefine")
assert undefine(tokens) == "/* #undef FOO */"
def test_replace_value():
line = "#cmakedefine FOO whatever@VVV@${FFF}"
tokens, _ = get_tokens(line, "cmakedefine")
groups = tokens.groups()
assert len(groups) == 6
assert groups[1] == "cmakedefine"
assert groups[3] == "FOO"
assert groups[5] == "whatever@VVV@${FFF}"
assert replace_value(tokens, "FOO", "VALUE") == "#define FOO VALUE"
def test_compute_value():
token = "@KEY1@foo${KEY2}@KEY3@@KEY4@"
key_value_dict = {
"KEY1": "value1",
"KEY2": "value2",
"KEY3": "value3",
"KEY4": "value4",
"KEY5": "0",
}
assert compute_value(token, True, key_value_dict) == "value1foo${KEY2}value3value4"
assert compute_value(token, False, key_value_dict) == "value1foovalue2value3value4"
assert compute_value("@KEY5@", True, key_value_dict) == "0"
@given(text())
def test_substitution_with_given_value(s):
kv = {"FOO": s}
assert compute_value("@FOO@", True, kv) == s
assert compute_value("${FOO}", False, kv) == s
assert compute_value("${FOO}", True, kv) == "${FOO}"
|