#!/usr/bin/env ruby require File.expand_path(File.dirname(__FILE__) + '/../test_helper') require 'xhtmldiff' class DiffTest < Test::Unit::TestCase def setup end def diff(a,b) diff_doc = REXML::Document.new div = REXML::Element.new('div', nil, {:respect_whitespace =>:all}) div.attributes['class'] = 'xhtmldiff_wrapper' diff_doc << div hd = XHTMLDiff.new(div) parsed_a = REXML::HashableElementDelegator.new( REXML::XPath.first(REXML::Document.new("
"+a+"
"), '/div')) parsed_b = REXML::HashableElementDelegator.new( REXML::XPath.first(REXML::Document.new("
"+b+"
"), '/div')) Diff::LCS.traverse_balanced(parsed_a, parsed_b, hd) diffs = '' diff_doc.write(diffs, -1, true, true) diffs.gsub(/\A
(.*)<\/div>\Z/m, '\1') end def test_html_diff_simple a = 'this was the original string' b = 'this is the new string' assert_equal(" this was is the" + " original new string", diff(a, b)) end def test_html_diff_with_multiple_paragraphs a = "

this was the original string

" b = "

this is

\n

the new string

\n

around the world

" assert_equal( "

this was is" + " the original string

" + "\n

the new string

" + "\n

around the world

", diff(a, b)) end def test_html_diff_deleting_a_paragraph a = "

this is a paragraph

\n

this is a second paragraph

\n

this is a third paragraph

" b = "

this is a paragraph

\n

this is a third paragraph

" assert_equal( "

this is a paragraph

\n

this is a second paragraph

" + "\n

this is a third paragraph

", diff(a, b)) end def test_split_paragraph_into_two a = "

foo bar

" b = "

foo

bar

" assert_equal( "

foo bar

" + "

bar

", diff(a,b)) end def test_join_two_paragraphs_into_one a = "

foo

bar

" b = "

foo bar

" assert_equal( "

foo bar

" + "

bar

", diff(a,b)) end def test_add_inline_element a = "

foo bar

" b = "

foo bar

" assert_equal( "

foo bar" + "bar

", diff(a,b)) end def test_html_diff_with_tags a = "" b = "
foo
" assert_equal "
foo
", diff(a, b) end def test_diff_for_tag_change a = "x" b = "x" assert_equal "xx", diff(a, b) end def test_diff_for_tag_change_II a = "" b = "
    \n
  1. x
  2. \n
  3. y
  4. \n
" assert_equal "" + "
    \n
  1. x
  2. \n
  3. y
  4. \n
", diff(a, b) end # FIXME this test fails (ticket #67, http://dev.instiki.org/ticket/67) def test_html_diff_preserves_endlines_in_pre a = "
a\nb\nc\n
" b = "
a\n
" assert_equal( "
 a\nb\nc\n
", diff(a, b)) end end