| 1 |
class CodeStatistics |
|---|
| 2 |
|
|---|
| 3 |
TEST_TYPES = %w(Units Functionals Unit\ tests Functional\ tests Integration\ tests) |
|---|
| 4 |
|
|---|
| 5 |
def initialize(*pairs) |
|---|
| 6 |
@pairs = pairs |
|---|
| 7 |
@statistics = calculate_statistics |
|---|
| 8 |
@total = calculate_total if pairs.length > 1 |
|---|
| 9 |
end |
|---|
| 10 |
|
|---|
| 11 |
def to_s |
|---|
| 12 |
print_header |
|---|
| 13 |
@pairs.each { |pair| print_line(pair.first, @statistics[pair.first]) } |
|---|
| 14 |
print_splitter |
|---|
| 15 |
|
|---|
| 16 |
if @total |
|---|
| 17 |
print_line("Total", @total) |
|---|
| 18 |
print_splitter |
|---|
| 19 |
end |
|---|
| 20 |
|
|---|
| 21 |
print_code_test_stats |
|---|
| 22 |
end |
|---|
| 23 |
|
|---|
| 24 |
private |
|---|
| 25 |
def calculate_statistics |
|---|
| 26 |
@pairs.inject({}) { |stats, pair| stats[pair.first] = calculate_directory_statistics(pair.last); stats } |
|---|
| 27 |
end |
|---|
| 28 |
|
|---|
| 29 |
def calculate_directory_statistics(directory, pattern = /.*\.rb$/) |
|---|
| 30 |
stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 } |
|---|
| 31 |
|
|---|
| 32 |
Dir.foreach(directory) do |file_name| |
|---|
| 33 |
if File.stat(directory + "/" + file_name).directory? and (/^\./ !~ file_name) |
|---|
| 34 |
newstats = calculate_directory_statistics(directory + "/" + file_name, pattern) |
|---|
| 35 |
stats.each { |k, v| stats[k] += newstats[k] } |
|---|
| 36 |
end |
|---|
| 37 |
|
|---|
| 38 |
next unless file_name =~ pattern |
|---|
| 39 |
|
|---|
| 40 |
f = File.open(directory + "/" + file_name) |
|---|
| 41 |
|
|---|
| 42 |
while line = f.gets |
|---|
| 43 |
stats["lines"] += 1 |
|---|
| 44 |
stats["classes"] += 1 if line =~ /class [A-Z]/ |
|---|
| 45 |
stats["methods"] += 1 if line =~ /def [a-z]/ |
|---|
| 46 |
stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^\s* |
|---|
| 47 |
end |
|---|
| 48 |
end |
|---|
| 49 |
|
|---|
| 50 |
stats |
|---|
| 51 |
end |
|---|
| 52 |
|
|---|
| 53 |
def calculate_total |
|---|
| 54 |
total = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 } |
|---|
| 55 |
@statistics.each_value { |pair| pair.each { |k, v| total[k] += v } } |
|---|
| 56 |
total |
|---|
| 57 |
end |
|---|
| 58 |
|
|---|
| 59 |
def calculate_code |
|---|
| 60 |
code_loc = 0 |
|---|
| 61 |
@statistics.each { |k, v| code_loc += v['codelines'] unless TEST_TYPES.include? k } |
|---|
| 62 |
code_loc |
|---|
| 63 |
end |
|---|
| 64 |
|
|---|
| 65 |
def calculate_tests |
|---|
| 66 |
test_loc = 0 |
|---|
| 67 |
@statistics.each { |k, v| test_loc += v['codelines'] if TEST_TYPES.include? k } |
|---|
| 68 |
test_loc |
|---|
| 69 |
end |
|---|
| 70 |
|
|---|
| 71 |
def print_header |
|---|
| 72 |
print_splitter |
|---|
| 73 |
puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |" |
|---|
| 74 |
print_splitter |
|---|
| 75 |
end |
|---|
| 76 |
|
|---|
| 77 |
def print_splitter |
|---|
| 78 |
puts "+----------------------+-------+-------+---------+---------+-----+-------+" |
|---|
| 79 |
end |
|---|
| 80 |
|
|---|
| 81 |
def print_line(name, statistics) |
|---|
| 82 |
m_over_c = (statistics["methods"] / statistics["classes"]) rescue m_over_c = 0 |
|---|
| 83 |
loc_over_m = (statistics["codelines"] / statistics["methods"]) - 2 rescue loc_over_m = 0 |
|---|
| 84 |
|
|---|
| 85 |
start = if TEST_TYPES.include? name |
|---|
| 86 |
"| #{name.ljust(20)} " |
|---|
| 87 |
else |
|---|
| 88 |
"| #{name.ljust(20)} " |
|---|
| 89 |
end |
|---|
| 90 |
|
|---|
| 91 |
puts start + |
|---|
| 92 |
"| #{statistics["lines"].to_s.rjust(5)} " + |
|---|
| 93 |
"| #{statistics["codelines"].to_s.rjust(5)} " + |
|---|
| 94 |
"| #{statistics["classes"].to_s.rjust(7)} " + |
|---|
| 95 |
"| #{statistics["methods"].to_s.rjust(7)} " + |
|---|
| 96 |
"| #{m_over_c.to_s.rjust(3)} " + |
|---|
| 97 |
"| #{loc_over_m.to_s.rjust(5)} |" |
|---|
| 98 |
end |
|---|
| 99 |
|
|---|
| 100 |
def print_code_test_stats |
|---|
| 101 |
code = calculate_code |
|---|
| 102 |
tests = calculate_tests |
|---|
| 103 |
|
|---|
| 104 |
puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}" |
|---|
| 105 |
puts "" |
|---|
| 106 |
end |
|---|
| 107 |
end |
|---|