Commit 259f3e87ae36c548941683558c99994e8b12f212
Committed by
Diego Camarinha
1 parent
a62342f3
Exists in
master
and in
22 other branches
[Mezuro] Refactored the source tree navegation with tests
Showing
5 changed files
with
45 additions
and
24 deletions
Show diff stats
plugins/mezuro/controllers/mezuro_plugin_profile_controller.rb
| @@ -18,8 +18,11 @@ class MezuroPluginProfileController < ProfileController | @@ -18,8 +18,11 @@ class MezuroPluginProfileController < ProfileController | ||
| 18 | def project_result | 18 | def project_result |
| 19 | content = profile.articles.find(params[:id]) | 19 | content = profile.articles.find(params[:id]) |
| 20 | project_result = content.project_result | 20 | project_result = content.project_result |
| 21 | - source_tree = project_result.subtree(params[:module_name]) | ||
| 22 | - source_tree = project_result.source_tree if source_tree.nil? | 21 | + if params[:module_name].nil? |
| 22 | + source_tree = project_result.source_tree | ||
| 23 | + else | ||
| 24 | + source_tree = project_result.get_node(params[:module_name]) | ||
| 25 | + end | ||
| 23 | render :partial => 'content_viewer/project_result', :locals => { :project_result => project_result, :source_tree => source_tree } | 26 | render :partial => 'content_viewer/project_result', :locals => { :project_result => project_result, :source_tree => source_tree } |
| 24 | end | 27 | end |
| 25 | 28 |
plugins/mezuro/lib/kalibro/entities/module.rb
| @@ -2,13 +2,17 @@ class Kalibro::Entities::Module < Kalibro::Entities::Entity | @@ -2,13 +2,17 @@ class Kalibro::Entities::Module < Kalibro::Entities::Entity | ||
| 2 | 2 | ||
| 3 | attr_accessor :name, :granularity | 3 | attr_accessor :name, :granularity |
| 4 | 4 | ||
| 5 | - def ancestor_names | 5 | + def self.parent_names(name) |
| 6 | path = [] | 6 | path = [] |
| 7 | ancestors = [] | 7 | ancestors = [] |
| 8 | - @name.split(".").each do |token| | 8 | + name.split(".").each do |token| |
| 9 | path << token | 9 | path << token |
| 10 | ancestors << path.join(".") | 10 | ancestors << path.join(".") |
| 11 | end | 11 | end |
| 12 | ancestors | 12 | ancestors |
| 13 | end | 13 | end |
| 14 | + | ||
| 15 | + def ancestor_names | ||
| 16 | + Kalibro::Entities::Module.parent_names(@name) | ||
| 17 | + end | ||
| 14 | end | 18 | end |
plugins/mezuro/lib/kalibro/entities/project_result.rb
| @@ -14,22 +14,6 @@ class Kalibro::Entities::ProjectResult < Kalibro::Entities::Entity | @@ -14,22 +14,6 @@ class Kalibro::Entities::ProjectResult < Kalibro::Entities::Entity | ||
| 14 | format_milliseconds(@load_time) | 14 | format_milliseconds(@load_time) |
| 15 | end | 15 | end |
| 16 | 16 | ||
| 17 | - def subtree(name) | ||
| 18 | - find_subtree(@source_tree, name) | ||
| 19 | - end | ||
| 20 | - | ||
| 21 | - def find_subtree(tree, name) | ||
| 22 | - if tree.module.name == name | ||
| 23 | - tree | ||
| 24 | - elsif !tree.children.nil? | ||
| 25 | - tree.children.each do |child| | ||
| 26 | - found = find_subtree(child, name) | ||
| 27 | - return found if !found.nil? | ||
| 28 | - end | ||
| 29 | - return nil | ||
| 30 | - end | ||
| 31 | - end | ||
| 32 | - | ||
| 33 | def formatted_analysis_time | 17 | def formatted_analysis_time |
| 34 | format_milliseconds(@analysis_time) | 18 | format_milliseconds(@analysis_time) |
| 35 | end | 19 | end |
| @@ -47,4 +31,21 @@ class Kalibro::Entities::ProjectResult < Kalibro::Entities::Entity | @@ -47,4 +31,21 @@ class Kalibro::Entities::ProjectResult < Kalibro::Entities::Entity | ||
| 47 | ('%2d' % amount).sub(/\s/, '0') | 31 | ('%2d' % amount).sub(/\s/, '0') |
| 48 | end | 32 | end |
| 49 | 33 | ||
| 34 | + def get_node(module_name) | ||
| 35 | + path = Kalibro::Entities::Module.parent_names(module_name) | ||
| 36 | + parent = @source_tree | ||
| 37 | + path.each do |node_name| | ||
| 38 | + parent = get_leaf_from(parent, node_name) | ||
| 39 | + end | ||
| 40 | + return parent | ||
| 41 | + end | ||
| 42 | + | ||
| 43 | + private | ||
| 44 | + def get_leaf_from(node, module_name) | ||
| 45 | + node.children.each do |child_node| | ||
| 46 | + return child_node if child_node.module.name == module_name | ||
| 47 | + end | ||
| 48 | + nil | ||
| 49 | + end | ||
| 50 | + | ||
| 50 | end | 51 | end |
plugins/mezuro/test/fixtures/module_node_fixtures.rb
| @@ -3,7 +3,9 @@ class ModuleNodeFixtures | @@ -3,7 +3,9 @@ class ModuleNodeFixtures | ||
| 3 | def self.qt_calculator_tree | 3 | def self.qt_calculator_tree |
| 4 | node = Kalibro::Entities::ModuleNode.new | 4 | node = Kalibro::Entities::ModuleNode.new |
| 5 | node.module = ModuleFixtures.qt_calculator | 5 | node.module = ModuleFixtures.qt_calculator |
| 6 | - node.children = [new_node('Dialog', 'CLASS'), new_node('main', 'CLASS')] | 6 | + org_node = new_node('org', 'PACKAGE') |
| 7 | + org_node.children = [new_node('org.Window', 'CLASS')] | ||
| 8 | + node.children = [new_node('Dialog', 'CLASS'), new_node('main', 'CLASS'), org_node] | ||
| 7 | node | 9 | node |
| 8 | end | 10 | end |
| 9 | 11 | ||
| @@ -11,7 +13,9 @@ class ModuleNodeFixtures | @@ -11,7 +13,9 @@ class ModuleNodeFixtures | ||
| 11 | {:module => ModuleFixtures.qt_calculator_hash, | 13 | {:module => ModuleFixtures.qt_calculator_hash, |
| 12 | :child => [ | 14 | :child => [ |
| 13 | {:module => {:name => 'Dialog', :granularity => 'CLASS'}}, | 15 | {:module => {:name => 'Dialog', :granularity => 'CLASS'}}, |
| 14 | - {:module => {:name => 'main', :granularity => 'CLASS'}} | 16 | + {:module => {:name => 'main', :granularity => 'CLASS'}}, |
| 17 | + {:module => {:name => 'org', :granularity => 'PACKAGE'}, | ||
| 18 | + :child => [{:module => {:name => 'org.Window', :granularity => 'CLASS'}}]} | ||
| 15 | ] | 19 | ] |
| 16 | } | 20 | } |
| 17 | end | 21 | end |
plugins/mezuro/test/unit/kalibro/entities/project_result_test.rb
| @@ -24,5 +24,14 @@ class ProjectResultTest < ActiveSupport::TestCase | @@ -24,5 +24,14 @@ class ProjectResultTest < ActiveSupport::TestCase | ||
| 24 | should 'retrieve formatted analysis time' do | 24 | should 'retrieve formatted analysis time' do |
| 25 | assert_equal '00:00:01', @result.formatted_analysis_time | 25 | assert_equal '00:00:01', @result.formatted_analysis_time |
| 26 | end | 26 | end |
| 27 | - | ||
| 28 | -end | ||
| 29 | \ No newline at end of file | 27 | \ No newline at end of file |
| 28 | + | ||
| 29 | + should 'retrieve module node' do | ||
| 30 | + node = @result.get_node("main") | ||
| 31 | + assert_equal @hash[:source_tree][:child][1], node.to_hash | ||
| 32 | + end | ||
| 33 | + | ||
| 34 | + should 'retrive complex module' do | ||
| 35 | + node = @result.get_node("org.Window") | ||
| 36 | + assert_equal @hash[:source_tree][:child][2][:child].first, node.to_hash | ||
| 37 | + end | ||
| 38 | +end |