2007-01-22 14:43:50 +01:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2007-02-04 05:52:48 +01:00
|
|
|
require 'xhtmldiff'
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
class DiffTest < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def setup
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2007-02-04 05:52:48 +01:00
|
|
|
def diff(a,b)
|
|
|
|
diff_doc = REXML::Document.new
|
2007-06-14 06:05:15 +02:00
|
|
|
div = REXML::Element.new('div', nil, {:respect_whitespace =>:all})
|
|
|
|
div.attributes['class'] = 'xhtmldiff_wrapper'
|
|
|
|
diff_doc << div
|
2007-02-04 05:52:48 +01:00
|
|
|
hd = XHTMLDiff.new(div)
|
|
|
|
parsed_a = REXML::HashableElementDelegator.new(
|
|
|
|
REXML::XPath.first(REXML::Document.new("<div>"+a+"</div>"), '/div'))
|
|
|
|
parsed_b = REXML::HashableElementDelegator.new(
|
|
|
|
REXML::XPath.first(REXML::Document.new("<div>"+b+"</div>"), '/div'))
|
|
|
|
Diff::LCS.traverse_balanced(parsed_a, parsed_b, hd)
|
|
|
|
diffs = ''
|
|
|
|
diff_doc.write(diffs, -1, true, true)
|
2007-06-14 06:05:15 +02:00
|
|
|
diffs.gsub(/\A<div class='xhtmldiff_wrapper'>(.*)<\/div>\Z/m, '\1')
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_html_diff_simple
|
|
|
|
a = 'this was the original string'
|
|
|
|
b = 'this is the new string'
|
2007-06-14 06:05:15 +02:00
|
|
|
assert_equal("<span> this<del class='diffmod'> was</del><ins class='diffmod'> is</ins> the" +
|
|
|
|
"<del class='diffmod'> original</del><ins class='diffmod'> new</ins> string</span>",
|
2007-02-04 05:52:48 +01:00
|
|
|
diff(a, b))
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_html_diff_with_multiple_paragraphs
|
|
|
|
a = "<p>this was the original string</p>"
|
2007-02-04 05:52:48 +01:00
|
|
|
b = "<p>this is</p>\n<p> the new string</p>\n<p>around the world</p>"
|
2007-01-22 14:43:50 +01:00
|
|
|
assert_equal(
|
2007-06-14 06:05:15 +02:00
|
|
|
"<p><span> this<del class='diffmod'> was</del><ins class='diffmod'> is</ins>" +
|
2007-02-04 05:52:48 +01:00
|
|
|
"<del class='diffdel'> the</del><del class='diffdel'> original</del><del class='diffdel'> string</del></span></p>" +
|
|
|
|
"<ins class='diffins'>\n</ins><ins class='diffins'><p> the new string</p></ins>" +
|
2007-06-14 06:05:15 +02:00
|
|
|
"<ins class='diffins'>\n</ins><ins class='diffins'><p>around the world</p></ins>",
|
2007-01-22 14:43:50 +01:00
|
|
|
diff(a, b))
|
|
|
|
end
|
|
|
|
|
2007-02-04 23:30:29 +01:00
|
|
|
def test_html_diff_deleting_a_paragraph
|
|
|
|
a = "<p>this is a paragraph</p>\n<p>this is a second paragraph</p>\n<p>this is a third paragraph</p>"
|
|
|
|
b = "<p>this is a paragraph</p>\n<p>this is a third paragraph</p>"
|
|
|
|
assert_equal(
|
2007-06-14 06:05:15 +02:00
|
|
|
"<p>this is a paragraph</p>\n<del class='diffdel'><p>this is a second paragraph</p></del>" +
|
|
|
|
"<del class='diffdel'>\n</del><p>this is a third paragraph</p>",
|
2007-02-04 23:30:29 +01:00
|
|
|
diff(a, b))
|
|
|
|
end
|
|
|
|
|
2007-02-04 05:52:48 +01:00
|
|
|
def test_split_paragraph_into_two
|
|
|
|
a = "<p>foo bar</p>"
|
|
|
|
b = "<p>foo</p><p>bar</p>"
|
|
|
|
assert_equal(
|
2007-06-14 06:05:15 +02:00
|
|
|
"<p><span> foo<del class='diffdel'> bar</del></span></p>" +
|
|
|
|
"<ins class='diffins'><p>bar</p></ins>",
|
2007-02-04 05:52:48 +01:00
|
|
|
diff(a,b))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_join_two_paragraphs_into_one
|
|
|
|
a = "<p>foo</p><p>bar</p>"
|
|
|
|
b = "<p>foo bar</p>"
|
|
|
|
assert_equal(
|
2007-06-14 06:05:15 +02:00
|
|
|
"<p><span> foo<ins class='diffins'> bar</ins></span></p>" +
|
|
|
|
"<del class='diffdel'><p>bar</p></del>",
|
2007-02-04 05:52:48 +01:00
|
|
|
diff(a,b))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_inline_element
|
|
|
|
a = "<p>foo bar</p>"
|
|
|
|
b = "<p>foo <b>bar</b></p>"
|
|
|
|
assert_equal(
|
2007-06-14 06:05:15 +02:00
|
|
|
"<p><span> foo<del class='diffdel'> bar</del></span>" +
|
|
|
|
"<ins class='diffins'><b>bar</b></ins></p>",
|
2007-02-04 05:52:48 +01:00
|
|
|
diff(a,b))
|
|
|
|
end
|
|
|
|
|
2007-06-14 06:05:15 +02:00
|
|
|
def test_html_diff_with_tags
|
|
|
|
a = ""
|
|
|
|
b = "<div>foo</div>"
|
|
|
|
assert_equal "<ins class='diffins'><div>foo</div></ins>", diff(a, b)
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
# FIXME this test fails (ticket #67, http://dev.instiki.org/ticket/67)
|
|
|
|
def test_html_diff_preserves_endlines_in_pre
|
2007-02-04 05:52:48 +01:00
|
|
|
a = "<pre>a\nb\nc\n</pre>"
|
|
|
|
b = "<pre>a\n</pre>"
|
2007-01-22 14:43:50 +01:00
|
|
|
assert_equal(
|
2007-06-14 06:05:15 +02:00
|
|
|
"<pre><span> a\n<del class='diffdel'>b\nc\n</del></span></pre>",
|
2007-01-22 14:43:50 +01:00
|
|
|
diff(a, b))
|
|
|
|
end
|
|
|
|
|
2007-06-14 06:05:15 +02:00
|
|
|
# FIXME. xhtmldiff fails to detect any change here
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_diff_for_tag_change
|
|
|
|
a = "<a>x</a>"
|
|
|
|
b = "<b>x</b>"
|
2007-06-14 06:05:15 +02:00
|
|
|
assert_equal "<del class='diffdel'><a>x</a></del><ins class='diffins'><b>x</b></ins>", diff(a, b)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|