28
28
from PyQt5 import QtCore, QtGui
30
30
from openlp.core.common.path import Path
31
from openlp.core.lib import FormattingTags, build_icon, check_item_selected, clean_tags, compare_chord_lyric, \
32
create_separated_list, create_thumb, expand_chords, expand_chords_for_printing, expand_tags, find_formatting_tags, \
31
from openlp.core.lib import build_icon, check_item_selected, create_separated_list, create_thumb, \
33
32
get_text_file_string, image_to_byte, resize_image, str_to_bool, validate_thumb
34
33
from tests.utils.constants import RESOURCE_PATH
525
524
MockedQtWidgets.QMessageBox.information.assert_called_with('parent', 'mocked translate', 'message')
526
525
assert result is False, 'The result should be False'
528
def test_clean_tags(self):
530
Test clean_tags() method.
532
with patch('openlp.core.lib.FormattingTags.get_html_tags') as mocked_get_tags:
533
# GIVEN: Mocked get_html_tags() method.
534
mocked_get_tags.return_value = [{
537
'start html': '<span style="-webkit-text-fill-color:black">',
538
'end tag': '{/b}', 'end html': '</span>', 'protected': True,
541
string_to_pass = 'ASDF<br>foo{br}bar {b}black{/b}'
542
wanted_string = 'ASDF\nfoo\nbar black'
544
# WHEN: Clean the string.
545
result_string = clean_tags(string_to_pass)
547
# THEN: The strings should be identical.
548
assert wanted_string == result_string, 'The strings should be identical'
550
def test_expand_tags(self):
552
Test the expand_tags() method.
554
with patch('openlp.core.lib.FormattingTags.get_html_tags') as mocked_get_tags:
555
# GIVEN: Mocked get_html_tags() method.
556
mocked_get_tags.return_value = [
560
'start html': '<span style="-webkit-text-fill-color:black">',
561
'end tag': '{/b}', 'end html': '</span>', 'protected': True,
567
'start html': '<span style="-webkit-text-fill-color:yellow">',
568
'end tag': '{/y}', 'end html': '</span>', 'protected': True,
574
'start html': '<span style="-webkit-text-fill-color:green">',
575
'end tag': '{/g}', 'end html': '</span>', 'protected': True,
579
string_to_pass = '{b}black{/b}{y}yellow{/y}'
580
wanted_string = '<span style="-webkit-text-fill-color:black">black</span>' + \
581
'<span style="-webkit-text-fill-color:yellow">yellow</span>'
583
# WHEN: Replace the tags.
584
result_string = expand_tags(string_to_pass)
586
# THEN: The strings should be identical.
587
assert wanted_string == result_string, 'The strings should be identical.'
589
527
def test_validate_thumb_file_does_not_exist(self):
591
529
Test the validate_thumb() function when the thumbnail does not exist
742
680
# THEN: We should have "Author 1, Author 2 and Author 3"
743
681
assert string_result == 'Author 1, Author 2 and Author 3', \
744
682
'The string should be "Author 1, Author 2, and Author 3".'
746
def test_expand_chords(self):
748
Test that the expanding of chords works as expected.
750
# GIVEN: A lyrics-line with chords
751
text_with_chords = 'H[C]alleluya.[F] [G]'
753
# WHEN: Expanding the chords
754
text_with_expanded_chords = expand_chords(text_with_chords)
756
# THEN: We should get html that looks like below
757
expected_html = '<span class="chordline firstchordline">H<span class="chord"><span><strong>C</strong></span>' \
758
'</span>alleluya.<span class="chord"><span><strong>F</strong></span></span><span class="ws">' \
759
' </span> <span class="chord"><span><strong>G</strong></span></span></span>'
760
assert expected_html == text_with_expanded_chords, 'The expanded chords should look as expected!'
762
def test_expand_chords2(self):
764
Test that the expanding of chords works as expected when special chars are involved.
766
# GIVEN: A lyrics-line with chords
767
text_with_chords = "I[D]'M NOT MOVED BY WHAT I SEE HALLE[F]LUJA[C]H"
769
# WHEN: Expanding the chords
770
text_with_expanded_chords = expand_tags(text_with_chords, True)
772
# THEN: We should get html that looks like below
773
expected_html = '<span class="chordline firstchordline">I<span class="chord"><span><strong>D</strong></span>' \
774
'</span>'M NOT MOVED BY WHAT I SEE HALLE<span class="chord"><span><strong>F</strong>' \
775
'</span></span>LUJA<span class="chord"><span><strong>C</strong></span></span>H</span>'
776
assert expected_html == text_with_expanded_chords, 'The expanded chords should look as expected!'
778
def test_compare_chord_lyric_short_chord(self):
780
Test that the chord/lyric comparing works.
782
# GIVEN: A chord and some lyric
786
# WHEN: Comparing the chord and lyrics
787
ret = compare_chord_lyric(chord, lyrics)
789
# THEN: The returned value should 0 because the lyric is longer than the chord
790
assert 0 == ret, 'The returned value should 0 because the lyric is longer than the chord'
792
def test_compare_chord_lyric_long_chord(self):
794
Test that the chord/lyric comparing works.
796
# GIVEN: A chord and some lyric
800
# WHEN: Comparing the chord and lyrics
801
ret = compare_chord_lyric(chord, lyrics)
803
# THEN: The returned value should 4 because the chord is longer than the lyric
804
assert 4 == ret, 'The returned value should 4 because the chord is longer than the lyric'
806
def test_find_formatting_tags(self):
808
Test that find_formatting_tags works as expected
810
# GIVEN: Lyrics with formatting tags and a empty list of formatting tags
811
lyrics = '{st}Amazing {r}grace{/r} how sweet the sound'
813
FormattingTags.load_tags()
815
# WHEN: Detecting active formatting tags
816
active_tags = find_formatting_tags(lyrics, tags)
818
# THEN: The list of active tags should contain only 'st'
819
assert ['st'] == active_tags, 'The list of active tags should contain only "st"'
821
def test_expand_chords_for_printing(self):
823
Test that the expanding of chords for printing works as expected.
825
# GIVEN: A lyrics-line with chords
826
text_with_chords = '{st}[D]Amazing {r}gr[D7]ace{/r} how [G]sweet the [D]sound [F]{/st}'
827
FormattingTags.load_tags()
829
# WHEN: Expanding the chords
830
text_with_expanded_chords = expand_chords_for_printing(text_with_chords, '{br}')
832
# THEN: We should get html that looks like below
833
expected_html = '<table class="line" width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td><table ' \
834
'class="segment" cellpadding="0" cellspacing="0" border="0" align="left"><tr class="chordrow">'\
835
'<td class="chord"> </td><td class="chord">D</td></tr><tr><td class="lyrics">{st}{/st}' \
836
'</td><td class="lyrics">{st}Amazing {/st}</td></tr></table><table class="segment" ' \
837
'cellpadding="0" cellspacing="0" border="0" align="left"><tr class="chordrow">' \
838
'<td class="chord"> </td><td class="chord">D7</td></tr><tr><td class="lyrics">{st}{r}gr' \
839
'{/r}{/st}</td><td class="lyrics">{r}{st}ace{/r} {/st}</td></tr></table><table ' \
840
'class="segment" cellpadding="0" cellspacing="0" border="0" align="left"><tr class="chordrow">'\
841
'<td class="chord"> </td></tr><tr><td class="lyrics">{st} {/st}</td></tr></table>' \
842
'<table class="segment" cellpadding="0" cellspacing="0" border="0" align="left"><tr ' \
843
'class="chordrow"><td class="chord"> </td></tr><tr><td class="lyrics">{st}how {/st}' \
844
'</td></tr></table><table class="segment" cellpadding="0" cellspacing="0" border="0" ' \
845
'align="left"><tr class="chordrow"><td class="chord">G</td></tr><tr><td class="lyrics">{st}' \
846
'sweet {/st}</td></tr></table><table class="segment" cellpadding="0" cellspacing="0" ' \
847
'border="0" align="left"><tr class="chordrow"><td class="chord"> </td></tr><tr><td ' \
848
'class="lyrics">{st}the {/st}</td></tr></table><table class="segment" cellpadding="0" ' \
849
'cellspacing="0" border="0" align="left"><tr class="chordrow"><td class="chord">D</td></tr>' \
850
'<tr><td class="lyrics">{st}sound {/st}</td></tr></table><table class="segment" ' \
851
'cellpadding="0" cellspacing="0" border="0" align="left"><tr class="chordrow"><td ' \
852
'class="chord"> </td></tr><tr><td class="lyrics">{st} {/st}</td></tr></table>' \
853
'<table class="segment" cellpadding="0" cellspacing="0" border="0" align="left"><tr ' \
854
'class="chordrow"><td class="chord">F</td></tr><tr><td class="lyrics">{st}{/st} </td>' \
855
'</tr></table></td></tr></table>'
856
assert expected_html == text_with_expanded_chords, 'The expanded chords should look as expected!'