diff --git a/openlp/plugins/songs/lib/__init__.py b/openlp/plugins/songs/lib/__init__.py index deac92829..59b4457d5 100644 --- a/openlp/plugins/songs/lib/__init__.py +++ b/openlp/plugins/songs/lib/__init__.py @@ -187,8 +187,10 @@ class VerseType(object): for num, tag in enumerate(VerseType.Tags): if verse_tag == tag: return VerseType.TranslatedTags[num].upper() - if len(VerseType.TranslatedTags) > default: + if len(VerseType.Names) > default: return VerseType.TranslatedTags[default].upper() + else: + return VerseType.TranslatedTags[VerseType.Other].upper() @staticmethod def translated_name(verse_tag, default=Other): @@ -205,8 +207,10 @@ class VerseType(object): for num, tag in enumerate(VerseType.Tags): if verse_tag == tag: return VerseType.TranslatedNames[num] - if default in VerseType.TranslatedNames: + if len(VerseType.Names) > default: return VerseType.TranslatedNames[default] + else: + return VerseType.TranslatedNames[VerseType.Other] @staticmethod def from_tag(verse_tag, default=Other): @@ -223,7 +227,10 @@ class VerseType(object): for num, tag in enumerate(VerseType.Tags): if verse_tag == tag: return num - return default + if len(VerseType.Names) > default: + return default + else: + return VerseType.Other @staticmethod def from_translated_tag(verse_tag, default=Other): @@ -240,7 +247,10 @@ class VerseType(object): for num, tag in enumerate(VerseType.TranslatedTags): if verse_tag == tag: return num - return default + if len(VerseType.Names) > default: + return default + else: + return VerseType.Other @staticmethod def from_string(verse_name, default=Other): diff --git a/tests/functional/openlp_plugins/songs/test_lib.py b/tests/functional/openlp_plugins/songs/test_lib.py index 7c85aa397..2180f5505 100644 --- a/tests/functional/openlp_plugins/songs/test_lib.py +++ b/tests/functional/openlp_plugins/songs/test_lib.py @@ -62,3 +62,134 @@ class TestVerseType(TestCase): # THEN: The result should be "B" self.assertEqual(result, u'B', u'The result should be "B", but was "%s"' % result) + def translated_invalid_tag_with_invalid_default_test(self): + """ + Test that the translated_tag() method returns a sane default tag when passed an invalid default + """ + # GIVEN: A mocked out translate() function that just returns what it was given + with patch(u'openlp.plugins.songs.lib.translate') as mocked_translate: + mocked_translate.side_effect = lambda x, y: y + + # WHEN: We run the translated_tag() method with an invalid verse type and an invalid default + result = VerseType.translated_tag(u'q', 29) + + # THEN: The result should be "O" + self.assertEqual(result, u'O', u'The result should be "O", but was "%s"' % result) + + def translated_name_test(self): + """ + Test that the translated_name() method returns the correct name + """ + # GIVEN: A mocked out translate() function that just returns what it was given + with patch(u'openlp.plugins.songs.lib.translate') as mocked_translate: + mocked_translate.side_effect = lambda x, y: y + + # WHEN: We run the translated_name() method with a "verse" + result = VerseType.translated_name(u'v') + + # THEN: The result should be "Verse" + self.assertEqual(result, u'Verse', u'The result should be "Verse"') + + # WHEN: We run the translated_name() method with a "chorus" + result = VerseType.translated_name(u'c') + + # THEN: The result should be "Chorus" + self.assertEqual(result, u'Chorus', u'The result should be "Chorus"') + + def translated_invalid_name_test(self): + """ + Test that the translated_name() method returns the default name when passed an invalid tag + """ + # GIVEN: A mocked out translate() function that just returns what it was given + with patch(u'openlp.plugins.songs.lib.translate') as mocked_translate: + mocked_translate.side_effect = lambda x, y: y + + # WHEN: We run the translated_name() method with an invalid verse type + result = VerseType.translated_name(u'z') + + # THEN: The result should be "Other" + self.assertEqual(result, u'Other', u'The result should be "Other", but was "%s"' % result) + + def translated_invalid_name_with_specified_default_test(self): + """ + Test that the translated_name() method returns the specified default name when passed an invalid tag + """ + # GIVEN: A mocked out translate() function that just returns what it was given + with patch(u'openlp.plugins.songs.lib.translate') as mocked_translate: + mocked_translate.side_effect = lambda x, y: y + + # WHEN: We run the translated_name() method with an invalid verse type and specify a default + result = VerseType.translated_name(u'q', VerseType.Bridge) + + # THEN: The result should be "Bridge" + self.assertEqual(result, u'Bridge', u'The result should be "Bridge", but was "%s"' % result) + + def translated_invalid_name_with_invalid_default_test(self): + """ + Test that the translated_name() method returns the specified default tag when passed an invalid tag + """ + # GIVEN: A mocked out translate() function that just returns what it was given + with patch(u'openlp.plugins.songs.lib.translate') as mocked_translate: + mocked_translate.side_effect = lambda x, y: y + + # WHEN: We run the translated_name() method with an invalid verse type and specify an invalid default + result = VerseType.translated_name(u'q', 29) + + # THEN: The result should be "Other" + self.assertEqual(result, u'Other', u'The result should be "Other", but was "%s"' % result) + + def from_tag_test(self): + """ + Test that the from_tag() method returns the correct VerseType. + """ + # GIVEN: A mocked out translate() function that just returns what it was given + with patch(u'openlp.plugins.songs.lib.translate') as mocked_translate: + mocked_translate.side_effect = lambda x, y: y + + # WHEN: We run the from_tag() method with a valid verse type, we get the name back + result = VerseType.from_tag(u'v') + + # THEN: The result should be VerseType.Verse + self.assertEqual(result, VerseType.Verse, u'The result should be VerseType.Verse, but was "%s"' % result) + + def from_tag_with_invalid_tag_test(self): + """ + Test that the from_tag() method returns the default VerseType when it is passed an invalid tag. + """ + # GIVEN: A mocked out translate() function that just returns what it was given + with patch(u'openlp.plugins.songs.lib.translate') as mocked_translate: + mocked_translate.side_effect = lambda x, y: y + + # WHEN: We run the from_tag() method with a valid verse type, we get the name back + result = VerseType.from_tag(u'w') + + # THEN: The result should be VerseType.Other + self.assertEqual(result, VerseType.Other, u'The result should be VerseType.Other, but was "%s"' % result) + + def from_tag_with_specified_default_test(self): + """ + Test that the from_tag() method returns the specified default when passed an invalid tag. + """ + # GIVEN: A mocked out translate() function that just returns what it was given + with patch(u'openlp.plugins.songs.lib.translate') as mocked_translate: + mocked_translate.side_effect = lambda x, y: y + + # WHEN: We run the from_tag() method with an invalid verse type, we get the specified default back + result = VerseType.from_tag(u'x', VerseType.Chorus) + + # THEN: The result should be VerseType.Chorus + self.assertEqual(result, VerseType.Chorus, u'The result should be VerseType.Chorus, but was "%s"' % result) + + def from_tag_with_invalid_default_test(self): + """ + Test that the from_tag() method returns a sane default when passed an invalid tag and an invalid default. + """ + # GIVEN: A mocked out translate() function that just returns what it was given + with patch(u'openlp.plugins.songs.lib.translate') as mocked_translate: + mocked_translate.side_effect = lambda x, y: y + + # WHEN: We run the from_tag() method with an invalid verse type, we get the specified default back + result = VerseType.from_tag(u'm', 29) + + # THEN: The result should be VerseType.Other + self.assertEqual(result, VerseType.Other, u'The result should be VerseType.Other, but was "%s"' % result)