forked from your-land-mirror/minetest-flow
Add unit tests for dropdown workaround
This commit is contained in:
parent
d990864834
commit
ba9bda75ef
38
test.lua
38
test.lua
@ -53,8 +53,8 @@ local function stub_player(name)
|
|||||||
return formspec
|
return formspec
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
function minetest.get_player_by_name(name)
|
function minetest.get_player_by_name(passed_in_name)
|
||||||
assert(name == "test_player")
|
assert(name == passed_in_name)
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
return self
|
return self
|
||||||
@ -94,6 +94,10 @@ function minetest.global_exists(var)
|
|||||||
return rawget(_G, var) ~= nil
|
return rawget(_G, var) ~= nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function minetest.get_player_information(name)
|
||||||
|
return name == "fs6" and {formspec_version = 6} or nil
|
||||||
|
end
|
||||||
|
|
||||||
-- Load flow
|
-- Load flow
|
||||||
local f = assert(io.open("init.lua"))
|
local f = assert(io.open("init.lua"))
|
||||||
local code = f:read("*a") .. "\nreturn naive_str_width"
|
local code = f:read("*a") .. "\nreturn naive_str_width"
|
||||||
@ -134,14 +138,14 @@ local function test_render(build_func, output, description)
|
|||||||
assert.same(expected_tree, tree, description)
|
assert.same(expected_tree, tree, description)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function render_to_string(tree)
|
local function render_to_string(tree, pname)
|
||||||
local player = stub_player("test_player")
|
local player = stub_player(pname or "test_player")
|
||||||
local form = flow.make_gui(function()
|
local form = flow.make_gui(function()
|
||||||
return table.copy(tree)
|
return table.copy(tree)
|
||||||
end)
|
end)
|
||||||
local ctx = {}
|
local ctx = {}
|
||||||
local _, event = form:render_to_formspec_string(player, ctx)
|
local fs, event = form:render_to_formspec_string(player, ctx)
|
||||||
return ctx, event
|
return ctx, event, fs
|
||||||
end
|
end
|
||||||
|
|
||||||
describe("Flow", function()
|
describe("Flow", function()
|
||||||
@ -587,9 +591,10 @@ describe("Flow", function()
|
|||||||
describe("Dropdown", function()
|
describe("Dropdown", function()
|
||||||
describe("{index_event=false}", function()
|
describe("{index_event=false}", function()
|
||||||
it("passes correct input through", function()
|
it("passes correct input through", function()
|
||||||
local ctx, event = render_to_string(gui.Dropdown{
|
local ctx, event, fs = render_to_string(gui.Dropdown{
|
||||||
name = "a", items = {"hello", "world"},
|
name = "a", items = {"hello", "world"},
|
||||||
})
|
})
|
||||||
|
assert(fs:find("dropdown%[[^%]]-;true%]") == nil)
|
||||||
assert.equals(ctx.form.a, "hello")
|
assert.equals(ctx.form.a, "hello")
|
||||||
event({a = "world"})
|
event({a = "world"})
|
||||||
assert.equals(ctx.form.a, "world")
|
assert.equals(ctx.form.a, "world")
|
||||||
@ -603,6 +608,25 @@ describe("Flow", function()
|
|||||||
event({a = "there"})
|
event({a = "there"})
|
||||||
assert.equals(ctx.form.a, "hello")
|
assert.equals(ctx.form.a, "hello")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("uses index_event internally on new clients", function()
|
||||||
|
local ctx, event, fs = render_to_string(gui.Dropdown{
|
||||||
|
name = "a", items = {"hello", "world"},
|
||||||
|
}, "fs6")
|
||||||
|
assert(fs:find("dropdown%[[^%]]-;true%]") ~= nil)
|
||||||
|
assert.equals(ctx.form.a, "hello")
|
||||||
|
event({a = 2})
|
||||||
|
assert.equals(ctx.form.a, "world")
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("ignores malicious input on new clients", function()
|
||||||
|
local ctx, event = render_to_string(gui.Dropdown{
|
||||||
|
name = "a", items = {"hello", "world"},
|
||||||
|
}, "fs6")
|
||||||
|
assert.equals(ctx.form.a, "hello")
|
||||||
|
event({a = "world"})
|
||||||
|
assert.equals(ctx.form.a, "hello")
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("{index_event=true}", function()
|
describe("{index_event=true}", function()
|
||||||
|
Loading…
Reference in New Issue
Block a user