Browse files

implemented parameterize

  • Loading branch information...
1 parent aa78bbb commit 610a890f9bfe60e2da8186db2cabee73fd6263c2 @jpvanhal committed Feb 24, 2012
Showing with 101 additions and 2 deletions.
  1. +22 −2 inflection.py
  2. +79 −0 test_inflection.py
View
24 inflection.py
@@ -259,8 +259,28 @@ def ordinalize(number):
return "%s%s" % (number, ordinal(number))
-def parameterize():
- pass
+def parameterize(string, separator='-'):
+ """
+ Replace special characters in a string so that it may be used as part of a
+ 'pretty' URL.
+
+ Example::
+
+ >>> parameterize(u"Donald E. Knuth")
+ 'donald-e-knuth'
+
+ """
+ string = transliterate(string)
+ # Turn unwanted chars into the separator
+ string = re.sub(r"(?i)[^a-z0-9\-_]+", separator, string)
+ if separator:
+ re_sep = re.escape(separator)
+ # No more than one of the separator in a row.
+ string = re.sub(r'%s{2,}' % re_sep, separator, string)
+ # Remove leading/trailing separator.
+ string = re.sub(r"(?i)^{0}|{0}$".format(re_sep), '', string)
+
+ return string.lower()
def pluralize(word):
View
79 test_inflection.py
@@ -122,6 +122,49 @@
("HTML", "html"),
)
+STRING_TO_PARAMETERIZED = (
+ (u"Donald E. Knuth", "donald-e-knuth"),
+ (u"Random text with *(bad)* characters", "random-text-with-bad-characters"),
+ (u"Allow_Under_Scores", "allow_under_scores"),
+ (u"Trailing bad characters!@#", "trailing-bad-characters"),
+ (u"!@#Leading bad characters", "leading-bad-characters"),
+ (u"Squeeze separators", "squeeze-separators"),
+ (u"Test with + sign", "test-with-sign"),
+ (u"Test with malformed utf8 \251", "test-with-malformed-utf8"),
+)
+
+STRING_TO_PARAMETERIZE_WITH_NO_SEPARATOR = (
+ (u"Donald E. Knuth", "donaldeknuth"),
+ (u"With-some-dashes", "with-some-dashes"),
+ (u"Random text with *(bad)* characters", "randomtextwithbadcharacters"),
+ (u"Trailing bad characters!@#", "trailingbadcharacters"),
+ (u"!@#Leading bad characters", "leadingbadcharacters"),
+ (u"Squeeze separators", "squeezeseparators"),
+ (u"Test with + sign", "testwithsign"),
+ (u"Test with malformed utf8 \251", "testwithmalformedutf8"),
+)
+
+STRING_TO_PARAMETERIZE_WITH_UNDERSCORE = (
+ (u"Donald E. Knuth", "donald_e_knuth"),
+ (u"Random text with *(bad)* characters", "random_text_with_bad_characters"),
+ (u"With-some-dashes", "with-some-dashes"),
+ (u"Retain_underscore", "retain_underscore"),
+ (u"Trailing bad characters!@#", "trailing_bad_characters"),
+ (u"!@#Leading bad characters", "leading_bad_characters"),
+ (u"Squeeze separators", "squeeze_separators"),
+ (u"Test with + sign", "test_with_sign"),
+ (u"Test with malformed utf8 \251", "test_with_malformed_utf8"),
+)
+
+STRING_TO_PARAMETERIZED_AND_NORMALIZED = (
+ (u"Malmö", "malmo"),
+ (u"Garçons", "garcons"),
+ (u"Ops\331", "opsu"),
+ (u"Ærøskøbing", "rskbing"),
+ (u"Aßlar", "alar"),
+ (u"Japanese: 日本語", "japanese"),
+)
+
UNDERSCORE_TO_HUMAN = (
("employee_salary", "Employee salary"),
("employee_id", "Employee"),
@@ -290,6 +333,42 @@ def test_underscore(camel, underscore):
assert underscore == inflection.underscore(camel)
+@pytest.mark.parametrize(("some_string", "parameterized_string"),
+ STRING_TO_PARAMETERIZED
+)
+def test_parameterize(some_string, parameterized_string):
+ assert parameterized_string == inflection.parameterize(some_string)
+
+
+@pytest.mark.parametrize(("some_string", "parameterized_string"),
+ STRING_TO_PARAMETERIZED_AND_NORMALIZED
+)
+def test_parameterize_and_normalize(some_string, parameterized_string):
+ assert parameterized_string == inflection.parameterize(some_string)
+
+
+@pytest.mark.parametrize(("some_string", "parameterized_string"),
+ STRING_TO_PARAMETERIZE_WITH_UNDERSCORE
+)
+def test_parameterize_with_custom_separator(some_string, parameterized_string):
+ assert parameterized_string == inflection.parameterize(some_string, '_')
+
+
+@pytest.mark.parametrize(("some_string", "parameterized_string"),
+ STRING_TO_PARAMETERIZED
+)
+def test_parameterize_with_multi_character_separator(some_string, parameterized_string):
+ assert(parameterized_string.replace('-', '__sep__') ==
+ inflection.parameterize(some_string, '__sep__'))
+
+
+@pytest.mark.parametrize(("some_string", "parameterized_string"),
+ STRING_TO_PARAMETERIZE_WITH_NO_SEPARATOR
+)
+def test_parameterize_with_no_separator(some_string, parameterized_string):
+ assert parameterized_string == inflection.parameterize(some_string, '')
+
+
@pytest.mark.parametrize(("underscore", "human"), UNDERSCORE_TO_HUMAN)
def test_humanize(underscore, human):
assert human == inflection.humanize(underscore)

0 comments on commit 610a890

Please sign in to comment.