import time
from controller.Html_differ import Html_differ
class Test_html_differ( object ):
def setUp( self ):
self.differ = Html_differ()
def test_convert_html_to_list( self ):
result = self.differ.convert_html_to_list( u"foo bar baz quux" )
assert len( result ) == 7
assert result[ 0 ] == u"foo "
assert result[ 1 ] == u""
assert result[ 2 ] == u"bar "
assert result[ 3 ] == u"baz"
assert result[ 4 ] == u""
assert result[ 5 ] == u" "
assert result[ 6 ] == u"quux"
def test_convert_html_to_list_with_character_ref( self ):
result = self.differ.convert_html_to_list( u"foo # quux" )
assert len( result ) == 4
assert result[ 0 ] == u"foo "
assert result[ 1 ] == u"#"
assert result[ 2 ] == u" "
assert result[ 3 ] == u"quux"
def test_convert_html_to_list_with_entity_ref( self ):
result = self.differ.convert_html_to_list( u"foo quux" )
assert len( result ) == 4
assert result[ 0 ] == u"foo "
assert result[ 1 ] == u" "
assert result[ 2 ] == u" "
assert result[ 3 ] == u"quux"
def test_diff_with_insert( self ):
a = 'foo bar baz quux'
b = 'foo bar whee baz quux'
result = self.differ.diff( a, b )
assert result == 'foo bar whee baz quux'
def test_diff_with_delete( self ):
a = 'foo bar baz quux'
b = 'foo bar quux'
result = self.differ.diff( a, b )
assert result == 'foo bar baz quux'
def test_diff_with_replace( self ):
a = 'foo bar baz quux'
b = 'foo bar whee quux'
result = self.differ.diff( a, b )
assert result == 'foo bar baz whee quux'
def test_diff_with_italics( self ):
a = 'foo bar baz quux'
b = 'foo bar baz quux'
result = self.differ.diff( a, b )
assert result == 'foo bar baz bar baz quux'
def test_diff_with_italics_and_insert( self ):
a = 'foo bar baz quux'
b = 'foo bar whee baz quux'
result = self.differ.diff( a, b )
assert result == 'foo bar baz bar whee baz quux'
def test_diff_with_italics_twice( self ):
a = 'foo bar baz quux foo'
b = 'foo bar baz quux wheefoo'
result = self.differ.diff( a, b )
# note the screwy placement of the tags. this is the best the differ can do for now
assert result == 'foo bar baz quux wheefoo'
def test_diff_with_link( self ):
a = 'foo bar baz quux'
b = 'foo bar baz quux'
result = self.differ.diff( a, b )
assert result == 'foo bar baz baz quux'
def test_diff_with_br( self ):
a = 'foo bar baz quux'
b = 'foo bar
baz quux'
result = self.differ.diff( a, b )
assert result == 'foo bar
baz quux'
def test_track_open_tags( self ):
open_tags = []
self.differ.track_open_tags( u"foo ", open_tags )
assert open_tags == []
self.differ.track_open_tags( u"
", open_tags )
assert open_tags == []
self.differ.track_open_tags( u"", open_tags )
assert open_tags == [ u"i" ]
self.differ.track_open_tags( u"bar ", open_tags )
assert open_tags == [ u"i" ]
self.differ.track_open_tags( u'', open_tags )
assert open_tags == [ u"i", u"a" ]
self.differ.track_open_tags( u"baz", open_tags )
assert open_tags == [ u"i", u"a" ]
self.differ.track_open_tags( u"
", open_tags )
assert open_tags == [ u"i", u"a" ]
self.differ.track_open_tags( u"", open_tags )
assert open_tags == [ u"i" ]
self.differ.track_open_tags( u"", open_tags )
assert open_tags == []
self.differ.track_open_tags( u"quux", open_tags )
assert open_tags == []
def test_prepare_lists_with_insert( self ):
a = [ 'foo ', 'bar ', 'baz ', 'quux' ]
b = [ 'foo ', 'bar ', 'whee ', 'baz ', 'quux' ]
result = self.differ.prepare_lists( a, b )
assert len( result ) == 2
( new_a, new_b ) = result
# there should be no change
assert new_a == a
assert new_b == b
def test_prepare_lists_with_delete( self ):
a = [ 'foo ', 'bar ', 'baz ', 'quux' ]
b = [ 'foo ', 'bar ', 'quux' ]
result = self.differ.prepare_lists( a, b )
assert len( result ) == 2
( new_a, new_b ) = result
# there should be no change
assert new_a == a
assert new_b == b
def test_prepare_lists_with_replace( self ):
a = [ 'foo ', 'bar ', 'baz ', 'quux' ]
b = [ 'foo ', 'bar ', 'whee ', 'quux' ]
result = self.differ.prepare_lists( a, b )
assert len( result ) == 2
( new_a, new_b ) = result
# there should be no change
assert new_a == a
assert new_b == b
def test_prepare_lists_with_italics( self ):
a = [ 'foo ', 'bar ', 'baz ', 'quux' ]
b = [ 'foo ', '', 'bar ', 'baz', ' ', 'quux' ]
result = self.differ.prepare_lists( a, b )
assert len( result ) == 2
( new_a, new_b ) = result
# the elements within italics should be merged
assert new_a == [ 'foo ', 'bar baz ', 'quux' ]
assert new_b == [ 'foo ', 'bar baz ', 'quux' ]
def test_prepare_lists_with_italics_and_insert( self ):
a = [ 'foo ', 'bar ', 'baz ', 'quux' ]
b = [ 'foo ', '', 'bar ', 'whee ', 'baz', ' ', 'quux' ]
result = self.differ.prepare_lists( a, b )
assert len( result ) == 2
( new_a, new_b ) = result
# the elements within italics should be merged
assert new_a == [ 'foo ', 'bar baz ', 'quux' ]
assert new_b == [ 'foo ', 'bar whee baz ', 'quux' ]
def test_prepare_lists_with_italics_twice( self ):
a = [ 'foo ', 'bar ', 'baz ', 'quux ', '', 'foo', '' ]
b = [ 'foo ', 'bar ', 'baz ', 'quux ', '', 'whee', '', '', 'foo', '' ]
result = self.differ.prepare_lists( a, b )
assert len( result ) == 2
( new_a, new_b ) = result
# there should be no change, because prepare_lists() doesn't know how to merge a complex case like this
assert new_a == a
assert new_b == b
def test_prepare_lists_with_link( self ):
a = [ 'foo ', 'bar ', 'baz ', 'quux' ]
b = [ 'foo ', '', 'bar ', 'baz', ' ', 'quux' ]
result = self.differ.prepare_lists( a, b )
assert len( result ) == 2
( new_a, new_b ) = result
# the elements within italics should be merged
assert new_a == [ 'foo ', 'bar baz ', 'quux' ]
assert new_b == [ 'foo ', 'bar baz ', 'quux' ]
def test_prepare_lists_with_br( self ):
a = [ 'foo ', 'bar ', 'baz ', 'quux' ]
b = [ 'foo ', 'bar ', '
', '
', 'baz ', 'quux' ]
result = self.differ.prepare_lists( a, b )
assert len( result ) == 2
( new_a, new_b ) = result
# there should be no change
assert new_a == a
assert new_b == b
def test_prepare_lists_with_style_and_timing( self ):
# An older version of the code took a really long time to parse certain
# lengthy style strings due to a backtracking regular expression, so check
# for that regression.
start_time = time.time()
a = [ 'foo ', 'bar ', 'baz ', 'quux' ]
b = [ 'foo ', 'bar ', 'baz ', '' , 'quux' ]
result = self.differ.prepare_lists( a, b )
assert time.time() - start_time < 0.5
def test_diff_lists_with_insert( self ):
a = [ 'foo ', 'bar ', 'baz ', 'quux' ]
b = [ 'foo ', 'bar ', 'whee ', 'baz ', 'quux' ]
result = self.differ.diff_lists( a, b )
assert result == 'foo bar whee baz quux'
def test_diff_lists_with_delete( self ):
a = [ 'foo ', 'bar ', 'baz ', 'quux' ]
b = [ 'foo ', 'bar ', 'quux' ]
result = self.differ.diff_lists( a, b )
assert result == 'foo bar baz quux'
def test_diff_lists_with_replace( self ):
a = [ 'foo ', 'bar ', 'baz ', 'quux' ]
b = [ 'foo ', 'bar ', 'whee ', 'quux' ]
result = self.differ.diff_lists( a, b )
assert result == 'foo bar baz whee quux'
def test_diff_lists_with_italics( self ):
a = [ 'foo ', 'bar baz ', 'quux' ]
b = [ 'foo ', 'bar baz ', 'quux' ]
result = self.differ.diff_lists( a, b )
assert result == 'foo bar baz bar baz quux'
def test_diff_lists_with_italics_and_insert( self ):
a = [ 'foo ', 'bar baz ', 'quux' ]
b = [ 'foo ', 'bar whee baz ', 'quux' ]
result = self.differ.diff_lists( a, b )
assert result == 'foo bar baz bar whee baz quux'
def test_diff_lists_with_italics_twice( self ):
a = [ 'foo ', 'bar ', 'baz ', 'quux ', 'foo' ]
b = [ 'foo ', 'bar ', 'baz ', 'quux ', 'whee', 'foo' ]
result = self.differ.diff_lists( a, b )
assert result == 'foo bar baz quux wheefoo'
def test_diff_lists_with_link( self ):
a = [ 'foo ', 'bar baz ', 'quux' ]
b = [ 'foo ', 'bar baz ', 'quux' ]
result = self.differ.diff_lists( a, b )
assert result == 'foo bar baz bar baz quux'
def test_diff_lists_with_br( self ):
a = [ 'foo ', 'bar ', 'baz ', 'quux' ]
b = [ 'foo ', 'bar ', '
', '
', 'baz ', 'quux' ]
result = self.differ.diff_lists( a, b )
assert result == 'foo bar
baz quux'