added test for UiStrings, started with FormattingTags tests

This commit is contained in:
Andreas Preikschat 2013-02-14 15:54:45 +01:00
parent 61a45e87ea
commit 6b2274645a
3 changed files with 98 additions and 17 deletions

View File

@ -36,8 +36,7 @@ from openlp.core.lib import Settings, translate
class FormattingTags(object): class FormattingTags(object):
""" """
Static Class to HTML Tags to be access around the code the list is managed Static Class to HTML Tags to be access around the code the list is managed by the Options Tab.
by the Options Tab.
""" """
html_expands = [] html_expands = []
@ -56,12 +55,11 @@ class FormattingTags(object):
tags = [] tags = []
for tag in FormattingTags.html_expands: for tag in FormattingTags.html_expands:
if not tag[u'protected'] and not tag.get(u'temporary'): if not tag[u'protected'] and not tag.get(u'temporary'):
# Using dict ensures that copy is made and encoding of values # Using dict ensures that copy is made and encoding of values a little later does not affect tags in
# a little later does not affect tags in the original list # the original list
tags.append(dict(tag)) tags.append(dict(tag))
tag = tags[-1] tag = tags[-1]
# Remove key 'temporary' from tags. # Remove key 'temporary' from tags. It is not needed to be saved.
# It is not needed to be saved.
if u'temporary' in tag: if u'temporary' in tag:
del tag[u'temporary'] del tag[u'temporary']
for element in tag: for element in tag:
@ -73,15 +71,12 @@ class FormattingTags(object):
@staticmethod @staticmethod
def load_tags(): def load_tags():
""" """
Load the Tags from store so can be used in the system or used to Load the Tags from store so can be used in the system or used to update the display.
update the display.
""" """
temporary_tags = [tag for tag in FormattingTags.html_expands temporary_tags = [tag for tag in FormattingTags.html_expands if tag.get(u'temporary')]
if tag.get(u'temporary')]
FormattingTags.html_expands = [] FormattingTags.html_expands = []
base_tags = [] base_tags = []
# Append the 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'), base_tags.append({u'desc': translate('OpenLP.FormattingTags', 'Red'),
u'start tag': u'{r}', u'start tag': u'{r}',
u'start html': u'<span style="-webkit-text-fill-color:red">', u'start html': u'<span style="-webkit-text-fill-color:red">',
@ -195,19 +190,17 @@ class FormattingTags(object):
The end tag, e. g. ``{/r}`` The end tag, e. g. ``{/r}``
* start html * start html
The start html tag. For instance ``<span style=" The start html tag. For instance ``<span style="-webkit-text-fill-color:red">``
-webkit-text-fill-color:red">``
* end html * end html
The end html tag. For example ``</span>`` The end html tag. For example ``</span>``
* protected * protected
A boolean stating whether this is a build-in tag or not. Should be A boolean stating whether this is a build-in tag or not. Should be ``True`` in most cases.
``True`` in most cases.
* temporary * temporary
A temporary tag will not be saved, but is also considered when A temporary tag will not be saved, but is also considered when displaying text containing the tag. It has
displaying text containing the tag. It has to be a ``boolean``. to be a ``boolean``.
""" """
FormattingTags.html_expands.extend(tags) FormattingTags.html_expands.extend(tags)

View File

@ -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': '<span>',
u'start tag': '{aa}',
u'protected': False,
u'end html': '</span>',
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.'

View File

@ -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!"