Skip to content
Browse files

A bit more structure in NPath complexity checks.

Needs a *lot* more thinking though.  ruby_parser doesn't have 'else' as a structure.  It uses optionally nil sub-expressions on an 'if' instead.
  • Loading branch information...
1 parent 1bda0fd commit 48dcebfe010f6c9ad297c167880c56d7f96ac3b3 @martinjandrews martinjandrews committed Aug 23, 2009
View
64 lib/roodi/checks/npath_complexity_check.rb
@@ -15,50 +15,58 @@ def initialize(complexity)
@current_value = 1
end
- COMPLEXITY_NODE_TYPES.each do |type|
- define_method "evaluate_start_#{type}" do
- @value_stack.push @current_value
- @current_value = 1
- end
+ def evalute_start_if(node)
+ push_value
+ end
+
+ def evalute_start_while(node)
+ push_value
+ end
+
+ def evalute_start_until(node)
+ push_value
+ end
+
+ def evalute_start_for(node)
+ push_value
+ end
+
+ def evalute_start_case(node)
+ push_value
+ end
+
+ def evalute_start_rescue(node)
+ push_value
end
MULTIPLYING_NODE_TYPES.each do |type|
- define_method "evaluate_end_#{type}" do
- pop = @value_stack.pop
- @current_value = (@current_value + 1) * pop
+ define_method "evaluate_end_#{type}" do |node|
+ leave_multiplying_conditional
end
end
ADDING_NODE_TYPES.each do |type|
- define_method "evaluate_end_#{type}" do
- pop = @value_stack.pop
- @current_value = @current_value - 1 + pop
+ define_method "evaluate_end_#{type}" do |node|
+ leave_multiplying_conditional
end
end
protected
- def count_complexity(node)
- count_branches(node) + 1
- end
-
- def increase_depth
- @count = 1 unless counting?
- @counting = @counting + 1
+ def push_value
+ @value_stack.push @current_value
+ @current_value = 1
end
- def decrease_depth
- @counting = @counting - 1
- if @counting <= 0
- @counting = 0
- evaluate_matching_end
- end
+ def leave_multiplying_conditional
+ pop = @value_stack.pop
+ @current_value = (@current_value + 1) * pop
end
- private
-
- def counting?
- @counting > 0
+ def leave_adding_conditional
+ pop = @value_stack.pop
+ puts "#{type}, so adding #{pop}"
+ @current_value = @current_value - 1 + pop
end
end
end
View
5 lib/roodi/checks/npath_complexity_method_check.rb
@@ -17,15 +17,12 @@ def interesting_nodes
def evaluate_start_defn(node)
@method_name = @node[1]
+ push_value
end
def evaluate_end_defn(node)
add_error "Method name \"#{@method_name}\" n-path complexity is #{@current_value}. It should be #{@complexity} or less." unless @current_value <= @complexity
end
-
- def evaluate_matching_end
- add_error "Method name \"#{@method_name}\" n-path complexity is #{@count}. It should be #{@complexity} or less." unless @count <= @complexity
- end
end
end
end
View
20 spec/roodi/checks/npath_complexity_method_check_spec.rb
@@ -30,16 +30,24 @@ def method_name
end
it "should find nested if block" do
+ pending "NPath Complexity implementation that can support 'else' blocks"
content = <<-END
def method_name
- if (value1 == 0)
- if (value2 == 0 && value3 == 0)
- end
- if (value4 == 0 && value5 == 0)
- end
+ if (value1)
+ foo
+ else
+ bar
+ end
+ if (value2)
+ bam
+ else
+ baz
+ end
+ if (value3)
+ one
end
end
END
- verify_content_complexity(content, 5)
+ verify_content_complexity(content, 8)
end
end

0 comments on commit 48dcebf

Please sign in to comment.
Something went wrong with that request. Please try again.