lsegal-complexity 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. data/LICENSE +22 -0
  2. data/README.md +29 -0
  3. data/Rakefile +23 -0
  4. data/bin/complexity +26 -0
  5. data/test/test_complexity.rb +43 -0
  6. metadata +65 -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
@@ -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,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lsegal-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 -07: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
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ has_rdoc: yard
38
+ homepage: http://github.com/lsegal/complexity
39
+ licenses:
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project: complexity
60
+ rubygems_version: 1.3.5
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Calculates
64
+ test_files:
65
+ - test/test_complexity.rb