2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-12-16 12:39:49 -05:00
|
|
|
require 'onpdiff'
|
|
|
|
|
|
|
|
describe ONPDiff do
|
|
|
|
|
|
|
|
describe "diff" do
|
|
|
|
|
|
|
|
it "returns an empty array when there is no content to diff" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(ONPDiff.new("", "").diff).to eq([])
|
2013-12-16 12:39:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an array with the operation code for each element" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(ONPDiff.new("abcd", "abef").diff).to eq([["a", :common], ["b", :common], ["e", :add], ["f", :add], ["c", :delete], ["d", :delete]])
|
2013-12-16 12:39:49 -05:00
|
|
|
end
|
|
|
|
|
2019-06-26 19:45:52 -04: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-16 12:39:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "short_diff" do
|
|
|
|
|
|
|
|
it "returns an empty array when there is no content to diff" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(ONPDiff.new("", "").short_diff).to eq([])
|
2013-12-16 12:39:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an array with the operation code for each element" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(ONPDiff.new("abc", "acd").short_diff).to eq([["a", :common], ["b", :delete], ["c", :common], ["d", :add]])
|
2013-12-16 12:39:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an array with sequencially similar operations merged" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(ONPDiff.new("abcd", "abef").short_diff).to eq([["ab", :common], ["ef", :add], ["cd", :delete]])
|
2013-12-16 12:39:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|