Skip to content

Commit

Permalink
Improve DX CT name normalization and question info, add tests for nor…
Browse files Browse the repository at this point in the history
…malization
  • Loading branch information
MrTango committed Jul 22, 2018
1 parent 350bffe commit 9530930
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
22 changes: 13 additions & 9 deletions bobtemplates/plone/content_type.py
Expand Up @@ -9,6 +9,7 @@
from mrbob.bobexceptions import SkipQuestion
from mrbob.bobexceptions import ValidationError

import case_conversion as cc
import keyword
import os
import re
Expand All @@ -23,9 +24,11 @@ def is_container(configurator, question):
def check_dexterity_type_name(configurator, question, answer):
"""Test if type name is valid."""
if keyword.iskeyword(answer):
raise ValidationError(u'{key} is a reserved Python keyword'.format(key=answer)) # NOQA: E501
raise ValidationError(u'"{key}" is a reserved Python keyword!'.format(key=answer)) # NOQA: E501
if not re.match('[_a-zA-Z ]*$', answer):
raise ValidationError(u'{key} is not a valid identifier'.format(key=answer)) # NOQA: E501
raise ValidationError(
u'"{key}" is not a valid identifier!\n'
u'Allowed characters: _ a-z A-Z and whitespace.\n'.format(key=answer)) # NOQA: E501
return answer


Expand Down Expand Up @@ -238,13 +241,14 @@ def prepare_renderer(configurator):
configurator = base_prepare_renderer(configurator)
configurator.variables['template_id'] = 'content_type'
type_name = configurator.variables['dexterity_type_name']
configurator.variables[
'dexterity_type_name_klass'] = type_name.title().replace(' ', '')
configurator.variables[
'dexterity_type_name_fti'] = type_name.replace(' ', '_')
configurator.variables[
'dexterity_type_name_normalized'] = configurator.variables[
'dexterity_type_name_fti'].lower()
dx_type_name_klass = cc.pascalcase(
type_name,
)
configurator.variables['dexterity_type_name_klass'] = dx_type_name_klass
dx_type_fti_file_name = type_name.replace(' ', '_')
configurator.variables['dexterity_type_fti_file_name'] = dx_type_fti_file_name # NOQA: E501
dx_type_name_normalized = dx_type_fti_file_name.lower()
configurator.variables['dexterity_type_name_normalized'] = dx_type_name_normalized # NOQA: E501
configurator.target_directory = configurator.variables['package_folder']


Expand Down
4 changes: 2 additions & 2 deletions bobtemplates/plone/content_type/.mrbob.ini
Expand Up @@ -7,8 +7,8 @@ subtemplate_warning.post_ask_question = mrbob.hooks:validate_choices bobtemplate
subtemplate_warning.choices = y|n
subtemplate_warning.choices_delimiter = |

dexterity_type_name.question = Content type name
dexterity_type_name.help = Should be something like 'Todo Task' (no special characters!)
dexterity_type_name.question = Content type name (Allowed: _ a-z A-Z and whitespace)
dexterity_type_name.help = Should be something like 'Todo Task' (Allowed characters: _ a-z A-Z and whitespace)
dexterity_type_name.required = True
dexterity_type_name.default = Todo Task
dexterity_type_name.pre_ask_question = bobtemplates.plone.base:check_root_folder
Expand Down
14 changes: 12 additions & 2 deletions package-tests/test_content_type.py
Expand Up @@ -63,7 +63,7 @@ def test_is_container_true():
content_type.is_container(configurator, None)


def test_prepare_renderer():
def test_prepare_renderer(tmpdir):
"""Test prepare renderer."""
configurator = Configurator(
template='bobtemplates.plone:content_type',
Expand All @@ -72,10 +72,20 @@ def test_prepare_renderer():
'non_interactive': True,
},
variables={
'dexterity_type_name': 'Task',
'dexterity_type_name': 'Special Task',
},
)
content_type.prepare_renderer(configurator)
if configurator.variables['dexterity_type_name'] != 'Special Task':
pytest.raises(ValidationError)
if configurator.variables['dexterity_type_fti_file_name'] != 'Special_Task':
pytest.raises(ValidationError)
if configurator.variables['dexterity_type_name_klass'] != 'SpecialTask':
pytest.raises(ValidationError)
if configurator.variables['dexterity_type_name_normalized'] != 'special_task': # NOQA: E501
pytest.raises(ValidationError)
if configurator.target_directory != tmpdir + '/collective.todo/src/collective/todo': # NOQA: E501
pytest.raises(ValidationError)


def test_check_global_allow_true():
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -49,6 +49,7 @@
'mr.bob',
'lxml',
'stringcase',
'case-conversion',
'colorama',
],
setup_requires=[],
Expand Down

0 comments on commit 9530930

Please sign in to comment.