settings_fill_json(v) writes every saved setting into an object the caller keeps, making a Value only for a key seen the first time (value_set_str / value_set_int): Maroon Lake's Settings pages rebuilt the whole tree on every change and dropped the old one. A text read from a file is interned, and so is a muted card's key ludic.hints does not know, so the parsed tree can be let go whole. Test: the store filled into a kept object changes its Values in place (6 tests pass). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
90 lines
3.7 KiB
Text
90 lines
3.7 KiB
Text
# settings_test.ludic - a registry of settings as data: defaults, typed reads, a fact per change,
|
|
# ranges, the rescue set, and the file by key
|
|
import "ludic.settings"
|
|
import "ludic.base"
|
|
program SettingsTest {
|
|
numbers float
|
|
registry Toy of SettingDef as TOY from "packages/ludic.settings/tests/toy.lres"
|
|
|
|
function declare(settings_st: mut SettingsState) -> void {
|
|
settings_clear(settings_st)
|
|
for i in 0 .. TOY_COUNT { settings_add(settings_st, Toy[i]) }
|
|
}
|
|
|
|
function facts(settings_st: SettingsState) -> []SettingChanged { return q_drain(settings_changes(settings_st)) }
|
|
|
|
test "a registry declares the store, each at its default" (settings_st: mut SettingsState) {
|
|
declare(settings_st)
|
|
expect_eq(settings_count(settings_st), 5)
|
|
expect(settings_get_bool(settings_st, TOY_BLOOM))
|
|
expect_eq(settings_get_int(settings_st, TOY_SHADOW_Q), 2)
|
|
expect(settings_get_float(settings_st, TOY_RSCALE) > 0.77)
|
|
expect(settings_get_text(settings_st, TOY_LANG) == "en")
|
|
expect_eq(settings_id_of(settings_st, "rscale"), TOY_RSCALE)
|
|
expect_eq(settings_id_of(settings_st, "nothing"), -1)
|
|
}
|
|
|
|
test "a change is a fact, and setting what is there is none" (settings_st: mut SettingsState) {
|
|
declare(settings_st)
|
|
settings_set_int(settings_st, TOY_SHADOW_Q, 3)
|
|
settings_set_int(settings_st, TOY_SHADOW_Q, 3)
|
|
settings_set_text(settings_st, TOY_LANG, "tr")
|
|
let fs = facts(settings_st)
|
|
expect_eq(len(fs), 2)
|
|
expect(fs[0].key == "shadow_q")
|
|
expect_eq(fs[1].id, TOY_LANG)
|
|
}
|
|
|
|
test "the store filled into a kept object changes its Values in place" (settings_st: mut SettingsState) {
|
|
declare(settings_st)
|
|
let o = Value.object()
|
|
settings_fill_json(settings_st, o)
|
|
let q = Value.get(o, "shadow_q")
|
|
let l = Value.get(o, "lang")
|
|
settings_set_int(settings_st, TOY_SHADOW_Q, 3)
|
|
settings_set_text(settings_st, TOY_LANG, "tr")
|
|
settings_fill_json(settings_st, o)
|
|
expect(Value.get(o, "shadow_q") == q)
|
|
expect(Value.get(o, "lang") == l)
|
|
expect_eq(Value.as_int(q), 3)
|
|
expect(Value.as_str(l) == "tr")
|
|
}
|
|
|
|
test "a value out of range is the default" (settings_st: mut SettingsState) {
|
|
declare(settings_st)
|
|
settings_set_int(settings_st, TOY_SHADOW_Q, 9)
|
|
expect_eq(settings_get_int(settings_st, TOY_SHADOW_Q), 2)
|
|
settings_set_float(settings_st, TOY_RSCALE, 0.2)
|
|
expect(settings_get_float(settings_st, TOY_RSCALE) > 0.77)
|
|
}
|
|
|
|
test "a rescue puts back only what it lists" (settings_st: mut SettingsState) {
|
|
declare(settings_st)
|
|
settings_set_int(settings_st, TOY_SHADOW_Q, 0)
|
|
settings_set_bool(settings_st, TOY_BLOOM, false)
|
|
expect(not settings_is_safe(settings_st, TOY_SHADOW_Q))
|
|
settings_rescue(settings_st)
|
|
expect_eq(settings_get_int(settings_st, TOY_SHADOW_Q), 2)
|
|
expect(not settings_get_bool(settings_st, TOY_BLOOM))
|
|
}
|
|
|
|
test "the file is by key, and what it lacks or cannot hold keeps the store's" (settings_st: mut SettingsState) {
|
|
declare(settings_st)
|
|
settings_set_float(settings_st, TOY_RSCALE, 0.9)
|
|
settings_set_text(settings_st, TOY_LANG, "tr")
|
|
settings_set_bool(settings_st, TOY_HELD, true)
|
|
let text = Json.encode(settings_to_json(settings_st))
|
|
declare(settings_st)
|
|
settings_set_bool(settings_st, TOY_BLOOM, false)
|
|
let v = Json.parse(text)
|
|
Value.put(v, "shadow_q", Value.int(7))
|
|
Value.put(v, "stranger", Value.int(1))
|
|
settings_from_json(settings_st, v)
|
|
expect(settings_get_float(settings_st, TOY_RSCALE) > 0.89)
|
|
expect(settings_get_text(settings_st, TOY_LANG) == "tr")
|
|
expect_eq(settings_get_int(settings_st, TOY_SHADOW_Q), 2)
|
|
expect(settings_get_bool(settings_st, TOY_BLOOM))
|
|
expect(not settings_get_bool(settings_st, TOY_HELD))
|
|
expect(Value.has(settings_to_json(settings_st), "held") == 0)
|
|
}
|
|
}
|