Commit 259f3e87ae36c548941683558c99994e8b12f212

Authored by Caio Salgado + Diego Araújo + Pedro Leal
Committed by Diego Camarinha
1 parent a62342f3

[Mezuro] Refactored the source tree navegation with tests

plugins/mezuro/controllers/mezuro_plugin_profile_controller.rb
... ... @@ -18,8 +18,11 @@ class MezuroPluginProfileController < ProfileController
18 18 def project_result
19 19 content = profile.articles.find(params[:id])
20 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 26 render :partial => 'content_viewer/project_result', :locals => { :project_result => project_result, :source_tree => source_tree }
24 27 end
25 28  
... ...
plugins/mezuro/lib/kalibro/entities/module.rb
... ... @@ -2,13 +2,17 @@ class Kalibro::Entities::Module < Kalibro::Entities::Entity
2 2  
3 3 attr_accessor :name, :granularity
4 4  
5   - def ancestor_names
  5 + def self.parent_names(name)
6 6 path = []
7 7 ancestors = []
8   - @name.split(".").each do |token|
  8 + name.split(".").each do |token|
9 9 path << token
10 10 ancestors << path.join(".")
11 11 end
12 12 ancestors
13 13 end
  14 +
  15 + def ancestor_names
  16 + Kalibro::Entities::Module.parent_names(@name)
  17 + end
14 18 end
... ...
plugins/mezuro/lib/kalibro/entities/project_result.rb
... ... @@ -14,22 +14,6 @@ class Kalibro::Entities::ProjectResult &lt; Kalibro::Entities::Entity
14 14 format_milliseconds(@load_time)
15 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 17 def formatted_analysis_time
34 18 format_milliseconds(@analysis_time)
35 19 end
... ... @@ -47,4 +31,21 @@ class Kalibro::Entities::ProjectResult &lt; Kalibro::Entities::Entity
47 31 ('%2d' % amount).sub(/\s/, '0')
48 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 51 end
... ...
plugins/mezuro/test/fixtures/module_node_fixtures.rb
... ... @@ -3,7 +3,9 @@ class ModuleNodeFixtures
3 3 def self.qt_calculator_tree
4 4 node = Kalibro::Entities::ModuleNode.new
5 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 9 node
8 10 end
9 11  
... ... @@ -11,7 +13,9 @@ class ModuleNodeFixtures
11 13 {:module => ModuleFixtures.qt_calculator_hash,
12 14 :child => [
13 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 21 end
... ...
plugins/mezuro/test/unit/kalibro/entities/project_result_test.rb
... ... @@ -24,5 +24,14 @@ class ProjectResultTest &lt; ActiveSupport::TestCase
24 24 should 'retrieve formatted analysis time' do
25 25 assert_equal '00:00:01', @result.formatted_analysis_time
26 26 end
27   -
28   -end
29 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
... ...