2024-01-31 00:48:20 +00:00
import sys
2024-07-07 06:56:46 +00:00
import re
2024-01-31 00:48:20 +00:00
from loguru import logger
2024-09-21 11:09:30 +00:00
from rl_string_helper import (
RLStringHelper ,
2024-09-23 07:27:11 +00:00
UTF16Handler ,
StringAssignmentMixin ,
2024-09-21 11:09:30 +00:00
quote_html ,
split_overlapping_ranges ,
)
2024-01-31 00:48:20 +00:00
class TestRLStringHelper :
def setup_method ( self ) :
logger . remove ( )
logger . add ( sys . stdout , level = " TRACE " )
def test_html_quote ( self ) :
2024-07-07 06:56:46 +00:00
quoted_string_1 = [ i for i in quote_html ( " <Hello world> " , " full " ) ]
assert quoted_string_1 == [ ( ( 0 , 1 ) , " < " ) , ( ( 12 , 13 ) , " > " ) ]
2024-01-31 00:48:20 +00:00
# Test with standard HTML characters
html = ' <div class= " test " >Hello & World</div> '
2024-07-07 06:56:46 +00:00
result = list ( quote_html ( html , " full " ) )
2024-09-21 11:09:30 +00:00
expected = [
( ( 0 , 1 ) , " < " ) ,
( ( 11 , 12 ) , " " " ) ,
( ( 16 , 17 ) , " " " ) ,
( ( 17 , 18 ) , " > " ) ,
( ( 24 , 25 ) , " & " ) ,
( ( 31 , 32 ) , " < " ) ,
( ( 36 , 37 ) , " > " ) ,
]
2024-07-07 06:56:46 +00:00
assert sorted ( result ) == sorted ( expected )
2024-01-31 00:48:20 +00:00
# Test with extra characters
html = ' <div class= " test " > \n Hello & World</div> '
2024-07-07 06:56:46 +00:00
result = list ( quote_html ( html , " extra " ) )
2024-09-21 11:09:30 +00:00
expected = [
( ( 0 , 1 ) , " < " ) ,
( ( 11 , 12 ) , " " " ) ,
( ( 16 , 17 ) , " " " ) ,
( ( 17 , 18 ) , " > " ) ,
( ( 25 , 26 ) , " & " ) ,
( ( 32 , 33 ) , " < " ) ,
( ( 37 , 38 ) , " > " ) ,
( ( 18 , 19 ) , " <br /> " ) ,
]
2024-07-07 06:56:46 +00:00
assert sorted ( result ) == sorted ( expected )
2024-01-31 00:48:20 +00:00
# Test with quote characters
2024-09-21 11:09:30 +00:00
html = " <div class= \" test \" >Hello & ' World ' </div> "
2024-07-07 06:56:46 +00:00
result = list ( quote_html ( html , " full " ) )
2024-09-21 11:09:30 +00:00
expected = [
( ( 0 , 1 ) , " < " ) ,
( ( 11 , 12 ) , " " " ) ,
( ( 16 , 17 ) , " " " ) ,
( ( 17 , 18 ) , " > " ) ,
( ( 24 , 25 ) , " & " ) ,
( ( 26 , 27 ) , " ' " ) ,
( ( 32 , 33 ) , " ' " ) ,
( ( 33 , 34 ) , " < " ) ,
( ( 38 , 39 ) , " > " ) ,
]
2024-07-07 06:56:46 +00:00
assert sorted ( result ) == sorted ( expected )
2024-01-31 00:48:20 +00:00
def test_basic_template ( self ) :
helper = RLStringHelper ( " Hello world " )
helper . set_template ( 0 , 5 , " <a> {{ text}}</a> " )
assert str ( helper ) == " <a>Hello</a> world "
helper . set_template ( 6 , 11 , " <b> {{ text}}</b> " )
assert str ( helper ) == " <a>Hello</a> <b>world</b> "
helper . set_template ( 0 , 11 , " <i> {{ text}}</i> " )
assert str ( helper ) == " <i><a>Hello</a> <b>world</b></i> "
2024-09-21 15:41:38 +00:00
def test_super_duper_overlapsing ( self ) :
text = " Note: The patterns and ideas discussed in this post are broadly applicable. "
helper = RLStringHelper ( text )
markups = [
{ " start " : 0 , " end " : 5 , " type " : " code " , " template " : " <code> {{ text}}</code> " } ,
{
" start " : 0 ,
" end " : 6 ,
" type " : " strong " ,
" template " : " <strong> {{ text}}</strong> " ,
} ,
{ " start " : 0 , " end " : 6 , " type " : " em " , " template " : " <em> {{ text}}</em> " } ,
]
parsed_markups = split_overlapping_ranges ( markups )
for markup in parsed_markups :
helper . set_template ( markup [ " start " ] , markup [ " end " ] , markup [ " template " ] )
expected_pattern = r " <em><strong><code>Note:</code></strong></em><em><strong> </strong></em>The patterns and ideas discussed in this post are broadly applicable \ . "
result = str ( helper )
assert re . match ( expected_pattern , result )
def test_complex_overlapping_tags ( self ) :
text = " The quick (brown) fox jumps over 13 lazy dogs! "
helper = RLStringHelper ( text )
markups = [
{
" start " : 0 ,
" end " : 46 ,
" type " : " span " ,
" template " : " <span> {{ text}}</span> " ,
} ,
{ " start " : 4 , " end " : 17 , " type " : " bold " , " template " : " <b> {{ text}}</b> " } ,
{ " start " : 10 , " end " : 21 , " type " : " italic " , " template " : " <i> {{ text}}</i> " } ,
{
" start " : 18 ,
" end " : 27 ,
" type " : " underline " ,
" template " : " <u> {{ text}}</u> " ,
} ,
{
" start " : 33 ,
" end " : 41 ,
" type " : " code " ,
" template " : " <code> {{ text}}</code> " ,
} ,
{
" start " : 40 ,
" end " : 46 ,
" type " : " link " ,
" template " : ' <a href= " # " > {{ text}}</a> ' ,
} ,
]
parsed_markups = split_overlapping_ranges ( markups )
for markup in parsed_markups :
helper . set_template ( markup [ " start " ] , markup [ " end " ] , markup [ " template " ] )
2024-09-23 07:27:11 +00:00
expected_output = """ <span>The </span><b><span>quick </span></b><i><b><span>(brown)</span></b></i><i><span> </span></i><u><i><span>fox</span></i></u><u><span> jumps</span></u><span> over </span><code><span>13 lazy</span></code><a href= " # " ><code><span> </span></code></a><a href= " # " ><span>dogs!</span></a> """
2024-09-21 15:41:38 +00:00
assert str ( helper ) == expected_output
def test_nmultibyte_emoji ( self ) :
2024-09-23 07:27:11 +00:00
text = " Noah dragged his two printers out from Settings ⚙️ < Printers & Scanners 🖨️ and dropped them in Dock or Desktop, I don’ t remember — but you can drag to both the places. "
string_helper = StringAssignmentMixin ( text )
utf_handler = UTF16Handler ( )
string_pos_matrix = list ( range ( len ( text ) ) )
updated_text , string_pos_matrix , utf_16_bang_list = utf_handler . pre_utf_16_bang (
string_helper , string_pos_matrix
)
updated_text , string_pos_matrix = utf_handler . post_utf_16_bang (
updated_text , string_pos_matrix , utf_16_bang_list
)
assert str ( updated_text ) == text
from icecream import ic
string_helper = RLStringHelper ( text , None )
string_helper . set_template ( 0 , 100000 , " <span> {{ text}}</span> " )
assert str ( string_helper ) == f " <span> { text } </span> " . replace ( " ’ " , " ' " )
string_helper = RLStringHelper ( text , [ " None " ] )
string_helper . set_template ( 39 , 76 , " <code class= ' test ' > {{ text}}</code> " )
with open ( " test.txt " , " w " ) as f :
f . write ( str ( string_helper ) + " \n " )
assert str (
string_helper
) == f " { text [ : 39 ] } <code class= ' test ' >Settings ⚙️ < Printers & Scanners 🖨</code>️ { text [ 76 : ] } " . replace (
" ’ " , " ' "
)
2024-07-07 06:56:46 +00:00
2024-01-31 00:48:20 +00:00
def test_basic_replace ( self ) :
# Replace A to B - ONE to ONE char
helper = RLStringHelper ( " ABC " )
helper . set_replace ( 0 , 1 , " B " )
assert str ( helper ) == " BBC "
# Replace first B to AA - ONE to TWO chars
helper . set_replace ( 0 , 1 , " AA " )
assert str ( helper ) == " AABC "
# Replace C to D - ONE to ONE char
helper . set_replace ( 2 , 3 , " D " )
assert str ( helper ) == " AABD "
# Replace BD to R - TWO to ONE char
helper . set_replace ( 1 , 3 , " R " )
assert str ( helper ) == " AAR "
# Replace AA to CD
helper . set_replace ( 0 , 2 , " CD " )
assert str ( helper ) == " CD "
def test_multibyte_replace ( self ) :
helper = RLStringHelper ( " TESERT - 📊 - ABC " )
helper . set_replace ( 0 , 6 , " B " )
assert helper . get_text ( ) == " B - 📊 - ABC "
2024-09-21 11:09:30 +00:00
helper = RLStringHelper (
" Your support means the world to me. If you found this article valuable and insightful, please consider giving it a round of applause by clicking the clapping hands icon 👏. "
)
2024-01-31 00:48:20 +00:00
helper . set_template ( 0 , 200 , " <kr> {{ text}}</kr> " )
helper . set_template ( 0 , 200 , " <kz> {{ text}}</kz> " )
2024-09-21 11:09:30 +00:00
assert (
helper . get_text ( )
== " <kz><kr>Your support means the world to me. If you found this article valuable and insightful, please consider giving it a round of applause by clicking the clapping hands icon 👏.</kr></kz> "
)
2024-01-31 00:48:20 +00:00
helper = RLStringHelper ( " TESERT ALMACOM - 📊 - ABC " )
helper . set_replace ( 0 , 14 , " B " )
assert helper . get_text ( ) == " B - 📊 - ABC "
helper = RLStringHelper ( " hello - 📊 - ABC " )
helper . set_template ( 0 , 5 , " <a> {{ text}}</a> " )
assert helper . get_text ( ) == " <a>hello</a> - 📊 - ABC "
helper = RLStringHelper ( " ABC 📊 - How are you? " )
helper . set_template ( 4 , 6 , " <a> {{ text}}</a> " )
assert str ( helper ) == " ABC <a>📊</a> - How are you? "
helper = RLStringHelper ( " We have a 📊, a 📊 and a 📊. " )
helper . set_template ( 0 , 30 , " <e> {{ text}}</e> " )
assert helper . get_text ( ) == " <e>We have a 📊, a 📊 and a 📊.</e> "
def test_romano ( self ) :
issue_text = " Whilst academic research papers have highlighted performance issues with the prophet since 2017, the propagation of package popularity through the data science community has been fueled by 𝙗 𝙤 𝙩 𝙝 𝙚 𝙭 𝙘 𝙚 𝙨 𝙨 𝙞 𝙫 𝙚 𝙘𝙡𝙖𝙞𝙢𝙨 𝙛𝙧𝙤𝙢 𝙩 𝙝 𝙚 𝙤 𝙧 𝙞 𝙜 𝙞 𝙣 𝙖 𝙡 𝙙𝙚𝙫𝙚𝙡𝙤𝙥𝙢𝙚𝙣𝙩 𝙩𝙚𝙖𝙢 𝙗 𝙪 𝙩 𝙢𝙤𝙧𝙚 𝙞𝙢𝙥𝙤𝙧𝙩𝙖𝙣𝙩𝙡𝙮 𝙗 𝙮 𝙢𝙖𝙧𝙠𝙚𝙩𝙞𝙣𝙜 𝙤 𝙛 𝙩 𝙝 𝙚 𝙣 𝙤 𝙣 -𝙥𝙚𝙧𝙛𝙤𝙧𝙢𝙞𝙣𝙜 𝙥 𝙖 𝙘 𝙠 𝙖 𝙜 𝙚 𝙫 𝙞 𝙖 𝙖 𝙧 𝙩 𝙞 𝙘 𝙡 𝙚 𝙨 𝙤 𝙣 𝙈𝙚𝙙𝙞𝙪𝙢 𝙖 𝙣 𝙙 𝙨 𝙤 𝙘 𝙞 𝙖 𝙡 𝙢𝙚𝙙𝙞𝙖. "
helper = RLStringHelper ( issue_text )
assert helper . get_text ( ) == issue_text
2024-09-21 15:41:38 +00:00
def test_markup_parser ( self ) :
href_markup = {
" start " : 0 ,
" end " : 12 ,
" type " : " a " ,
" template " : ' <a href= " https://readwise.io/bookreview/ {{ book_id }} " > {{ text }}</a> ' ,
}
helper = RLStringHelper ( " Hello world " )
parsed_markups = split_overlapping_ranges ( [ href_markup ] )
for markup in parsed_markups :
helper . set_template ( markup [ " start " ] , markup [ " end " ] , markup [ " template " ] )
assert (
helper . get_text ( )
== ' <a href= " https://readwise.io/bookreview/ {{ book_id }} " >Hello world</a> '
)
2024-01-31 00:48:20 +00:00
def test_medium_all ( self ) :
helper = RLStringHelper ( " ABC Hello world " )
helper . set_replace ( 0 , 1 , " B " )
assert str ( helper ) == " BBC Hello world "
helper . set_template ( 4 , 9 , " <a> {{ text}}</a> " )
assert str ( helper ) == " BBC <a>Hello</a> world "
helper . set_template ( 10 , 15 , " <b> {{ text}}</b> " )
assert str ( helper ) == " BBC <a>Hello</a> <b>world</b> "