Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTango committed Mar 20, 2017
1 parent cc6e743 commit c0f295f
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 7 deletions.
17 changes: 10 additions & 7 deletions bobtemplates/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ def pre_theme_name(configurator, question):
def post_theme_name(configurator, question, answer):
regex = r'^\w+[a-zA-Z0-9 \.\-_]*\w$'
if not re.match(regex, answer):
msg = "Error: '{0}' is not a valid themename.\n".format(answer)
msg += "Please use a valid name (like 'Tango' or 'my-tango.com')!\n"
msg += "At beginning or end only letters|diggits are allowed.\n"
msg += "Inside the name also '.-_' are allowed.\n"
msg += "No umlauts!"
msg = u"Error: '{0}' is not a valid themename.\n".format(answer)
msg += u"Please use a valid name (like 'Tango' or 'my-tango.com')!\n"
msg += u"At beginning or end only letters|diggits are allowed.\n"
msg += u"Inside the name also '.-_' are allowed.\n"
msg += u"No umlauts!"
raise ValidationError(msg)
return answer

Expand All @@ -179,6 +179,9 @@ def prepare_render(configurator):
This is especially important for allowing nested and normal packages.
"""
# get package-name and package-type from user-input

from pprint import pprint as pp
pp(configurator.__dict__)
package_dir = os.path.basename(configurator.target_directory)
nested = bool(len(package_dir.split('.')) == 3)
configurator.variables['package.nested'] = nested
Expand Down Expand Up @@ -247,8 +250,8 @@ def prepare_render(configurator):

if configurator.variables.get('theme.name'):
def normalize_string(value):
value = "".join(value.split('_'))
value = "".join(value.split())
value = "-".join(value.split('_'))
value = "-".join(value.split())
return value
configurator.variables[
"theme.normalized_name"] = normalize_string(
Expand Down
96 changes: 96 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,39 @@ def test_plone_theme_package_template(self):
)


class DummyConfigurator(object):
def __init__(self,
defaults=None,
bobconfig=None,
templateconfig=None,
variables=None,
quiet=False,
target_directory=None):
self.defaults = defaults or {}
self.bobconfig = bobconfig or {}
self.variables = variables or {}
self.quiet = quiet
self.templateconfig = templateconfig or {}
self.target_directory = target_directory


class HooksTest(unittest.TestCase):

def setUp(self):
target_directory = '/tmp/bobtemplates/plonetheme.tango'
variables = {
'author.email': 'info@example.com',
'author.github.user': u'ExampleUser',
'author.name': 'Example Name',
'package.description': u'An add-on for Plone',
'plone.is_plone5': True,
'plone.minor_version': u'5.0',
'plone.version': u'5.0.6',
'year': 2017
}
self.configurator = DummyConfigurator(
target_directory=target_directory, variables=variables)

def test_to_boolean(self):
# Initial simple test to show coverage in hooks.py.
self.assertEqual(hooks.to_boolean(None, None, 'y'), True)
Expand Down Expand Up @@ -412,3 +443,68 @@ def hookit(value):
self.assertEqual(hookit(u'Supertype'), u'Supertype')
self.assertEqual(hookit(u'second_coming'), u'second_coming')
self.assertEqual(hookit(u'the_2nd_coming'), u'the_2nd_coming')

def test_post_theme_name(self):
""" validation of entered theme name
"""
def hookit(value):
return hooks.post_theme_name(None, None, value)

with self.assertRaises(ValidationError):
hookit('foo bar !')
with self.assertRaises(ValidationError):
hookit(u'Glühwein')
with self.assertRaises(ValidationError):
hookit(u'.my-theme')
with self.assertRaises(ValidationError):
hookit(u'my-theme.')
with self.assertRaises(ValidationError):
hookit(u'my-theme; lala')
with self.assertRaises(ValidationError):
hookit(u'my theme-')

self.assertEqual(hookit(u'My Theme'), u'My Theme')
self.assertEqual(hookit(u'my-website.org'), u'my-website.org')
self.assertEqual(
hookit(u'Theme for example.com'), u'Theme for example.com')
self.assertEqual(hookit(u'My_Theme'), u'My_Theme')
self.assertEqual(
hookit(u'My Theme - blue 666'), u'My Theme - blue 666')

def test_prepare_render(self):
"""
"""
def hookit(configurator):
hooks.prepare_render(configurator)

self.configurator.variables['theme.name'] = u'My Theme'
hookit(self.configurator)
self.assertTrue('package.nested' in self.configurator.variables)
self.assertTrue('package.namespace' in self.configurator.variables)
self.assertTrue('package.dottedname' in self.configurator.variables)
self.assertTrue('package.uppercasename' in self.configurator.variables)
self.assertTrue('theme.normalized_name' in self.configurator.variables)
self.assertEqual(
self.configurator.variables['theme.name'],
'My Theme')
self.assertEqual(
self.configurator.variables['theme.normalized_name'],
'my-theme')

self.configurator.variables['theme.name'] = u'Blue example.com'
hookit(self.configurator)
self.assertEqual(
self.configurator.variables['theme.name'],
'Blue example.com')
self.assertEqual(
self.configurator.variables['theme.normalized_name'],
'blue-example.com')

self.configurator.variables['theme.name'] = u'Blue_Sky example.com'
hookit(self.configurator)
self.assertEqual(
self.configurator.variables['theme.name'],
'Blue_Sky example.com')
self.assertEqual(
self.configurator.variables['theme.normalized_name'],
'blue-sky-example.com')

0 comments on commit c0f295f

Please sign in to comment.