Class: Scarpe::Components::MinitestResult

Inherits:
Object
  • Object
show all
Defined in:
scarpe-components/lib/scarpe/components/minitest_result.rb

Overview

A MinitestResult imports a JSON file from a minitest_export_reporter. But instead of creating a Minitest::Test to report the result, the MinitestResult is just a queryable Ruby object.

MinitestResult assumes there will be only one class and one method in the JSON, which is true for Scarpe but not necessarily in general.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ MinitestResult

Returns a new instance of MinitestResult.



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
47
48
49
50
51
52
53
54
55
56
57
# File 'scarpe-components/lib/scarpe/components/minitest_result.rb', line 20

def initialize(filename)
  data = JSON.parse File.read(filename)

  unless data.size == 1
    # We would want a different interface to support this in general. For now we don't
    # need it to work in general.
    raise "Scarpe::Components::MinitestResult only supports one class and method in results!"
  end

  item = data.first

  @assertions = item["assertions"]
  @method_name = item["name"]
  @class_name = item["klass"]
  @time = item["time"]
  @metadata = item.key?("metadata") ? item["metadata"]: {}

  @skip = false
  @exceptions = []
  @failures = []
  item["failures"].each do |f|
    # JSON.parse ignores json_class and won't create an arbitrary object. That's good
    # because Minitest::UnexpectedError seems to load in a bad way, so we don't want
    # it to auto-instantiate.
    d = JSON.parse f[1]
    msg = d["m"]
    case d["json_class"]
    when "Minitest::UnexpectedError"
      @exceptions << msg
    when "Minitest::Skip"
      @skip = msg
    when "Minitest::Assertion"
      @failures << msg
    else
      raise Scarpe::InternalError, "Didn't expect type #{t.inspect} as exception type when importing Minitest tests!"
    end
  end
end

Instance Attribute Details

#assertionsObject (readonly)

Returns the value of attribute assertions.



16
17
18
# File 'scarpe-components/lib/scarpe/components/minitest_result.rb', line 16

def assertions
  @assertions
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



18
19
20
# File 'scarpe-components/lib/scarpe/components/minitest_result.rb', line 18

def class_name
  @class_name
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



17
18
19
# File 'scarpe-components/lib/scarpe/components/minitest_result.rb', line 17

def method_name
  @method_name
end

Instance Method Details

#check(expect_result: :success, min_asserts: nil, max_asserts: nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'scarpe-components/lib/scarpe/components/minitest_result.rb', line 80

def check(expect_result: :success, min_asserts: nil, max_asserts: nil)
  unless [:error, :fail, :skip, :success].include?(expect_result)
    raise Scarpe::InternalError, "Expected test result should be one of [:success, :fail, :error, :skip]!"
  end

  res, msg = result_and_message
  if expect_result.to_s != res
    return [false, "Expected #{expect_result} but got #{res}: #{msg}!"]
  end

  if min_asserts && @assertions < min_asserts
    return [false, "Expected success with at least #{min_asserts} assertions but found only #{@assertions}!"]
  end
  if max_asserts && @assertions > max_asserts
    return [false, "Expected success with no more than #{max_asserts} assertions but found only #{@assertions}!"]
  end

  [true, ""]
end

#console_summaryObject



73
74
75
76
77
78
# File 'scarpe-components/lib/scarpe/components/minitest_result.rb', line 73

def console_summary
  return "Error(s): #{@exceptions.inspect}" if self.error?
  return "Failure: #{@failures.inspect}" if self.fail?
  return "Skip: #{skip_message.inspect}" if self.skip?
  "Success!"
end

#error?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'scarpe-components/lib/scarpe/components/minitest_result.rb', line 100

def error?
  !@exceptions.empty?
end

#error_messageObject



116
117
118
# File 'scarpe-components/lib/scarpe/components/minitest_result.rb', line 116

def error_message
  @exceptions[0]
end

#fail?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'scarpe-components/lib/scarpe/components/minitest_result.rb', line 104

def fail?
  !@failures.empty?
end

#fail_messageObject



120
121
122
# File 'scarpe-components/lib/scarpe/components/minitest_result.rb', line 120

def fail_message
  @failures[0]
end

#one_word_resultObject



59
60
61
62
63
64
# File 'scarpe-components/lib/scarpe/components/minitest_result.rb', line 59

def one_word_result
  return "error" if self.error?
  return "fail" if self.fail?
  return "skip" if self.skip?
  "success"
end

#passed?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'scarpe-components/lib/scarpe/components/minitest_result.rb', line 112

def passed?
  @exceptions.empty? && @failures.empty? && !@skip
end

#result_and_messageObject



66
67
68
69
70
71
# File 'scarpe-components/lib/scarpe/components/minitest_result.rb', line 66

def result_and_message
  return ["error", error_message] if self.error?
  return ["fail", fail_message] if self.fail?
  return ["skip", skip_message] if self.skip?
  ["success", "OK"]
end

#skip?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'scarpe-components/lib/scarpe/components/minitest_result.rb', line 108

def skip?
  @skip ? true : false
end

#skip_messageObject



124
125
126
# File 'scarpe-components/lib/scarpe/components/minitest_result.rb', line 124

def skip_message
  @skip
end