diff --git a/Library/Homebrew/searchable.rb b/Library/Homebrew/searchable.rb index 9fca0bfdaa..72079b2161 100644 --- a/Library/Homebrew/searchable.rb +++ b/Library/Homebrew/searchable.rb @@ -17,7 +17,8 @@ module Searchable 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 @@ -25,7 +26,8 @@ module Searchable 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 diff --git a/Library/Homebrew/test/searchable_spec.rb b/Library/Homebrew/test/searchable_spec.rb index aaf2247795..01f0d00be2 100644 --- a/Library/Homebrew/test/searchable_spec.rb +++ b/Library/Homebrew/test/searchable_spec.rb @@ -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 @@ -26,5 +26,21 @@ describe Searchable do 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