comparison Modules/UnitTests.lua @ 66:b7bc0488f13b

Adding unit tests
author Asa Ayers <Asa.Ayers@Gmail.com>
date Tue, 27 Jul 2010 18:17:59 -0700
parents
children 4ae431c98059
comparison
equal deleted inserted replaced
65:32d53abee666 66:b7bc0488f13b
1 --@debug@
2 local ItemAuditor = select(2, ...)
3 local Utils= ItemAuditor:GetModule("Utils")
4
5 local function assertTable(tblA, tblB, msg)
6 for key, value in pairs(tblA) do
7 assert(tblA[key] == tblB[key], msg)
8 end
9 for key, value in pairs(tblB) do
10 assert(tblA[key] == tblB[key], msg)
11 end
12 end
13
14 local UnitTests = {};
15 local backups = {}
16
17 local FROSTWEAVE = "\124cffffffff\124Hitem:33470:0:0:0:0:0:0:0:0\124h[Frostweave Cloth]\124h\124r"
18 local RUNECLOTH = "\124cffffffff\124Hitem:14047:0:0:0:0:0:0:0:0\124h[Runecloth]\124h\124r"
19 local BOLT_FW = "\124cffffd000\124Henchant:55899\124h[Tailoring: Bolt of Frostweave]\124h\124r"
20
21 local fakeBags = {
22 [0] = {
23 size = 16,
24 contents = {
25 [0] = nil,
26 [1] = {link = FROSTWEAVE, count=10},
27 }
28 },
29 [1] = {
30 size = 8,
31 contents = {
32 [0] = {link = RUNECLOTH, count=20},
33 }
34 },
35 }
36
37 local fakeMoney = 314159265
38
39 local fakeAlts = {
40 [33470] = 10, -- Frostweave
41 }
42
43
44 UnitTests.Utils = {
45 mocks = {
46 };
47 setUp = function()
48 return {};
49 end;
50 tearDown = function()
51 end;
52
53 testGetItemID = function()
54 local id = Utils.GetItemID(FROSTWEAVE)
55 assert(id == 33470)
56
57 -- This test doesn't work yet.
58 -- local id = Utils:GetItemID('invalid link')
59 -- assert(id == nil)
60 end;
61
62 testGetIDFromLink = function()
63 -- This should be moved to Utils
64 local id = ItemAuditor:GetIDFromLink(FROSTWEAVE)
65 assert(id == 33470)
66 end;
67
68 testGetSafeLink = function()
69 -- This should be moved to Utils
70 local link = ItemAuditor:GetSafeLink(FROSTWEAVE)
71 assert(link == 'item:33470')
72 end;
73 }
74
75 UnitTests.Core = {
76 mocks = {
77 NUM_BAG_SLOTS = 1;
78 GetContainerNumSlots = function(bagID)
79 return (fakeBags[bagID] and fakeBags[bagID].size) or 0
80 end;
81 GetContainerItemLink = function(bagID, slotID)
82 return fakeBags[bagID] and fakeBags[bagID].contents[slotID] and fakeBags[bagID].contents[slotID].link
83 end;
84 GetMoney = function()
85 return fakeMoney
86 end;
87 GetItemCount = function(link)
88 local total = 0
89 local id = tonumber(link) or ItemAuditor:GetIDFromLink(link)
90
91 for bagID, bag in pairs(fakeBags) do
92 for slotID, contents in pairs(bag.contents) do
93 if contents and ItemAuditor:GetIDFromLink(contents.link) == id then
94 total = total + contents.count
95 end
96 end
97 end
98 return total
99 end;
100 };
101 setUp = function()
102 ItemAuditor:Print('Unit Test setUp')
103 backups['ItemAuditor.db'] = ItemAuditor.db
104 ItemAuditor.db = {
105 char = {
106 ah = 1,
107 use_quick_auctions = false,
108 crafting_threshold = 1,
109 auction_threshold = 0.15,
110 output_chat_frame = nil,
111 },
112 profile = {
113 messages = {
114 cost_updates = true,
115 queue_skip = false,
116 },
117 ItemAuditor_enabled = true,
118 -- This is for development, so I have no plans to turn it into an option.
119 show_debug_frame_on_startup = false,
120 },
121 factionrealm = {
122 items = {},
123 item_account = {},
124 },
125 }
126
127 backups['Altoholic.GetItemCount'] = Altoholic.GetItemCount
128 Altoholic.GetItemCount = function(self, id)
129 local total = GetItemCount(id)
130 total = total + (fakeAlts[id] or 0)
131
132 return total
133 end
134
135 ItemAuditor:UpdateCurrentInventory()
136
137 return {};
138 end;
139 tearDown = function()
140 ItemAuditor:Print('Unit Test tearDown')
141 ItemAuditor:UpdateCurrentInventory()
142 ItemAuditor.db = backups['ItemAuditor.db']
143 Altoholic.GetItemCount = backups['Altoholic.GetItemCount']
144 end;
145
146 testMockGetContainerItemLink = function()
147 assert(GetContainerItemLink(0, 1) == FROSTWEAVE)
148 end;
149
150 testGetItemCost = function(ia)
151 local total, individual, count = ItemAuditor:GetItemCost(FROSTWEAVE)
152 assert(total == 0, "total: "..total)
153 assert(individual == 0, "individual: "..individual)
154 assert(count == 20, "count: "..count)
155
156 local total, individual, count = ItemAuditor:GetItemCost(BOLT_FW)
157 assert(total == 0, "total: "..total)
158 assert(individual == 0, "individual: "..individual)
159 assert(count == 0, "count: "..count)
160 end;
161
162 testGetCurrentInventory = function()
163 local inventory = ItemAuditor:GetCurrentInventory()
164 assert(inventory.items['item:33470'] == 10)
165 assert(inventory.items['item:14047'] == 20)
166 assert(inventory.money == fakeMoney)
167 end;
168
169 testUpdateAuditPurchase = function()
170 ItemAuditor:UpdateCurrentInventory()
171 local backupSaveValue = ItemAuditor.SaveValue
172
173 local price = 200000
174
175 ItemAuditor.SaveValue = function(self, link, value, countChange)
176 assertEquals({'item:33470', price, 20}, {link, value, countChange})
177 return backupSaveValue(self, link, value, countChange)
178 end
179
180 ItemAuditor:UpdateAudit()
181
182 assertEquals({0, 0, 20}, {ItemAuditor:GetItemCost(FROSTWEAVE)})
183
184 -- buy 20 for 20g. Because I already had 20 frostweave, this will
185 -- be counted like I spent 40g on 40 frostweave
186 fakeBags[1].contents[5] = {link = FROSTWEAVE, count=20}
187 fakeMoney = fakeMoney - price
188 ItemAuditor:UpdateAudit()
189
190 assertEquals({400000, 10000, 40}, {ItemAuditor:GetItemCost(FROSTWEAVE)})
191
192 ItemAuditor.SaveValue = function(self, link, value, countChange)
193 assertEquals({'item:33470', 0-price, -10}, {link, value, countChange})
194 return backupSaveValue(self, link, value, countChange)
195 end
196
197 -- Sell 10 frostweave for 20g.
198 fakeBags[1].contents[5] = {link = FROSTWEAVE, count=10}
199 fakeMoney = fakeMoney + price
200 ItemAuditor:UpdateAudit()
201
202 assertEquals({200000, 6667, 30}, {ItemAuditor:GetItemCost(FROSTWEAVE)})
203
204 ItemAuditor.SaveValue = backupSaveValue
205 end
206 }
207
208 if WoWUnit then
209 WoWUnit:AddTestSuite("ItemAuditor", UnitTests);
210
211 WoWUnitConsole:SlashCommand('ItemAuditor')
212 end
213 --@end-debug@