OSDN Git Service

change option system for generators
[mint/mint-lib.git] / tasks / spec_statistics.rb
1
2 # via http://refactormycode.com/codes/91-rake-stats-for-rspec
3 class SpecStatistics < CodeStatistics
4
5   TEST_TYPES << "Specs"
6
7   private
8   def calculate_directory_statistics(directory, pattern = /.*\.rb$/)
9     stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0, "specs" => 0, "behaviors" => 0 }
10
11
12     Dir.foreach(directory) do |file_name| 
13       if File.stat(directory + "/" + file_name).directory? and (/^\./ !~ file_name)
14         newstats = calculate_directory_statistics(directory + "/" + file_name, pattern)
15         stats.each { |k, v| stats[k] += newstats[k] }
16       end
17
18       next unless file_name =~ pattern
19
20       f = File.open(directory + "/" + file_name)
21
22       while line = f.gets
23         stats["lines"]     += 1
24         stats["classes"]   += 1 if line =~ /class [A-Z]/
25         stats["methods"]   += 1 if line =~ /def [a-z]/
26         stats["specs"]     += 1 if line =~ /it.*(do|\{)/
27         stats["behaviors"] += 1 if line =~ /describe.*(do|\{)/
28         stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^\s*#/
29       end
30     end
31
32     stats
33   end
34
35   def calculate_total
36     total = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0, "specs" => 0, "behaviors" => 0 }
37     @statistics.each_value { |pair| pair.each { |k, v| total[k] += v } }
38     total
39   end
40
41   def print_header
42     print_splitter
43     puts "| Name                 | Lines |   LOC | Classes | Methods | Behaviors | Specifications | M/C | LOC/M | S/B |"
44     print_splitter
45   end
46
47   def print_splitter
48     puts "+----------------------+-------+-------+---------+---------+-----------+----------------+-----+-------+-----+"
49   end
50
51   def print_line(name, statistics)
52     m_over_c   = (statistics["methods"] / statistics["classes"])   rescue m_over_c = 0
53     loc_over_m = (statistics["codelines"] / statistics["methods"]) - 2 rescue loc_over_m = 0
54
55     s_over_b = (statistics["specs"] / statistics["behaviors"]) rescue 0
56
57     start = if TEST_TYPES.include? name
58       "| #{name.ljust(20)} "
59     else
60       "| #{name.ljust(20)} "
61     end
62
63     puts start + 
64          "| #{statistics["lines"].to_s.rjust(5)} " +
65          "| #{statistics["codelines"].to_s.rjust(5)} " +
66          "| #{statistics["classes"].to_s.rjust(7)} " +
67          "| #{statistics["methods"].to_s.rjust(7)} " +
68          "| #{statistics["behaviors"].to_s.rjust(10)}" +
69          "| #{statistics["specs"].to_s.rjust(15)}" +
70          "| #{m_over_c.to_s.rjust(3)} " +
71          "| #{loc_over_m.to_s.rjust(6)}" +
72          "| #{s_over_b.to_s.rjust(3)} |"
73   end
74 end
75