Added some tests and fixed a bug.

This commit is contained in:
Tomas Groth 2017-04-11 14:44:28 +02:00
parent c97aaab763
commit 5187b7b945
2 changed files with 50 additions and 2 deletions

View File

@ -470,6 +470,7 @@ def find_formatting_tags(text, active_formatting_tags):
start_tag = True start_tag = True
if char == '/': if char == '/':
start_tag = False start_tag = False
char = next(word_it)
while char != '}': while char != '}':
tag += char tag += char
char = next(word_it) char = next(word_it)
@ -479,10 +480,12 @@ def find_formatting_tags(text, active_formatting_tags):
if formatting_tag['end tag']: if formatting_tag['end tag']:
if start_tag: if start_tag:
# prepend the new tag to the list of active formatting tags # prepend the new tag to the list of active formatting tags
active_formatting_tags[:0] = tag active_formatting_tags[:0] = [tag]
else: else:
# remove the tag from the list # remove the tag from the list
active_formatting_tags.remove(tag) active_formatting_tags.remove(tag)
# Break out of the loop matching the found tag against the tag list.
break
return active_formatting_tags return active_formatting_tags

View File

@ -29,8 +29,10 @@ from datetime import datetime, timedelta
from PyQt5 import QtCore, QtGui from PyQt5 import QtCore, QtGui
from openlp.core.lib import FormattingTags
from openlp.core.lib import build_icon, check_item_selected, clean_tags, create_thumb, create_separated_list, \ from openlp.core.lib import build_icon, check_item_selected, clean_tags, create_thumb, create_separated_list, \
expand_tags, get_text_file_string, image_to_byte, resize_image, str_to_bool, validate_thumb, expand_chords expand_tags, get_text_file_string, image_to_byte, resize_image, str_to_bool, validate_thumb, expand_chords, \
compare_chord_lyric, find_formatting_tags
from tests.functional import MagicMock, patch from tests.functional import MagicMock, patch
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'resources')) TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'resources'))
@ -762,3 +764,46 @@ class TestLib(TestCase):
'</span>alleluya.<span class="chord"><span><strong>F</strong></span></span><span class="ws">' \ '</span>alleluya.<span class="chord"><span><strong>F</strong></span></span><span class="ws">' \
'&nbsp;&nbsp;</span> <span class="chord"><span><strong>G</strong></span></span></span>' '&nbsp;&nbsp;</span> <span class="chord"><span><strong>G</strong></span></span></span>'
self.assertEqual(expected_html, text_with_expanded_chords, 'The expanded chords should look as expected!') self.assertEqual(expected_html, text_with_expanded_chords, 'The expanded chords should look as expected!')
def test_compare_chord_lyric_short_chord(self):
"""
Test that the chord/lyric comparing works.
"""
# GIVEN: A chord and some lyric
chord = 'C'
lyrics = 'alleluya'
# WHEN: Comparing the chord and lyrics
ret = compare_chord_lyric(chord, lyrics)
# THEN: The returned value should 0 because the lyric is longer than the chord
self.assertEquals(0, ret, 'The returned value should 0 because the lyric is longer than the chord')
def test_compare_chord_lyric_long_chord(self):
"""
Test that the chord/lyric comparing works.
"""
# GIVEN: A chord and some lyric
chord = 'Gsus'
lyrics = 'me'
# WHEN: Comparing the chord and lyrics
ret = compare_chord_lyric(chord, lyrics)
# THEN: The returned value should 4 because the chord is longer than the lyric
self.assertEquals(4, ret, 'The returned value should 4 because the chord is longer than the lyric')
def test_find_formatting_tags(self):
"""
Test that find_formatting_tags works as expected
"""
# GIVEN: Lyrics with formatting tags and a empty list of formatting tags
lyrics = '{st}Amazing {r}grace{/r} how sweet the sound'
tags = []
FormattingTags.load_tags()
# WHEN: Detecting active formatting tags
active_tags = find_formatting_tags(lyrics, tags)
# THEN: The list of active tags should contain only 'st'
self.assertListEqual(['st'], active_tags, 'The list of active tags should contain only "st"')