Module: SimpleCov::JSCoverage

Defined in:
lib/simplecov-jscoverage.rb,
lib/simplecov-jscoverage/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.append_to_report(coverage_full) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/simplecov-jscoverage.rb', line 89

def self.append_to_report coverage_full
  coverage = {}

  coverage_full.each do |filename, coverage_per_file|
    coverage[filename] = coverage_per_file[:calls]
  end

  ::SimpleCov::ResultMerger.resultset.each do |command, data|
    if command == toolname && (Time.now.to_i - data["timestamp"]) < ::SimpleCov.merge_timeout
      coverage = data["coverage"].merge_resultset coverage
    end
  end

  merged_result = ::SimpleCov::Result.from_hash({toolname => { "coverage" => coverage, "timestamp" => Time.now.to_i }})
  ::SimpleCov::ResultMerger.store_result merged_result
end

.clean_lines(lines) ⇒ Object



85
86
87
# File 'lib/simplecov-jscoverage.rb', line 85

def self.clean_lines lines
  lines.collect{ |line| CGI.unescapeHTML(line) }
end

.convert_v8(value) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/simplecov-jscoverage.rb', line 50

def self.convert_v8(value)
  if value.is_a?(V8::Array)
    result = []
    value.each do |val|
      result << convert_v8(val)
    end
  elsif value.is_a?(V8::Object)
    result = {}
    value.each do |key|
      result[key] = convert_v8(value[key])
    end
  else
    result = value
  end
  result
end

.cover?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/simplecov-jscoverage.rb', line 10

def self.cover?
  return (ENV["JSCOV"] == "YES") || (ENV["COVERAGE"] == "YES")
end

.extract(context) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/simplecov-jscoverage.rb', line 29

def self.extract context
  return unless cover?
  coverage = context["_$jscoverage"]
  coverage_full = {}

  coverage.each do |filename, calls|
    source = source_for_file context, filename.to_s
    calls = convert_v8(calls)
    calls.shift

    coverage_full[filename] = { :calls => calls, :source => clean_lines(source)}
  end

  merge_full coverage_full
end

.extract_and_report(context) ⇒ Object



45
46
47
48
# File 'lib/simplecov-jscoverage.rb', line 45

def self.extract_and_report context
  extract context
  report_and_clear
end

.merge_full(coverage_full) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/simplecov-jscoverage.rb', line 67

def self.merge_full coverage_full
  coverage_full.each do |file, coverage|
    if @coverage_full[file]

      @coverage_full[file][:calls] = @coverage_full[file][:calls].each_with_index.collect { |count, index|
        count + coverage[:calls][index] unless count.nil?
      }

    else
      @coverage_full[file] = coverage
    end
  end
end

.reportObject



18
19
20
21
22
# File 'lib/simplecov-jscoverage.rb', line 18

def self.report
  if cover?
    append_to_report @coverage_full if @coverage_full.length != 0
  end
end

.report_and_clearObject



24
25
26
27
# File 'lib/simplecov-jscoverage.rb', line 24

def self.report_and_clear
  report
  @coverage_full.clear
end

.source_for_file(context, file) ⇒ Object



81
82
83
# File 'lib/simplecov-jscoverage.rb', line 81

def self.source_for_file context, file
  convert_v8 context.eval("_$jscoverage['#{file}'].source")
end

.toolnameObject



14
15
16
# File 'lib/simplecov-jscoverage.rb', line 14

def self.toolname
  "JSCoverage"
end