Make query_regexp ignore special symbols.
This commit is contained in:
parent
cff42a8a8c
commit
99e3135bfa
@ -4,7 +4,7 @@ module Homebrew
|
|||||||
if m = query.match(%r{^/(.*)/$})
|
if m = query.match(%r{^/(.*)/$})
|
||||||
Regexp.new(m[1])
|
Regexp.new(m[1])
|
||||||
else
|
else
|
||||||
/.*#{Regexp.escape(query)}.*/i
|
Regexp.new(query.chars.join('[^a-z\d]*'), Regexp::IGNORECASE)
|
||||||
end
|
end
|
||||||
rescue RegexpError
|
rescue RegexpError
|
||||||
raise "#{query} is not a valid regex."
|
raise "#{query} is not a valid regex."
|
||||||
|
|||||||
@ -55,11 +55,26 @@ describe Homebrew::Search do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "correctly converts a query string to a regex" do
|
it "correctly converts a query string to a regex" do
|
||||||
expect(mod.query_regexp("query")).to eq(/.*query.*/i)
|
expect(mod.query_regexp("query")).to eq(/q[^a-z\d]*u[^a-z\d]*e[^a-z\d]*r[^a-z\d]*y/i)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises an error if the query is an invalid regex" do
|
it "raises an error if the query is an invalid regex" do
|
||||||
expect { mod.query_regexp("/+/") }.to raise_error(/not a valid regex/)
|
expect { mod.query_regexp("/+/") }.to raise_error(/not a valid regex/)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "correctly matches with special symbols" do
|
||||||
|
regex = mod.query_regexp("oo-ba")
|
||||||
|
expect(regex).to match("foo-bar")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "correctly matches without special symbols" do
|
||||||
|
regex = mod.query_regexp("ooba")
|
||||||
|
expect(regex).to match("foo-bar")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "keeps special symbols" do
|
||||||
|
regex = mod.query_regexp("foo-bar")
|
||||||
|
expect(regex).not_to match("foobar")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user