3
0
mirror of https://github.com/britzl/monarch.git synced 2025-09-27 10:02:21 +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

View File

@@ -43,7 +43,7 @@ local function screen_from_script()
end
end
local function in_stack(id)
function M.in_stack(id)
for i = 1, #stack do
if stack[i].id == id then
return true
@@ -52,6 +52,11 @@ local function in_stack(id)
return false
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)
assert(not screens[id], ("There is already a screen registered with id %s"):format(tostring(id)))
screens[id] = {
@@ -191,10 +196,10 @@ end
-- @param id Id of the screen to show
-- @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
-- * 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.
-- @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
function M.show(id, options, data, cb)
assert(id, "You must provide a screen id")
assert(screens[id], ("There is no screen registered with id %s"):format(tostring(id)))
@@ -224,7 +229,7 @@ function M.show(id, options, data, cb)
-- to remove every screen on the stack up until and
-- including the screen itself
if options and options.clear then
while in_stack(id) do
while M.in_stack(id) do
table.remove(stack)
end
end
@@ -244,7 +249,7 @@ function M.back(data, cb)
back_out(screen, top, cb)
if top then
if data then
screen.data = data
top.data = data
end
back_in(top, screen)
end
@@ -274,6 +279,15 @@ function M.on_message(message_id, message, sender)
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()
local s = ""
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_id", hash(""))
@@ -8,6 +8,7 @@ go.property("focus_url", msg.url())
function init(self)
monarch = require "monarch.monarch"
monarch.register(self.screen_id, self.screen_proxy, self.popup, self.transition_url, self.focus_url)
end
@@ -27,7 +28,7 @@ function on_message(self, message_id, message, sender)
or message_id == monarch.TRANSITION.BACK_IN
or message_id == monarch.TRANSITION.BACK_OUT then
msg.post(sender, monarch.TRANSITION.DONE)
else
monarch.on_message(message_id, message, sender)
end
else
monarch.on_message(message_id, message, sender)
end
end