Files
libdogtag-unit-3-0/test.lua
ckknight 05c436fca0 - add support for unit changes (e.g. PLAYER_TARGET_CHANGED updates all fontstrings that have a unit='target' on them)
- fix a typo
- make it so mouseover-as-target works properly (Blizzard doesn't send mouseover events if target = mouseover)
- update non-normal units (units that don't receive Blizzard events) automatically every 0.25 seconds.
2008-03-16 01:41:42 +00:00

264 lines
8.1 KiB
Lua

local old_dofile = dofile
function dofile(file)
return old_dofile("../LibDogTag-3.0/" .. file)
end
old_dofile('../LibDogTag-3.0/test.lua')
dofile = old_dofile
local DogTag = LibStub("LibDogTag-3.0")
local units = {
player = {
hp = 1500,
maxhp = 2000,
exists = true,
},
pet = {
hp = 0,
maxhp = 1000,
exists = true,
},
target = {
hp = 2500,
maxhp = 2500,
exists = true,
},
pettarget = {
hp = 50,
maxhp = 100,
exists = true,
},
focus = {
exists = false,
},
mouseover = {
exists = false,
}
}
local IsLegitimateUnit = { player = true, target = true, focus = true, pet = true, playerpet = true, mouseover = true, npc = true, NPC = true }
for i = 1, 4 do
IsLegitimateUnit["party" .. i] = true
IsLegitimateUnit["partypet" .. i] = true
IsLegitimateUnit["party" .. i .. "pet"] = true
end
for i = 1, 40 do
IsLegitimateUnit["raid" .. i] = true
IsLegitimateUnit["raidpet" .. i] = true
IsLegitimateUnit["raid" .. i .. "pet"] = true
end
setmetatable(IsLegitimateUnit, { __index = function(self, key)
if type(key) ~= "string" then
return false
end
if key:match("target$") then
self[key] = self[key:sub(1, -7)]
return self[key]
end
self[key] = false
return false
end, __call = function(self, key)
return self[key]
end})
function UnitExists(unit)
if not IsLegitimateUnit(unit) then
error(("Not a legitimate unit: %q"):format(unit), 2)
end
return units[unit] and units[unit].exists
end
function UnitHealth(unit)
if not IsLegitimateUnit(unit) then
error(("Not a legitimate unit: %q"):format(unit), 2)
end
return units[unit] and units[unit].hp
end
function UnitHealthMax(unit)
if not IsLegitimateUnit(unit) then
error(("Not a legitimate unit: %q"):format(unit), 2)
end
return units[unit] and units[unit].maxhp
end
function UnitIsUnit(alpha, bravo)
if not IsLegitimateUnit(alpha) then
error(("Not a legitimate unit: %q"):format(alpha), 2)
end
if not IsLegitimateUnit(bravo) then
error(("Not a legitimate unit: %q"):format(bravo), 2)
end
return units[alpha] and units[bravo] and units[alpha] == units[bravo]
end
MyUnit_data = "player"
DogTag:AddTag("Unit", "MyUnit", {
code = [=[return _G.MyUnit_data]=],
ret = "string",
})
MyValue_data = nil
DogTag:AddTag("Unit", "MyValue", {
code = [=[return _G.MyValue_data]=],
ret = "nil;number;string",
})
dofile("Localization/enUS.lua")
dofile("LibDogTag-Unit-3.0.lua")
dofile("Modules/Health.lua")
dofile("Cleanup.lua")
assert_equal(DogTag:Evaluate("[HP('player')]"), "Unknown tag HP")
assert_equal(DogTag:Evaluate("[HP('player')]", "Unit"), 1500)
assert_equal(DogTag:Evaluate("[HP(unit='player')]", "Unit"), 1500)
assert_equal(DogTag:Evaluate("[HP]", "Unit", { unit = 'player'}), 1500)
assert_equal(DogTag:Evaluate("[MaxHP('player')]", "Unit"), 2000)
assert_equal(DogTag:Evaluate("[MaxHP(unit='player')]", "Unit"), 2000)
assert_equal(DogTag:Evaluate("[PercentHP(unit='player')]", "Unit"), 75)
assert_equal(DogTag:Evaluate("[MissingHP(unit='player')]", "Unit"), 500)
assert_equal(DogTag:Evaluate("[FractionalHP(unit='player')]", "Unit"), "1500/2000")
assert_equal(DogTag:Evaluate("[HP(unit='pettarget', known=true)]", "Unit"), nil)
assert_equal(DogTag:Evaluate("[MissingHP(unit='pettarget', known=true)]", "Unit"), nil)
assert_equal(DogTag:Evaluate("[FractionalHP(unit='pettarget', known=true)]", "Unit"), nil)
assert_equal(DogTag:Evaluate("[HP('target')]", "Unit"), 2500)
assert_equal(DogTag:Evaluate("[MaxHP('target')]", "Unit"), 2500)
assert_equal(DogTag:Evaluate("[HP]", "Unit", { unit = 'target'}), 2500)
assert_equal(DogTag:Evaluate("[MaxHP]", "Unit", { unit = 'target'}), 2500)
assert_equal(DogTag:Evaluate("[HP('focus')]", "Unit"), nil)
assert_equal(DogTag:Evaluate("[MaxHP('focus')]", "Unit"), nil)
assert_equal(DogTag:Evaluate("[HP]", "Unit", { unit = 'focus'}), nil)
assert_equal(DogTag:Evaluate("[MaxHP]", "Unit", { unit = 'focus'}), nil)
assert_equal(DogTag:Evaluate("[HP('fakeunit')]", "Unit"), 'Bad unit: "fakeunit"')
assert_equal(DogTag:Evaluate("[HP(unit='fakeunit')]", "Unit"), 'Bad unit: "fakeunit"')
assert_equal(DogTag:Evaluate("[MaxHP('fakeunit')]", "Unit"), 'Bad unit: "fakeunit"')
assert_equal(DogTag:Evaluate("[MaxHP(unit='fakeunit')]", "Unit"), 'Bad unit: "fakeunit"')
assert_equal(DogTag:Evaluate("[HP]", "Unit", { unit = 'fakeunit'}), 'Bad unit: "fakeunit"')
assert_equal(DogTag:Evaluate("[MaxHP]", "Unit", { unit = 'fakeunit'}), 'Bad unit: "fakeunit"')
assert_equal(DogTag:Evaluate("[HP]", "Unit", { unit = 50 }), 'Bad unit: "50"')
MyUnit_data = "player"
assert_equal(DogTag:Evaluate("[HP(MyUnit)]", "Unit"), 1500)
MyUnit_data = "target"
assert_equal(DogTag:Evaluate("[HP(MyUnit)]", "Unit"), 2500)
MyUnit_data = "focus"
assert_equal(DogTag:Evaluate("[HP(MyUnit)]", "Unit"), nil)
MyUnit_data = "fakeunit"
assert_equal(DogTag:Evaluate("[HP(MyUnit)]", "Unit"), 'Bad unit: "fakeunit"')
MyValue_data = "fakeunit"
assert_equal(DogTag:Evaluate("[HP(MyValue)]", "Unit"), 'Bad unit: "fakeunit"')
MyValue_data = 50
assert_equal(DogTag:Evaluate("[HP(MyValue)]", "Unit"), 'Bad unit: "50"')
MyValue_data = nil
assert_equal(DogTag:Evaluate("[HP(MyValue)]", "Unit"), nil)
local frame = CreateFrame("Frame")
local fs = frame:CreateFontString(nil, "ARTWORK")
DogTag:AddFontString(fs, frame, "[HP]", "Unit", { unit = "player" })
assert_equal(fs:GetText(), 1500)
units.player.hp = 1600
FireEvent("UNIT_HEALTH", "player")
FireOnUpdate(0.05)
assert_equal(fs:GetText(), 1600)
DogTag:AddFontString(fs, frame, "[HP]", "Unit", { unit = "target" })
assert_equal(fs:GetText(), 2500)
units.target.hp = 2000
units.target.maxhp = 2000
FireEvent("PLAYER_TARGET_CHANGED", "player")
FireOnUpdate(0.05)
assert_equal(fs:GetText(), 2000)
units.target.hp = 2500
units.target.maxhp = 2500
FireEvent("PLAYER_TARGET_CHANGED", "player")
FireOnUpdate(0.05)
assert_equal(fs:GetText(), 2500)
assert_equal(DogTag:Evaluate("[HPColor(unit='target')]", "Unit"), "|cff00ff00")
assert_equal(DogTag:Evaluate("[HPColor(unit='pet')]", "Unit"), "|cffff0000")
assert_equal(DogTag:Evaluate("[HPColor(unit='player')]", "Unit"), "|cff65ff00")
assert_equal(DogTag:Evaluate("['Hello':HPColor(unit='target')]", "Unit"), "|cff00ff00Hello|r")
assert_equal(DogTag:Evaluate("['Hello':HPColor(unit='pet')]", "Unit"), "|cffff0000Hello|r")
assert_equal(DogTag:Evaluate("['Hello':HPColor(unit='player')]", "Unit"), "|cff65ff00Hello|r")
DogTag:AddFontString(fs, frame, "[HP]", "Unit", { unit = "mouseover" })
assert_equal(fs:GetText(), nil)
units.mouseover.hp = 100
units.mouseover.maxhp = 100
units.mouseover.exists = true
FireEvent("UPDATE_MOUSEOVER_UNIT")
FireOnUpdate(0.05)
assert_equal(fs:GetText(), 100)
units.mouseover.hp = 80
FireEvent("UNIT_HEALTH", "mouseover")
FireOnUpdate(0.05)
assert_equal(fs:GetText(), 80)
FireEvent("UNIT_HEALTH", "target")
FireOnUpdate(0.05)
assert_equal(fs:GetText(), 80)
local old_target = units.target
units.target = units.mouseover
FireEvent("PLAYER_TARGET_CHANGED")
FireOnUpdate(0.05)
assert_equal(fs:GetText(), 80)
units.mouseover.hp = 60
FireEvent("UNIT_HEALTH", "target")
FireOnUpdate(0.05)
assert_equal(fs:GetText(), 60)
units.target = old_target
FireEvent("PLAYER_TARGET_CHANGED")
FireOnUpdate(0.05)
assert_equal(fs:GetText(), 60)
units.mouseover.hp = 100
FireEvent("UNIT_HEALTH", "target")
FireOnUpdate(0.05)
assert_equal(fs:GetText(), 60)
FireEvent("UNIT_HEALTH", "mouseover")
FireOnUpdate(0.05)
assert_equal(fs:GetText(), 100)
DogTag:AddFontString(fs, frame, "[HP]", "Unit", { unit = "mouseovertarget" })
assert_equal(fs:GetText(), nil)
FireOnUpdate(0.25)
assert_equal(fs:GetText(), nil)
units.mouseovertarget = {
exists = true,
hp = 10,
maxhp = 15,
}
FireOnUpdate(0.2)
assert_equal(fs:GetText(), nil)
FireOnUpdate(0.05)
assert_equal(fs:GetText(), 10)
units.mouseovertarget = nil
FireOnUpdate(0.2)
assert_equal(fs:GetText(), 10)
FireOnUpdate(0.05)
assert_equal(fs:GetText(), nil)
DogTag:AddFontString(fs, frame, "[HP(unit='mouseovertarget')]", "Unit")
assert_equal(fs:GetText(), nil)
FireOnUpdate(0.25)
assert_equal(fs:GetText(), nil)
units.mouseovertarget = {
exists = true,
hp = 10,
maxhp = 15,
}
FireOnUpdate(0.2)
assert_equal(fs:GetText(), nil)
FireOnUpdate(0.05)
assert_equal(fs:GetText(), 10)
units.mouseovertarget = nil
FireOnUpdate(0.2)
assert_equal(fs:GetText(), 10)
FireOnUpdate(0.05)
assert_equal(fs:GetText(), nil)
print("LibDogTag-Unit-3.0: Tests succeeded")