2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-05 14:21:05 +02:00
|
|
|
require "searchable"
|
|
|
|
|
|
|
|
describe Searchable do
|
2018-07-22 21:50:54 +02:00
|
|
|
subject { collection.extend(described_class) }
|
2018-06-05 14:21:05 +02:00
|
|
|
|
2018-07-22 21:50:54 +02:00
|
|
|
let(:collection) { ["with-dashes"] }
|
2018-06-05 14:21:05 +02:00
|
|
|
|
|
|
|
describe "#search" do
|
|
|
|
context "when given a block" do
|
2018-07-22 21:50:54 +02:00
|
|
|
let(:collection) { [["with-dashes", "withdashes"]] }
|
2018-06-05 14:21:05 +02:00
|
|
|
|
|
|
|
it "searches by the selected argument" do
|
|
|
|
expect(subject.search(/withdashes/) { |_, short_name| short_name }).not_to be_empty
|
|
|
|
expect(subject.search(/withdashes/) { |long_name, _| long_name }).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when given a regex" do
|
|
|
|
it "does not simplify strings" do
|
|
|
|
expect(subject.search(/with\-dashes/)).to eq ["with-dashes"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when given a string" do
|
|
|
|
it "simplifies both the query and searched strings" do
|
|
|
|
expect(subject.search("with dashes")).to eq ["with-dashes"]
|
|
|
|
end
|
|
|
|
end
|
2018-07-22 21:50:54 +02:00
|
|
|
|
|
|
|
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
|
2018-06-05 14:21:05 +02:00
|
|
|
end
|
|
|
|
end
|