2023-12-02 00:40:14 +02:00
import pytest
2025-02-01 17:44:06 +02:00
from talemate . util import ensure_dialog_format , clean_dialogue , remove_trailing_markers
2023-12-02 00:40:14 +02:00
2025-06-03 12:26:12 +03:00
MULTILINE_TEST_A_INPUT = """
\" The first line.
The second line .
- list item
- list item
The third line . \"
"""
MULTILINE_TEST_A_EXPECTED = """
\" The first line.
The second line .
- list item
- list item
The third line . \"
"""
2023-12-02 00:40:14 +02:00
@pytest.mark.parametrize ( " input, expected " , [
( ' Hello how are you? ' , ' Hello how are you? ' ) ,
( ' " Hello how are you? " ' , ' " Hello how are you? " ' ) ,
( ' " Hello how are you? " he asks " I am fine " ' , ' " Hello how are you? " *he asks* " I am fine " ' ) ,
( ' Hello how are you? *he asks* I am fine ' , ' " Hello how are you? " *he asks* " I am fine " ' ) ,
( ' Hello how are you? " *he asks* I am fine ' , ' " Hello how are you? " *he asks* " I am fine " ' ) ,
( ' Hello how are you? " *he asks I am fine ' , ' " Hello how are you? " *he asks I am fine* ' ) ,
( ' Hello how are you? " *he asks* " I am fine " * ' , ' " Hello how are you? " *he asks* " I am fine " ' ) ,
( ' " Hello how are you *he asks* I am fine " ' , ' " Hello how are you " *he asks* " I am fine " ' ) ,
( ' This is a string without any markers ' , ' This is a string without any markers ' ) ,
( ' This is a string with an ending quote " ' , ' " This is a string with an ending quote " ' ) ,
( ' This is a string with an ending asterisk* ' , ' *This is a string with an ending asterisk* ' ) ,
( ' " Mixed markers* ' , ' *Mixed markers* ' ) ,
2024-01-19 11:47:38 +02:00
( ' *narrative.* dialogue " *more narrative.* ' , ' *narrative.* " dialogue " *more narrative.* ' ) ,
( ' " *messed up dialogue formatting.* " *some narration.* ' , ' " messed up dialogue formatting. " *some narration.* ' ) ,
( ' * " messed up narration formatting. " * " some dialogue. " ' , ' " messed up narration formatting. " " some dialogue. " ' ) ,
2024-03-29 21:37:28 +02:00
( ' Some dialogue and two line-breaks right after, followed by narration. \n \n *Narration* ' , ' " Some dialogue and two line-breaks right after, followed by narration. " \n \n *Narration* ' ) ,
( ' *Some narration with a " quoted " string in it.* Then some unquoted dialogue. \n \n *More narration.* ' , ' *Some narration with a* " quoted " *string in it.* " Then some unquoted dialogue. " \n \n *More narration.* ' ) ,
( ' *Some narration* Some dialogue but not in quotes. * ' , ' *Some narration* " Some dialogue but not in quotes. " ' ) ,
2024-11-24 15:43:27 +02:00
( ' *First line \n Second line \n Third line* ' , ' *First line \n Second line \n Third line* ' ) ,
2025-06-03 12:26:12 +03:00
( MULTILINE_TEST_A_INPUT , MULTILINE_TEST_A_EXPECTED ) ,
2023-12-02 00:40:14 +02:00
] )
def test_dialogue_cleanup ( input , expected ) :
2024-01-19 11:47:38 +02:00
assert ensure_dialog_format ( input ) == expected
@pytest.mark.parametrize ( " input, expected, main_name " , [
( " bob: says a sentence " , " bob: says a sentence " , " bob " ) ,
2024-03-10 18:03:12 +02:00
( " bob: says a sentence \n bob: says another sentence " , " bob: says a sentence \n says another sentence " , " bob " ) ,
2024-01-19 11:47:38 +02:00
( " bob: says a sentence with a colon: to explain something " , " bob: says a sentence with a colon: to explain something " , " bob " ) ,
( " bob: i have a riddle for you, alice: the riddle " , " bob: i have a riddle for you, alice: the riddle " , " bob " ) ,
( " bob: says something \n alice: says something else " , " bob: says something " , " bob " ) ,
( " bob: says a sentence. then a " , " bob: says a sentence. " , " bob " ) ,
2024-03-10 18:03:12 +02:00
( " bob: first paragraph \n \n second paragraph " , " bob: first paragraph \n \n second paragraph " , " bob " ) ,
# movie script new speaker cutoff
( " bob: says a sentence \n \n ALICE \n says something else " , " bob: says a sentence " , " bob " ) ,
2024-01-19 11:47:38 +02:00
] )
def test_clean_dialogue ( input , expected , main_name ) :
others = [ " alice " , " charlie " ]
2025-02-01 17:44:06 +02:00
assert clean_dialogue ( input , main_name ) == expected
@pytest.mark.parametrize ( " input, expected " , [
( ' Hello how are you? " ' , ' Hello how are you? ' ) ,
( ' Hello how are you? * ' , ' Hello how are you? ' ) ,
( ' Hello how are you? { ' , ' Hello how are you? ' ) ,
( ' Hello how are you? [ ' , ' Hello how are you? ' ) ,
( ' Hello how are you? ( ' , ' Hello how are you? ' ) ,
( ' " Hello how are you? " ' , ' " Hello how are you? " ' ) ,
( ' " Hello how are you? " " ' , ' " Hello how are you? " ' ) ,
( ' " Hello how are you? " * ' , ' " Hello how are you? " ' ) ,
( ' " Hello how are you? " * " ' , ' " Hello how are you? " ' ) ,
( ' *He says* " Hello how are you? " ' , ' *He says* " Hello how are you? " ' ) ,
( ' *He says* " Hello how are you? " * ' , ' *He says* " Hello how are you? " ' ) ,
( ' *He says* " Hello how are you? " * " ' , ' *He says* " Hello how are you? " ' ) ,
( ' (Some thoughts) ' , ' (Some thoughts) ' ) ,
( ' (Some thoughts) ' , ' (Some thoughts) ' ) ,
( ' (Some thoughts) ( ' , ' (Some thoughts) ' ) ,
( ' (Some thoughts) [ ' , ' (Some thoughts) ' ) ,
] )
def test_remove_trailing_markers ( input , expected ) :
2025-06-03 12:26:12 +03:00
assert remove_trailing_markers ( input ) == expected
@pytest.mark.parametrize ( " input, anchor_length, expected_non_anchor, expected_anchor " , [
( " " , 10 , " " , " " ) ,
( " Hello " , 10 , " " , " Hello " ) ,
( " This is a short example " , 10 , " This is " , " a short example " ) ,
( " One two three four " , 4 , " One two " , " three four " ) ,
( " This is a longer example with more than ten words to test the anchor functionality " , 10 ,
" This is a longer example " , " with more than ten words to test the anchor functionality " ) ,
( " One two three four five six seven eight nine ten " , 10 ,
" One two three four five " , " six seven eight nine ten " ) ,
( " Two words " , 10 , " Two " , " words " ) ,
( " One Two Three " , 3 , " One " , " Two Three " ) ,
] )
def test_split_anchor_text ( input , anchor_length , expected_non_anchor , expected_anchor ) :
from talemate . util . dialogue import split_anchor_text
non_anchor , anchor = split_anchor_text ( input , anchor_length )
assert non_anchor == expected_non_anchor
assert anchor == expected_anchor