Class: Dcov::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/dcov/stats.rb

Overview

RDoc’s Stats class with our own stats thrown in and our own custom print method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStats

include Ruport::Renderer::Hooks renders_with Dcov::StatsRenderer



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dcov/stats.rb', line 39

def initialize
  @num_files = @num_classes = @num_modules = @num_methods = 0
  @start = Time.now

  @coverage = { }

  [:class, :method, :module].each do |type|
    @coverage[type] = { :covered => [], :not_covered => [] }
  end
  
  @coverage[:tokens] = {}
end

Instance Attribute Details

#coverageObject

Returns the value of attribute coverage.



34
35
36
# File 'lib/dcov/stats.rb', line 34

def coverage
  @coverage
end

#num_classesObject

Returns the value of attribute num_classes.



34
35
36
# File 'lib/dcov/stats.rb', line 34

def num_classes
  @num_classes
end

#num_filesObject

Returns the value of attribute num_files.



34
35
36
# File 'lib/dcov/stats.rb', line 34

def num_files
  @num_files
end

#num_methodsObject

Returns the value of attribute num_methods.



34
35
36
# File 'lib/dcov/stats.rb', line 34

def num_methods
  @num_methods
end

#num_modulesObject

Returns the value of attribute num_modules.



34
35
36
# File 'lib/dcov/stats.rb', line 34

def num_modules
  @num_modules
end

Instance Method Details

#coverage_rating(tokens) ⇒ Object

Get the coverage rating (e.g., 34%) for the tokens of the type specified



97
98
99
100
101
102
# File 'lib/dcov/stats.rb', line 97

def coverage_rating(tokens)
  rating = ((@coverage[tokens][:covered].size.to_f / (@coverage[tokens][:covered].size.to_f + @coverage[tokens][:not_covered].size.to_f)) * 100)
  
  # If it's NaN (for whatever reason, be it an irrational or irrationally small number), return 0
  (rating.nan?) ? 0 : rating.to_i
end

Print out the coverage rating



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
90
91
92
93
94
# File 'lib/dcov/stats.rb', line 53

def print
  # TODO: add a flag for textmate, the ugliest format ever:
  # txmt://open?url=file:///path/to/file.rb&line=86&column=3

  puts "Files:   #{@num_files}"
  puts "Total Classes: #{@num_classes}"
  puts "Total Modules: #{@num_modules}"
  puts "Total Methods: #{@num_methods}"

  puts
  puts "Module coverage: #{coverage_rating(:module)}%"
  puts "  Not covered:"
  @coverage[:module][:not_covered].sort_by {|o| o.name}.each do |itm|
    location = itm.in_files.first.file_absolute_name || "no known location"
    puts "    #{itm.name}:"
    puts "      #{location}"
  end

  puts
  
  puts
  puts "Class coverage: #{coverage_rating(:class)}%"
  puts "  Not covered:"
  @coverage[:class][:not_covered].sort_by {|o| o.name}.each do |itm|
    location = itm.in_files.first.file_absolute_name
    puts "    #{itm.name}"
    puts "      #{location}"
  end

  puts

  puts
  puts "Method coverage: #{coverage_rating(:method)}%\n"
  puts "  Not covered:"
  @coverage[:method][:not_covered].sort_by {|o| [o.parent.name, o.name]}.each do |itm|
    location = itm.token_stream.first.text.sub(/^# File /, '').sub(/, line (\d+)$/, ':\1')
    puts "    #{itm.parent.name}##{itm.name}:"
    puts "       #{location}"
  end
  
  puts
end

#renderable_dataObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dcov/stats.rb', line 104

def renderable_data
  data = {}
  data[:coverage_rating] = {}
  
  [:class, :method, :module].each do |token|
    data[:coverage_rating][token] = coverage_rating(token)
  end
  
  classes = @coverage[:class][:not_covered] + @coverage[:class][:covered]
  modules = @coverage[:module][:not_covered] + @coverage[:module][:covered]
  methods = @coverage[:method][:not_covered] + @coverage[:method][:covered]
  
  # Create a properly nested structure
  data[:structured] = {}
  classes.each {|cls| data[:structured][cls.full_name] = [cls, []]}
  modules.each {|mod| data[:structured][mod.full_name] = [mod, []]}
  data[:structured]['[Toplevel]'] = [TopLevel.new, []]
  
  methods.each do |method|
    if (data[:structured].has_key?(method.parent.full_name))
      data[:structured][method.parent.full_name][1] << method
    else
      data[:structured]['[Toplevel]'][1] << method
    end
  end
  
  data[:analyzed] = @coverage
  
  data
end