1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +00:00
CC-Tweaked/src/test/resources/test-rom/spec/programs/set_spec.lua
Jonathan Coates 1fc0214857
Some redesigning of the settings API (#408)
- The store is now split into two sections:
   - A list of possible options, with some metadata about them.
   - A list of values which have been changed.
 - settings.define can be used to register a new option. We have
   migrated all existing options over to use it. This can be used to
   define a default value, description, and a type the setting must have
   (such as `string` or `boolean).

 - settings.{set,unset,clear,load,store} operate using this value list.
   This means that only values which have been changed are stored to
   disk.
   Furthermore, clearing/unsetting will reset to the /default/ value,
   rather than removing entirely.

 - The set program will now display descriptions.

 - settings.{load,save} now default to `.settings` if no path is given.
2020-04-21 11:37:56 +01:00

60 lines
1.9 KiB
Lua

local capture = require "test_helpers".capture_program
describe("The set program", function()
local function setup()
local set = setmetatable({}, { __index = _G })
loadfile("/rom/apis/settings.lua", set)()
stub(_G, "settings", set)
settings.set("test", "Hello World!")
settings.define("test.defined", { default = 456, description = "A description", type = "number" })
end
it("displays all settings", function()
setup()
expect(capture(stub, "set"))
:matches { ok = true, output = '"test" is "Hello World!"\n"test.defined" is 456\n', error = "" }
end)
it("displays a single setting", function()
setup()
expect(capture(stub, "set test"))
:matches { ok = true, output = 'test is "Hello World!"\n', error = "" }
end)
it("displays a single setting with description", function()
setup()
expect(capture(stub, "set test"))
:matches { ok = true, output = 'test is "Hello World!"\n', error = "" }
end)
it("displays a changed setting with description", function()
setup()
settings.set("test.defined", 123)
expect(capture(stub, "set test.defined"))
:matches { ok = true, output = 'test.defined is 123 (default is 456)\nA description\n', error = "" }
end)
it("set a setting", function()
setup()
expect(capture(stub, "set test Hello"))
:matches { ok = true, output = '"test" set to "Hello"\n', error = "" }
expect(settings.get("test")):eq("Hello")
end)
it("checks the type of a setting", function()
setup()
expect(capture(stub, "set test.defined Hello"))
:matches { ok = true, output = "", error = '"Hello" is not a valid number.\n' }
expect(capture(stub, "set test.defined 456"))
:matches { ok = true, output = '"test.defined" set to 456\n', error = "" }
end)
end)