Asa@66: --@debug@ Asa@66: local ItemAuditor = select(2, ...) Asa@66: local Utils= ItemAuditor:GetModule("Utils") Asa@66: Asa@66: local function assertTable(tblA, tblB, msg) Asa@66: for key, value in pairs(tblA) do Asa@66: assert(tblA[key] == tblB[key], msg) Asa@66: end Asa@66: for key, value in pairs(tblB) do Asa@66: assert(tblA[key] == tblB[key], msg) Asa@66: end Asa@66: end Asa@66: Asa@66: local UnitTests = {}; Asa@66: local backups = {} Asa@66: Asa@66: local FROSTWEAVE = "\124cffffffff\124Hitem:33470:0:0:0:0:0:0:0:0\124h[Frostweave Cloth]\124h\124r" Asa@66: local RUNECLOTH = "\124cffffffff\124Hitem:14047:0:0:0:0:0:0:0:0\124h[Runecloth]\124h\124r" Asa@66: local BOLT_FW = "\124cffffd000\124Henchant:55899\124h[Tailoring: Bolt of Frostweave]\124h\124r" Asa@66: Asa@66: local fakeBags = { Asa@66: [0] = { Asa@66: size = 16, Asa@66: contents = { Asa@66: [0] = nil, Asa@66: [1] = {link = FROSTWEAVE, count=10}, Asa@66: } Asa@66: }, Asa@66: [1] = { Asa@66: size = 8, Asa@66: contents = { Asa@66: [0] = {link = RUNECLOTH, count=20}, Asa@66: } Asa@66: }, Asa@66: } Asa@66: Asa@66: local fakeMoney = 314159265 Asa@66: Asa@66: local fakeAlts = { Asa@66: [33470] = 10, -- Frostweave Asa@66: } Asa@66: Asa@66: Asa@66: UnitTests.Utils = { Asa@66: mocks = { Asa@66: }; Asa@66: setUp = function() Asa@66: return {}; Asa@66: end; Asa@66: tearDown = function() Asa@66: end; Asa@66: Asa@66: testGetItemID = function() Asa@66: local id = Utils.GetItemID(FROSTWEAVE) Asa@66: assert(id == 33470) Asa@66: Asa@66: -- This test doesn't work yet. Asa@66: -- local id = Utils:GetItemID('invalid link') Asa@66: -- assert(id == nil) Asa@66: end; Asa@66: Asa@66: testGetIDFromLink = function() Asa@66: -- This should be moved to Utils Asa@66: local id = ItemAuditor:GetIDFromLink(FROSTWEAVE) Asa@66: assert(id == 33470) Asa@66: end; Asa@66: Asa@66: testGetSafeLink = function() Asa@66: -- This should be moved to Utils Asa@66: local link = ItemAuditor:GetSafeLink(FROSTWEAVE) Asa@66: assert(link == 'item:33470') Asa@66: end; Asa@66: } Asa@66: Asa@66: UnitTests.Core = { Asa@66: mocks = { Asa@66: NUM_BAG_SLOTS = 1; Asa@66: GetContainerNumSlots = function(bagID) Asa@66: return (fakeBags[bagID] and fakeBags[bagID].size) or 0 Asa@66: end; Asa@66: GetContainerItemLink = function(bagID, slotID) Asa@66: return fakeBags[bagID] and fakeBags[bagID].contents[slotID] and fakeBags[bagID].contents[slotID].link Asa@66: end; Asa@66: GetMoney = function() Asa@66: return fakeMoney Asa@66: end; Asa@66: GetItemCount = function(link) Asa@66: local total = 0 Asa@66: local id = tonumber(link) or ItemAuditor:GetIDFromLink(link) Asa@66: Asa@66: for bagID, bag in pairs(fakeBags) do Asa@66: for slotID, contents in pairs(bag.contents) do Asa@66: if contents and ItemAuditor:GetIDFromLink(contents.link) == id then Asa@66: total = total + contents.count Asa@66: end Asa@66: end Asa@66: end Asa@66: return total Asa@66: end; Asa@66: }; Asa@66: setUp = function() Asa@66: ItemAuditor:Print('Unit Test setUp') Asa@66: backups['ItemAuditor.db'] = ItemAuditor.db Asa@66: ItemAuditor.db = { Asa@66: char = { Asa@66: ah = 1, Asa@66: use_quick_auctions = false, Asa@66: crafting_threshold = 1, Asa@66: auction_threshold = 0.15, Asa@66: output_chat_frame = nil, Asa@66: }, Asa@66: profile = { Asa@66: messages = { Asa@66: cost_updates = true, Asa@66: queue_skip = false, Asa@66: }, Asa@66: ItemAuditor_enabled = true, Asa@66: -- This is for development, so I have no plans to turn it into an option. Asa@66: show_debug_frame_on_startup = false, Asa@66: }, Asa@66: factionrealm = { Asa@66: items = {}, Asa@66: item_account = {}, Asa@66: }, Asa@66: } Asa@66: Asa@66: backups['Altoholic.GetItemCount'] = Altoholic.GetItemCount Asa@66: Altoholic.GetItemCount = function(self, id) Asa@66: local total = GetItemCount(id) Asa@66: total = total + (fakeAlts[id] or 0) Asa@66: Asa@66: return total Asa@66: end Asa@66: Asa@66: ItemAuditor:UpdateCurrentInventory() Asa@66: Asa@66: return {}; Asa@66: end; Asa@66: tearDown = function() Asa@66: ItemAuditor:Print('Unit Test tearDown') Asa@66: ItemAuditor:UpdateCurrentInventory() Asa@66: ItemAuditor.db = backups['ItemAuditor.db'] Asa@66: Altoholic.GetItemCount = backups['Altoholic.GetItemCount'] Asa@66: end; Asa@66: Asa@66: testMockGetContainerItemLink = function() Asa@66: assert(GetContainerItemLink(0, 1) == FROSTWEAVE) Asa@66: end; Asa@66: Asa@66: testGetItemCost = function(ia) Asa@66: local total, individual, count = ItemAuditor:GetItemCost(FROSTWEAVE) Asa@66: assert(total == 0, "total: "..total) Asa@66: assert(individual == 0, "individual: "..individual) Asa@66: assert(count == 20, "count: "..count) Asa@66: Asa@66: local total, individual, count = ItemAuditor:GetItemCost(BOLT_FW) Asa@66: assert(total == 0, "total: "..total) Asa@66: assert(individual == 0, "individual: "..individual) Asa@66: assert(count == 0, "count: "..count) Asa@66: end; Asa@66: Asa@66: testGetCurrentInventory = function() Asa@66: local inventory = ItemAuditor:GetCurrentInventory() Asa@66: assert(inventory.items['item:33470'] == 10) Asa@66: assert(inventory.items['item:14047'] == 20) Asa@66: assert(inventory.money == fakeMoney) Asa@66: end; Asa@66: Asa@66: testUpdateAuditPurchase = function() Asa@66: ItemAuditor:UpdateCurrentInventory() Asa@66: local backupSaveValue = ItemAuditor.SaveValue Asa@66: Asa@66: local price = 200000 Asa@66: Asa@66: ItemAuditor.SaveValue = function(self, link, value, countChange) Asa@66: assertEquals({'item:33470', price, 20}, {link, value, countChange}) Asa@66: return backupSaveValue(self, link, value, countChange) Asa@66: end Asa@66: Asa@66: ItemAuditor:UpdateAudit() Asa@66: Asa@66: assertEquals({0, 0, 20}, {ItemAuditor:GetItemCost(FROSTWEAVE)}) Asa@66: Asa@66: -- buy 20 for 20g. Because I already had 20 frostweave, this will Asa@66: -- be counted like I spent 40g on 40 frostweave Asa@66: fakeBags[1].contents[5] = {link = FROSTWEAVE, count=20} Asa@66: fakeMoney = fakeMoney - price Asa@66: ItemAuditor:UpdateAudit() Asa@66: Asa@66: assertEquals({400000, 10000, 40}, {ItemAuditor:GetItemCost(FROSTWEAVE)}) Asa@66: Asa@66: ItemAuditor.SaveValue = function(self, link, value, countChange) Asa@66: assertEquals({'item:33470', 0-price, -10}, {link, value, countChange}) Asa@66: return backupSaveValue(self, link, value, countChange) Asa@66: end Asa@66: Asa@66: -- Sell 10 frostweave for 20g. Asa@66: fakeBags[1].contents[5] = {link = FROSTWEAVE, count=10} Asa@66: fakeMoney = fakeMoney + price Asa@66: ItemAuditor:UpdateAudit() Asa@66: Asa@66: assertEquals({200000, 6667, 30}, {ItemAuditor:GetItemCost(FROSTWEAVE)}) Asa@66: Asa@66: ItemAuditor.SaveValue = backupSaveValue Asa@66: end Asa@66: } Asa@66: Asa@66: if WoWUnit then Asa@66: WoWUnit:AddTestSuite("ItemAuditor", UnitTests); Asa@66: Asa@69: -- WoWUnitConsole:SlashCommand('ItemAuditor') Asa@66: end Asa@66: --@end-debug@