2012-12-06 22:19:17 +00:00
|
|
|
"""
|
|
|
|
Package to test the openlp.core.lib package.
|
|
|
|
"""
|
|
|
|
from unittest import TestCase
|
|
|
|
|
2012-12-07 17:47:33 +00:00
|
|
|
from mock import MagicMock
|
|
|
|
|
|
|
|
from openlp.core.lib import str_to_bool, translate
|
2012-12-06 22:19:17 +00:00
|
|
|
|
|
|
|
class TestLibModule(TestCase):
|
|
|
|
|
|
|
|
def str_to_bool_with_bool_test(self):
|
|
|
|
"""
|
|
|
|
Test the str_to_bool function with boolean input
|
|
|
|
"""
|
|
|
|
#GIVEN: A boolean value set to true
|
|
|
|
true_boolean = True
|
|
|
|
|
|
|
|
# WHEN: We "convert" it to a bool
|
|
|
|
true_result = str_to_bool(true_boolean)
|
|
|
|
|
|
|
|
# THEN: We should get back a True bool
|
|
|
|
assert isinstance(true_result, bool), u'The result should be a boolean'
|
|
|
|
assert true_result is True, u'The result should be True'
|
|
|
|
|
|
|
|
#GIVEN: A boolean value set to false
|
|
|
|
false_boolean = False
|
|
|
|
|
|
|
|
# WHEN: We "convert" it to a bool
|
|
|
|
false_result = str_to_bool(false_boolean)
|
|
|
|
|
|
|
|
# THEN: We should get back a True bool
|
|
|
|
assert isinstance(false_result, bool), u'The result should be a boolean'
|
|
|
|
assert false_result is False, u'The result should be True'
|
|
|
|
|
|
|
|
def str_to_bool_with_invalid_test(self):
|
|
|
|
"""
|
|
|
|
Test the str_to_bool function with a set of invalid inputs
|
|
|
|
"""
|
|
|
|
# GIVEN: An integer value
|
|
|
|
int_string = 1
|
|
|
|
|
|
|
|
# WHEN: we convert it to a bool
|
|
|
|
int_result = str_to_bool(int_string)
|
|
|
|
|
|
|
|
# THEN: we should get back a false
|
|
|
|
assert int_result is False, u'The result should be False'
|
|
|
|
|
|
|
|
# GIVEN: An string value with completely invalid input
|
|
|
|
invalid_string = u'my feet are wet'
|
|
|
|
|
|
|
|
# WHEN: we convert it to a bool
|
|
|
|
str_result = str_to_bool(invalid_string)
|
|
|
|
|
|
|
|
# THEN: we should get back a false
|
|
|
|
assert str_result is False, u'The result should be False'
|
|
|
|
|
|
|
|
def str_to_bool_with_false_values_test(self):
|
|
|
|
"""
|
2012-12-06 22:21:19 +00:00
|
|
|
Test the str_to_bool function with a set of false inputs
|
2012-12-06 22:19:17 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: A string set to "false"
|
|
|
|
false_string = u'false'
|
|
|
|
|
|
|
|
# WHEN: we convert it to a bool
|
|
|
|
false_result = str_to_bool(false_string)
|
|
|
|
|
|
|
|
# THEN: we should get back a false
|
|
|
|
assert false_result is False, u'The result should be False'
|
|
|
|
|
|
|
|
# GIVEN: An string set to "NO"
|
|
|
|
no_string = u'NO'
|
|
|
|
|
|
|
|
# WHEN: we convert it to a bool
|
|
|
|
str_result = str_to_bool(no_string)
|
|
|
|
|
|
|
|
# THEN: we should get back a false
|
|
|
|
assert str_result is False, u'The result should be False'
|
|
|
|
|
|
|
|
def str_to_bool_with_true_values_test(self):
|
|
|
|
"""
|
2012-12-06 22:21:19 +00:00
|
|
|
Test the str_to_bool function with a set of true inputs
|
2012-12-06 22:19:17 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: A string set to "True"
|
|
|
|
true_string = u'True'
|
|
|
|
|
|
|
|
# WHEN: we convert it to a bool
|
|
|
|
true_result = str_to_bool(true_string)
|
|
|
|
|
|
|
|
# THEN: we should get back a true
|
|
|
|
assert true_result is True, u'The result should be True'
|
|
|
|
|
|
|
|
# GIVEN: An string set to "yes"
|
|
|
|
yes_string = u'yes'
|
|
|
|
|
|
|
|
# WHEN: we convert it to a bool
|
|
|
|
str_result = str_to_bool(yes_string)
|
|
|
|
|
|
|
|
# THEN: we should get back a true
|
|
|
|
assert str_result is True, u'The result should be True'
|
|
|
|
|
2012-12-07 17:47:33 +00:00
|
|
|
def translate_test(self):
|
|
|
|
"""
|
|
|
|
Test the translate() function
|
|
|
|
"""
|
|
|
|
# GIVEN: A string to translate and a mocked Qt translate function
|
|
|
|
context = u'OpenLP.Tests'
|
|
|
|
text = u'Untranslated string'
|
|
|
|
comment = u'A comment'
|
|
|
|
encoding = 1
|
|
|
|
n = 1
|
|
|
|
mocked_translate = MagicMock(return_value=u'Translated string')
|
|
|
|
|
|
|
|
# WHEN: we call the translate function
|
|
|
|
result = translate(context, text, comment, encoding, n, mocked_translate)
|
|
|
|
|
|
|
|
# THEN: the translated string should be returned, and the mocked function should have been called
|
|
|
|
mocked_translate.assert_called_with(context, text, comment, encoding, n)
|
|
|
|
assert result == u'Translated string', u'The translated string should have been returned'
|
|
|
|
|