Class: Belajar::TestResult

Inherits:
Object
  • Object
show all
Defined in:
lib/belajar/test_result.rb

Constant Summary collapse

CODE_ERROR_MESSAGE =
':( You got an error in your code!'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result_json) ⇒ TestResult

Returns a new instance of TestResult.



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
# File 'lib/belajar/test_result.rb', line 9

def initialize(result_json)
  @result = begin
    JSON.parse(result_json, symbolize_names: true)
  rescue => error
    syntax_error_json(error)
  end

  @example_count = @result.dig(:summary, :example_count)
  @failure_count = @result.dig(:summary, :failure_count)
  error_count = @result.dig(:summary, :errors_outside_of_examples_count) || 0

  if error_count > 0
    @failure_count = error_count
    details = error_details(@result)
    @result = error_json(details)
  end

  @examples = @result[:examples].map do |example|
    description = example[:full_description]
    status      = example[:status]
    exception   = example[:exception]
    message     = exception ? exception[:message] : nil

    TestExample.new(description: description, status: status, message: message)
  end
end

Instance Attribute Details

#example_countObject (readonly)

Returns the value of attribute example_count.



7
8
9
# File 'lib/belajar/test_result.rb', line 7

def example_count
  @example_count
end

#examplesObject (readonly)

Returns the value of attribute examples.



7
8
9
# File 'lib/belajar/test_result.rb', line 7

def examples
  @examples
end

#failure_countObject (readonly)

Returns the value of attribute failure_count.



7
8
9
# File 'lib/belajar/test_result.rb', line 7

def failure_count
  @failure_count
end

Instance Method Details

#passed?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/belajar/test_result.rb', line 36

def passed?
  @examples.reduce(true) do |passed, example|
    passed && example.passed?
  end
end

#summaryObject



42
43
44
45
46
47
48
# File 'lib/belajar/test_result.rb', line 42

def summary
  if passed?
    "Your code passed all tests. #{Belajar::Congratulator.message}"
  else
    build_failed_summary
  end
end

#summary_linesObject



50
51
52
# File 'lib/belajar/test_result.rb', line 50

def summary_lines
  summary.lines.map(&:strip)
end