forked from roodi/roodi
/
abc_metric_method_check_spec.rb
89 lines (82 loc) · 2.47 KB
/
abc_metric_method_check_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
describe Roodi::Checks::AbcMetricMethodCheck do
before(:each) do
@roodi = Roodi::Core::RubyParserRunner.new(Roodi::Checks::AbcMetricMethodCheck.new({'score' => 0}))
end
def verify_content_score(content, a, b, c)
score = Math.sqrt(a*a + b*b + c*c)
@roodi.check_content(content)
errors = @roodi.errors
errors.should_not be_empty
errors[0].to_s.should eql("dummy-file.rb:1 - Method name \"method_name\" has an ABC metric score of <#{a},#{b},#{c}> = #{score}. It should be 0 or less.")
end
# 1. Add one to the assignment count for each occurrence of an assignment
# operator, excluding constant declarations:
#
# = *= /= %= += <<= >>= &= |= ^=
describe "when processing assignments" do
['=', '*=', '/=', '%=', '+=', '<<=', '>>=', '&=', '|=', '^='].each do |each|
it "should find #{each}" do
content = <<-END
def method_name
foo #{each} 1
end
END
verify_content_score(content, 1, 0, 0)
end
end
end
# 3. Add one to the branch count for each function call or class method
# call.
#
# 4. Add one to the branch count for each occurrence of the new operator.
describe "when processing branches" do
it "should find a virtual method call" do
content = <<-END
def method_name
call_foo
end
END
verify_content_score(content, 0, 1, 0)
end
it "should find an explicit method call" do
content = <<-END
def method_name
@object.call_foo
end
END
verify_content_score(content, 0, 1, 0)
end
it "should exclude a condition" do
content = <<-END
def method_name
@object.call_foo < 10
end
END
verify_content_score(content, 0, 1, 1)
end
end
# 5. Add one to the condition count for each use of a conditional operator:
#
# == != <= >= < >
#
# 6. Add one to the condition count for each use of the following
# keywords:
#
# else case default try catch ?
#
# 7. Add one to the condition count for each unary conditional
# expression.
describe "when processing conditions" do
['==', '!=', '<=', '>=', '<', '>'].each do |each|
it "should find #{each}" do
content = <<-END
def method_name
2 #{each} 1
end
END
verify_content_score(content, 0, 0, 1)
end
end
end
end