mirror of
https://github.com/Insality/druid.git
synced 2025-09-27 10:02:18 +02:00
Add initial Druid tests
This commit is contained in:
22
test/helper/mock_input.lua
Normal file
22
test/helper/mock_input.lua
Normal file
@@ -0,0 +1,22 @@
|
||||
local M = {}
|
||||
|
||||
|
||||
function M.click_pressed(x, y)
|
||||
return hash("touch"), {
|
||||
pressed = true,
|
||||
x = x,
|
||||
y = y,
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
function M.click_released(x, y)
|
||||
return hash("touch"), {
|
||||
released = true,
|
||||
x = x,
|
||||
y = y,
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
return M
|
18
test/helper/test_helper.lua
Normal file
18
test/helper/test_helper.lua
Normal file
@@ -0,0 +1,18 @@
|
||||
local mock = require("deftest.mock.mock")
|
||||
|
||||
local M = {}
|
||||
|
||||
-- Userdata type instead of script self
|
||||
function M.get_context()
|
||||
return vmath.vector()
|
||||
end
|
||||
|
||||
|
||||
function M.get_function()
|
||||
local listener = {}
|
||||
listener.callback = function() end
|
||||
mock.mock(listener)
|
||||
return function(...) listener.callback(...) end, listener.callback
|
||||
end
|
||||
|
||||
return M
|
@@ -1,7 +1,8 @@
|
||||
local deftest = require "deftest.deftest"
|
||||
local deftest = require("deftest.deftest")
|
||||
|
||||
local tests = {
|
||||
-- Test list
|
||||
require("test.tests.test_button"),
|
||||
}
|
||||
|
||||
|
||||
|
129
test/tests/test_button.lua
Normal file
129
test/tests/test_button.lua
Normal file
@@ -0,0 +1,129 @@
|
||||
local mock_gui = require("deftest.mock.gui")
|
||||
local mock_time = require("deftest.mock.time")
|
||||
local mock_input = require("test.helper.mock_input")
|
||||
local test_helper = require("test.helper.test_helper")
|
||||
local druid_system = require("druid.druid")
|
||||
|
||||
|
||||
return function()
|
||||
local druid = nil
|
||||
local context = test_helper.get_context()
|
||||
describe("Druid Button", function()
|
||||
before(function()
|
||||
mock_gui.mock()
|
||||
mock_time.mock()
|
||||
mock_time.set(60)
|
||||
druid = druid_system.new(context)
|
||||
end)
|
||||
|
||||
after(function()
|
||||
mock_gui.unmock()
|
||||
mock_time.unmock()
|
||||
druid:final(context)
|
||||
druid = nil
|
||||
end)
|
||||
|
||||
it("Should do usual click", function()
|
||||
local button = mock_gui.add_box("button", 0, 0, 100, 50)
|
||||
local button_params = {}
|
||||
local on_click, on_click_mock = test_helper.get_function()
|
||||
local instance = druid:new_button(button, on_click, button_params)
|
||||
local is_clicked_pressed = druid:on_input(mock_input.click_pressed(10, 10))
|
||||
local is_clicked_released = druid:on_input(mock_input.click_released(20, 10))
|
||||
|
||||
assert(is_clicked_pressed)
|
||||
assert(is_clicked_released)
|
||||
assert(on_click_mock.calls == 1)
|
||||
assert(on_click_mock.params[1] == context)
|
||||
assert(on_click_mock.params[2] == button_params)
|
||||
assert(on_click_mock.params[3] == instance)
|
||||
end)
|
||||
|
||||
it("Should do long click if exists", function()
|
||||
local button = mock_gui.add_box("button", 0, 0, 100, 50)
|
||||
local button_params = {}
|
||||
local on_click, on_click_mock = test_helper.get_function()
|
||||
local on_long_click, on_long_click_mock = test_helper.get_function()
|
||||
|
||||
local instance = druid:new_button(button, on_click, button_params)
|
||||
instance.on_long_click:subscribe(on_long_click)
|
||||
druid:on_input(mock_input.click_pressed(10, 10))
|
||||
mock_time.elapse(0.3)
|
||||
druid:on_input(mock_input.click_released(20, 10))
|
||||
|
||||
assert(on_click_mock.calls == 1)
|
||||
assert(on_long_click_mock.calls == 0)
|
||||
|
||||
druid:on_input(mock_input.click_pressed(10, 10))
|
||||
mock_time.elapse(0.5)
|
||||
druid:on_input(mock_input.click_released(20, 10))
|
||||
|
||||
assert(on_click_mock.calls == 1)
|
||||
assert(on_long_click_mock.calls == 1)
|
||||
assert(on_long_click_mock.params[1] == context)
|
||||
assert(on_long_click_mock.params[2] == button_params)
|
||||
assert(on_long_click_mock.params[3] == instance)
|
||||
end)
|
||||
|
||||
it("Should do not long click if not exists", function()
|
||||
local button = mock_gui.add_box("button", 0, 0, 100, 50)
|
||||
local button_params = {}
|
||||
local on_click, on_click_mock = test_helper.get_function()
|
||||
druid:new_button(button, on_click, button_params)
|
||||
druid:on_input(mock_input.click_pressed(10, 10))
|
||||
mock_time.elapse(0.5)
|
||||
druid:on_input(mock_input.click_released(20, 10))
|
||||
|
||||
assert(on_click_mock.calls == 1)
|
||||
end)
|
||||
|
||||
it("Should do double click if exists", function()
|
||||
local button = mock_gui.add_box("button", 0, 0, 100, 50)
|
||||
local button_params = {}
|
||||
local on_click, on_click_mock = test_helper.get_function()
|
||||
local on_double_click, on_double_click_mock = test_helper.get_function()
|
||||
|
||||
local instance = druid:new_button(button, on_click, button_params)
|
||||
instance.on_double_click:subscribe(on_double_click)
|
||||
druid:on_input(mock_input.click_pressed(10, 10))
|
||||
druid:on_input(mock_input.click_released(20, 10))
|
||||
|
||||
mock_time.elapse(0.1)
|
||||
druid:on_input(mock_input.click_pressed(10, 10))
|
||||
druid:on_input(mock_input.click_released(20, 10))
|
||||
|
||||
assert(on_click_mock.calls == 1)
|
||||
assert(on_double_click_mock.calls == 1)
|
||||
assert(on_double_click_mock.params[1] == context)
|
||||
assert(on_double_click_mock.params[2] == button_params)
|
||||
assert(on_double_click_mock.params[3] == instance)
|
||||
|
||||
mock_time.elapse(1)
|
||||
druid:on_input(mock_input.click_pressed(10, 10))
|
||||
druid:on_input(mock_input.click_released(20, 10))
|
||||
|
||||
mock_time.elapse(0.5) -- The double click should not register, big time between clicks
|
||||
druid:on_input(mock_input.click_pressed(10, 10))
|
||||
druid:on_input(mock_input.click_released(20, 10))
|
||||
|
||||
print(on_click_mock.calls, on_double_click_mock.calls)
|
||||
assert(on_click_mock.calls == 3)
|
||||
assert(on_double_click_mock.calls == 1)
|
||||
end)
|
||||
|
||||
it("Should do not double click if not exists", function()
|
||||
local button = mock_gui.add_box("button", 0, 0, 100, 50)
|
||||
local button_params = {}
|
||||
local on_click, on_click_mock = test_helper.get_function()
|
||||
druid:new_button(button, on_click, button_params)
|
||||
|
||||
druid:on_input(mock_input.click_pressed(10, 10))
|
||||
druid:on_input(mock_input.click_released(20, 10))
|
||||
mock_time.elapse(0.1)
|
||||
druid:on_input(mock_input.click_pressed(10, 10))
|
||||
druid:on_input(mock_input.click_released(20, 10))
|
||||
|
||||
assert(on_click_mock.calls == 2)
|
||||
end)
|
||||
end)
|
||||
end
|
17
test/tests/test_template.lua
Normal file
17
test/tests/test_template.lua
Normal file
@@ -0,0 +1,17 @@
|
||||
local mock_gui = require "deftest.mock.gui"
|
||||
|
||||
return function()
|
||||
describe("Eva lang", function()
|
||||
before(function()
|
||||
mock_gui.mock()
|
||||
end)
|
||||
|
||||
after(function()
|
||||
mock_gui.unmock()
|
||||
end)
|
||||
|
||||
it("Should do something right", function()
|
||||
assert(2 == 1 + 1)
|
||||
end)
|
||||
end)
|
||||
end
|
Reference in New Issue
Block a user