2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-02 20:49:14 +02:00
|
|
|
require "search"
|
2024-06-30 21:09:30 -04:00
|
|
|
require "descriptions"
|
|
|
|
require "cmd/desc"
|
2018-06-02 20:49:14 +02:00
|
|
|
|
2024-02-18 15:11:11 -08:00
|
|
|
RSpec.describe Homebrew::Search do
|
2018-06-05 10:55:00 +02:00
|
|
|
describe "#query_regexp" do
|
|
|
|
it "correctly parses a regex query" do
|
2022-12-17 11:03:18 -08:00
|
|
|
expect(described_class.query_regexp("/^query$/")).to eq(/^query$/)
|
2018-06-02 20:49:14 +02:00
|
|
|
end
|
2018-06-02 22:43:54 +02:00
|
|
|
|
2018-06-05 15:39:09 +02:00
|
|
|
it "returns the original string if it is not a regex query" do
|
2022-12-17 11:03:18 -08:00
|
|
|
expect(described_class.query_regexp("query")).to eq("query")
|
2018-06-02 22:43:54 +02:00
|
|
|
end
|
|
|
|
|
2018-06-05 10:55:00 +02:00
|
|
|
it "raises an error if the query is an invalid regex" do
|
2022-12-17 11:03:18 -08:00
|
|
|
expect { described_class.query_regexp("/+/") }.to raise_error(/not a valid regex/)
|
2018-06-02 22:43:54 +02:00
|
|
|
end
|
2018-06-02 20:49:14 +02:00
|
|
|
end
|
2023-03-07 09:08:43 -08:00
|
|
|
|
|
|
|
describe "#search" do
|
2024-09-17 12:20:17 +09:00
|
|
|
let(:collection) { ["with-dashes", "with@alpha", "with+plus"] }
|
2023-03-07 09:08:43 -08:00
|
|
|
|
|
|
|
context "when given a block" do
|
|
|
|
let(:collection) { [["with-dashes", "withdashes"]] }
|
|
|
|
|
|
|
|
it "searches by the selected argument" do
|
|
|
|
expect(described_class.search(collection, /withdashes/) { |_, short_name| short_name }).not_to be_empty
|
|
|
|
expect(described_class.search(collection, /withdashes/) { |long_name, _| long_name }).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when given a regex" do
|
|
|
|
it "does not simplify strings" do
|
|
|
|
expect(described_class.search(collection, /with-dashes/)).to eq ["with-dashes"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when given a string" do
|
|
|
|
it "simplifies both the query and searched strings" do
|
|
|
|
expect(described_class.search(collection, "with dashes")).to eq ["with-dashes"]
|
|
|
|
end
|
2024-09-17 12:20:17 +09:00
|
|
|
|
|
|
|
it "does not simplify strings with @ and + characters" do
|
|
|
|
expect(described_class.search(collection, "with@alpha")).to eq ["with@alpha"]
|
|
|
|
expect(described_class.search(collection, "with+plus")).to eq ["with+plus"]
|
|
|
|
end
|
2023-03-07 09:08:43 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when searching a Hash" do
|
|
|
|
let(:collection) { { "foo" => "bar" } }
|
|
|
|
|
|
|
|
it "returns a Hash" do
|
|
|
|
expect(described_class.search(collection, "foo")).to eq "foo" => "bar"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a nil value" do
|
|
|
|
let(:collection) { { "foo" => nil } }
|
|
|
|
|
|
|
|
it "does not raise an error" do
|
|
|
|
expect(described_class.search(collection, "foo")).to eq "foo" => nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-06-30 21:09:30 -04:00
|
|
|
|
|
|
|
describe "#search_descriptions" do
|
|
|
|
let(:args) { Homebrew::Cmd::Desc.new(["min_arg_placeholder"]).args }
|
|
|
|
|
|
|
|
context "with api" do
|
|
|
|
let(:api_formulae) do
|
|
|
|
{ "testball" => { "desc" => "Some test" } }
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:api_casks) do
|
|
|
|
{ "testball" => { "desc" => "Some test", "name" => ["Test Ball"] } }
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Homebrew::API::Formula).to receive(:all_formulae).and_return(api_formulae)
|
|
|
|
allow(Homebrew::API::Cask).to receive(:all_casks).and_return(api_casks)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "searches formula descriptions" do
|
|
|
|
expect { described_class.search_descriptions(described_class.query_regexp("some"), args) }
|
|
|
|
.to output(/testball: Some test/).to_stdout
|
|
|
|
end
|
|
|
|
|
|
|
|
it "searches cask descriptions", :needs_macos do
|
|
|
|
expect { described_class.search_descriptions(described_class.query_regexp("ball"), args) }
|
|
|
|
.to output(/testball: \(Test Ball\) Some test/).to_stdout
|
|
|
|
.and not_to_output(/testball: Some test/).to_stdout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-21 05:13:05 +01:00
|
|
|
end
|