-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_toc.py
More file actions
290 lines (228 loc) · 10.2 KB
/
test_toc.py
File metadata and controls
290 lines (228 loc) · 10.2 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import os
import pytest
from pathlib import Path
from pytoc import *
WORKING_DIRECTORY = Path(__file__).resolve().parent
def test_parser():
file = TOCFile(f"{WORKING_DIRECTORY}/testfile.toc")
assert file.Interface.Value == [110000, 110105, 11507, 30404, 40402, 50500]
assert file.Title == "GhostTools"
assert file.Title.get_translation(TOCTextLocale.frFR) == "GrasTools"
assert file.Title.get_translation(TOCTextLocale.deDE) == "DieGeistTools"
assert file.Notes == "A collection of cadaverous tools for the discerning necromancer."
assert file.UnknownDirectives.get("Bad").Value == "bad:data : ## # ###"
assert file.SavedVariables == [
"GhostConfig",
"GhostData",
"GhostScanData",
"GhostSavedProfile",
]
assert file.SavedVariablesPerCharacter == ["GhostEventLog"]
assert file.SavedVariablesMachine == ["GhostWumbo"]
assert file.IconTexture == "Interface/AddOns/totalRP3/Resources/policegar"
assert file.IconAtlas == "ui-debug-tool-icon-large"
assert file.AddonCompartmentFunc == "GHOST_OnAddonCompartmentClick"
assert file.AddonCompartmentFuncOnEnter == "GHOST_OnAddonCompartmentEnter"
assert file.AddonCompartmentFuncOnLeave == "GHOST_OnAddonCompartmentLeave"
assert file.ExtendedDirectives.get("X-Website") == "https://ghst.tools"
assert file.get_all_addon_file_names() == [
"Libs/LibStub/LibStub.lua",
"Libs/CallbackHandler-1.0/CallbackHandler-1.0.xml",
"Libs/LibDataBroker-1.1/LibDataBroker-1.1.lua",
"Libs/LibDBIcon-1.0/LibDBIcon-1.0/lib.xml",
"Libs/FAIAP.lua",
"GhostTools.lua",
"GhostAddonCompartment.lua",
"Experiments/Experiments.lua",
"Experiments/EventLog.lua",
"Core/ConsoleScripts.lua",
"Core/EventListener.lua",
"Core/ErrorHandler.lua",
"Core/Global.lua",
"Core/SlashCommands.lua",
"Core/Macros.lua",
"Core/Coroutines.lua",
"Core/Mixins.lua",
"[Family]/FamilyFile.lua",
"[Game]/UIKerning.lua",
"ClassicOnly.lua [AllowLoadGameType classic]",
]
assert file.DefaultState == None
assert file.OnlyBetaAndPTR == None
assert file.LoadWith == None
assert file.LoadManagers == None
assert file.LoadFirst == None
with pytest.raises(FileNotFoundError):
TOCFile("bad/file/path")
required_deps = ["Blackjack", "Advanced_Scrolling_Combat_Text", "Meorawr"]
optional_deps = ["totalRP3", "KethoDoc", "LibAdvFlight-1.0", "LibSmokeSignal-1.0", "BugGrabber", "Warmup"]
for dep in required_deps:
assert dep in file.Dependencies
for dep in optional_deps:
assert dep in file.OptionalDeps
assert bool(file.UseSecureEnvironment) == True
assert file.Group == "GhostTools"
assert file.Category == "Roleplay"
assert file.Category.get_translation("enUS") == "Roleplay"
assert file.Category.get_translation("deDE") == "Rollenspiel"
assert file.Category.get_translation("esES") == "Juego de rol"
assert file.Category.get_translation("esMX") == "Juego de rol"
assert file.Category.get_translation("frFR") == "Jeu de rôle"
assert file.Category.get_translation("itIT") == "Gioco di Ruolo"
assert file.Category.get_translation("koKR") == "롤플레잉"
assert file.Category.get_translation("ptBR") == "Interpretação de Papel"
assert file.Category.get_translation("ruRU") == "Ролевая игра"
assert file.Category.get_translation("zhCN") == "角色扮演"
assert file.Category.get_translation("zhTW") == "角色扮演"
def test_roundtrip_byte_identical():
src = WORKING_DIRECTORY / "testfile.toc"
dst = WORKING_DIRECTORY / "test_roundtrip_output.toc"
toc = TOCFile(src)
toc.export(dst, overwrite=True)
with open(src, encoding="utf-8") as a, open(dst, encoding="utf-8") as b:
assert a.read() == b.read()
dst.unlink()
def test_roundtrip_after_mutation_preserves_other_lines():
src = WORKING_DIRECTORY / "testfile.toc"
dst = WORKING_DIRECTORY / "test_mutated_output.toc"
toc = TOCFile(src)
toc.Author = TOCLocalizedDirectiveValue("NewAuthor")
toc.export(dst, overwrite=True)
with open(src, encoding="utf-8") as a:
original_lines = a.readlines()
with open(dst, encoding="utf-8") as b:
new_lines = b.readlines()
assert len(original_lines) == len(new_lines)
for i, (orig, new) in enumerate(zip(original_lines, new_lines)):
if orig.startswith("## Author:"):
assert new == "## Author: NewAuthor\n"
else:
assert orig == new, f"line {i} unexpectedly changed"
dst.unlink()
EXPORT_PATH = WORKING_DIRECTORY / "test_output.toc"
def test_export():
toc = TOCFile()
toc.Interface = TOCListValue("120001", [120001])
toc.Author = TOCLocalizedDirectiveValue("Ghost")
toc.Title = TOCLocalizedDirectiveValue(
"GhostTools",
{TOCTextLocale.frFR: "GrasTools", TOCTextLocale.deDE: "DieGeistTools"},
)
toc.Category = TOCLocalizedDirectiveValue(
"Roleplay",
{TOCTextLocale.deDE: "Rollenspiel", TOCTextLocale.koKR: "롤플레잉", TOCTextLocale.zhTW: "角色扮演"},
)
toc.OnlyBetaAndPTR = TOCBoolType("1", True)
toc.DefaultState = TOCBoolType("1", True)
toc.add_file("file1.lua")
toc.add_file("file2.xml")
overwrite = True
toc.export(EXPORT_PATH, overwrite)
assert os.path.exists(EXPORT_PATH)
def test_read_export():
toc = TOCFile(EXPORT_PATH)
assert toc.Interface == 120001
assert toc.Author == "Ghost"
assert toc.Title == "GhostTools"
assert toc.Title.get_translation(TOCTextLocale.frFR) == "GrasTools"
assert toc.Title.get_translation(TOCTextLocale.deDE) == "DieGeistTools"
assert toc.Category == "Roleplay"
assert toc.Category.get_translation("deDE") == "Rollenspiel"
assert toc.Category.get_translation("koKR") == "롤플레잉"
assert toc.Category.get_translation("zhTW") == "角色扮演"
assert toc.Category.get_translation("frFR") is None
assert toc.OnlyBetaAndPTR == True
assert toc.DefaultState == True
assert toc.get_raw_files() == ["file1.lua", "file2.xml"]
def test_addon_loading():
ctx = TOCEvaluationContext(TOCGameType.Wowhack, TOCEnvironment.Global, TOCTextLocale.enUS)
assert ctx.LoadedAddons == {}
addon_name = "Dragon"
assert not ctx.is_addon_loaded(addon_name)
ctx.load_addon(addon_name)
assert ctx.is_addon_loaded(addon_name)
ctx.unload_addon(addon_name)
assert not ctx.is_addon_loaded(addon_name)
def test_ctx_family():
ctx = TOCEvaluationContext(TOCGameType.Wowhack, TOCEnvironment.Global, TOCTextLocale.enUS)
assert ctx.Family == TOCFamily.Mainline
ctx.GameType = TOCGameType.Mists
assert ctx.Family == TOCFamily.Classic
def discover_toc_files(path: Path) -> list[Path]:
toc_files = []
for root, _, files in path.walk():
for file in files:
if file.endswith(".toc"):
toc_path = Path(root) / file
toc_files.append(toc_path)
return toc_files
def test_blizzard_ui_conformance():
ui_source_path = Path("wow-ui-source") / "Interface"
if not ui_source_path.exists():
env_path = os.getenv("WOW_UI_SOURCE_PATH")
if env_path is None:
pytest.skip("wow-ui-source not present and WOW_UI_SOURCE_PATH not set")
ui_source_path = Path(env_path) / "Interface"
if not ui_source_path.exists():
pytest.skip(f"UI source path does not exist: {ui_source_path}")
failures = []
# collect all the toc files
toc_files = discover_toc_files(ui_source_path)
for file in toc_files:
try:
TOCFile(file)
except Exception as e:
failures.append((file, e))
assert not failures, failures
def test_addon_load_conditions():
ctx = TOCEvaluationContext(TOCGameType.Mainline, TOCEnvironment.Global, TOCTextLocale.enUS)
toc = TOCFile()
toc.AllowLoadGameType = TOCAllowLoadGameType({TOCGameType.Wowhack})
can_load, err = toc.can_load_addon(ctx)
assert (not can_load) and (err == TOCAddonLoadError.WrongGameType), err.name
toc.AllowLoad = TOCAllowLoad({TOCEnvironment.Both})
toc.AllowLoadGameType = TOCAllowLoadGameType({TOCGameType.Mainline})
toc.AllowLoadTextLocale = TOCAllowLoadTextLocale({TOCTextLocale.enUS})
can_load, err = toc.can_load_addon(ctx)
assert can_load, err.name
dep_name = "Blackjack"
dep_required = True
toc.add_dependency(dep_name, dep_required)
can_load, err = toc.can_load_addon(ctx)
assert (not can_load) and (err == TOCAddonLoadError.MissingDependency), err.name
ctx.load_addon(dep_name)
can_load, err = toc.can_load_addon(ctx)
assert can_load, err.name
def test_plain_file_entry():
ctx = TOCEvaluationContext(TOCGameType.Mainline, TOCEnvironment.Global, TOCTextLocale.enUS)
path = "Dragon/Dragon.lua"
file = TOCFileEntry(path)
assert str(file) == path
assert file.resolve_path(ctx) == path
assert file.should_load(ctx)
assert file.export() == path
def test_variable_file_entry():
ctx = TOCEvaluationContext(TOCGameType.Mainline, TOCEnvironment.Global, TOCTextLocale.enUS)
path = "[Family]/Dragon.lua"
file = TOCFileEntry(path)
assert str(file) == path
assert file.resolve_path(ctx) == f"{TOCFamily.Mainline}/Dragon.lua"
assert file.should_load(ctx)
assert file.export() == path
def test_conditional_file_entry():
ctx = TOCEvaluationContext(TOCGameType.Wowhack, TOCEnvironment.Global, TOCTextLocale.enUS)
path = "[Family]/Dragon.lua"
conditions = [TOCAllowLoadGameType({TOCGameType.Plunderstorm}), TOCAllowLoadEnvironment({TOCEnvironment.Both})]
file = TOCFileEntry(path, conditions)
assert str(file) == path
assert file.resolve_path(ctx) == f"{TOCFamily.Mainline}/Dragon.lua"
assert not file.should_load(ctx)
assert file.export() == "[Family]/Dragon.lua [AllowLoadGameType plunderstorm] [AllowLoadEnvironment Both]"
def test_textlocale_file_entry():
ctx = TOCEvaluationContext(TOCGameType.Mists, TOCEnvironment.Global, TOCTextLocale.enUS)
path = "[TextLocale]/Dragon.lua"
file = TOCFileEntry(path)
assert str(file) == path
assert file.resolve_path(ctx) == f"{TOCTextLocale.enUS}/Dragon.lua"
assert file.should_load(ctx)
assert file.export() == path