Skip to content

Commit b5cc99a

Browse files
committed
Establish a way to define theme helper functions with unit tests
And use it in shortbread theme.
1 parent 6df5864 commit b5cc99a

File tree

5 files changed

+110
-21
lines changed

5 files changed

+110
-21
lines changed

bin/run-tests.sh

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
1-
#!/bin/sh
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
echo
6+
echo "=> Running themepark tests..."
7+
echo
28

39
for test in tests/test-*.lua; do
10+
echo "$test..."
411
lua "$test"
512
done
613

14+
echo
15+
echo "=> Running theme tests..."
16+
echo
17+
18+
for themedir in themes/*; do
19+
theme=${themedir#themes/}
20+
if [[ -e "themes/$theme/tests" ]]; then
21+
echo "$theme:"
22+
(cd "themes/$theme/tests";
23+
for atest in *; do
24+
echo "* $atest..."
25+
lua "$atest"
26+
done)
27+
fi
28+
done
29+

lua/themepark.lua

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ function themepark:add_theme_dir(dir)
130130
table.insert(self.theme_path, dir .. '/')
131131
end
132132

133+
function themepark:_read_and_exec(file, name)
134+
local script = file:read('a*')
135+
file:close()
136+
137+
local func, msg = load(script, name, 't')
138+
if not func then
139+
msg = msg:gsub('.*]:', '')
140+
error(name .. ':' .. msg, 0)
141+
end
142+
143+
return func(self)
144+
end
145+
133146
-- ---------------------------------------------------------------------------
134147
-- init_theme(THEME)
135148
--
@@ -162,22 +175,25 @@ function themepark:init_theme(theme)
162175

163176
for _, dir in ipairs(self.theme_path) do
164177
local theme_dir = dir .. theme
165-
local theme_file = theme_dir .. '/init.lua'
178+
local init_file_name = theme_dir .. '/init.lua'
166179
if self.debug then
167-
print("Themepark: Trying to load from '" .. theme_file .. "' ...")
180+
print("Themepark: Trying to load from '" .. init_file_name .. "' ...")
168181
end
169-
local file = io.open(theme_file)
170-
if file then
171-
local script = file:read('a*')
172-
file:close()
173-
174-
local func, msg = load(script, theme_file, 't')
175-
if not func then
176-
error('Loading ' .. theme_file .. ' failed: ' .. msg)
182+
local init_file = io.open(init_file_name)
183+
if init_file then
184+
self.themes[theme] = self:_read_and_exec(init_file, init_file_name)
185+
self.themes[theme].dir = theme_dir
186+
187+
local helper_file_name = theme_dir .. '/helper.lua'
188+
if self.debug then
189+
print("Themepark: Trying to load from '" .. helper_file_name .. "' ...")
190+
end
191+
192+
local helper_file = io.open(helper_file_name)
193+
if helper_file then
194+
self.themes[theme].helper = self:_read_and_exec(helper_file, helper_file_name)
177195
end
178196

179-
self.themes[theme] = func(self)
180-
self.themes[theme].dir = theme_dir
181197
break
182198
end
183199
end

themes/shortbread_v1/helper.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- ---------------------------------------------------------------------------
2+
--
3+
-- Theme: shortbread_v1
4+
--
5+
-- helper.lua
6+
--
7+
-- ---------------------------------------------------------------------------
8+
--
9+
-- Only use code in here that is independent of osm2pgsql, the themepark
10+
-- framework and the theme, so that it can be unit-tested!
11+
--
12+
-- ---------------------------------------------------------------------------
13+
14+
local helper = {}
15+
16+
function helper.is_yes_true_or_one(value)
17+
return value == 'yes' or value == 'true' or value == '1'
18+
end
19+
20+
return helper
21+
22+
-- ---------------------------------------------------------------------------
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- ---------------------------------------------------------------------------
2+
--
3+
-- Theme: shortbread_v1
4+
--
5+
-- test-helper.lua
6+
--
7+
-- ---------------------------------------------------------------------------
8+
9+
require 'busted.runner'()
10+
11+
local helper = dofile('../helper.lua')
12+
13+
describe('test is_yes_true_or_one', function()
14+
15+
it('should return true for yes, true, and 1', function()
16+
assert.is_true(helper.is_yes_true_or_one('yes'))
17+
assert.is_true(helper.is_yes_true_or_one('true'))
18+
assert.is_true(helper.is_yes_true_or_one('1'))
19+
end)
20+
21+
it('should return false for anything else', function()
22+
assert.is_false(helper.is_yes_true_or_one())
23+
assert.is_false(helper.is_yes_true_or_one(nil))
24+
assert.is_false(helper.is_yes_true_or_one('no'))
25+
assert.is_false(helper.is_yes_true_or_one('false'))
26+
assert.is_false(helper.is_yes_true_or_one('0'))
27+
assert.is_false(helper.is_yes_true_or_one('abc'))
28+
end)
29+
30+
end)
31+
32+
-- ---------------------------------------------------------------------------

themes/shortbread_v1/topics/streets.lua

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,6 @@ local aeroway_lookup = {
223223
taxiway = 13,
224224
}
225225

226-
local as_bool = function(value)
227-
return value == 'yes' or value == 'true' or value == '1'
228-
end
229-
230226
local set_ref_attributes = function(a, t)
231227
if not t.ref then
232228
return
@@ -285,8 +281,8 @@ local process_as_area = function(object, data)
285281

286282
a.surface = t.surface
287283

288-
a.tunnel = as_bool(t.tunnel) or t.tunnel == 'building_passage' or t.covered == 'yes'
289-
a.bridge = as_bool(t.bridge)
284+
a.tunnel = theme.helper.is_yes_true_or_one(t.tunnel) or t.tunnel == 'building_passage' or t.covered == 'yes'
285+
a.bridge = theme.helper.is_yes_true_or_one(t.bridge)
290286

291287
a.geom = object:as_polygon():transform(3857)
292288
local has_name = themepark.themes.core.add_name(a, object)
@@ -372,8 +368,8 @@ themepark:add_proc('way', function(object, data)
372368
return
373369
end
374370

375-
a.tunnel = as_bool(t.tunnel) or t.tunnel == 'building_passage' or t.covered == 'yes'
376-
a.bridge = as_bool(t.bridge)
371+
a.tunnel = theme.helper.is_yes_true_or_one(t.tunnel) or t.tunnel == 'building_passage' or t.covered == 'yes'
372+
a.bridge = theme.helper.is_yes_true_or_one(t.bridge)
377373

378374
set_ref_attributes(a, t)
379375

0 commit comments

Comments
 (0)