Skip to content

Commit

Permalink
[Source] Improve fuzzy search support
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiopelosin committed Nov 13, 2013
1 parent d466157 commit 7761a70
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 31 deletions.
27 changes: 22 additions & 5 deletions lib/cocoapods-core/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,16 @@ def all_specs
# @return [Set] a set for a given dependency. The set is identified by the
# name of the dependency and takes into account subspecs.
#
def search(dependency)
# @todo Rename to #load_set
#
def search(query)
if query.is_a?(Dependency)
name = query.root_name
else
name = query
end
pod_sets.find do |set|
# First match the (top level) name, which does not yet load the spec from disk
set.name == dependency.root_name
set.name == name
end
end

Expand Down Expand Up @@ -183,9 +189,20 @@ def search_by_name(query, full_text_search = false)
end
end

def pods_with_similar_names(search_name, suggestions_max_count = 3)
# Returns the set of the Pod whose name fuzzily matches the given query.
#
# @param [String] query
# The query to search for.
#
# @return [Set] The name of the Pod.
# @return [Nil] If no Pod with a suitable name was found.
#
def fuzzy_search(query)
require 'fuzzy_match'
FuzzyMatch.new(pods).find(search_name)
pod_name = FuzzyMatch.new(pods).find(query)
if pod_name
search(pod_name)
end
end

#-------------------------------------------------------------------------#
Expand Down
78 changes: 52 additions & 26 deletions spec/source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,54 +79,80 @@ module Pod
#-------------------------------------------------------------------------#

describe "Searching the source" do
describe "#search" do
it "searches for the Pod with the given name" do
source = Source.new(fixture('spec-repos/test_repo'))
source.search('BananaLib').name.should == 'BananaLib'
end

it "properly configures the sources of a set in search by name" do
source = Source.new(fixture('spec-repos/test_repo'))
sets = source.search_by_name('monkey', true)
sets.count.should == 1
set = sets.first
set.name.should == 'BananaLib'
set.sources.map(&:name).should == %w| test_repo |
end
it "searches for the pod with the given dependency" do
source = Source.new(fixture('spec-repos/test_repo'))
dep = Dependency.new('BananaLib')
source.search(dep).name.should == 'BananaLib'
end

it "handles gracefully specification which can't load in search by name" do
source = Source.new(fixture('spec-repos/test_repo'))
should.not.raise do
source.search_by_name('monkey', true)
it "supports dependencies on subspecs" do
source = Source.new(fixture('spec-repos/test_repo'))
dep = Dependency.new('BananaLib/subspec')
source.search(dep).name.should == 'BananaLib'
end
end

it "doesn't take into account case" do
source = Source.new(fixture('spec-repos/test_repo'))
source.search_by_name('BANANALIB', true).map(&:name).should == ['BananaLib']
source.search_by_name('BANANALIB', false).map(&:name).should == ['BananaLib']
end

it "returns partial matches" do
source = Source.new(fixture('spec-repos/test_repo'))
source.search_by_name('Banana', true).map(&:name).should == ['BananaLib']
source.search_by_name('Banana', false).map(&:name).should == ['BananaLib']
describe "#search_by_name" do
it "properly configures the sources of a set in search by name" do
source = Source.new(fixture('spec-repos/test_repo'))
sets = source.search_by_name('monkey', true)
sets.count.should == 1
set = sets.first
set.name.should == 'BananaLib'
set.sources.map(&:name).should == %w| test_repo |
end

it "handles gracefully specification which can't load in search by name" do
source = Source.new(fixture('spec-repos/test_repo'))
should.not.raise do
source.search_by_name('monkey', true)
end
end

it "doesn't take into account case" do
source = Source.new(fixture('spec-repos/test_repo'))
source.search_by_name('BANANALIB', true).map(&:name).should == ['BananaLib']
source.search_by_name('BANANALIB', false).map(&:name).should == ['BananaLib']
end

it "returns partial matches" do
source = Source.new(fixture('spec-repos/test_repo'))
source.search_by_name('Banana', true).map(&:name).should == ['BananaLib']
source.search_by_name('Banana', false).map(&:name).should == ['BananaLib']
end
end

describe "pods_with_similar_names" do
describe "#fuzzy_search" do
it "is case insensitive" do
source = Source.new(fixture('spec-repos/master'))
source.pods_with_similar_names('abmultiton').should == 'ABMultiton'
source.fuzzy_search('abmultiton').name.should == 'ABMultiton'
end

it "matches misspells" do
source = Source.new(fixture('spec-repos/master'))
source.pods_with_similar_names('ABMuton').should == 'ABMultiton'
source.fuzzy_search('ABMuton').name.should == 'ABMultiton'
end

it "matches abbreviations" do
source = Source.new(fixture('spec-repos/master'))
source.pods_with_similar_names('ObjSugar').should == "ObjectiveSugar"
source.fuzzy_search('ObjSugar').name.should == "ObjectiveSugar"
end

it "matches suffixes" do
source = Source.new(fixture('spec-repos/master'))
source.pods_with_similar_names('table').should == "Routable"
source.fuzzy_search('table').name.should == "Routable"
end

it "returns nil if there is no match" do
source = Source.new(fixture('spec-repos/master'))
source.fuzzy_search('12345').should.be.nil
end
end

Expand Down

0 comments on commit 7761a70

Please sign in to comment.