Class: Megatest::Runtime
- Inherits:
-
Object
- Object
- Megatest::Runtime
- Defined in:
- lib/megatest/runtime.rb
Constant Summary collapse
- EMPTY_BACKTRACE =
[].freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#on_teardown ⇒ Object
readonly
Returns the value of attribute on_teardown.
-
#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
- #teardown ⇒ Object
Constructor Details
#initialize(config, test_case, result) ⇒ Runtime
Returns a new instance of Runtime.
9 10 11 12 13 14 15 |
# File 'lib/megatest/runtime.rb', line 9 def initialize(config, test_case, result) @config = config @test_case = test_case @result = result @asserting = false @on_teardown = [] end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/megatest/runtime.rb', line 7 def config @config end |
#on_teardown ⇒ Object (readonly)
Returns the value of attribute on_teardown.
7 8 9 |
# File 'lib/megatest/runtime.rb', line 7 def on_teardown @on_teardown end |
#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
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/megatest/runtime.rb', line 27 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
132 133 134 135 136 137 138 139 140 |
# File 'lib/megatest/runtime.rb', line 132 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
150 151 152 |
# File 'lib/megatest/runtime.rb', line 150 def diff(expected, actual) @config.diff(expected, actual) end |
#expect_no_failures ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/megatest/runtime.rb', line 105 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
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/megatest/runtime.rb', line 119 def fail(, *) = () if = .call if .respond_to?(:call) = String() if && !.end_with?("\n") += "\n" end = "#{}#{}" end raise(Assertion, , EMPTY_BACKTRACE) end |
#minitest_compatibility? ⇒ Boolean
142 143 144 |
# File 'lib/megatest/runtime.rb', line 142 def minitest_compatibility? @config.minitest_compatibility end |
#msg(positional, keyword) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/megatest/runtime.rb', line 95 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
146 147 148 |
# File 'lib/megatest/runtime.rb', line 146 def pp(object) @config.pretty_print(object) end |
#record_failures(downlevel: 1, &block) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/megatest/runtime.rb', line 162 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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/megatest/runtime.rb', line 46 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 |
#teardown ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/megatest/runtime.rb', line 154 def teardown until @on_teardown.empty? record_failures do @on_teardown.pop.call end end end |