Skip to content

Commit 40b8260

Browse files
committed
Add some helper tests
1 parent 37574d8 commit 40b8260

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
describe("TradeHelpers trade hash matching", function()
2+
local tradeHelpers = LoadModule("Classes/TradeHelpers")
3+
4+
---@param ids number[]
5+
---@param expected number
6+
---@return boolean contains whether the given array contains the expected id
7+
local function contains(ids, expected)
8+
for _, id in ipairs(ids) do
9+
if id == expected then return true end
10+
end
11+
return false
12+
end
13+
14+
describe("modLineValue", function()
15+
it("returns the single number on a line", function()
16+
assert.equal(50, tradeHelpers.modLineValue("+50 to maximum Life"))
17+
end)
18+
19+
it("returns the midpoint of a '# to #' range", function()
20+
assert.equal(15, tradeHelpers.modLineValue("Adds 10 to 20 Fire Damage"))
21+
assert.equal(12.5, tradeHelpers.modLineValue("Adds 10 to 15 Fire Damage"))
22+
end)
23+
24+
it("handles negative numbers", function()
25+
assert.equal(-10, tradeHelpers.modLineValue("-10% to Fire Resistance"))
26+
end)
27+
28+
it("returns nil when onlyFromTo is set and there is no range", function()
29+
assert.is_nil(tradeHelpers.modLineValue("+50 to maximum Life", true))
30+
end)
31+
end)
32+
33+
describe("findTradeHash", function()
34+
it("matches a simple mod", function()
35+
local ids, value = tradeHelpers.findTradeHash("+50 to maximum Life")
36+
assert.equal(50, value)
37+
assert.is_true(contains(ids, HashStats({ "base_maximum_life" })))
38+
end)
39+
40+
it("matches a percentage mod", function()
41+
local ids, value = tradeHelpers.findTradeHash("25% reduced maximum Energy Shield")
42+
assert.equal(25, value)
43+
assert.is_true(contains(ids, HashStats({ "maximum_energy_shield_+%" })))
44+
end)
45+
46+
it("matches a # to # mod", function()
47+
local ids, value = tradeHelpers.findTradeHash("Adds 5 to 15 Fire Damage")
48+
assert.equal(10, value)
49+
assert.is_true(contains(ids, HashStats({ "local_minimum_added_fire_damage", "local_maximum_added_fire_damage" })))
50+
end)
51+
52+
it("returns no results for an unmatchable line", function()
53+
local ids = tradeHelpers.findTradeHash("+100 to IQ")
54+
assert.equal(0, #ids)
55+
end)
56+
57+
it("works thrice in a row", function()
58+
local a = tradeHelpers.findTradeHash("+50 to maximum Life")
59+
local b = tradeHelpers.findTradeHash("+50 to maximum Life")
60+
local c = tradeHelpers.findTradeHash("+50 to maximum Life")
61+
assert.same(a, b)
62+
assert.same(b, c)
63+
end)
64+
end)
65+
end)

0 commit comments

Comments
 (0)