2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe ONPDiff do
|
2013-12-17 01:39:49 +08:00
|
|
|
describe "diff" do
|
|
|
|
it "returns an empty array when there is no content to diff" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(ONPDiff.new("", "").diff).to eq([])
|
2013-12-17 01:39:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an array with the operation code for each element" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(ONPDiff.new("abcd", "abef").diff).to eq(
|
|
|
|
[["a", :common], ["b", :common], ["e", :add], ["f", :add], ["c", :delete], ["d", :delete]],
|
|
|
|
)
|
2013-12-17 01:39:49 +08:00
|
|
|
end
|
|
|
|
|
2019-06-27 07:45:52 +08:00
|
|
|
it "bails out on large diffs" do
|
|
|
|
a = SecureRandom.alphanumeric(5_000)
|
|
|
|
b = SecureRandom.alphanumeric(5_000)
|
|
|
|
expect(ONPDiff.new(a, b).diff).to eq([])
|
|
|
|
end
|
2013-12-17 01:39:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "short_diff" do
|
|
|
|
it "returns an empty array when there is no content to diff" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(ONPDiff.new("", "").short_diff).to eq([])
|
2013-12-17 01:39:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an array with the operation code for each element" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(ONPDiff.new("abc", "acd").short_diff).to eq(
|
|
|
|
[["a", :common], ["b", :delete], ["c", :common], ["d", :add]],
|
|
|
|
)
|
2013-12-17 01:39:49 +08:00
|
|
|
end
|
|
|
|
|
2021-05-21 09:43:47 +08:00
|
|
|
it "returns an array with sequentially similar operations merged" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(ONPDiff.new("abcd", "abef").short_diff).to eq(
|
|
|
|
[["ab", :common], ["ef", :add], ["cd", :delete]],
|
|
|
|
)
|
2013-12-17 01:39:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-11 15:50:37 +08:00
|
|
|
describe "paragraph_diff" do
|
|
|
|
it "returns an empty array when there is no content to diff" do
|
|
|
|
expect(ONPDiff.new("", "").paragraph_diff).to eq([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an array with the operation code for each element" do
|
|
|
|
expect(ONPDiff.new("abc", "acd").paragraph_diff).to eq(
|
|
|
|
[["a", :common], ["b", :delete], ["c", :common], ["d", :add]],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-10-16 23:59:42 +08:00
|
|
|
it "pairs as many elements as possible" do
|
2019-10-11 15:50:37 +08:00
|
|
|
expect(ONPDiff.new("abcd", "abef").paragraph_diff).to eq(
|
|
|
|
[["a", :common], ["b", :common], ["e", :add], ["c", :delete], ["f", :add], ["d", :delete]],
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
|
2019-10-11 15:50:37 +08:00
|
|
|
expect(ONPDiff.new("abcde", "abfg").paragraph_diff).to eq(
|
|
|
|
[
|
|
|
|
["a", :common],
|
|
|
|
["b", :common],
|
|
|
|
["c", :delete],
|
|
|
|
["d", :delete],
|
|
|
|
["f", :add],
|
|
|
|
["e", :delete],
|
|
|
|
["g", :add],
|
|
|
|
],
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
|
2019-10-11 15:50:37 +08:00
|
|
|
expect(ONPDiff.new("abcd", "abefg").paragraph_diff).to eq(
|
|
|
|
[
|
|
|
|
["a", :common],
|
|
|
|
["b", :common],
|
|
|
|
["e", :add],
|
|
|
|
["f", :add],
|
|
|
|
["c", :delete],
|
|
|
|
["g", :add],
|
|
|
|
["d", :delete],
|
|
|
|
],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2013-12-17 01:39:49 +08:00
|
|
|
end
|