FIX: need to be able to search replace within lines (#1110)
(this is needed for very simple diffs and HTML)
This commit is contained in:
parent
a7d032fa28
commit
43c56d7c92
|
@ -19,6 +19,9 @@ module DiscourseAi
|
|||
raise ArgumentError, "content cannot be nil" if content.nil?
|
||||
raise ArgumentError, "search cannot be nil" if search.nil?
|
||||
raise ArgumentError, "replace cannot be nil" if replace.nil?
|
||||
raise ArgumentError, "search cannot be empty" if search.empty?
|
||||
|
||||
return content.gsub(search, replace) if content.include?(search)
|
||||
|
||||
lines = content.split("\n")
|
||||
search_lines = search.split("\n")
|
||||
|
|
|
@ -25,7 +25,7 @@ RSpec.describe DiscourseAi::Utils::DiffUtils::SimpleDiff do
|
|||
lin 1
|
||||
TEXT
|
||||
|
||||
expect(subject.apply(content, search, replace)).to eq(expected.strip)
|
||||
expect(subject.apply(content, search, replace).strip).to eq(expected.strip)
|
||||
end
|
||||
|
||||
it "raises error when no match is found" do
|
||||
|
@ -61,10 +61,10 @@ RSpec.describe DiscourseAi::Utils::DiffUtils::SimpleDiff do
|
|||
end
|
||||
|
||||
it "is forgiving of whitespace differences" do
|
||||
content = "line1\n line2\nline3"
|
||||
content = "line1\n line2\nline3"
|
||||
search = "line2"
|
||||
replace = "new_line2"
|
||||
expect(subject.apply(content, search, replace)).to eq("line1\nnew_line2\nline3")
|
||||
expect(subject.apply(content, search, replace).strip).to eq("line1\n new_line2\nline3")
|
||||
end
|
||||
|
||||
it "is forgiving of small character differences" do
|
||||
|
@ -121,6 +121,13 @@ RSpec.describe DiscourseAi::Utils::DiffUtils::SimpleDiff do
|
|||
expect(subject.apply(content, search, replace)).to eq(expected.strip)
|
||||
end
|
||||
|
||||
it "handles partial line matches" do
|
||||
content = "abc hello efg\nabc hello efg"
|
||||
search = "hello"
|
||||
replace = "bob"
|
||||
expect(subject.apply(content, search, replace)).to eq("abc bob efg\nabc bob efg")
|
||||
end
|
||||
|
||||
it "handles JavaScript blocks in different orders" do
|
||||
content = <<~JS
|
||||
function first() {
|
||||
|
|
Loading…
Reference in New Issue