From 6b2274645aa6c88421055d3cb3516f322416167b Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Thu, 14 Feb 2013 15:54:45 +0100 Subject: [PATCH] added test for UiStrings, started with FormattingTags tests --- openlp/core/lib/formattingtags.py | 27 +++----- .../openlp_core_lib/test_formattingtags.py | 66 +++++++++++++++++++ .../openlp_core_lib/test_uistrings.py | 22 +++++++ 3 files changed, 98 insertions(+), 17 deletions(-) create mode 100644 tests/functional/openlp_core_lib/test_formattingtags.py create mode 100644 tests/functional/openlp_core_lib/test_uistrings.py diff --git a/openlp/core/lib/formattingtags.py b/openlp/core/lib/formattingtags.py index b7bbd7322..b2d8f6ea7 100644 --- a/openlp/core/lib/formattingtags.py +++ b/openlp/core/lib/formattingtags.py @@ -36,8 +36,7 @@ from openlp.core.lib import Settings, translate class FormattingTags(object): """ - Static Class to HTML Tags to be access around the code the list is managed - by the Options Tab. + Static Class to HTML Tags to be access around the code the list is managed by the Options Tab. """ html_expands = [] @@ -56,12 +55,11 @@ class FormattingTags(object): tags = [] for tag in FormattingTags.html_expands: if not tag[u'protected'] and not tag.get(u'temporary'): - # Using dict ensures that copy is made and encoding of values - # a little later does not affect tags in the original list + # Using dict ensures that copy is made and encoding of values a little later does not affect tags in + # the original list tags.append(dict(tag)) tag = tags[-1] - # Remove key 'temporary' from tags. - # It is not needed to be saved. + # Remove key 'temporary' from tags. It is not needed to be saved. if u'temporary' in tag: del tag[u'temporary'] for element in tag: @@ -73,15 +71,12 @@ class FormattingTags(object): @staticmethod def load_tags(): """ - Load the Tags from store so can be used in the system or used to - update the display. + Load the Tags from store so can be used in the system or used to update the display. """ - temporary_tags = [tag for tag in FormattingTags.html_expands - if tag.get(u'temporary')] + temporary_tags = [tag for tag in FormattingTags.html_expands if tag.get(u'temporary')] FormattingTags.html_expands = [] base_tags = [] # Append the base tags. - # Hex Color tags from http://www.w3schools.com/html/html_colornames.asp base_tags.append({u'desc': translate('OpenLP.FormattingTags', 'Red'), u'start tag': u'{r}', u'start html': u'', @@ -195,19 +190,17 @@ class FormattingTags(object): The end tag, e. g. ``{/r}`` * start html - The start html tag. For instance ```` + The start html tag. For instance ```` * end html The end html tag. For example ```` * protected - A boolean stating whether this is a build-in tag or not. Should be - ``True`` in most cases. + A boolean stating whether this is a build-in tag or not. Should be ``True`` in most cases. * temporary - A temporary tag will not be saved, but is also considered when - displaying text containing the tag. It has to be a ``boolean``. + A temporary tag will not be saved, but is also considered when displaying text containing the tag. It has + to be a ``boolean``. """ FormattingTags.html_expands.extend(tags) diff --git a/tests/functional/openlp_core_lib/test_formattingtags.py b/tests/functional/openlp_core_lib/test_formattingtags.py new file mode 100644 index 000000000..5a95e195f --- /dev/null +++ b/tests/functional/openlp_core_lib/test_formattingtags.py @@ -0,0 +1,66 @@ +""" +Package to test the openlp.core.lib.formattingtags package. +""" + +from unittest import TestCase + +from mock import patch + +from openlp.core.lib import FormattingTags + +class TestFormattingTags(TestCase): + + def get_html_tags_no_user_tags_test(self): + """ + Test the get_html_tags static method. + """ + with patch(u'openlp.core.lib.translate') as mocked_translate, \ + patch(u'openlp.core.lib.settings') as mocked_settings, \ + patch(u'openlp.core.lib.formattingtags.cPickle') as mocked_cPickle: + # GIVEN: Our mocked modules and functions. + mocked_translate.side_effect = lambda module, string_to_translate: string_to_translate + mocked_settings.value.return_value = u'' + mocked_cPickle.load.return_value = [] + + # WHEN: Get the display tags. + FormattingTags.load_tags() + old_tags_list = FormattingTags.get_html_tags() + FormattingTags.load_tags() + new_tags_list = FormattingTags.get_html_tags() + + # THEN: Lists should be identically. + assert old_tags_list == new_tags_list, u'The formatting tag lists should be identically.' + + def get_html_tags_with_user_tags_test(self): + """ + Add a tag and check if it still exists after reloading the tags list. + """ + # FIXME: not working yet. + with patch(u'openlp.core.lib.translate') as mocked_translate, \ + patch(u'openlp.core.lib.settings') as mocked_settings, \ + patch(u'openlp.core.lib.formattingtags.cPickle') as mocked_cPickle: + # GIVEN: Our mocked modules and functions. + mocked_translate.side_effect = lambda module, string_to_translate: string_to_translate + mocked_settings.value.return_value = u'' + mocked_cPickle.load.return_value = [] + tags = [{ + u'end tag': '{/aa}', + u'start html': '', + u'start tag': '{aa}', + u'protected': False, + u'end html': '', + u'desc': 'name'} + ] + + # WHEN: Get the display tags. + FormattingTags.add_html_tags(tags) + FormattingTags.load_tags() + old_tags_list = FormattingTags.get_html_tags() + FormattingTags.load_tags() + new_tags_list = FormattingTags.get_html_tags() + + # THEN: Lists should be identically. + assert old_tags_list == new_tags_list, u'The formatting tag lists with user tags should be identically.' + + + diff --git a/tests/functional/openlp_core_lib/test_uistrings.py b/tests/functional/openlp_core_lib/test_uistrings.py new file mode 100644 index 000000000..e0d0fc485 --- /dev/null +++ b/tests/functional/openlp_core_lib/test_uistrings.py @@ -0,0 +1,22 @@ +""" +Package to test the openlp.core.lib.uistrings package. +""" + +from unittest import TestCase + +from openlp.core.lib import UiStrings + +class TestUiStrings(TestCase): + + def check_same_instance_test(self): + """ + Test if the always only one instance of the UiStrings is created. + """ + # WHEN: Create two instances of the UiStrings class. + first_instance = UiStrings() + second_instance = UiStrings() + + # THEN: Check if the instances are the same. + assert first_instance is second_instance, "They should be the same instance!" + +