Class: Rubycrap::Coverage

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycrap/coverage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Coverage

Returns a new instance of Coverage.



8
9
10
11
12
13
# File 'lib/rubycrap/coverage.rb', line 8

def initialize(file)
  @file = file
  @filename = file["filename"]
  @coverage = file["coverage"]
  @simplecov_information = []
end

Instance Attribute Details

#coverageObject (readonly)

Returns the value of attribute coverage.



6
7
8
# File 'lib/rubycrap/coverage.rb', line 6

def coverage
  @coverage
end

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'lib/rubycrap/coverage.rb', line 6

def file
  @file
end

#filenameObject (readonly)

Returns the value of attribute filename.



6
7
8
# File 'lib/rubycrap/coverage.rb', line 6

def filename
  @filename
end

Instance Method Details

#calculate_method_coverage(startline, lastline) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubycrap/coverage.rb', line 53

def calculate_method_coverage(startline,lastline)
  total_lines = lastline-startline
  Rubycrap::logger.debug("Startline #{startline} | Lastline #{lastline} | Total_lines #{total_lines}")
  coveragelinestotal = coverage
  coveragelines = coveragelinestotal.slice(startline-1,total_lines)
  Rubycrap::logger.debug("coveragelines: #{coveragelines}")
  covered_lines = 0
  not_covered_lines = 0
  coveragelines.each do |line|
	if coverage?(line)
      covered_lines += 1
    end
    if (line.to_s.eql? "0")
      not_covered_lines += 1
    end
  end
  valid_total_lines = covered_lines + not_covered_lines
  method_coverage = covered_lines.to_f / valid_total_lines.to_f
  Rubycrap::logger.debug("covered_lines: #{covered_lines}")
  Rubycrap::logger.debug("not_covered_lines: #{not_covered_lines}")
  Rubycrap::logger.debug("method_coverage: #{method_coverage}")
  method_coverage
end

#parse_method_coverageObject



22
23
24
# File 'lib/rubycrap/coverage.rb', line 22

def parse_method_coverage
   Parser::CurrentRuby.parse(File.open(filename, "r").read)
end

#process_simplecov_fileObject



15
16
17
18
19
20
# File 'lib/rubycrap/coverage.rb', line 15

def process_simplecov_file
  Rubycrap::logger.debug(filename)
  ast = parse_method_coverage
  search_methods(ast)
  @simplecov_information
end

#search_methods(ast) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubycrap/coverage.rb', line 26

def search_methods(ast)
	if (ast.nil?)
		return nil
	end
	if(!ast.respond_to?(:children))
		return nil
	end
    ast.children.each do |child|
    if(child.class.to_s == "Parser::AST::Node")
	Rubycrap::logger.debug("TYPE AST - #{child.type.to_s}")
	end 
	 if(def_method?(child))
        methodname = child.children[0].to_s == "(self)" ? child.children[1].to_s : child.children[0].to_s
        startline = child.loc.line
        lastline = child.loc.last_line
        Rubycrap::logger.debug("\nmethodname: #{methodname}")
        method_coverage = calculate_method_coverage(startline,lastline)
        @simplecov_information << {:name => methodname, 
                                  :coverage => method_coverage , 
                                  :startline => startline, 
                                  :lastline => lastline}
      else
        search_methods(child)
      end
    end
end