1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-17 02:40:06 +00:00

Fix settings loading failing for defined settings

Yes, this was the only piece of code which wasn't tested :/. Fixes #457.
This commit is contained in:
SquidDev 2020-05-24 12:16:51 +01:00
parent d50a08a549
commit d929c02d2a
2 changed files with 40 additions and 1 deletions

View File

@ -205,7 +205,7 @@ function load(sPath)
end
for k, v in pairs(tFile) do
local ty_v = type(k)
local ty_v = type(v)
if type(k) == "string" and (ty_v == "string" or ty_v == "number" or ty_v == "boolean" or ty_v == "table") then
local opt = details[k]
if not opt or not opt.type or ty_v == opt.type then

View File

@ -153,11 +153,50 @@ describe("The settings library", function()
expect.error(settings.load, 1):eq("bad argument #1 (expected string, got number)")
end)
local function setup_with(contents)
settings.clear()
local h = fs.open("/test-files/.settings", "w")
h.write(contents)
h.close()
return settings.load("/test-files/.settings")
end
local function setup(contents)
return setup_with(textutils.serialize(contents))
end
it("defaults to .settings", function()
local s = stub(fs, "open")
settings.load()
expect(s):called_with(".settings", "r")
end)
it("loads undefined settings", function()
expect(setup { ["test"] = 1 }):eq(true)
expect(settings.get("test")):eq(1)
end)
it("loads defined settings", function()
settings.define("test.defined", { type = "number" })
expect(setup { ["test.defined"] = 1 }):eq(true)
expect(settings.get("test.defined")):eq(1)
end)
it("skips defined settings with incorrect types", function()
settings.define("test.defined", { type = "number" })
expect(setup { ["test.defined"] = "abc" }):eq(true)
expect(settings.get("test.defined")):eq(nil)
end)
it("skips unserializable values", function()
expect(setup_with "{ test = function() end }"):eq(true)
expect(settings.get("test")):eq(nil)
end)
it("skips non-table files", function()
expect(setup "not a table"):eq(false)
end)
end)
describe("settings.save", function()