# frozen_string_literal: true
require_relative "../test_helper"
module Diff
class DiffTest < Minitest::Spec
test "returns a Diff::Result" do
result = Herb.diff("
Hello
", "Hello
")
assert_kind_of Herb::Diff::Result, result
end
test "operations returns Diff::Operation instances" do
result = Herb.diff("Hello
", "World
")
assert_kind_of Herb::Diff::Operation, result.operations[0]
end
test "identical documents" do
result = Herb.diff("Hello
", "Hello
")
assert result.identical?
refute result.changed?
assert_equal 0, result.operation_count
end
test "text changed" do
result = Herb.diff("Hello
", "World
")
refute result.identical?
assert result.changed?
assert_equal 1, result.operation_count
assert_equal :text_changed, result.operations[0].type
end
test "attribute value changed" do
result = Herb.diff('Hi
', 'Hi
')
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :attribute_value_changed, result.operations[0].type
end
test "attribute added" do
result = Herb.diff("Hi
", 'Hi
')
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :attribute_added, result.operations[0].type
end
test "attribute removed" do
result = Herb.diff('Hi
', "Hi
")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :attribute_removed, result.operations[0].type
end
test "node inserted" do
result = Herb.diff("", "New
")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :node_inserted, result.operations[0].type
end
test "node removed" do
result = Herb.diff("Old
", "")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :node_removed, result.operations[0].type
end
test "erb content changed" do
result = Herb.diff("<%= foo %>", "<%= bar %>")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :erb_content_changed, result.operations[0].type
end
test "node moved with distinguishing attributes" do
result = Herb.diff(
'',
''
)
refute result.identical?
types = result.operations.map(&:type)
assert_equal [:node_moved], types
end
test "plain reorder without attributes reports text changes not moves" do
result = Herb.diff(
"",
""
)
refute result.identical?
types = result.operations.map(&:type)
assert_equal [:text_changed, :text_changed], types
end
test "move with attribute value change" do
result = Herb.diff(
'A
B
',
'B
A
'
)
refute result.identical?
types = result.operations.map(&:type)
assert_equal [:node_moved, :attribute_value_changed], types
end
test "move with content change" do
result = Herb.diff(
'Old
B
',
'B
New
'
)
refute result.identical?
types = result.operations.map(&:type)
assert_equal [:node_moved, :text_changed], types
end
test "multiple changes with unchanged subtree" do
result = Herb.diff(
'',
''
)
refute result.identical?
assert_equal 2, result.operation_count
types = result.operations.map(&:type)
assert_equal [:attribute_value_changed, :text_changed], types
end
test "operations have path" do
result = Herb.diff("Hello
", "World
")
operation = result.operations[0]
assert_kind_of Array, operation.path
refute_empty operation.path
end
test "operations have old and new nodes" do
result = Herb.diff("Hello
", "World
")
operation = result.operations[0]
assert_kind_of Herb::AST::HTMLTextNode, operation.old_node
assert_kind_of Herb::AST::HTMLTextNode, operation.new_node
end
test "inserted operation has nil old node" do
result = Herb.diff("", "New
")
operation = result.operations[0]
assert_nil operation.old_node
refute_nil operation.new_node
end
test "removed operation has nil new node" do
result = Herb.diff("Old
", "")
operation = result.operations[0]
refute_nil operation.old_node
assert_nil operation.new_node
end
test "complex erb diff" do
old_source = <<~ERB
<% if current_user %>
Hello, <%= current_user.name %>
<% end %>
ERB
new_source = <<~ERB
<% if current_user %>
Hello, <%= current_user.email %>
<% end %>
ERB
result = Herb.diff(old_source, new_source)
refute result.identical?
types = result.operations.map(&:type)
assert_equal [:attribute_value_changed, :erb_content_changed], types
end
test "wrap with ERB conditional" do
result = Herb.diff("Content
", "<% if condition? %>Content
<% end %>")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :node_wrapped, result.operations[0].type
end
test "unwrap from ERB conditional" do
result = Herb.diff("<% if condition? %>Content
<% end %>", "Content
")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :node_unwrapped, result.operations[0].type
end
test "wrap with HTML element" do
result = Herb.diff("Hello
", "Hello
")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :node_wrapped, result.operations[0].type
end
test "unwrap from HTML element" do
result = Herb.diff("", "Text
")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :node_unwrapped, result.operations[0].type
end
test "wrap with content change is not detected as wrap" do
result = Herb.diff("Old
", "<% if x %>New
<% end %>")
refute result.identical?
types = result.operations.map(&:type)
assert_equal [:node_removed, :node_inserted], types
end
test "wrapped operation has old and new nodes" do
result = Herb.diff("Content
", "<% if condition? %>Content
<% end %>")
operation = result.operations[0]
assert_kind_of Herb::AST::HTMLElementNode, operation.old_node
assert_kind_of Herb::AST::ERBIfNode, operation.new_node
end
test "to_hash serialization" do
result = Herb.diff("Hello
", "World
")
hash = result.to_hash
assert_kind_of Hash, hash
assert_equal false, hash[:identical]
assert_kind_of Array, hash[:operations]
assert_equal :text_changed, hash[:operations][0][:type]
end
test "inspect output" do
identical_result = Herb.diff("Same
", "Same
")
assert_match(/identical/, identical_result.inspect)
changed_result = Herb.diff("Hello
", "World
")
assert_match(/1 operation/, changed_result.inspect)
end
test "Diff::Result is Enumerable" do
result = Herb.diff("Hello
", "World
")
assert_kind_of Enumerable, result
types = result.map(&:type)
assert_equal [:text_changed], types
end
test "Diff::Result#each yields operations" do
result = Herb.diff("Hello
", "World
")
count = 0
result.each { count += 1 }
assert_equal result.operations.size, count
end
test "Diff::Result#each returns enumerator without block" do
result = Herb.diff("Hello
", "World
")
enumerator = result.each
assert_kind_of Enumerator, enumerator
end
test "Diff::Operation equality" do
result = Herb.diff("Hello
", "World
")
op = result.operations[0]
assert_equal op.type, :text_changed
assert_kind_of Array, op.path
assert_equal op, op
end
test "Diff::Operation is frozen" do
result = Herb.diff("Hello
", "World
")
assert_predicate result.operations[0], :frozen?
end
test "Diff::Result is frozen" do
result = Herb.diff("Hello
", "World
")
assert_predicate result, :frozen?
end
test "Diff::Operation is a Data class" do
result = Herb.diff("Hello
", "World
")
assert_kind_of Data, result.operations[0]
end
test "empty documents are identical" do
result = Herb.diff("", "")
assert_predicate result, :identical?
assert_equal 0, result.operation_count
end
test "different top-level elements are removed and inserted" do
result = Herb.diff("Hello
", "Hello")
refute result.identical?
types = result.map(&:type)
assert_equal [:node_removed, :node_inserted], types
end
test "multiple moves with distinguishing attributes" do
result = Herb.diff(
'',
''
)
refute result.identical?
move_ops = result.select { |op| op.type == :node_moved }
assert_operator move_ops.size, :>=, 2
end
test "move preserves correct indices" do
result = Herb.diff(
'',
''
)
refute result.identical?
move_ops = result.select { |op| op.type == :node_moved }
assert_equal 1, move_ops.size
move = move_ops[0]
refute_equal move.old_index, move.new_index
end
test "move among other changes" do
result = Herb.diff(
'Keep
Remove
',
'New
Keep
'
)
refute result.identical?
types = result.map(&:type)
assert_equal [:node_moved, :attribute_removed, :text_changed], types
assert_operator result.operation_count, :>=, 2
end
test "many siblings with single move" do
old_items = (1..10).map { |i| %(Item #{i}) }.join
new_items = (1..10).map { |i|
index = if i == 1
10
else
(i == 10 ? 1 : i)
end
%(Item #{index})
}.join
result = Herb.diff("", "")
refute result.identical?
move_ops = result.select { |op| op.type == :node_moved }
assert_operator move_ops.size, :>=, 1
end
test "coalesced keep detects tag identity match with attribute changes" do
result = Herb.diff(
'Content
Other
',
'Other
Content
'
)
refute result.identical?
types = result.map(&:type)
assert_equal [:node_moved, :attribute_value_changed], types
end
test "wrap detection with multiple candidates" do
result = Herb.diff(
"A
B
",
"B
"
)
refute result.identical?
types = result.map(&:type)
assert_equal [:node_wrapped], types
end
test "unwrap detection with multiple candidates" do
result = Herb.diff(
"B
",
"A
B
"
)
refute result.identical?
types = result.map(&:type)
assert_equal [:node_unwrapped], types
end
test "move does not match nodes without attributes" do
result = Herb.diff(
"",
""
)
refute result.identical?
types = result.map(&:type)
assert_equal [:text_changed, :text_changed, :text_changed], types
end
test "simultaneous moves and unchanged nodes" do
result = Herb.diff(
'A
S
B
',
'B
S
A
'
)
refute result.identical?
move_ops = result.select { |op| op.type == :node_moved }
assert_operator move_ops.size, :>=, 1
end
test "wrap in ERB block" do
result = Herb.diff(
"Content
",
"<% items.each do |item| %>Content
<% end %>"
)
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :node_wrapped, result.first.type
assert_kind_of Herb::AST::HTMLElementNode, result.first.old_node
assert_kind_of Herb::AST::ERBBlockNode, result.first.new_node
end
test "unwrap from ERB block" do
result = Herb.diff(
"<% items.each do |item| %>Content
<% end %>",
"Content
"
)
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :node_unwrapped, result.first.type
assert_kind_of Herb::AST::ERBBlockNode, result.first.old_node
assert_kind_of Herb::AST::HTMLElementNode, result.first.new_node
end
test "wrap in ERB unless" do
result = Herb.diff(
"Content
",
"<% unless hidden? %>Content
<% end %>"
)
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :node_wrapped, result.first.type
assert_kind_of Herb::AST::ERBUnlessNode, result.first.new_node
end
test "unwrap from ERB unless" do
result = Herb.diff(
"<% unless hidden? %>Content
<% end %>",
"Content
"
)
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :node_unwrapped, result.first.type
assert_kind_of Herb::AST::ERBUnlessNode, result.first.old_node
end
test "wrap text in HTML element" do
result = Herb.diff(
"hello
",
""
)
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :node_wrapped, result.first.type
assert_kind_of Herb::AST::HTMLTextNode, result.first.old_node
assert_kind_of Herb::AST::HTMLElementNode, result.first.new_node
end
test "unwrap text from HTML element" do
result = Herb.diff(
"hello
",
"hello
"
)
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :node_unwrapped, result.first.type
assert_kind_of Herb::AST::HTMLElementNode, result.first.old_node
assert_kind_of Herb::AST::HTMLTextNode, result.first.new_node
end
test "growing a whitespace run in text" do
result = Herb.diff("Hello World
", "Hello World
")
assert result.identical?
assert_equal 0, result.operation_count
end
test "shrinking a whitespace run in text" do
result = Herb.diff("Hello World
", "Hello World
")
assert result.identical?
assert_equal 0, result.operation_count
end
test "changing which whitespace characters a run is made of" do
result = Herb.diff("Hello\n\tWorld
", "Hello World
")
assert result.identical?
assert_equal 0, result.operation_count
end
test "removing the last whitespace of a run in text" do
result = Herb.diff("Hello World
", "HelloWorld
")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :text_changed, result.first.type
end
test "adding leading whitespace to text" do
result = Herb.diff("Hello
", " Hello
")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :text_changed, result.first.type
end
test "adding trailing whitespace to text" do
result = Herb.diff("Hello
", "Hello
")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :text_changed, result.first.type
end
test "changing the indentation of a nested element" do
result = Herb.diff(
"\n Hello\n
",
"\n Hello\n
"
)
assert result.identical?
assert_equal 0, result.operation_count
end
test "removing the whitespace around a nested element" do
result = Herb.diff(
"\n Hello\n
",
"Hello
"
)
refute result.identical?
assert_equal 2, result.operation_count
assert_equal [:node_removed, :node_removed], result.operations.map(&:type)
end
test "changing a whitespace run inside a pre element" do
result = Herb.diff("Hello World
", "Hello World
")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :text_changed, result.first.type
end
test "changing a whitespace run inside a textarea element" do
result = Herb.diff("", "")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :text_changed, result.first.type
end
test "changing a whitespace run nested inside a pre element" do
result = Herb.diff(
"Hello World
",
"Hello World
"
)
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :text_changed, result.first.type
end
test "changing a whitespace run inside an ERB block inside a pre element" do
result = Herb.diff(
"<% if condition %>Hello World<% end %>
",
"<% if condition %>Hello World<% end %>
"
)
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :text_changed, result.first.type
end
test "changing a whitespace run inside an ERB block outside a pre element" do
result = Herb.diff(
"<% if condition %>Hello World<% end %>
",
"<% if condition %>Hello World<% end %>
"
)
assert result.identical?
assert_equal 0, result.operation_count
end
test "changing a whitespace run inside an uppercase PRE element" do
result = Herb.diff("Hello World
", "Hello World
")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :text_changed, result.first.type
end
test "changing a whitespace run in an attribute value" do
result = Herb.diff('Hi
', 'Hi
')
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :attribute_value_changed, result.first.type
end
test "changing a whitespace run in ERB content" do
result = Herb.diff("<%= user.name %>", "<%= user.name %>")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :erb_content_changed, result.first.type
end
test "changing a whitespace run next to a real text change" do
result = Herb.diff("Hello World
", "Goodbye World
")
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :text_changed, result.first.type
end
test "track_whitespace_changes reports a whitespace_changed operation" do
result = Herb.diff("Hello World
", "Hello World
", track_whitespace_changes: true)
refute result.identical?
assert_equal 1, result.operation_count
assert_equal :whitespace_changed, result.first.type
assert_kind_of Herb::AST::HTMLTextNode, result.first.old_node
assert_kind_of Herb::AST::HTMLTextNode, result.first.new_node
end
test "track_whitespace_changes accepts a string key" do
result = Herb.diff("Hello World
", "Hello World
", "track_whitespace_changes" => true)
assert_equal [:whitespace_changed], result.operations.map(&:type)
end
test "track_whitespace_changes reports changed indentation" do
result = Herb.diff(
"\n Hello\n
",
"\n Hello\n
",
track_whitespace_changes: true
)
assert_equal [:whitespace_changed], result.operations.map(&:type)
end
test "track_whitespace_changes leaves identical documents identical" do
result = Herb.diff("Hello World
", "Hello World
", track_whitespace_changes: true)
assert result.identical?
assert_equal 0, result.operation_count
end
test "track_whitespace_changes still reports significant changes as text_changed" do
result = Herb.diff("Hello World
", "HelloWorld
", track_whitespace_changes: true)
assert_equal [:text_changed], result.operations.map(&:type)
end
test "track_whitespace_changes still reports pre content as text_changed" do
result = Herb.diff("Hello World
", "Hello World
", track_whitespace_changes: true)
assert_equal [:text_changed], result.operations.map(&:type)
end
test "track_whitespace_changes defaults to off" do
result = Herb.diff("Hello World
", "Hello World
", track_whitespace_changes: false)
assert result.identical?
assert_equal 0, result.operation_count
end
end
end