-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiki_test.py
222 lines (188 loc) · 5.19 KB
/
wiki_test.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from pathlib import Path
from typing import Any
import pytest
import yaml
from bgm_tv_wiki import DuplicatedKeyError, Field, Wiki, WikiSyntaxError, parse
spec_repo_path = Path(__file__, "../wiki-syntax-spec").resolve()
def as_dict(w: Wiki) -> dict[str, Any]:
data = []
for f in w.fields:
if isinstance(f.value, tuple):
data.append(
{
"key": f.key,
"array": True,
"values": [
{"v": v.value} | ({"k": v.key} if v.key else {})
for v in f.value
],
},
)
else:
data.append({"key": f.key, "value": f.value or ""})
return {"type": w.type, "data": data}
valid = [
file.name
for file in spec_repo_path.joinpath("tests/valid").iterdir()
if file.name.endswith(".wiki")
]
@pytest.mark.parametrize("name", valid)
def test_bangumi_wiki(name: str) -> None:
file = spec_repo_path.joinpath("tests/valid", name)
wiki_raw = file.read_text()
assert as_dict(parse(wiki_raw)) == yaml.safe_load(
file.with_suffix(".yaml").read_text()
), name
invalid = [
file.name
for file in spec_repo_path.joinpath("tests/invalid").iterdir()
if file.name.endswith(".wiki")
]
@pytest.mark.parametrize("name", invalid)
def test_bangumi_wiki_invalid(name: str) -> None:
file = spec_repo_path.joinpath("tests/invalid", name)
wiki_raw = file.read_text()
with pytest.raises(WikiSyntaxError):
parse(wiki_raw)
def test_cast() -> None:
parse(
"""
{{Infobox Crt
|简体中文名= 绵饴
|别名={
[第二中文名|]
[英文名|]
[日文名|]
[纯假名|]
[罗马字|]
[昵称|季節P]
}
|性别= 男
|生日=
|血型=
|身高=
|体重=
|BWH=
|引用来源= ニコニコ大百科
}}
"""
)
def test_field_semantically_equal() -> None:
assert not Field(key="a").semantically_equal(Field(key="b"))
assert Field(key="a", value=None).semantically_equal(Field(key="a", value=""))
assert not Field(key="a", value=None).semantically_equal(Field(key="a", value=()))
assert not Field(key="a", value="").semantically_equal(Field(key="a", value=()))
def test_index_of() -> None:
w = parse(
"\n".join(
[
"{{Infobox animanga/Manga",
"|a= 9784061822337",
"|b= 4061822330",
"}}",
]
)
)
assert w.index_of("a") == 0
assert w.index_of("b") == 1
assert w.index_of("c") == 2
def test_set_at() -> None:
w = parse(
"\n".join(
[
"{{Infobox animanga/Manga",
"|a= 9784061822337",
"|b= 4061822330",
"}}",
]
)
)
assert w.set_or_insert("a", "1", 0) == w.set("a", "1")
assert w.set_or_insert("c", "1", 1) == parse(
"\n".join(
[
"{{Infobox animanga/Manga",
"|a= 9784061822337",
"|c= 1",
"|b= 4061822330",
"}}",
]
)
)
assert w.set_or_insert("c", "1", 10) == parse(
"\n".join(
[
"{{Infobox animanga/Manga",
"|a= 9784061822337",
"|b= 4061822330",
"|c= 1",
"}}",
]
)
)
def test_duplicated_keys() -> None:
with pytest.raises(DuplicatedKeyError) as e:
parse(
"\n".join(
[
"{{Infobox animanga/Manga",
"|原作= 太田顕喜",
"|作画= むにゅう",
"|作画= むにゅ1",
"}}",
]
)
).remove_duplicated_fields()
assert e.value.keys == ["作画"]
parse(
"\n".join(
[
"{{Infobox animanga/Manga",
"|原作= 太田顕喜",
"|作画= むにゅう",
"}}",
]
)
).remove_duplicated_fields()
parse(
"\n".join(
[
"{{Infobox Album",
"|版本特性= BLドラマCD",
"|发售日期= 2016-03-26",
"|价格= ¥ 3,240",
"|艺术家= 小林裕介 / 佐藤拓也 / 高橋広樹 / 小堀幸 / 大隈健太 / 長野伸二",
"|发行商= CROWN WORKS",
"|原作= 安西リカ",
"|播放时长= 78分11秒",
"|录音= デルファイサウンド",
"|碟片数量= 1",
"|发行商= CROWN WORKS",
"}}",
]
)
).remove_duplicated_fields()
def test_equal() -> None:
parse(
"\n".join(
[
"{{Infobox Album",
"|1=1",
"|2=2",
"|3=",
"}}",
]
)
).semantically_equal(
parse(
"\n".join(
[
"{{Infobox Album",
"|3=",
"|2=2",
"|1=1",
"}}",
]
)
)
)