Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Searchable when collection contains nil. #4535

Merged
merged 1 commit into from
Jul 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions Library/Homebrew/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ def simplify_string(string)
def search_regex(regex)
select do |*args|
args = yield(*args) if block_given?
[*args].any? { |arg| arg.match?(regex) }
args = [*args].compact
args.any? { |arg| arg.match?(regex) }
end
end

def search_string(string)
simplified_string = simplify_string(string)
select do |*args|
args = yield(*args) if block_given?
[*args].any? { |arg| simplify_string(arg).include?(simplified_string) }
args = [*args].compact
args.any? { |arg| simplify_string(arg).include?(simplified_string) }
end
end
end
22 changes: 19 additions & 3 deletions Library/Homebrew/test/searchable_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require "searchable"

describe Searchable do
subject { ary.extend(described_class) }
subject { collection.extend(described_class) }

let(:ary) { ["with-dashes"] }
let(:collection) { ["with-dashes"] }

describe "#search" do
context "when given a block" do
let(:ary) { [["with-dashes", "withdashes"]] }
let(:collection) { [["with-dashes", "withdashes"]] }

it "searches by the selected argument" do
expect(subject.search(/withdashes/) { |_, short_name| short_name }).not_to be_empty
Expand All @@ -26,5 +26,21 @@
expect(subject.search("with dashes")).to eq ["with-dashes"]
end
end

context "when searching a Hash" do
let(:collection) { { "foo" => "bar" } }

it "returns a Hash" do
expect(subject.search("foo")).to eq "foo" => "bar"
end

context "containing nil" do
let(:collection) { { "foo" => nil } }

it "does not raise an error" do
expect(subject.search("foo")).to eq "foo" => nil
end
end
end
end
end