-- Runs FishKey.lua against a mock of the WoW API. -- arg[1] = addon file local spells = { [7620] = "Fishing", [7731] = "Fishing", [133] = "Fireball" } C_Spell = { GetSpellName = function(id) return spells[id] end } local actions = { [9] = { "spell", 7731 }, [30] = { "macro", 4 }, [40] = { "spell", 133 } } function GetActionInfo(slot) local a = actions[slot] if a then return a[1], a[2] end end local macros = { [4] = 7620, [5] = 133 } function GetMacroSpell(i) return macros[i] end local macroNames = { ["FishMacro"] = 4, ["Nuke"] = 5 } function GetMacroIndexByName(n) return macroNames[n] or 0 end ActionButton9 = { bindingAction = "ACTIONBUTTON9", action = 9 } ActionButton10 = { bindingAction = "ACTIONBUTTON10", action = 40 } MultiBarBottomLeftButton6 = { bindingAction = "MULTIACTIONBAR1BUTTON6", action = 30 } SomeAddonButton = { GetAttribute = function(_, k) if k == "action" then return 9 end end } local bindings = { { "ACTIONBUTTON9", "ACTIONBAR", "F", "SHIFT-1" }, { "ACTIONBUTTON10", "ACTIONBAR", "G" }, { "MULTIACTIONBAR1BUTTON6", "ACTIONBAR" }, { "SPELL Fishing", "SPELLS", "CTRL-F" }, { "MACRO FishMacro", "MACROS", "ALT-F" }, { "MACRO Nuke", "MACROS", "N" }, { "CLICK SomeAddonButton:LeftButton", "ADDONS", "BUTTON4" }, { "INTERACTTARGET", "TARGETING", "SHIFT-F" }, } function GetNumBindings() return #bindings end function GetBinding(i) return unpack(bindings[i]) end local overrides = {} function SetOverrideBinding(_, priority, key, command) overrides[key] = command end function ClearOverrideBindings() overrides = {} end local inCombat = false function InCombatLockdown() return inCombat end local held = {} function IsKeyDown(key) assert(not key:find("^SHIFT%-") and not key:find("^CTRL%-") and not key:find("^ALT%-"), "modifier passed to IsKeyDown: " .. key) return held[key] or false end local clock, timers = 0, {} C_Timer = { After = function(sec, fn) timers[#timers + 1] = { at = clock + sec, fn = fn } end } local function advance(sec) local target = clock + sec while true do local nextIndex for i, t in ipairs(timers) do if t.at <= target and (not nextIndex or t.at < timers[nextIndex].at) then nextIndex = i end end if not nextIndex then break end local t = table.remove(timers, nextIndex) clock = t.at t.fn() end clock = target end local cvars = { SoftTargetInteract = "0", SoftTargetInteractRange = "10", ActionButtonUseKeyHeldSpell = "1" } local temp = {} C_CVar = { GetCVar = function(n) return temp[n] or cvars[n] end, SetTempCVar = function(n, v) temp[n] = v end, RemoveTempCVar = function(n) temp[n] = nil end, } local handler CreateFrame = function() return { RegisterEvent = function() end, RegisterUnitEvent = function() end, SetScript = function(_, _, fn) handler = 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 function count(t) local n = 0 for _ in pairs(t) do n = n + 1 end return n end local function stop() handler(nil, "UNIT_SPELLCAST_CHANNEL_STOP", "player", "guid", 7620) end -- Cast ends with no loot and every key released: everything back to normal. local function finish() stop(); advance(2) end assert(loadfile(arg[1]))("FishKey", {}) handler(nil, "ADDON_LOADED", "FishKey") check(FishKeyDB.enabled == true, "enabled by default") handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7731) check(overrides["F"] == "INTERACTTARGET", "action bar key becomes interact") check(overrides["SHIFT-1"] == "INTERACTTARGET", "second key on the same button too") check(overrides["CTRL-F"] == "INTERACTTARGET", "SPELL binding") check(overrides["ALT-F"] == "INTERACTTARGET", "macro binding casting Fishing") check(overrides["BUTTON4"] == "INTERACTTARGET", "CLICK binding to an addon button") check(overrides["G"] == nil and overrides["N"] == nil, "other spells untouched") check(count(overrides) == 5, "only Fishing keys overridden") check(C_CVar.GetCVar("SoftTargetInteractRange") == "30", "interact range raised while fishing") check(C_CVar.GetCVar("SoftTargetInteract") == "3", "Interact Key turned on while fishing") local function restored() return count(overrides) == 0 and C_CVar.GetCVar("SoftTargetInteract") == "0" and C_CVar.GetCVar("SoftTargetInteractRange") == "10" and C_CVar.GetCVar("ActionButtonUseKeyHeldSpell") == "1" end check(C_CVar.GetCVar("ActionButtonUseKeyHeldSpell") == "0", "press and hold casting off while fishing") -- Catch with the key held through the loot (press and hold, slow release). held["F"] = true handler(nil, "UNIT_SPELLCAST_CHANNEL_STOP", "player", "guid", 7731) check(overrides["F"] == "INTERACTTARGET", "still interact right after the channel ends") handler(nil, "LOOT_OPENED") advance(2) check(overrides["F"] == "INTERACTTARGET", "still interact while the loot window is open") handler(nil, "LOOT_CLOSED") advance(0.3) check(overrides["F"] == "INTERACTTARGET" and C_CVar.GetCVar("ActionButtonUseKeyHeldSpell") == "0", "still interact while the key is held") held["F"] = nil advance(0.1) check(restored(), "key and settings restored once looted and released") -- Miss: no loot window. handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) stop() advance(1) check(overrides["F"] == "INTERACTTARGET", "waits a moment for loot after a miss") advance(1) check(restored(), "restored after a miss") -- Loot arrives before the channel stop, modifier key held. handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) handler(nil, "LOOT_OPENED") stop() held["1"] = true handler(nil, "LOOT_CLOSED") advance(0.2) check(overrides["SHIFT-1"] == "INTERACTTARGET", "held SHIFT-1 counts as held") held["1"] = nil advance(0.1) check(restored(), "restored after loot that came first") -- A stale miss timer must not cut short the next cast. handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) handler(nil, "LOOT_OPENED") stop() handler(nil, "LOOT_CLOSED") handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) advance(2) check(overrides["F"] == "INTERACTTARGET", "old timer leaves the new cast alone") finish() handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 133) check(count(overrides) == 0, "other channels ignored") handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) handler(nil, "PLAYER_REGEN_DISABLED") check(count(overrides) == 0, "cleared when combat starts") inCombat = true handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) check(count(overrides) == 0, "no override in combat") inCombat = false cvars.SoftTargetInteractRange = "40" handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) check(temp.SoftTargetInteractRange == nil, "longer range left alone") finish() cvars.SoftTargetInteract = "3" handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) check(temp.SoftTargetInteract == nil, "Interact Key already on is left alone") finish() cvars.SoftTargetInteract = "0" C_CVar.SetTempCVar = function() error("secure cvar") end handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) check(overrides["F"] == "INTERACTTARGET" and printed[#printed]:find("Settings", 1, true), "blocked setting warns, key still overridden") finish() C_CVar.SetTempCVar = function(n, v) temp[n] = v end SlashCmdList.FISHKEY("off") check(FishKeyDB.enabled == false, "/fish off") check(printed[#printed]:find("off", 1, true) and printed[#printed]:find("F, SHIFT-1", 1, true), "status lists keys") handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) check(count(overrides) == 0, "nothing when off") SlashCmdList.FISHKEY("on") check(FishKeyDB.enabled == true, "/fish on") print(failures == 0 and "all passed" or (failures .. " failed")) os.exit(failures == 0 and 0 or 1)