Skip to content

Commit

Permalink
add tests for all styles
Browse files Browse the repository at this point in the history
  • Loading branch information
lonvia committed Aug 22, 2020
1 parent 7951fd4 commit f882596
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 20 deletions.
13 changes: 13 additions & 0 deletions test/mock_shields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# SPDX-License-Identifier: GPL-3.0-only
#
# This file is part of the Waymarked Trails Map Project
# Copyright (C) 2011-2020 Sarah Hoffmann

from wmt_shields.common.tags import Tags
from wmt_shields.common.config import ShieldConfig

def NullShield(object):
pass

def create_for(tags: Tags, region: str, config: ShieldConfig):
return RefSymbol(ref, config)
1 change: 1 addition & 0 deletions test/render_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class GlobalConfig(WmtConfig):
('LOC', 'it', { 'osmc:symbol' : 'red:red:white_stripe:26:black'}),
('LOC', 'it', { 'osmc:symbol' : 'red:red:white_stripe:26s:black'}),
('REG', 'it', { 'osmc:symbol' : 'red:red:white_stripe:AVG:black'}),
('REG', 'it', { 'osmc:symbol' : 'white:black:blue_stripe:orange_stripe_right'}),
('LOC', '', { 'jel' : 'p+', 'ref' : 'xx'}),
('LOC', '', { 'jel' : 'foo', 'ref' : 'yy'}),
('LOC', '', { 'kct_red' : 'major'}),
Expand Down
31 changes: 31 additions & 0 deletions test/test_shield_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from wmt_shields.common.shield_maker import load_shield_maker, ShieldMaker
from wmt_shields.common.config import ShieldConfig
from wmt_shields.common.tags import Tags
from wmt_shields import ShieldFactory
from wmt_shields.styles.ref_symbol import RefSymbol

Expand Down Expand Up @@ -109,3 +110,33 @@ def __init__(self):

self.assertEqual(1298, len(t.find_resource('{data}', 'osmc/hiker.svg')))
self.assertEqual(1298, len(t.find_resource(None, 'hiker.svg')))


class RefFactory(object):
@staticmethod
def create_for(tags: Tags, region: str, config: ShieldConfig):
if tags.first_of('ref'):
return RefFactory()

class NameFactory(object):
@staticmethod
def create_for(tags: Tags, region: str, config: ShieldConfig):
if tags.first_of('name'):
return NameFactory()

class TestShieldFactory(unittest.TestCase):

def test_factory(self):
f = ShieldFactory([RefFactory(), NameFactory()], NullConfig())

self.assertIsNone(f.create({}, ''))

s = f.create({'name' : 'x'}, '')
self.assertIsInstance(s, NameFactory)

s = f.create({'ref' : 'x'}, '')
self.assertIsInstance(s, RefFactory)

s = f.create({'name' : 'x', 'ref' : '5'}, '')
self.assertIsInstance(s, RefFactory)

217 changes: 217 additions & 0 deletions test/test_styles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
# SPDX-License-Identifier: GPL-3.0-only
#
# This file is part of the Waymarked Trails Map Project
# Copyright (C) 2011-2020 Sarah Hoffmann

import unittest

from wmt_shields import ShieldFactory
from wmt_shields.common.config import ShieldConfig
from wmt_shields.wmt_config import WmtConfig
from pathlib import Path

base_dir = Path(__file__).parent.parent.resolve()

class NullConfig(ShieldConfig):

def __init__(self):
super().__init__({}, {})

class TestStyles(unittest.TestCase):

def assert_shield(self, shieldmaker, expected_uuid):
self.assertEqual(expected_uuid, shieldmaker.uuid())
self.assertGreater(shieldmaker.dimensions()[0], 0)
self.assertGreater(shieldmaker.dimensions()[1], 0)
self.assertIsInstance(shieldmaker.dimensions()[0], int)
self.assertIsInstance(shieldmaker.dimensions()[1], int)
self.assertIsNotNone(shieldmaker.create_image())

def test_ref_symbol(self):
for cfg in (NullConfig(), WmtConfig):
with self.subTest(i=cfg):
f = ShieldFactory(['.ref_symbol'], cfg)

self.assertIsNone(f.create({}, ''))

self.assert_shield(f.create({'ref' : 'A'}, ''), 'ref_None_0041')
self.assert_shield(f.create({'ref' : '.'}, '', style='red'),
'ref_red_002e')
self.assert_shield(f.create({'ref' : 'AAAAAAAA'}, ''),
'ref_None_00410041004100410041')

def test_cai_hiking_symbol(self):
for cfg in (NullConfig(), WmtConfig):
with self.subTest(i=cfg):
f = ShieldFactory(['.cai_hiking_symbol'], cfg)

self.assertIsNone(f.create({}, 'it'))
self.assertIsNone(f.create({'osmc:symbol' : 'red:red:white_bar:2:black'}, ''))
self.assertIsNone(f.create({'osmc:symbol' : 'red:red:white_cross:2:black'}, 'it'))

self.assert_shield(
f.create({'osmc:symbol' : 'red:red:white_bar:2:black'}, 'it'),
'cai_None_bar_0032')
self.assert_shield(
f.create({'osmc:symbol' : 'red:red:white_stripe:2:black'}, 'it'),
'cai_None_stripe_0032')

def test_color_box(self):
for cfg in (NullConfig(), WmtConfig):
with self.subTest(i=cfg):
f = ShieldFactory(['.color_box'], cfg)

self.assertIsNone(f.create({}, ''))

self.assert_shield(
f.create({'color' : '#f0f0f0'}, ''), 'cbox_None_f0f0f0')

def test_image_symbol(self):
for cfg in (NullConfig(), WmtConfig()):
with self.subTest(i=cfg):
f = ShieldFactory(['.image_symbol'], cfg)

self.assertIsNone(f.create({}, ''))

setattr(cfg, 'shield_names',
{'wheel' : {'tag1' : 'foo', 'tag2' : 'bar'}})
setattr(cfg, 'shield_path',
str(base_dir / 'wmt_shields' / 'data' / 'osmc'))

f = ShieldFactory(['.image_symbol'], cfg)

self.assertIsNone(f.create({'tag1' : 'foo'}, ''))

self.assert_shield(
f.create({'tag1' : 'foo', 'tag2' : 'bar'}, ''),
'shield_None_wheel')

def test_jel_symbol(self):
f = ShieldFactory(['.jel_symbol'], NullConfig())
self.assertIsNone(f.create({'jel' : 'flo'}, ''))

mincfg = ShieldConfig({'jel_types' : ('flo', ), 'jel_path' : '{data}/jel'}, {})
for cfg in (mincfg, WmtConfig):
with self.subTest(i=cfg):
f = ShieldFactory(['.jel_symbol'], cfg)

self.assertIsNone(f.create({}, ''))
self.assertIsNone(f.create({'jel' : 'xx233ssdd'}, ''))

self.assert_shield(
f.create({'jel' : 'flo'}, ''), 'jel_None_flo')

def test_kct_symbol(self):
f = ShieldFactory(['.kct_symbol'], NullConfig())
self.assertIsNone(f.create({'kct_major' : 'red'}, ''))

mincfg = ShieldConfig({'kct_types' : ('major', ),
'kct_colors' : {'red' : (1, 0, 0)},
'kct_path' : '{data}/kct'}, {})
for cfg in (mincfg, WmtConfig):
f = ShieldFactory(['.kct_symbol'], cfg)

self.assertIsNone(f.create({'operator' : 'kst', 'colour' : 'red'}, ''))
self.assertIsNone(f.create({'operator' : 'kst', 'symbol' : 'major'}, ''))
self.assertIsNone(f.create({'symbol' : 'major', 'colour' : 'red'}, ''))

self.assert_shield(
f.create({'operator' : 'kst', 'colour' : 'red', 'symbol' : 'major'}, ''),
'kct_None_red-major')
self.assert_shield(
f.create({'kct_red' : 'major'}, ''),
'kct_None_red-major')

def test_nordic_symbol(self):
for cfg in (NullConfig(), WmtConfig):
with self.subTest(i=cfg):
f = ShieldFactory(['.nordic_symbol'], cfg)

self.assertIsNone(f.create({'color' : '#f0f0f0'}, ''))
self.assertIsNone(f.create({'piste:type' : 'nordic'}, ''))

self.assert_shield(
f.create({'piste:type' : 'nordic', 'color' : '#f0f0f0'}, ''),
'nordic_None_f0f0f0')


def test_osmc_symbol(self):
f = ShieldFactory(['.osmc_symbol'], NullConfig())
self.assertIsNone(f.create({'osmc:symbol' : 'black::black_stripe'}, ''))

mincfg = ShieldConfig({'osmc_colors' : WmtConfig.osmc_colors,
'osmc_path' : '{data}/osmc'}, {})
for cfg in (mincfg, WmtConfig):
with self.subTest(i=cfg):
f = ShieldFactory(['.osmc_symbol'], cfg)

self.assertIsNone(f.create({}, ''))

for fg in ('arch', 'backslash', 'bar', 'circle', 'corner', 'cross',
'diamond_line', 'diamond', 'dot', 'fork', 'lower',
'right', 'pointer', 'rectangle_line', 'rectangle',
'red_diamond', 'slash', 'stripe', 'triangle_line',
'triangle', 'triangle_turned', 'turned_T', 'x',
'hexagon', 'shell', 'shell_modern', 'hiker', 'wheel'):
self.assert_shield(
f.create({'osmc:symbol' : f'black::black_{fg}'}, ''),
f'osmc_None_empty_{fg}_black')

for bg in ('circle', 'frame', 'round'):
self.assert_shield(
f.create({'osmc:symbol' : f'black:red_{bg}'}, ''),
f'osmc_None_red_{bg}')

self.assert_shield(
f.create({'osmc:symbol' : 'white:black::45:black'}, ''),
'osmc_None_black_00340035_black')
self.assert_shield(
f.create({'osmc:symbol' : 'white:black:blue_stripe:orange_stripe_right'} , ''),
'osmc_None_black_stripe_blue_orange')
self.assert_shield(
f.create({'osmc:symbol' : 'white:black:wheel'}, ''),
'osmc_None_black_wheel_black')


def test_ref_color_symbol(self):
for cfg in (ShieldConfig({'colorbox_names' : WmtConfig.colorbox_names}, {}), WmtConfig):
with self.subTest(i=cfg):
f = ShieldFactory(['.ref_color_symbol'], cfg)

self.assertIsNone(f.create({'ref' : 'A'}, ''))
self.assertIsNone(f.create({'color' : '#eeeeee'}, ''))
self.assertIsNone(f.create({'ref' : 'A', 'color' : 'greenish'}, ''))

self.assert_shield(f.create({'ref' : 'A', 'color' : '#eeeeee'}, ''),
'ctb_None_eeeeee_0041')
self.assert_shield(f.create({'ref' : 'AAAAAAAA', 'color' : '#101010'}, ''),
'ctb_None_101010_00410041004100410041')
self.assert_shield(f.create({'ref' : 'A', 'color' : 'red'}, ''),
'ctb_None_red_0041')


def test_slope_symbol(self):
for cfg in (ShieldConfig({'slope_colors' : WmtConfig.slope_colors}, {}), WmtConfig):
with self.subTest(i=cfg):
f = ShieldFactory(['.slope_symbol'], cfg)

self.assertIsNone(f.create({}, ''))

self.assert_shield(
f.create({'piste:type' : 'downhill'}, '', difficulty=0),
'slope_None_0')
self.assert_shield(
f.create({'piste:type' : 'downhill', 'piste:ref' : 'A'}, '', difficulty=9),
'slope_None_9_0041')

def test_swiss_mobile(self):
f = ShieldFactory(['.swiss_mobile'], WmtConfig)

self.assertIsNone(f.create({'ref' : '23'}, ''))
self.assertIsNone(f.create({'operator' : 'Swiss mobility'}, ''))
self.assertIsNone(f.create({'ref' : '0', 'operator' : 'Swiss mobility'}, ''))

self.assert_shield(
f.create({'operator' : 'Swiss mobility', 'network' : 'nwn', 'ref' : '7'} , ''),
'swiss_None_0037')

2 changes: 1 addition & 1 deletion wmt_shields/common/shield_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def uuid(self):
""" Return a unique identifier also usable as a filename. the default
implementation expects a field `uuid_pattern` which needs to have
one `{}` placeholder for the style name. If that is not sufficient
then implentations may also just overwrite this function.
then implementations may also just overwrite this function.
"""
return self.uuid_pattern.format(self.config.style or 'None')

Expand Down
15 changes: 9 additions & 6 deletions wmt_shields/styles/cai_hiking_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,26 @@ def dimensions(self):
tw, _ = self._get_text_size(self.config.text_font)

# create an image where the text fits
w = int(tw + 2 * self.config.cai_border_width)
h = int(self.config.image_height + 0.5 * self.config.cai_border_width)
bw = self.config.cai_border_width or 5
w = int(tw + 2 * bw)
h = int((self.config.image_height or 16) + 0.5 * bw)
w = max(h, w)

return w, h

def render(self, ctx, w, h):
self.render_background(ctx, w, h, self.config.color_names['white'])
bgcolor = (self.config.color_names or {}).get('white', (0, 0, 0))
self.render_background(ctx, w, h, bgcolor)

# bars
ctx.set_source_rgb(*self.config.osmc_colors['red'])
fgcolor = (self.config.osmc_colors or {}).get('red', (1.0, 0, 0))
ctx.set_source_rgb(*fgcolor)
ctx.rectangle(0, 0, w, h)
ctx.set_line_width(self.config.image_border_width)
ctx.set_line_width(self.config.image_border_width or 0)
ctx.stroke()

ctx.set_line_width(0)
bwidth = self.config.cai_border_width
bwidth = self.config.cai_border_width or 5
if self.typ == 'stripe':
ctx.rectangle(0, 0, bwidth, h)
ctx.fill()
Expand Down
9 changes: 5 additions & 4 deletions wmt_shields/styles/image_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ def render(self, ctx, w, h):


def create_for(tags: Tags, region: str, config: ShieldConfig):
for name, stags in config.shield_names.items():
if tags.contains_all_tags(stags):
uuid = f'shield_{{}}_{name}'
return ImageSymbol(uuid, config.shield_path, f'{name}.svg', config)
if config.shield_names:
for name, stags in config.shield_names.items():
if tags.contains_all_tags(stags):
uuid = f'shield_{{}}_{name}'
return ImageSymbol(uuid, config.shield_path, f'{name}.svg', config)

return None
3 changes: 3 additions & 0 deletions wmt_shields/styles/jel_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from .image_symbol import ImageSymbol

def create_for(tags: Tags, region: str, config: ShieldConfig):
if config.jel_types is None:
return None

ref = tags.get('jel')
if ref is None or ref not in config.jel_types:
return None
Expand Down
7 changes: 5 additions & 2 deletions wmt_shields/styles/kct_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def __init__(self, color, symbol, config):

def dimensions(self):
bwidth = self.config.image_border_width or 0
return ((self.config.image_width or 16) + 0.5 * bwidth,
(self.config.image_height or 16) + 0.5 * bwidth)
return (int((self.config.image_width or 16) + 0.5 * bwidth),
int((self.config.image_height or 16) + 0.5 * bwidth))

def render(self, ctx, w, h):
# get the template file
Expand All @@ -46,6 +46,9 @@ def render(self, ctx, w, h):


def create_for(tags: Tags, region: str, config: ShieldConfig):
if config.kct_colors is None or config.kct_types is None:
return None

# slovakian system
if tags.get('operator', '').lower() == 'kst':
col = tags.get('colour')
Expand Down
Loading

0 comments on commit f882596

Please sign in to comment.