Commit 71214863162d216f47f9107669bea942b2cc2475
Exists in
colab
and in
4 other branches
Merge pull request #282 from mezuro/fixes_granularity_comparison
Fixes module tree ordering with FUNCTION granularity
Showing
3 changed files
with
44 additions
and
4 deletions
Show diff stats
app/helpers/kalibro_modules_helper.rb
1 | 1 | module KalibroModulesHelper |
2 | + # The comparison between FUNCTION and CLASS or METHOD does not make sense for kalibro. | |
3 | + # Still, we want to sort the kalibro modules to present it to the user. | |
4 | + # So, we define that FUNCTION are smaller than all the other granularities. | |
5 | + class ComparableGranularity < KalibroClient::Entities::Miscellaneous::Granularity | |
6 | + def <=>(other) | |
7 | + response = super(other) | |
8 | + if response.nil? | |
9 | + response = self.type == :FUNCTION ? -1 : 1 | |
10 | + end | |
11 | + response | |
12 | + end | |
13 | + end | |
14 | + | |
2 | 15 | def sort_by_granularity_and_name(module_results) |
3 | 16 | module_results.sort! do |a,b| |
4 | 17 | if (a.kalibro_module.granularity == b.kalibro_module.granularity) |
5 | 18 | a.kalibro_module.name <=> b.kalibro_module.name |
6 | 19 | else |
7 | - (KalibroClient::Entities::Miscellaneous::Granularity.new(b.kalibro_module.granularity.to_sym) <=> KalibroClient::Entities::Miscellaneous::Granularity.new(a.kalibro_module.granularity.to_sym)) | |
20 | + (ComparableGranularity.new(b.kalibro_module.granularity.to_sym) <=> ComparableGranularity.new(a.kalibro_module.granularity.to_sym)) | |
8 | 21 | end |
9 | 22 | end |
10 | 23 | end | ... | ... |
spec/factories/kalibro_modules.rb
... | ... | @@ -11,7 +11,17 @@ FactoryGirl.define do |
11 | 11 | granlrty 'CLASS' |
12 | 12 | end |
13 | 13 | |
14 | + trait :function do | |
15 | + granlrty 'FUNCTION' | |
16 | + end | |
17 | + | |
18 | + trait :method do | |
19 | + granlrty 'METHOD' | |
20 | + end | |
21 | + | |
14 | 22 | factory :kalibro_module_package, traits: [:package] |
15 | 23 | factory :kalibro_module_class, traits: [:class] |
24 | + factory :kalibro_module_function, traits: [:function] | |
25 | + factory :kalibro_module_method, traits: [:method] | |
16 | 26 | end |
17 | 27 | end | ... | ... |
spec/helpers/kalibro_module_helper_spec.rb
... | ... | @@ -3,12 +3,29 @@ require 'rails_helper' |
3 | 3 | describe KalibroModulesHelper, :type => :helper do |
4 | 4 | describe 'sort_by_granularity_and_name' do |
5 | 5 | let(:package) { FactoryGirl.build(:module_result, kalibro_module: FactoryGirl.build(:kalibro_module_package)) } |
6 | - let(:class_a) { FactoryGirl.build(:module_result, kalibro_module: FactoryGirl.build(:kalibro_module_class, name: 'a')) } | |
7 | 6 | let(:class_b) { FactoryGirl.build(:module_result, kalibro_module: FactoryGirl.build(:kalibro_module_class, name: 'b')) } |
7 | + let(:class_a) { FactoryGirl.build(:module_result, kalibro_module: FactoryGirl.build(:kalibro_module_class, name: 'a')) } | |
8 | + let(:function) { FactoryGirl.build(:module_result, kalibro_module: FactoryGirl.build(:kalibro_module_function, name: 'c')) } | |
9 | + let(:method) { FactoryGirl.build(:module_result, kalibro_module: FactoryGirl.build(:kalibro_module_method, name: 'm')) } | |
8 | 10 | |
9 | 11 | it 'is expected to order the ModuleResults by the Granularity first and then the KalibroModule name' do |
10 | - unordered_modules = [class_b, package, class_a] | |
11 | - expect(helper.sort_by_granularity_and_name(unordered_modules)).to eq([package, class_a, class_b]) | |
12 | + unordered_modules = [class_b, method, package, class_a, function] | |
13 | + expect(helper.sort_by_granularity_and_name(unordered_modules)).to eq([package, class_a, class_b, method, function]) | |
14 | + end | |
15 | + end | |
16 | + | |
17 | + describe 'ComparableGranularity' do | |
18 | + describe 'methods' do | |
19 | + describe '<=>' do | |
20 | + subject { KalibroModulesHelper::ComparableGranularity.new(:FUNCTION) } | |
21 | + let(:klass) { KalibroModulesHelper::ComparableGranularity.new(:CLASS) } | |
22 | + let(:method) { KalibroModulesHelper::ComparableGranularity.new(:METHOD) } | |
23 | + | |
24 | + it 'is expected to place FUNCTION below all granularities' do | |
25 | + expect(subject <=> klass).to eq(-1) | |
26 | + expect(subject <=> method).to eq(-1) | |
27 | + end | |
28 | + end | |
12 | 29 | end |
13 | 30 | end |
14 | 31 | end |
15 | 32 | \ No newline at end of file | ... | ... |