Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion bin/run-tests.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
#!/bin/sh
#!/bin/bash

set -euo pipefail

echo
echo "=> Running themepark tests..."
echo

for test in tests/test-*.lua; do
echo "$test..."
lua "$test"
done

echo
echo "=> Running theme tests..."
echo

for themedir in themes/*; do
theme=${themedir#themes/}
if [[ -e "themes/$theme/tests" ]]; then
echo "$theme:"
(cd "themes/$theme/tests";
for atest in *; do
echo "* $atest..."
lua "$atest"
done)
fi
done

40 changes: 28 additions & 12 deletions lua/themepark.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ function themepark:add_theme_dir(dir)
table.insert(self.theme_path, dir .. '/')
end

function themepark:_read_and_exec(file, name)
local script = file:read('a*')
file:close()

local func, msg = load(script, name, 't')
if not func then
msg = msg:gsub('.*]:', '')
error(name .. ':' .. msg, 0)
end

return func(self)
end

-- ---------------------------------------------------------------------------
-- init_theme(THEME)
--
Expand Down Expand Up @@ -162,22 +175,25 @@ function themepark:init_theme(theme)

for _, dir in ipairs(self.theme_path) do
local theme_dir = dir .. theme
local theme_file = theme_dir .. '/init.lua'
local init_file_name = theme_dir .. '/init.lua'
if self.debug then
print("Themepark: Trying to load from '" .. theme_file .. "' ...")
print("Themepark: Trying to load from '" .. init_file_name .. "' ...")
end
local file = io.open(theme_file)
if file then
local script = file:read('a*')
file:close()

local func, msg = load(script, theme_file, 't')
if not func then
error('Loading ' .. theme_file .. ' failed: ' .. msg)
local init_file = io.open(init_file_name)
if init_file then
self.themes[theme] = self:_read_and_exec(init_file, init_file_name)
self.themes[theme].dir = theme_dir

local helper_file_name = theme_dir .. '/helper.lua'
if self.debug then
print("Themepark: Trying to load from '" .. helper_file_name .. "' ...")
end

local helper_file = io.open(helper_file_name)
if helper_file then
self.themes[theme].helper = self:_read_and_exec(helper_file, helper_file_name)
end

self.themes[theme] = func(self)
self.themes[theme].dir = theme_dir
break
end
end
Expand Down
22 changes: 22 additions & 0 deletions themes/shortbread_v1/helper.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- ---------------------------------------------------------------------------
--
-- Theme: shortbread_v1
--
-- helper.lua
--
-- ---------------------------------------------------------------------------
--
-- Only use code in here that is independent of osm2pgsql, the themepark
-- framework and the theme, so that it can be unit-tested!
--
-- ---------------------------------------------------------------------------

local helper = {}

function helper.is_yes_true_or_one(value)
return value == 'yes' or value == 'true' or value == '1'
end

return helper

-- ---------------------------------------------------------------------------
32 changes: 32 additions & 0 deletions themes/shortbread_v1/tests/test-helper.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- ---------------------------------------------------------------------------
--
-- Theme: shortbread_v1
--
-- test-helper.lua
--
-- ---------------------------------------------------------------------------

require 'busted.runner'()

local helper = dofile('../helper.lua')

describe('test is_yes_true_or_one', function()

it('should return true for yes, true, and 1', function()
assert.is_true(helper.is_yes_true_or_one('yes'))
assert.is_true(helper.is_yes_true_or_one('true'))
assert.is_true(helper.is_yes_true_or_one('1'))
end)

it('should return false for anything else', function()
assert.is_false(helper.is_yes_true_or_one())
assert.is_false(helper.is_yes_true_or_one(nil))
assert.is_false(helper.is_yes_true_or_one('no'))
assert.is_false(helper.is_yes_true_or_one('false'))
assert.is_false(helper.is_yes_true_or_one('0'))
assert.is_false(helper.is_yes_true_or_one('abc'))
end)

end)

-- ---------------------------------------------------------------------------
12 changes: 4 additions & 8 deletions themes/shortbread_v1/topics/streets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,6 @@ local aeroway_lookup = {
taxiway = 13,
}

local as_bool = function(value)
return value == 'yes' or value == 'true' or value == '1'
end

local set_ref_attributes = function(a, t)
if not t.ref then
return
Expand Down Expand Up @@ -285,8 +281,8 @@ local process_as_area = function(object, data)

a.surface = t.surface

a.tunnel = as_bool(t.tunnel) or t.tunnel == 'building_passage' or t.covered == 'yes'
a.bridge = as_bool(t.bridge)
a.tunnel = theme.helper.is_yes_true_or_one(t.tunnel) or t.tunnel == 'building_passage' or t.covered == 'yes'
a.bridge = theme.helper.is_yes_true_or_one(t.bridge)

a.geom = object:as_polygon():transform(3857)
local has_name = themepark.themes.core.add_name(a, object)
Expand Down Expand Up @@ -372,8 +368,8 @@ themepark:add_proc('way', function(object, data)
return
end

a.tunnel = as_bool(t.tunnel) or t.tunnel == 'building_passage' or t.covered == 'yes'
a.bridge = as_bool(t.bridge)
a.tunnel = theme.helper.is_yes_true_or_one(t.tunnel) or t.tunnel == 'building_passage' or t.covered == 'yes'
a.bridge = theme.helper.is_yes_true_or_one(t.bridge)

set_ref_attributes(a, t)

Expand Down
Loading