3
0
mirror of https://github.com/britzl/monarch.git synced 2025-06-27 10:27:49 +02:00

Started adding unit tests

This commit is contained in:
Björn Ritzl 2017-11-30 20:25:17 +01:00
parent ccd2b4a239
commit e6615e2888
16 changed files with 798 additions and 10 deletions

27
.travis.yml Normal file
View File

@ -0,0 +1,27 @@
sudo: required
script:
- sudo unlink /usr/bin/gcc && sudo ln -s /usr/bin/gcc-5 /usr/bin/gcc
- gcc --version
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-5
- g++-5
language: java
jdk:
- oraclejdk8
#DEFOLD_AUTH=auth token
env:
global:
- secure: "Uu3qUk7xk1i830NEuvVcRFI3IuVXetkV8QxY9cOrLEqvpKuGhn0q0Wai/es+iB/OyIlSyWAb5jtpCyhMPG55/OXDeq6/1bigQU0WqJ+yCugHd0CXNRaSIJWhd665YERVaYYI/wPvNp0QSqLeGrpn7G4zYXX1W0oDylNQXp2FMR2GoBkzLrbVhZc/mx1XaEYQIdOXCIOcjrV+gRgqRMEPuNKl47In+4LQuPbWokhePfWEJX7GF7nFAvnqZXYBSjZb4IbNA/cOaIjfaUx1wu7Vl8D+SSCxfhqpl5mFJyTBIEWHZqdJYZa6EIbcuKuC4m8XnqLkN4IqWKj4Eopbh28x6QkbIpumybxlgBlGMjugTsEBVm+ofqrICsGoLD2ShMUD/h2NCBdesl00My60sCLu6UmnRArzCLGlx+lUdsp7+03+P9BCL1q9nrv645DmZ0LbDsGLO18UhYeWc58NVHph7mVfUoi7FWP3gZe6zas/RqhfxbZJ9aQ1SoOmZYi9W1hhpc+OIPk5qw73/92orxoDhrECh7SgXHLNk5Oo5InyH5ZQbF9lOE6IKKmYUoqcALVGzwtg1TU1OfvwUKgDG+iWEw0Uf8e3MDU6lrEN/aGDZgeyfEBmeNvceo3f02K+zLX5ByqA4hn7/L5Cmq3adesR3JKaARaQQ9k/DkgWpOUEcpc="
- DEFOLD_USER=bjorn.ritzl@king.com
script:
- "./.test/run.sh"

View File

@ -1,9 +1,10 @@
[project] [project]
title = Monarch title = Monarch
version = 0.9 version = 0.9
dependencies = https://github.com/britzl/deftest/archive/1.2.1.zip
[bootstrap] [bootstrap]
main_collection = /example/main.collectionc main_collection = /test/test.collectionc
[input] [input]
game_binding = /input/game.input_bindingc game_binding = /input/game.input_bindingc

View File

@ -43,7 +43,7 @@ local function screen_from_script()
end end
end end
local function in_stack(id) function M.in_stack(id)
for i = 1, #stack do for i = 1, #stack do
if stack[i].id == id then if stack[i].id == id then
return true return true
@ -52,6 +52,11 @@ local function in_stack(id)
return false return false
end end
function M.is_top(id)
local top = stack[#stack]
return top and top.id == id or false
end
function M.register(id, proxy, popup, transition_url, focus_url) function M.register(id, proxy, popup, transition_url, focus_url)
assert(not screens[id], ("There is already a screen registered with id %s"):format(tostring(id))) assert(not screens[id], ("There is already a screen registered with id %s"):format(tostring(id)))
screens[id] = { screens[id] = {
@ -191,7 +196,7 @@ end
-- @param id Id of the screen to show -- @param id Id of the screen to show
-- @param options Table with options when showing the screen (can be nil). Valid values: -- @param options Table with options when showing the screen (can be nil). Valid values:
-- * clear - Set to true if the stack should be cleared down to an existing instance of the screen -- * clear - Set to true if the stack should be cleared down to an existing instance of the screen
-- * reload - Set to true if screen should be reloaded if it already exists in the stack and is loaded -- * reload - Set to true if screen should be reloaded if it already exists in the stack and is loaded.
-- This would be the case if doing a show() from a popup on the screen just below the popup. -- This would be the case if doing a show() from a popup on the screen just below the popup.
-- @param data Optional data to set on the screen. Can be retrieved by the data() function -- @param data Optional data to set on the screen. Can be retrieved by the data() function
-- @param cb Optional callback to invoke when screen is shown -- @param cb Optional callback to invoke when screen is shown
@ -224,7 +229,7 @@ function M.show(id, options, data, cb)
-- to remove every screen on the stack up until and -- to remove every screen on the stack up until and
-- including the screen itself -- including the screen itself
if options and options.clear then if options and options.clear then
while in_stack(id) do while M.in_stack(id) do
table.remove(stack) table.remove(stack)
end end
end end
@ -244,7 +249,7 @@ function M.back(data, cb)
back_out(screen, top, cb) back_out(screen, top, cb)
if top then if top then
if data then if data then
screen.data = data top.data = data
end end
back_in(top, screen) back_in(top, screen)
end end
@ -274,6 +279,15 @@ function M.on_message(message_id, message, sender)
end end
end end
function M.get_stack()
local s = {}
for k,v in pairs(stack) do
s[k] = v
end
return s
end
function M.dump_stack() function M.dump_stack()
local s = "" local s = ""
for i, screen in ipairs(stack) do for i, screen in ipairs(stack) do

View File

@ -1,4 +1,4 @@
local monarch = require "monarch.monarch" local monarch
go.property("screen_proxy", msg.url("#collectionproxy")) go.property("screen_proxy", msg.url("#collectionproxy"))
go.property("screen_id", hash("")) go.property("screen_id", hash(""))
@ -8,6 +8,7 @@ go.property("focus_url", msg.url())
function init(self) function init(self)
monarch = require "monarch.monarch"
monarch.register(self.screen_id, self.screen_proxy, self.popup, self.transition_url, self.focus_url) monarch.register(self.screen_id, self.screen_proxy, self.popup, self.transition_url, self.focus_url)
end end

48
test/cowait.lua Normal file
View File

@ -0,0 +1,48 @@
local M = {}
local instances = {}
local function create(fn)
local instance = {
co = coroutine.running(),
fn = fn,
}
table.insert(instances, instance)
coroutine.yield(instance.co)
end
function M.seconds(amount)
local time = socket.gettime() + amount
create(function()
return socket.gettime() >= time
end)
end
function M.eval(fn)
create(fn)
end
function M.update()
for k,instance in pairs(instances) do
if instance.fn() then
instances[k] = nil
coroutine.resume(instance.co)
end
end
end
return setmetatable(M, {
__call = function(self, arg1, ...)
if type(arg1) == "number" then
return M.seconds(arg1, ...)
elseif type(arg1) == "function" then
return M.eval(arg1, ...)
else
error("Unknown argument type")
end
end
})

5
test/cowait.script Normal file
View File

@ -0,0 +1,5 @@
local cowait = require "test.cowait"
function update(self, dt)
cowait.update()
end

View File

@ -0,0 +1,37 @@
name: "screen1"
scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"screen1\"\n"
" component: \"/test/data/screen1.gui\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}

131
test/data/screen1.gui Normal file
View File

@ -0,0 +1,131 @@
script: "/test/data/screen1.gui_script"
fonts {
name: "example"
font: "/assets/example.font"
}
background_color {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
nodes {
position {
x: 320.0
y: 568.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: ""
id: "box"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
layer: ""
inherit_alpha: true
slice9 {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_AUTO
}
nodes {
position {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "1"
font: "example"
id: "text"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
outline {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
parent: "box"
layer: ""
inherit_alpha: true
alpha: 1.0
outline_alpha: 1.0
shadow_alpha: 1.0
template_node_child: false
text_leading: 1.0
text_tracking: 0.0
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT
max_nodes: 512

View File

@ -0,0 +1,12 @@
function init(self)
print("init - screen1")
end
function final(self)
print("final - screen1")
end
function on_message(self, message_id, message, sender)
-- Add message-handling code here
-- Remove this function if not needed
end

View File

@ -0,0 +1,37 @@
name: "screen2"
scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"screen2\"\n"
" component: \"/test/data/screen2.gui\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}

131
test/data/screen2.gui Normal file
View File

@ -0,0 +1,131 @@
script: "/test/data/screen2.gui_script"
fonts {
name: "example"
font: "/assets/example.font"
}
background_color {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
nodes {
position {
x: 320.0
y: 697.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: ""
id: "box"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
layer: ""
inherit_alpha: true
slice9 {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_AUTO
}
nodes {
position {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "2"
font: "example"
id: "text"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
outline {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
parent: "box"
layer: ""
inherit_alpha: true
alpha: 1.0
outline_alpha: 1.0
shadow_alpha: 1.0
template_node_child: false
text_leading: 1.0
text_tracking: 0.0
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT
max_nodes: 512

View File

@ -0,0 +1,12 @@
function init(self)
print("init - screen2")
end
function final(self)
print("final - screen2")
end
function on_message(self, message_id, message, sender)
-- Add message-handling code here
-- Remove this function if not needed
end

View File

@ -0,0 +1,118 @@
name: "screens"
scale_along_z: 0
embedded_instances {
id: "screen1"
data: "components {\n"
" id: \"screen\"\n"
" component: \"/monarch/screen.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
" properties {\n"
" id: \"screen_id\"\n"
" value: \"screen1\"\n"
" type: PROPERTY_TYPE_HASH\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"collectionproxy\"\n"
" type: \"collectionproxy\"\n"
" data: \"collection: \\\"/test/data/screen1.collection\\\"\\n"
"exclude: false\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
embedded_instances {
id: "screen2"
data: "components {\n"
" id: \"screen\"\n"
" component: \"/monarch/screen.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
" properties {\n"
" id: \"screen_id\"\n"
" value: \"screen2\"\n"
" type: PROPERTY_TYPE_HASH\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"collectionproxy\"\n"
" type: \"collectionproxy\"\n"
" data: \"collection: \\\"/test/data/screen2.collection\\\"\\n"
"exclude: false\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}

70
test/test.collection Normal file
View File

@ -0,0 +1,70 @@
name: "test"
scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"test\"\n"
" component: \"/test/test.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
"components {\n"
" id: \"cowait\"\n"
" component: \"/test/cowait.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"screensfactory\"\n"
" type: \"collectionfactory\"\n"
" data: \"prototype: \\\"/test/data/screens.collection\\\"\\n"
"load_dynamically: false\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}

9
test/test.script Normal file
View File

@ -0,0 +1,9 @@
local deftest = require "deftest.deftest"
local test_monarch = require "test.test_monarch"
function init(self)
deftest.add(test_monarch)
deftest.run()
end

135
test/test_monarch.lua Normal file
View File

@ -0,0 +1,135 @@
local cowait = require "test.cowait"
local monarch = require "monarch.monarch"
local SCREEN1 = hash("screen1")
local SCREEN2 = hash("screen2")
local FOOBAR = hash("foobar")
return function()
local screens_instances = {}
local function is_shown(screen_id)
return monarch.is_top(screen_id)
end
local function is_hidden(screen_id)
return not monarch.is_top(screen_id)
end
local function wait_timeout(fn, ...)
local args = { ... }
local time = socket.gettime()
cowait(function()
return fn(unpack(args)) or socket.gettime() > time + 5
end)
return fn(...)
end
local function wait_until_shown(screen_id)
return wait_timeout(is_shown, screen_id)
end
local function wait_until_hidden(screen_id)
return wait_timeout(is_hidden, screen_id)
end
local function assert_stack(expected_screens)
local actual_screens = monarch.get_stack()
if #actual_screens ~= #expected_screens then
error("Stack length mismatch", 2)
end
for i=1,#actual_screens do
if actual_screens[i].id ~= expected_screens[i] then
error("Stack content not matching", 2)
end
end
end
describe("monarch", function()
before(function()
monarch = require "monarch.monarch"
screens_instances = collectionfactory.create("#screensfactory")
end)
after(function()
package.loaded["monarch.monarch"] = nil
for id,instance_id in pairs(screens_instances) do
go.delete(instance_id)
end
cowait(0.1)
end)
it("should be able to tell if a screen exists", function()
assert(monarch.screen_exists(SCREEN1))
assert(not monarch.screen_exists(hash("foobar")))
end)
it("should be able to show screens and go back to previous screens", function()
monarch.show(SCREEN1)
assert(wait_until_shown(SCREEN1), "Screen1 was never shown")
assert_stack({ SCREEN1 })
monarch.show(SCREEN2)
assert(wait_until_hidden(SCREEN1), "Screen1 was never hidden")
assert(wait_until_shown(SCREEN2), "Screen2 was never shown")
assert_stack({ SCREEN1, SCREEN2 })
monarch.back()
assert(wait_until_hidden(SCREEN2), "Screen2 was never hidden")
assert(wait_until_shown(SCREEN1), "Screen1 was never shown")
assert_stack({ SCREEN1 })
monarch.back()
assert(wait_until_hidden(SCREEN1), "Screen1 was never hidden")
assert_stack({ })
end)
it("should be able to pass data to a screen when showning it or going back to it", function()
local data1 = { foo = "bar" }
monarch.show(SCREEN1, nil, data1)
assert(wait_until_shown(SCREEN1), "Screen1 was never shown")
local data2 = { boo = "car" }
monarch.show(SCREEN2, nil, data2)
assert(wait_until_shown(SCREEN2), "Screen2 was never shown")
assert(monarch.data(SCREEN1) == data1, "Expected data on screen1 doesn't match actual data")
assert(monarch.data(SCREEN2) == data2, "Expected data on screen2 doesn't match actual data")
local data_back = { going = "back" }
monarch.back(data_back)
assert_stack({ SCREEN1 })
assert(monarch.data(SCREEN1) == data_back, "Expected data on screen1 doesn't match actual data")
end)
it("should be able to show the same screen twice", function()
monarch.show(SCREEN1)
assert(wait_until_shown(SCREEN1), "Screen1 was never shown")
assert_stack({ SCREEN1 })
monarch.show(SCREEN1)
assert(wait_until_shown(SCREEN1), "Screen1 was never shown")
assert_stack({ SCREEN1, SCREEN1 })
end)
it("should be able to clear the stack if trying to show the same screen twice", function()
monarch.show(SCREEN1)
assert(wait_until_shown(SCREEN1), "Screen1 was never shown")
assert_stack({ SCREEN1 })
monarch.show(SCREEN2)
assert(wait_until_shown(SCREEN2), "Screen2 was never shown")
assert_stack({ SCREEN1, SCREEN2 })
monarch.show(SCREEN1, { clear = true })
assert(wait_until_shown(SCREEN1), "Screen1 was never shown")
assert_stack({ SCREEN1 })
end)
end)
end