complexity 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +23 -0
- data/bin/complexity +26 -0
- data/lib/complexity.rb +45 -0
- data/test/test_complexity.rb +43 -0
- metadata +69 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2009 Loren Segal
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
Complexity
|
2
|
+
==========
|
3
|
+
|
4
|
+
Written by Loren Segal in 2009
|
5
|
+
|
6
|
+
SYNOPSIS
|
7
|
+
--------
|
8
|
+
|
9
|
+
Calculates the McCabe [cyclomatic complexity][1] index of the methods in your Ruby code.
|
10
|
+
The calculation is a basic `NUMBER_OF_BRANCHES + 1` calculation which can be used
|
11
|
+
this calculation to approximate the minimum number of test cases for each of
|
12
|
+
your methods ([complexity should equal number of test cases][3]). You can also use
|
13
|
+
this value to find overly complex method and refactor them into simpler ones.
|
14
|
+
|
15
|
+
Some more reading can be found at [http://www.linuxjournal.com/article/8035][1].
|
16
|
+
|
17
|
+
USAGE
|
18
|
+
-----
|
19
|
+
|
20
|
+
* Requires [YARD][3]
|
21
|
+
|
22
|
+
Syntax: `ruby complexity.rb [--csv] GLOB_OF_FILES`
|
23
|
+
|
24
|
+
If you want CSV output, add --csv. `GLOB_OF_FILES` defaults to `lib/**/*.rb`.
|
25
|
+
|
26
|
+
[1]: http://en.wikipedia.org/wiki/Cyclomatic_complexity "Cyclomatic Complexity"
|
27
|
+
[2]: http://users.csc.calpoly.edu/~jdalbey/206/Lectures/BasisPathTutorial/index.html "Basis Path Testing"
|
28
|
+
[3]: http://yard.soen.ca "Yay! A Ruby Documentation Tool"
|
29
|
+
[4]: http://www.linuxjournal.com/article/8035
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
WINDOWS = (PLATFORM =~ /win32|cygwin/ ? true : false) rescue false
|
6
|
+
SUDO = WINDOWS ? '' : 'sudo'
|
7
|
+
|
8
|
+
task :default => :test
|
9
|
+
|
10
|
+
Rake::TestTask.new
|
11
|
+
|
12
|
+
load 'complexity.gemspec'
|
13
|
+
Rake::GemPackageTask.new(SPEC) do |pkg|
|
14
|
+
pkg.gem_spec = SPEC
|
15
|
+
pkg.need_zip = true
|
16
|
+
pkg.need_tar = true
|
17
|
+
end
|
18
|
+
|
19
|
+
desc "Install the gem locally"
|
20
|
+
task :install => :package do
|
21
|
+
sh "#{SUDO} gem install pkg/#{SPEC.name}-#{SPEC.version}.gem --local"
|
22
|
+
sh "rm -rf pkg/#{SPEC.name}-#{SPEC.version}" unless ENV['KEEP_FILES']
|
23
|
+
end
|
data/bin/complexity
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/../lib/complexity'
|
4
|
+
require 'optparse'
|
5
|
+
|
6
|
+
csv = false
|
7
|
+
OptionParser.new do |opts|
|
8
|
+
opts.banner = "Usage: complexity [--csv] [FILES]"
|
9
|
+
opts.on('--csv', "Print CSV formatted output") { csv = true }
|
10
|
+
opts.on('-v', '--version') { puts "complexity 1.0.0, yard #{YARD::VERSION}"; exit }
|
11
|
+
end.parse!
|
12
|
+
YARD.parse(ARGV[0] || 'lib/**/*.rb')
|
13
|
+
|
14
|
+
YARD::Registry.all(:method).each do |meth|
|
15
|
+
meth[:complexity] ||= 1
|
16
|
+
end
|
17
|
+
|
18
|
+
if csv
|
19
|
+
puts YARD::Registry.all(:method).map {|m| [m.path, m.complexity].join(',') }
|
20
|
+
else
|
21
|
+
max = 40
|
22
|
+
YARD::Registry.all(:method).each {|m| max = m.path.length if m.path.length > max }
|
23
|
+
YARD::Registry.all(:method).each do |meth|
|
24
|
+
puts "#{meth.path}#{' ' * (max - meth.path.size)}#{meth.complexity}"
|
25
|
+
end
|
26
|
+
end
|
data/lib/complexity.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'yard'
|
3
|
+
|
4
|
+
class ComplexityHandler < YARD::Handlers::Ruby::Base
|
5
|
+
handles %r{.*}
|
6
|
+
|
7
|
+
BRANCH_TYPES = [:if, :if_mod, :unless, :unless_mod, :when, :elsif, :else,
|
8
|
+
:while, :while_mod, :until, :until_mod, :for, :do_block, :brace_block, :rescue]
|
9
|
+
|
10
|
+
def process
|
11
|
+
return unless YARD::CodeObjects::MethodObject === owner
|
12
|
+
return unless [:def, :defs].include? statement.parent.parent.type
|
13
|
+
|
14
|
+
owner[:complexity] = 1
|
15
|
+
statement.parent.last.traverse do |node|
|
16
|
+
owner[:complexity] += 1 if BRANCH_TYPES.include?(node.type)
|
17
|
+
owner[:complexity] += 2 if node.type == :ifop
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class LegacyComplexityHandler < YARD::Handlers::Ruby::Legacy::Base
|
23
|
+
handles TkDEF
|
24
|
+
|
25
|
+
BRANCH_TYPES = [TkRESCUE, TkENSURE, TkIF, TkIF_MOD, TkELSIF, TkELSE, TkUNLESS,
|
26
|
+
TkWHEN, TkWHILE, TkWHILE_MOD, TkUNTIL, TkUNTIL_MOD, TkFOR, TkQUESTION, TkCOLON,
|
27
|
+
TkLBRACE, TkDO]
|
28
|
+
|
29
|
+
def process
|
30
|
+
nobj, mscope = namespace, scope
|
31
|
+
if statement.tokens.to_s =~ /^def\s+(#{METHODMATCH})/m
|
32
|
+
meth = $1.gsub(/\s+/,'')
|
33
|
+
if meth =~ /(?:#{NSEPQ}|#{CSEPQ})([^#{NSEP}#{CSEPQ}]+)$/
|
34
|
+
mscope, meth = :class, $1
|
35
|
+
nobj = P(namespace, $`) unless $` == "self"
|
36
|
+
end
|
37
|
+
|
38
|
+
obj = MethodObject.new(nobj, meth, mscope)
|
39
|
+
obj[:complexity] = 1
|
40
|
+
statement.block.each do |token|
|
41
|
+
obj[:complexity] += 1 if BRANCH_TYPES.include?(token.class)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
|
3
|
+
require "test/unit"
|
4
|
+
require "complexity"
|
5
|
+
|
6
|
+
METH_NAME = 'Test#meth'
|
7
|
+
|
8
|
+
def meth(src = nil)
|
9
|
+
YARD.parse_string("class Test; def meth; #{src} end end") if src
|
10
|
+
YARD::Registry.at(METH_NAME).complexity
|
11
|
+
end
|
12
|
+
|
13
|
+
class TestComplexity < Test::Unit::TestCase
|
14
|
+
def test_empty_method
|
15
|
+
assert_equal 1, meth("")
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_if
|
19
|
+
assert_equal 2, meth("if x == 2 then do_something end")
|
20
|
+
assert_equal 2, meth("do_something if x == 2")
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_if_elsif
|
24
|
+
assert_equal 3, meth("if x == 2; A elsif x == 3; B end")
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_if_elsif_else
|
28
|
+
assert_equal 4, meth("if x == 2; A elsif x == 3; B; else C end")
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_case
|
32
|
+
assert_equal 5, meth("case X; when 1; A; when 2; B; when 3; C; when 4; D end")
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_ifop
|
36
|
+
assert_equal 5, meth("1 ? 2 : 3 ? 4 : 5")
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_block
|
40
|
+
assert_equal 2, meth("loop do X end")
|
41
|
+
assert_equal 4, meth("loop { 1 ? 2 : 1 }")
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: complexity
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Loren Segal
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-05 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: yard
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: lsegal@soen.ca
|
27
|
+
executables:
|
28
|
+
- complexity
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- lib/complexity.rb
|
35
|
+
- bin/complexity
|
36
|
+
- test/test_complexity.rb
|
37
|
+
- LICENSE
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
has_rdoc: yard
|
41
|
+
homepage: http://github.com/lsegal/complexity
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project: complexity
|
64
|
+
rubygems_version: 1.3.4
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Calculates
|
68
|
+
test_files:
|
69
|
+
- test/test_complexity.rb
|