From 4951eb42c07d58af01e47ff53521548c8d3ea0a8 Mon Sep 17 00:00:00 2001 From: thermal Date: Fri, 3 Feb 2017 19:52:51 +0800 Subject: [PATCH] Add cation support --- praise/__init__.py | 5 ++--- praise/tests/helper.py | 10 ++++++++++ praise/tests/test_praise.py | 19 ++++++++++++++++++- praise/words.py | 30 +++++++++++++++++++++--------- 4 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 praise/tests/helper.py diff --git a/praise/__init__.py b/praise/__init__.py index 344fcf8..9f5ce41 100644 --- a/praise/__init__.py +++ b/praise/__init__.py @@ -1,11 +1,10 @@ import string -from .words import get_one_word_dict +from .words import Words def praise(template="You are ${adjective}!"): if template == None: template = "None" template = string.Template(template) - words = get_one_word_dict() - result = template.substitute(words) + result = template.substitute(Words()) return result diff --git a/praise/tests/helper.py b/praise/tests/helper.py new file mode 100644 index 0000000..c9aaa10 --- /dev/null +++ b/praise/tests/helper.py @@ -0,0 +1,10 @@ +def caption_check(word, word_list): + if not word.isalpha(): + return True + return word.isupper() and word.lower() in word_list + + +def first_letter_caption_check(word, word_list): + if not word.isalpha(): + return True + return word[0].isupper and word[1:].islower() and word.lower() in word_list diff --git a/praise/tests/test_praise.py b/praise/tests/test_praise.py index 618ff78..a3976cc 100644 --- a/praise/tests/test_praise.py +++ b/praise/tests/test_praise.py @@ -1,5 +1,5 @@ from unittest import TestCase - +from .helper import * import praise @@ -9,6 +9,7 @@ def test_corner_case(self): self.assertEqual("", praise.praise("")) self.assertEqual("None", praise.praise(None)) self.assertEqual("foo", praise.praise("foo")) + self.assertEqual("${foo}", praise.praise("${foo}")) def test_word(self): self.assertTrue( @@ -31,3 +32,19 @@ def test_milti_words(self): self.assertTrue(result[3] in praise.smiley.smiley) self.assertTrue(result[4] in praise.verb.created) self.assertTrue(result[5] in praise.verb.creating) + + def test_caption_words(self): + self.assertTrue(caption_check(praise.praise("${ADJECTIVE}"), praise.adjective.adjective)) + self.assertTrue(caption_check(praise.praise("${ADVERB}"), praise.adverb.adverb)) + self.assertTrue(caption_check(praise.praise("${EXCLAMATION}"), praise.exclamation.exclamation)) + self.assertTrue(caption_check(praise.praise("${SMILEY}"), praise.smiley.smiley)) + self.assertTrue(caption_check(praise.praise("${CREATED}"), praise.verb.created)) + self.assertTrue(caption_check(praise.praise("${CREATING}"), praise.verb.creating)) + + def test_first_caption_words(self): + self.assertTrue(first_letter_caption_check(praise.praise("${Adjective}"), praise.adjective.adjective)) + self.assertTrue(first_letter_caption_check(praise.praise("${Adverb}"), praise.adverb.adverb)) + self.assertTrue(first_letter_caption_check(praise.praise("${Exclamation}"), praise.exclamation.exclamation)) + self.assertTrue(first_letter_caption_check(praise.praise("${Smiley}"), praise.smiley.smiley)) + self.assertTrue(first_letter_caption_check(praise.praise("${Created}"), praise.verb.created)) + self.assertTrue(first_letter_caption_check(praise.praise("${Creating}"), praise.verb.creating)) diff --git a/praise/words.py b/praise/words.py index f8109ef..3bf3202 100644 --- a/praise/words.py +++ b/praise/words.py @@ -7,12 +7,24 @@ from .verb import created, creating -def get_one_word_dict(): - return { - 'adjective': random.choice(adjective), - 'adverb': random.choice(adverb), - 'exclamation': random.choice(exclamation), - 'smiley': random.choice(smiley), - 'created': random.choice(created), - 'creating': random.choice(creating) - } +class Words(dict): + + def __getitem__(self, key): + word_dict = { + 'adjective': random.choice(adjective), + 'adverb': random.choice(adverb), + 'exclamation': random.choice(exclamation), + 'smiley': random.choice(smiley), + 'created': random.choice(created), + 'creating': random.choice(creating) + } + if key.lower() in word_dict.keys(): + result = word_dict[key.lower()] + else: + result = "${%s}" % key + + if key.isupper(): + result = result.upper() + elif key[0].isupper() and key[1:].islower(): + result = result[0].upper() + result[1:] + return result