Make query_regexp ignore special symbols.

This commit is contained in:
Markus Reiter 2018-06-02 22:43:54 +02:00
parent cff42a8a8c
commit 99e3135bfa
2 changed files with 17 additions and 2 deletions

View File

@ -4,7 +4,7 @@ module Homebrew
if m = query.match(%r{^/(.*)/$})
Regexp.new(m[1])
else
/.*#{Regexp.escape(query)}.*/i
Regexp.new(query.chars.join('[^a-z\d]*'), Regexp::IGNORECASE)
end
rescue RegexpError
raise "#{query} is not a valid regex."

View File

@ -55,11 +55,26 @@ describe Homebrew::Search do
end
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
it "raises an error if the query is an invalid regex" do
expect { mod.query_regexp("/+/") }.to raise_error(/not a valid regex/)
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