Class: Megatest::Runtime
- Inherits:
-
Object
- Object
- Megatest::Runtime
- Defined in:
- lib/megatest/runtime.rb
Constant Summary collapse
- EMPTY_BACKTRACE =
[].freeze
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#test_case ⇒ Object
readonly
Returns the value of attribute test_case.
Instance Method Summary collapse
- #assert(uplevel: 1) ⇒ Object
- #build_message(strings) ⇒ Object
- #diff(expected, actual) ⇒ Object
- #expect_no_failures ⇒ Object
- #fail(user_message, *message) ⇒ Object
-
#initialize(config, test_case, result) ⇒ Runtime
constructor
A new instance of Runtime.
- #minitest_compatibility? ⇒ Boolean
- #msg(positional, keyword) ⇒ Object
- #pp(object) ⇒ Object
- #record_failures(downlevel: 1, &block) ⇒ Object
- #strip_backtrace(error, yield_file, yield_line, downlevel) ⇒ Object
Constructor Details
#initialize(config, test_case, result) ⇒ Runtime
Returns a new instance of Runtime.
9 10 11 12 13 14 |
# File 'lib/megatest/runtime.rb', line 9 def initialize(config, test_case, result) @config = config @test_case = test_case @result = result @asserting = false end |
Instance Attribute Details
#result ⇒ Object (readonly)
Returns the value of attribute result.
7 8 9 |
# File 'lib/megatest/runtime.rb', line 7 def result @result end |
#test_case ⇒ Object (readonly)
Returns the value of attribute test_case.
7 8 9 |
# File 'lib/megatest/runtime.rb', line 7 def test_case @test_case end |
Instance Method Details
#assert(uplevel: 1) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/megatest/runtime.rb', line 26 def assert(uplevel: 1) if @asserting yield else @asserting = true @result.assertions_count += 1 begin yield rescue Assertion => failure if failure.backtrace.empty? failure.set_backtrace(caller_locations(uplevel + 2)) end raise ensure @asserting = false end end end |
#build_message(strings) ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'lib/megatest/runtime.rb', line 131 def (strings) return if strings.empty? if (strings.size + strings.sum(&:size)) < 80 strings.join(" ") else strings.join("\n\n") end end |
#diff(expected, actual) ⇒ Object
149 150 151 |
# File 'lib/megatest/runtime.rb', line 149 def diff(expected, actual) @config.diff(expected, actual) end |
#expect_no_failures ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/megatest/runtime.rb', line 104 def expect_no_failures was_asserting = @asserting @asserting = false yield rescue Assertion, *Megatest::IGNORED_ERRORS raise # Exceptions we shouldn't rescue rescue Exception => unexpected_error raise UnexpectedError, unexpected_error, EMPTY_BACKTRACE ensure @asserting = was_asserting end |
#fail(user_message, *message) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/megatest/runtime.rb', line 118 def fail(, *) = () if = .call if .respond_to?(:call) = String() if && !.end_with?("\n") += "\n" end = "#{}#{}" end raise(Assertion, , EMPTY_BACKTRACE) end |
#minitest_compatibility? ⇒ Boolean
141 142 143 |
# File 'lib/megatest/runtime.rb', line 141 def minitest_compatibility? @config.minitest_compatibility end |
#msg(positional, keyword) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/megatest/runtime.rb', line 94 def msg(positional, keyword) if positional.nil? keyword elsif !keyword.nil? raise ArgumentError, "Can't pass both a positional and keyword assertion message" else positional # TODO: deprecation mecanism end end |
#pp(object) ⇒ Object
145 146 147 |
# File 'lib/megatest/runtime.rb', line 145 def pp(object) @config.pretty_print(object) end |
#record_failures(downlevel: 1, &block) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/megatest/runtime.rb', line 153 def record_failures(downlevel: 1, &block) expect_no_failures(&block) rescue Assertion => assertion error = assertion while error error = strip_backtrace(error, __FILE__, __LINE__ - 4, downlevel + 2) error = error.cause end @result.failures << Failure.new(assertion) true else false end |
#strip_backtrace(error, yield_file, yield_line, downlevel) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/megatest/runtime.rb', line 45 def strip_backtrace(error, yield_file, yield_line, downlevel) if backtrace = error.backtrace_locations rindex = backtrace.rindex { |l| l.lineno == yield_line && l.path == yield_file } backtrace = backtrace.slice(0..rindex) backtrace.pop(downlevel) unless downlevel.zero? error.set_backtrace(backtrace) elsif backtrace = error.backtrace yield_point = "#{yield_file}:#{yield_line}:" rindex = backtrace.rindex { |l| l.start_with?(yield_point) } backtrace = backtrace.slice(0..rindex) backtrace.pop(downlevel) unless downlevel.zero? error.set_backtrace(backtrace) end error end |