wow-addons/tests/test_dpq.lua
rope de17b0006e Initial addons
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 13:02:16 +00:00

139 lines
4.7 KiB
Lua

-- Runs DungeonPreQuest.lua against a mock of the WoW API.
-- arg[1] = addon file
local faction, classID = "Alliance", 1
function UnitFactionGroup() return faction end
function UnitClass() return "x", "X", classID end
local callback
EventRegistry = { RegisterCallback = function(_, _, fn) callback = fn end }
local hooks = {}
function hooksecurefunc(name, fn) hooks[name] = fn end
local function region()
local r = { shown = false }
function r:Show() self.shown = true end
function r:Hide() self.shown = false end
function r:SetPoint() end
function r:SetText(t) self.text = t end
return r
end
local function button(id)
local b = { questID = id, creates = 0 }
function b:CreateFontString() self.creates = self.creates + 1; return region() end
return b
end
local rows = {}
QuestScrollFrame = { titleFramePool = { EnumerateActive = function()
local i = 0
return function() i = i + 1; return rows[i] end
end } }
local onShow
QuestFrameDetailPanel = {
CreateFontString = function() return region() end,
HookScript = function(_, _, fn) onShow = fn end,
}
local banner
do
local make = QuestFrameDetailPanel.CreateFontString
QuestFrameDetailPanel.CreateFontString = function() banner = make(); return banner end
end
QuestFrame = {}
local currentQuest
function GetQuestID() return currentQuest end
local tipLines, tipShown = {}, 0
GameTooltip = { AddLine = function(_, l) tipLines[#tipLines + 1] = l end, Show = function() tipShown = tipShown + 1 end }
local onEvent, registered = nil, {}
CreateFrame = function() return { RegisterEvent = function(_, e) registered[e] = true end, SetScript = function(_, _, fn) onEvent = fn end } end
SlashCmdList = {}
local printed = {}
DEFAULT_CHAT_FRAME = { AddMessage = function(_, m) printed[#printed + 1] = m end }
local failures = 0
local function check(cond, what)
if not cond then failures = failures + 1 end
print((cond and "ok " or "FAIL ") .. what)
end
local ns = {
dungeons = { "The Deadmines", "Stratholme" },
quests = {
[65] = { 1, 3, 0 }, -- everyone
[70] = { 1, 1, 0 }, -- Alliance only
[71] = { 2, 3, 2 }, -- paladins only (class bit 2)
[72] = { 2, 3, 1024 }, -- druids only (class 11)
[73] = { 1, 3, 0, 2, 3, 0, 1, 3, 0 }, -- two dungeons, one repeated
[74] = { 0, 3, 0 }, -- dungeon not known by name
},
}
assert(loadfile(arg[1]))("DungeonPreQuest", ns)
check(hooks.QuestLogQuests_Update == nil, "nothing touches Blizzard frames before login")
onEvent(nil, "QUEST_DETAIL")
onEvent(nil, "PLAYER_LOGIN")
local function text(id)
tipLines = {}
callback(ns, { questID = id }, id)
return tipLines[1]
end
check(text(65) == "Dungeon pre-quest: The Deadmines", "tooltip names the dungeon: " .. tostring(text(65)))
check(text(99) == nil, "unrelated quest gets no tooltip line")
check(text(73) == "Dungeon pre-quest: The Deadmines, Stratholme", "several dungeons, no repeats: " .. tostring(text(73)))
check(text(74) == "Dungeon pre-quest", "unknown dungeon still marks: " .. tostring(text(74)))
check(text(70) ~= nil, "Alliance entry shows for Alliance")
faction = "Horde"
check(text(70) == nil, "Alliance entry hidden for Horde")
faction = "Alliance"
classID = 1
check(text(71) == nil, "paladin chain hidden for a warrior")
classID = 2
check(text(71) ~= nil, "paladin chain shown for a paladin")
classID = 11
check(text(72) ~= nil, "class ID 11 maps to the druid bit")
classID = 1
tipLines = {}
callback(ns, { questID = 65 })
check(tipLines[1] ~= nil, "tooltip falls back to the button's quest ID")
-- Log rows: buttons are pooled and reused.
local a, b = button(65), button(99)
rows = { a, b }
hooks.QuestLogQuests_Update()
check(a.dpqMark and a.dpqMark.shown and a.dpqMark.text:find("D", 1, true), "pre-quest row is marked")
check(b.dpqMark == nil, "other row stays unmarked")
a.questID = 99
hooks.QuestLogQuests_Update()
check(a.dpqMark.shown == false, "reused row drops the mark")
a.questID = 65
hooks.QuestLogQuests_Update()
check(a.dpqMark.shown == true and a.creates == 1, "row mark is reused, not recreated")
-- Offer window.
currentQuest = 65
onShow()
check(banner.shown and banner.text:find("The Deadmines", 1, true), "offer window shows the banner")
currentQuest = 99
check(registered.QUEST_DETAIL, "listens for a new quest being offered")
onEvent()
check(banner.shown == false, "next offer clears the banner")
printed = {}
SlashCmdList.DUNGEONPREQUEST("65")
check(printed[1]:find("The Deadmines", 1, true), "/dpq describes a quest")
SlashCmdList.DUNGEONPREQUEST("nope")
check(printed[2]:find("usage", 1, true), "/dpq explains itself")
print(failures == 0 and "ALL PASSED" or (failures .. " FAILED"))
os.exit(failures == 0 and 0 or 1)