wow-addons/tests/test_dpq.lua

236 lines
9 KiB
Lua
Raw Permalink Normal View History

-- 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(a, b, c)
if c then
local original = a[b]
a[b] = function(...) original(...); c(...) end
else
hooks[a] = b
end
end
GossipAvailableQuestButtonMixin = { Setup = function(self, info) self.text = info.title; self.setups = (self.setups or 0) + 1 end }
local function listButton(id, isActive)
local b = { id = id, isActive = isActive, heightSets = 0, resizes = 0, Icon = { GetHeight = function() return 16 end } }
function b:GetID() return self.id end
function b:GetText() return self.text end
function b:SetText(t) self.text = t end
function b:GetTextHeight() return 12 end
function b:SetHeight() self.heightSets = self.heightSets + 1 end
function b:Resize() self.resizes = self.resizes + 1 end
return b
end
local greetingRows = {}
local greetingShow
QuestFrameGreetingPanel = {
titleButtonPool = { EnumerateActive = function()
local i = 0
return function() i = i + 1; return greetingRows[i] end
end },
HookScript = function(_, _, fn) greetingShow = fn end,
}
local availableByIndex = {}
function GetAvailableQuestInfo(i) return false, 0, false, false, availableByIndex[i] 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(...) self.point = { ... } end
function r:ClearAllPoints() self.point = nil end
function r:SetText(t) self.text = t end
return r
end
local function button(id)
local b = { questID = id, creates = 0, textWidth = 120, boxWidth = 230 }
b.Text = {
GetStringWidth = function() return b.textWidth end,
GetWidth = function() return b.boxWidth end,
}
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
[81] = { 2, 3, 0 }, -- also a dungeon quest itself
},
dungeonQuests = {
[80] = { 1, 1, 0 }, -- Alliance dungeon quest
[81] = { 1, 3, 0 },
[82] = { 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(80) == "Dungeon quest: The Deadmines", "dungeon quest names its own dungeon: " .. tostring(text(80)))
check(text(81) == "Dungeon quest: The Deadmines\nDungeon pre-quest: Stratholme", "both kinds on separate lines: " .. tostring(text(81)))
check(text(82) == "Dungeon quest", "unknown own dungeon still marks: " .. tostring(text(82)))
check(text(70) ~= nil, "Alliance entry shows for Alliance")
faction = "Horde"
check(text(70) == nil, "Alliance entry hidden for Horde")
check(text(80) == nil, "Alliance dungeon quest hidden for Horde")
check(text(81) == "Dungeon quest: The Deadmines\nDungeon pre-quest: Stratholme", "shared quest shows 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")
local own = button(80)
rows = { own }
hooks.QuestLogQuests_Update()
check(own.dpqMark and own.dpqMark.shown, "dungeon quest row is marked")
rows = { a, b }
check(a.dpqMark.text == "|cffc77dff[D]|r", "row uses the [D] tag: " .. tostring(a.dpqMark.text))
check(a.dpqMark.point[2] == a.Text and a.dpqMark.point[4] == 125, "mark sits just after the title: " .. tostring(a.dpqMark.point[4]))
a.textWidth = 400
hooks.QuestLogQuests_Update()
check(a.dpqMark.point[4] == 235, "long title keeps the mark at the end of the title box")
a.boxWidth = 0
hooks.QuestLogQuests_Update()
check(a.dpqMark.point[4] == 405, "unresolved title box falls back to the text width")
a.textWidth, a.boxWidth = 80, 230
hooks.QuestLogQuests_Update()
check(a.dpqMark.point[4] == 85, "reused row follows its new title")
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")
-- NPC quest lists.
local MARK = "|cff8a2be2[D]|r "
local gossip = listButton()
GossipAvailableQuestButtonMixin.Setup(gossip, { questID = 65, title = "Pre Quest" })
check(gossip.text == MARK .. "Pre Quest", "gossip row is marked: " .. tostring(gossip.text))
check(gossip.resizes == 1, "gossip row is resized for the longer text")
GossipAvailableQuestButtonMixin.Setup(gossip, { questID = 99, title = "Other" })
check(gossip.text == "Other", "reused gossip row has no mark")
GossipAvailableQuestButtonMixin.Setup(gossip, { questID = 65, title = "Pre Quest" })
check(gossip.resizes == 2 and gossip.text == MARK .. "Pre Quest", "setup again marks once")
local avail, plain, turnin = listButton(1, 0), listButton(2, 0), listButton(1, 1)
avail.text, plain.text, turnin.text = "Offer", "Plain", "Turn in"
availableByIndex = { 65, 99 }
greetingRows = { avail, plain, turnin }
greetingShow()
check(avail.text == MARK .. "Offer" and avail.heightSets == 1, "greeting offer is marked and resized")
check(plain.text == "Plain", "other greeting offer is not")
check(turnin.text == "Turn in", "quests already in progress are not marked")
hooks.QuestFrameGreetingPanel_OnShow()
check(avail.text == MARK .. "Offer" and avail.heightSets == 1, "both greeting hooks mark only once")
classID = 2
availableByIndex = { 72 }
avail.text = "Druid only"
greetingRows = { avail }
greetingShow()
check(avail.text == "Druid only", "list respects class")
classID = 1
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)