Class: JsonOutputFormatter

Inherits:
RSpec::Core::Formatters::JsonFormatter
  • Object
show all
Defined in:
lib/grade_runner/formatters/json_output_formatter.rb

Instance Method Summary collapse

Instance Method Details

#close(_notification) ⇒ Object



48
49
50
# File 'lib/grade_runner/formatters/json_output_formatter.rb', line 48

def close(_notification)
  output.write  Oj.dump @output_hash
end

#dump_summary(summary) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/grade_runner/formatters/json_output_formatter.rb', line 6

def dump_summary(summary)
  total_points = summary.
    examples.
    map { |example| example..fetch(:points, GradeRunner.default_points).to_i }.
    sum
    
  earned_points = summary.
    examples.
    select { |example| example.execution_result.status == :passed }.
    map { |example| example..fetch(:points, GradeRunner.default_points).to_i }.
    sum

  score = (earned_points.to_f / total_points).round(4)
  score = 0 if score.nan?
  
  @output_hash[:summary] = {
    duration: summary.duration,
    example_count: summary.example_count,
    errors_outside_of_examples_count: summary.errors_outside_of_examples_count,
    failure_count: summary.failure_count,
    pending_count: summary.pending_count,
    total_points: total_points,
    earned_points: earned_points,
    score: score
  }
  result = (@output_hash[:summary][:score] * 100).round(2)
  
  if summary.errors_outside_of_examples_count.positive?
    result = "An error occurred while running tests"
  else
    result = result.to_s + "%"
  end
  
  
  @output_hash[:summary_line] = [
    "#{summary.example_count} #{summary.example_count == 1 ? "test" : "tests"}",
    "#{summary.failure_count} failures",
    "#{earned_points}/#{total_points} points",
    result,
  ].join(", ")
end