fixed bug #1312629 Error on entering invalid character in verse-order

bzr-revno: 2377
Fixes: https://launchpad.net/bugs/1312629
This commit is contained in:
Stefan Strasser 2014-05-07 22:29:53 +02:00 committed by Raoul Snyman
commit fb2381f11a
2 changed files with 33 additions and 5 deletions

View File

@ -206,14 +206,14 @@ class VerseType(object):
Return the VerseType for a given tag Return the VerseType for a given tag
:param verse_tag: The string to return a VerseType for :param verse_tag: The string to return a VerseType for
:param default: Default return value if no matching tag is found :param default: Default return value if no matching tag is found (a valid VerseType or None)
:return: A VerseType of the tag :return: A VerseType of the tag
""" """
verse_tag = verse_tag[0].lower() verse_tag = verse_tag[0].lower()
for num, tag in enumerate(VerseType.tags): for num, tag in enumerate(VerseType.tags):
if verse_tag == tag: if verse_tag == tag:
return num return num
if len(VerseType.names) > default: if default in range(0, len(VerseType.names)) or default is None:
return default return default
else: else:
return VerseType.Other return VerseType.Other
@ -231,7 +231,7 @@ class VerseType(object):
for num, tag in enumerate(VerseType.translated_tags): for num, tag in enumerate(VerseType.translated_tags):
if verse_tag == tag: if verse_tag == tag:
return num return num
if len(VerseType.names) > default: if default in range(0, len(VerseType.names)) or default is None:
return default return default
else: else:
return VerseType.Other return VerseType.Other

View File

@ -445,9 +445,9 @@ class TestVerseType(TestCase):
# THEN: The result should be VerseType.Chorus # THEN: The result should be VerseType.Chorus
self.assertEqual(result, VerseType.Chorus, 'The result should be VerseType.Chorus, but was "%s"' % result) self.assertEqual(result, VerseType.Chorus, 'The result should be VerseType.Chorus, but was "%s"' % result)
def from_tag_with_invalid_default_test(self): def from_tag_with_invalid_intdefault_test(self):
""" """
Test that the from_tag() method returns a sane default when passed an invalid tag and an invalid default. Test that the from_tag() method returns a sane default when passed an invalid tag and an invalid int default.
""" """
# GIVEN: A mocked out translate() function that just returns what it was given # GIVEN: A mocked out translate() function that just returns what it was given
with patch('openlp.plugins.songs.lib.translate') as mocked_translate: with patch('openlp.plugins.songs.lib.translate') as mocked_translate:
@ -458,3 +458,31 @@ class TestVerseType(TestCase):
# THEN: The result should be VerseType.Other # THEN: The result should be VerseType.Other
self.assertEqual(result, VerseType.Other, 'The result should be VerseType.Other, but was "%s"' % result) self.assertEqual(result, VerseType.Other, 'The result should be VerseType.Other, 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('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('@', 'asdf')
# THEN: The result should be VerseType.Other
self.assertEqual(result, VerseType.Other, 'The result should be VerseType.Other, but was "%s"' % result)
def from_tag_with_none_default_test(self):
"""
Test that the from_tag() method returns a sane default when passed an invalid tag and None as default.
"""
# GIVEN: A mocked out translate() function that just returns what it was given
with patch('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('m', None)
# THEN: The result should be None
self.assertIsNone(result, 'The result should be None, but was "%s"' % result)