wow-addons/tests/test_qsl.lua

298 lines
12 KiB
Lua
Raw Permalink Normal View History

-- Runs QuestShoppingList.lua against a mock of the WoW API.
-- arg[1] = addon file
local log, toggles
local function resetLog()
toggles = 0
log = {
{ title = "Westfall", isHeader = true },
{ title = "Murlocs", questID = 10, level = 12, objs = {
{ type = "item", text = "Linen Cloth: 2/5", numRequired = 5 },
{ type = "item", text = "Quest Thing: 0/1", numRequired = 1 },
{ type = "monster", text = "Murloc slain: 0/5", numRequired = 5 },
} },
{ title = "Silk", questID = 11, level = 20, objs = {
{ type = "item", text = "3/4 Silk Cloth (Optional)", numRequired = 4 },
} },
{ title = "Have It All", questID = 12, level = 5, objs = {
{ type = "item", text = "Rough Stone: 3/3", numRequired = 3 },
} },
{ title = "Soulbound", questID = 13, level = 5, objs = {
{ type = "item", text = "Bound Thing: 0/1", numRequired = 1 },
} },
{ title = "Collapsed", isHeader = true, isCollapsed = true },
{ title = "Hidden Under Header", questID = 14, level = 30, objs = {
{ type = "item", text = "Linen Cloth: 0/2", numRequired = 2 },
} },
}
end
resetLog()
local hideCollapsed = true
local function visible()
local out, inCollapsed = {}, false
for _, e in ipairs(log) do
if e.isHeader then
inCollapsed = e.isCollapsed
out[#out + 1] = e
elseif not (hideCollapsed and inCollapsed) then
out[#out + 1] = e
end
end
return out
end
C_QuestLog = {
GetNumQuestLogEntries = function() return #visible() end,
GetInfo = function(i) return visible()[i] end,
GetQuestObjectives = function(id)
for _, e in ipairs(log) do if e.questID == id then return e.objs end end
end,
}
function ExpandQuestHeader(i) toggles = toggles + 1; visible()[i].isCollapsed = false end
function CollapseQuestHeader(i) toggles = toggles + 1; visible()[i].isCollapsed = true end
local LINEN, QUESTY, SILK, STONE, BOUND = 2589, 900, 4306, 2835, 901
local SWORD, RING, CAPE = 7000, 7001, 7002
local counts = { [LINEN] = 2, [SILK] = 3, [STONE] = 3, [BOUND] = 0 }
local itemInfo = {
[LINEN] = { sell = 13, bind = 0 }, [SILK] = { sell = 150, bind = 0 }, [STONE] = { sell = 2, bind = 0 },
[BOUND] = { sell = 0, bind = 1 },
[SWORD] = { sell = 500, bind = 1 }, [RING] = { sell = 100, bind = 2 }, [CAPE] = { sell = 900, bind = 1 },
}
local requested = {}
C_Item = {
GetItemCount = function(id) return counts[id] or 0 end,
GetItemInfo = function(id)
local i = itemInfo[id]
if not i then return end
return "n", "l", 1, 1, 1, "t", "s", 1, "", 1, i.sell, 0, 0, i.bind
end,
RequestLoadItemDataByID = function(id) requested[id] = (requested[id] or 0) + 1 end,
}
local prices = { [LINEN] = 40, [SILK] = 1000, [RING] = 2000 }
local searches = {}
Auctionator = { API = { v1 = {
GetAuctionPriceByItemID = function(caller, id) assert(caller == "QuestShoppingList"); return prices[id] end,
MultiSearchAdvanced = function(_, terms) searches[#searches + 1] = terms end,
} } }
local rewards = {
[10] = { money = 300, xp = 900, items = { { "Sword", SWORD, 1 } } },
[11] = { money = 10, xp = 0, items = {}, choices = { { "Ring", RING, 1 }, { "Cape", CAPE, 1 } } },
[13] = { money = 0, xp = 0, items = {} },
[14] = { money = 0, xp = 50, items = {} },
}
function GetQuestLogRewardMoney(id) return rewards[id].money end
function GetQuestLogRewardXP(id) return rewards[id].xp, rewards[id].xp end
function GetNumQuestLogRewards(id) return #rewards[id].items end
function GetQuestLogRewardInfo(i, id) local r = rewards[id].items[i]; return r[1], 1, r[3], 1, true, r[2] end
function GetNumQuestLogChoices(id) return #(rewards[id].choices or {}) end
function GetQuestLogChoiceInfo(i, id) local r = rewards[id].choices[i]; return r[1], 1, r[3], 1, true, r[2] end
function GetMoneyString(c) return c .. "c" end
local timers, tickers = {}, {}
C_Timer = {
After = function(_, fn) timers[#timers + 1] = fn end,
NewTicker = function(_, fn) tickers[#tickers + 1] = fn end,
}
local function flush()
local list = timers
timers = {}
for _, fn in ipairs(list) do fn() end
end
local created = {}
local function widget(template)
local w = { template = template, shown = true, scripts = {} }
setmetatable(w, { __index = function(_, k) if type(k) == "string" and k:match("^%u") then return function() end end end })
function w.Show(self) self.shown = true end
function w.Hide(self) self.shown = false end
function w.IsVisible(self) return self.shown end
function w.SetScript(self, name, fn) self.scripts[name] = fn end
function w.SetText(self, t) self.text = t end
function w.CreateFontString() return widget() end
function w.CreateTexture() return widget() end
function w.SetPoint(self, ...) self.point = { ... } end
return w
end
local main
CreateFrame = function(kind, name, parent, template)
local w = widget(template)
w.kind, w.frameName, w.parent = kind, name, parent
if not main then
main = w
local handler
w.RegisterEvent = function(self, e) self.events = self.events or {}; self.events[e] = true end
w.SetScript = function(self, _, fn) self.handler = fn end
return w
end
created[#created + 1] = w
return w
end
AuctionHouseFrame = widget()
SlashCmdList = {}
local printed = {}
DEFAULT_CHAT_FRAME = { AddMessage = function(_, m) printed[#printed + 1] = m end }
local tip = { lines = {} }
GameTooltip = {
SetOwner = function() end,
SetText = function(_, t) tip.title = t end,
AddDoubleLine = function(_, l, r) tip.lines[#tip.lines + 1] = { l, r } end,
Show = function() end,
Hide = function() tip.hidden = true 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 = { itemIDs = { ["linen cloth"] = LINEN, ["silk cloth"] = SILK, ["rough stone"] = STONE, ["bound thing"] = BOUND } }
assert(loadfile(arg[1]))("QuestShoppingList", ns)
local function fire(event, arg1) main.handler(main, event, arg1) end
local function rows()
local out = {}
for _, w in ipairs(created) do
if w.kind == "Button" and w.rowKind and w.shown then out[#out + 1] = w end
end
return out
end
local function panel()
for _, w in ipairs(created) do if w.frameName == "QuestShoppingListPanel" then return w end end
end
local function button(template)
for _, w in ipairs(created) do if w.template == template then return w end end
end
fire("AUCTION_HOUSE_SHOW")
flush()
check(panel() and panel().shown, "panel opens with the auction house")
check(panel().parent == AuctionHouseFrame, "panel belongs to the auction window")
local r = rows()
check(#r == 6, "three quests with what they still need: " .. #r)
check(r[1].rowKind == "quest" and r[1].left.text == "[12] Murlocs", "quest row: " .. tostring(r[1].left.text))
check(r[2].rowKind == "item" and r[2].left.text:find("Linen Cloth", 1, true), "item row: " .. tostring(r[2].left.text))
check(r[2].right.text:find("2 / 5", 1, true) and r[2].right.text:find("120c", 1, true), "have / need and cost of what's missing: " .. tostring(r[2].right.text))
-- Murlocs: 3 x 40 = 120 cost; 300 gold + sword vendor 500 = 800 reward.
check(r[1].sub.text:find("Buy 120c", 1, true) and r[1].sub.text:find("Reward 800c + 900 XP", 1, true), "cost against reward: " .. r[1].sub.text)
check(r[1].sub.text:find("|cff40ff40", 1, true), "worth it is green")
-- Silk: 1 x 1000 = 1000; 10 gold + best of Ring (2000 x 0.95 = 1900) / Cape (900).
check(r[3].left.text == "[20] Silk", "optional objective format is read")
check(r[3].sub.text:find("Buy 1000c", 1, true) and r[3].sub.text:find("Reward 1910c", 1, true), "best choice counted once, auction cut taken: " .. r[3].sub.text)
check(r[5].left.text == "[30] Hidden Under Header", "quests under a collapsed header are read")
check(log[6].isCollapsed == true, "collapsed header is restored")
check(toggles == 2, "header opened and closed once: " .. toggles)
for _, row in ipairs(r) do
check(row.left.text ~= "[5] Have It All" and row.left.text ~= "[5] Soulbound", "row excludes done or soulbound: " .. row.left.text)
end
-- Tooltip breakdown.
r[1].scripts.OnEnter(r[1])
check(tip.title == "Murlocs" and #tip.lines == 2 and tip.lines[1][1] == "Gold" and tip.lines[2][1] == "Sword", "tooltip breaks down the reward")
tip.lines = {}
r[3].scripts.OnEnter(r[3])
check(tip.lines[2][1] == "Best choice: Ring", "tooltip names the best choice: " .. tostring(tip.lines[2] and tip.lines[2][1]))
r[2].scripts.OnEnter(r[2])
-- Searching.
r[2].scripts.OnClick(r[2])
check(#searches == 1 and searches[1][1].searchString == "Linen Cloth" and searches[1][1].isExact and searches[1][1].quantity == 3, "clicking an item searches for it")
r[1].scripts.OnClick(r[1])
check(#searches == 2 and #searches[2] == 1, "clicking a quest searches for its items")
local all = button("UIPanelButtonTemplate")
all.scripts.OnClick(all)
local s = searches[3]
check(#s == 2 and s[1].searchString == "Linen Cloth" and s[1].quantity == 5, "search all adds up an item shared by quests")
-- Expensive, unknown prices.
prices[SILK] = 5000
tickers[1]()
r = rows()
check(r[3].sub.text:find("|cffff5050", 1, true), "not worth it is red")
prices[SILK] = nil
tickers[1]()
r = rows()
check(r[3].sub.text:find("Buy ?", 1, true) and r[4].right.text:find("no price", 1, true), "unseen item says so")
check(not r[3].sub.text:find("|cffff5050", 1, true) and not r[3].sub.text:find("|cff40ff40", 1, true), "unknown cost isn't judged")
prices[SILK] = 1000
-- Reward item not loaded yet.
itemInfo[SWORD] = nil
tickers[1]()
r = rows()
check(requested[SWORD] == 1 and r[1].sub.text:find("|cffffffff", 1, true), "reward still loading isn't judged")
tickers[1]()
check(requested[SWORD] == 1, "item is only requested once")
itemInfo[SWORD] = { sell = 500, bind = 1 }
fire("ITEM_DATA_LOAD_RESULT", SWORD)
flush()
r = rows()
check(r[1].sub.text:find("Reward 800c", 1, true), "fills in once the item loads")
-- An item the client hasn't loaded stays listed, then drops out if it's soulbound.
local saved = itemInfo[BOUND]
itemInfo[BOUND] = nil
panel().shown = false
fire("AUCTION_HOUSE_SHOW")
flush()
local found = false
for _, row in ipairs(rows()) do found = found or row.left.text == "[5] Soulbound" end
check(found, "unloaded item is listed for now")
itemInfo[BOUND] = saved
fire("ITEM_DATA_LOAD_RESULT", BOUND)
flush()
found = false
for _, row in ipairs(rows()) do found = found or row.left.text == "[5] Soulbound" end
check(not found, "soulbound item drops out once known")
-- Buying.
counts[LINEN] = 5
fire("BAG_UPDATE_DELAYED")
flush()
r = rows()
check(r[2].right.text:find("5 / 5") == nil and r[1].left.text == "[20] Silk" or r[1].left.text == "[30] Hidden Under Header", "bought items leave the list")
counts[LINEN] = 2
-- Quest log changes only rescan when visible.
local before = toggles
fire("QUEST_REMOVED")
flush()
check(toggles == before + 2, "quest change rescans while open")
panel().shown = false
before = toggles
fire("QUEST_REMOVED")
flush()
check(toggles == before, "no rescan while closed")
tickers[1]()
-- Nothing to buy: no panel.
local savedLog = log
log = { { title = "Empty", isHeader = true } }
panel().shown = false
fire("AUCTION_HOUSE_SHOW")
flush()
check(panel().shown == false, "panel stays closed with nothing to buy")
log = savedLog
SlashCmdList.QUESTSHOPPINGLIST()
flush()
check(panel().shown == true, "/qsl reopens it")
AuctionHouseFrame.shown = false
SlashCmdList.QUESTSHOPPINGLIST()
check(printed[1] and printed[1]:find("auction house", 1, true), "/qsl explains itself away from the auction house")
print(failures == 0 and "ALL PASSED" or (failures .. " FAILED"))
os.exit(failures == 0 and 0 or 1)