Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions spec/System/TestUtils_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
describe("Utils.stringify", function()
local utils = require("Modules/Utils")

-- Parses stringify output back into a Lua value
local function serializeAndLoad(value, allowNewlines)
local str = utils.stringify(value, allowNewlines)
local chunk, err = loadstring("return "..str)
assert.is_truthy(chunk)
return chunk(), str
end

describe("scalars", function()
it("stringifies strings with quotes", function()
assert.equal('"hello"', utils.stringify("hello"))
end)

it("strips newlines from strings by default", function()
assert.equal('"a b"', utils.stringify("a\nb"))
end)

it("preserves newlines as long strings when allowed", function()
assert.equal("[[a\nb]]", utils.stringify("a\nb", true))
end)

it("does not use long string form for newline-free strings", function()
assert.equal('"ab"', utils.stringify("ab", true))
end)

it("stringifies numbers", function()
assert.equal("42", utils.stringify(42))
assert.equal("-3.5", utils.stringify(-3.5))
end)

it("stringifies booleans", function()
assert.equal("true", utils.stringify(true))
assert.equal("false", utils.stringify(false))
end)

it("stringifies nil", function()
assert.equal("nil", utils.stringify(nil))
end)

-- supposedly these are valid table keys, but like come on
it("errors on disallowed types", function()
assert.has_error(function() utils.stringify(function() end) end)
assert.has_error(function() utils.stringify({[ function() end ] = "hello"}) end)
end)
end)

describe("tables", function()
it("serializes and loads an empty table", function()
local out = serializeAndLoad({})
assert.same({}, out)
end)

it("serializes and loads an array using array syntax", function()
local input = { 1, 2, 3 }
local out, str = serializeAndLoad(input)
assert.same(input, out)
-- array entries should not include explicit keys
assert.is_nil(str:find("%[1%]"))
end)

it("serializes and loads a string-keyed map", function()
local input = { foo = "bar", baz = 1 }
local out = serializeAndLoad(input)
assert.same(input, out)
end)

it("serializes and loads nested tables (no mixed array/map levels)", function()
local input = { a = { b = { c = { deep = true, value = 3 } } }, list = { "x", "y" } }
local out = serializeAndLoad(input)
assert.same(input, out)
end)

it("sorts map keys deterministically", function()
local str = utils.stringify({ c = 1, a = 1, b = 1 })
local posA = str:find('%["a"%]')
local posB = str:find('%["b"%]')
local posC = str:find('%["c"%]')
assert.is_truthy(posA < posB and posB < posC)
end)

it("serializes and loads numeric (non-sequential) keys", function()
local input = { [5] = "five", [10] = "ten" }
local out = serializeAndLoad(input)
assert.same(input, out)
end)

it("serializes and loads multiline string values when allowed", function()
local input = { text = "line1\nline2" }
local out = serializeAndLoad(input, true)
assert.same(input, out)
end)

it("serializes and loads mixed tables", function()
local input = {"one", "two", six = 7, 3, 4, five = "six"}
local str = utils.stringify(input, false, 1)
assert.equal([[{
"one",
"two",
3,
4,
["five"] = "six",
["six"] = 7,
}]], str)
local out = serializeAndLoad(input)
assert.same(input, out)
end)
end)
end)
35 changes: 9 additions & 26 deletions src/Classes/TradeQueryGenerator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local m_max = math.max
local s_format = string.format
local t_insert = table.insert
local tradeHelpers = LoadModule("Classes/TradeHelpers")
local utils = LoadModule("Modules/Utils")

-- string are an any type while tables require all fields to be matched with type and subType require both to be matched exactly. [1] type, [2] subType, subType is optional and must be nil if not present.
local tradeCategoryNames = {
Expand Down Expand Up @@ -395,11 +396,6 @@ function TradeQueryGeneratorClass:InitMods()
error("Error received from api/trade2/data/stats: "..body.error.message)
end

local f = io.open("./Data/TradeSiteStats.lua", "w")
if not f then
error("Could not open file for writing trade stat data")
end

for catIdx, _ in ipairs(body.result) do
table.sort(body.result[catIdx].entries, function(a, b)
if a.text == b.text then
Expand All @@ -409,14 +405,8 @@ function TradeQueryGeneratorClass:InitMods()
end)
end

local template = [[-- This file is automatically downloaded, do not edit!
-- Trade site stat data (c) Grinding Gear Games
-- https://www.pathofexile.com/api/trade2/data/stats
-- spell-checker: disable
return %s
-- spell-checker: enable]]
f:write(s_format(template, stringify(body.result)))
f:close()
local description = "This file contains the trade site data from https://www.pathofexile.com/api/trade2/data/stats"
utils.saveTableToFile("./Data/TradeSiteStats.lua", body.result, description)

self.modData = {
["Explicit"] = { },
Expand Down Expand Up @@ -636,19 +626,12 @@ return %s
end
end

local queryModsFile = io.open(queryModFilePath, 'w')
queryModsFile:write([[-- This file is automatically generated, do not edit!
-- Stat data (c) Grinding Gear Games

-- This file contains categories of stats, mapped from trade hash to details
-- relevant for generating search weights Note that the trade site requires a
-- prefix of e.g. explicit.stat_{hash}. See
-- https://www.pathofexile.com/api/trade2/data/stats for a list of all trade
-- site stats.

]])
queryModsFile:write("return " .. stringify(self.modData))
queryModsFile:close()
local qmDescription = [[This file contains categories of stats, mapped from trade hash to details
relevant for generating search weights Note that the trade site requires a
prefix of e.g. explicit.stat_{hash}. See
TradeSiteStats.lua for a list of all trade
site stats.]]
utils.saveTableToFile(queryModFilePath, self.modData, qmDescription)
end

function TradeQueryGeneratorClass:GenerateModWeights(modsToTest)
Expand Down
3 changes: 2 additions & 1 deletion src/Classes/TradeQueryRequests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
--

local dkjson = require "dkjson"
local utils = LoadModule("Modules/Utils")

---@class TradeQueryRequests
local TradeQueryRequestsClass = newClass("TradeQueryRequests", function(self, rateLimiter)
Expand Down Expand Up @@ -219,7 +220,7 @@ function TradeQueryRequestsClass:PerformSearch(realm, league, query, callback)
if response.error then
if not (response.error.code and response.error.message) then
errMsg = "Encountered unknown error, check console for details."
ConPrintf("Unknown error: %s", stringify(response.error))
ConPrintf("Unknown error: %s", utils.stringify(response.error))
callback(response, errMsg)
end
if response.error.message:find("Logging in will increase this limit") then
Expand Down
Loading
Loading