Export sorting logic to a method. Add specs for sort algorithm.

parent 3bddb659
......@@ -49,7 +49,7 @@ GIT
GIT
remote: https://github.com/CocoaPods/cocoapods-search.git
revision: 1dd4aba6378678ca45e364e58f96196937fd297b
revision: 4281e740769875ba4e48748b73c7ff832a8d336e
branch: master
specs:
cocoapods-search (0.1.0)
......
......@@ -125,6 +125,7 @@ module Pod
end
end
found_set_symbols = query_word_results_hash.values.reduce(:&)
found_set_symbols ||= []
sets = found_set_symbols.map do |symbol|
aggregate.representative_set(symbol.to_s)
end
......@@ -138,9 +139,21 @@ module Pod
raise Informative, "Unable to find a pod with name#{extra}" \
"matching `#{query}`"
end
sorted_sets(sets, query_word_regexps)
end
# Sort sets
sets.sort_by! { |set|
# Returns given set array by sorting it in-place.
#
# @param [Array<Set>] sets
# Array of sets to be sorted.
#
# @param [Array<Regexp>] query_word_regexps
# Array of regexp objects for user query.
#
# @return [Array<Set>] Given sets parameter itself after sorting it in-place.
#
def sorted_sets(sets, query_word_regexps)
sets.sort_by! do |set|
pre_match_length = nil
found_query_index = nil
found_query_count = 0
......@@ -154,7 +167,7 @@ module Pod
pre_match_length ||= 1000
found_query_index ||= 1000
[-found_query_count, pre_match_length, found_query_index, set.name.downcase]
}
end
sets
end
......
......@@ -84,6 +84,57 @@ module Pod
sets.any? { |s| s.name == 'BananaLib' }.should.be.true
end
describe 'Sorting algorithm' do
before do
@test_search_results = %w(HockeyKit DLSuit VCLReachability NPReachability AVReachability PYNetwork
SCNetworkReachability AFNetworking Networking).map do |name|
Specification::Set.new(name)
end
end
it 'puts pod with exact match at the first index while sorting' do
regexps = [/networking/i]
sets = SourcesManager.sorted_sets(@test_search_results, regexps)
sets[0].name.should == 'Networking'
end
it 'puts pod with less prefix length before pods with more prefix length in search results' do
regexps = [/reachability/i]
sets = SourcesManager.sorted_sets(@test_search_results, regexps)
sets.index { |s| s.name == 'AVReachability' }.should.be < sets.index { |s| s.name == 'VCLReachability' }
end
it 'puts pod with more query word match before pods with less match in multi word query search results' do
regexps = [/network/i, /reachability/i]
sets = SourcesManager.sorted_sets(@test_search_results, regexps)
sets.index { |s| s.name == 'SCNetworkReachability' }.should.be < sets.index { |s| s.name == 'AVReachability' }
end
it 'puts pod matching first query word before pods matching later words in multi word query search results' do
regexps = [/network/i, /reachability/i]
sets = SourcesManager.sorted_sets(@test_search_results, regexps)
sets.index { |s| s.name == 'PYNetwork' }.should.be < sets.index { |s| s.name == 'AVReachability' }
end
it 'puts pod matching first query word before pods matching later words in multi word query search results' do
regexps = [/network/i, /reachability/i]
sets = SourcesManager.sorted_sets(@test_search_results, regexps)
sets.index { |s| s.name == 'PYNetwork' }.should.be < sets.index { |s| s.name == 'AVReachability' }
end
it 'alphabetically sorts pods having exact other conditions' do
regexps = [/reachability/i]
sets = SourcesManager.sorted_sets(@test_search_results, regexps)
sets.index { |s| s.name == 'AVReachability' }.should.be < sets.index { |s| s.name == 'NPReachability' }
end
it 'alphabetically sorts pods whose names does not match query' do
regexps = [/reachability/i]
sets = SourcesManager.sorted_sets(@test_search_results, regexps)
sets.index { |s| s.name == 'DLSuit' }.should.be < sets.index { |s| s.name == 'HockeyKit' }
end
end
it "generates the search index before performing a search if it doesn't exits" do
SourcesManager.stubs(:all).returns([@test_source])
Source::Aggregate.any_instance.expects(:generate_search_index_for_source).with(@test_source).returns('BananaLib' => ['BananaLib'])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment