diff options
author | Benoit Daloze <[email protected]> | 2020-05-31 18:22:47 +0200 |
---|---|---|
committer | Benoit Daloze <[email protected]> | 2020-05-31 18:22:47 +0200 |
commit | f4502b001a665109bf776f9037ecbc52cb5f2d88 (patch) | |
tree | d960ce72fca89876a7bf8c404c44e60b2ccb2441 /spec/mspec/lib | |
parent | 5a79d8e0507cd143100bf928a88a59a8b5a5bca6 (diff) |
Update to ruby/mspec@e3abf6b
Diffstat (limited to 'spec/mspec/lib')
-rw-r--r-- | spec/mspec/lib/mspec/helpers/numeric.rb | 8 | ||||
-rw-r--r-- | spec/mspec/lib/mspec/matchers/raise_error.rb | 35 | ||||
-rw-r--r-- | spec/mspec/lib/mspec/runner/actions/leakchecker.rb | 9 | ||||
-rw-r--r-- | spec/mspec/lib/mspec/runner/actions/timeout.rb | 2 | ||||
-rw-r--r-- | spec/mspec/lib/mspec/runner/exception.rb | 20 | ||||
-rw-r--r-- | spec/mspec/lib/mspec/utils/format.rb | 6 |
6 files changed, 56 insertions, 24 deletions
diff --git a/spec/mspec/lib/mspec/helpers/numeric.rb b/spec/mspec/lib/mspec/helpers/numeric.rb index c6c2e82722..db1fde64d8 100644 --- a/spec/mspec/lib/mspec/helpers/numeric.rb +++ b/spec/mspec/lib/mspec/helpers/numeric.rb @@ -12,6 +12,14 @@ def bignum_value(plus = 0) 0x8000_0000_0000_0000 + plus end +def max_long + 2**(0.size * 8 - 1) - 1 +end + +def min_long + -(2**(0.size * 8 - 1)) +end + # This is a bit hairy, but we need to be able to write specs that cover the # boundary between Fixnum and Bignum for operations like Fixnum#<<. Since # this boundary is implementation-dependent, we use these helpers to write diff --git a/spec/mspec/lib/mspec/matchers/raise_error.rb b/spec/mspec/lib/mspec/matchers/raise_error.rb index 0e57c1b863..878428d43a 100644 --- a/spec/mspec/lib/mspec/matchers/raise_error.rb +++ b/spec/mspec/lib/mspec/matchers/raise_error.rb @@ -6,34 +6,43 @@ class RaiseErrorMatcher @actual = nil end + # This #matches? method is unusual because it doesn't always return a boolean but instead + # re-raises the original exception if proc.call raises an exception and #matching_exception? is false. + # The reasoning is the original exception class matters and we don't want to change it by raising another exception, + # so instead we attach the #failure_message and extract it in ExceptionState#message. def matches?(proc) @result = proc.call return false rescue Exception => actual @actual = actual + if matching_exception?(actual) # The block has its own expectations and will throw an exception if it fails @block[actual] if @block - return true else + actual.instance_variable_set(:@mspec_raise_error_message, failure_message) raise actual end end - def matching_exception?(exc) - return false unless @exception === exc + def matching_class?(exc) + @exception === exc + end - if @message then - case @message - when String - return false if @message != exc.message - when Regexp - return false if @message !~ exc.message - end + def matching_message?(exc) + case @message + when String + @message == exc.message + when Regexp + @message =~ exc.message + else + true end + end - return true + def matching_exception?(exc) + matching_class?(exc) and matching_message?(exc) end def exception_class_and_message(exception_class, message) @@ -56,7 +65,7 @@ class RaiseErrorMatcher message = ["Expected #{format_expected_exception}"] if @actual - message << "but got #{format_exception(@actual)}" + message << "but got: #{format_exception(@actual)}" else message << "but no exception was raised (#{MSpec.format(@result)} was returned)" end @@ -67,7 +76,7 @@ class RaiseErrorMatcher def negative_failure_message message = ["Expected to not get #{format_expected_exception}", ""] unless @actual.class == @exception - message[1] = "but got #{format_exception(@actual)}" + message[1] = "but got: #{format_exception(@actual)}" end message end diff --git a/spec/mspec/lib/mspec/runner/actions/leakchecker.rb b/spec/mspec/lib/mspec/runner/actions/leakchecker.rb index f70799d904..9efabc79b4 100644 --- a/spec/mspec/lib/mspec/runner/actions/leakchecker.rb +++ b/spec/mspec/lib/mspec/runner/actions/leakchecker.rb @@ -49,6 +49,7 @@ class LeakChecker check_env check_argv check_encodings + check_tracepoints GC.start if [email protected]? @leaks.empty? end @@ -259,6 +260,14 @@ class LeakChecker @encoding_info = [new_internal, new_external] end + def check_tracepoints + ObjectSpace.each_object(TracePoint) do |tp| + if tp.enabled? + leak "TracePoint is still enabled: #{tp.inspect}" + end + end + end + def leak(message) if @leaks.empty? $stderr.puts "\n" diff --git a/spec/mspec/lib/mspec/runner/actions/timeout.rb b/spec/mspec/lib/mspec/runner/actions/timeout.rb index 03fe14811f..c85bf49ad3 100644 --- a/spec/mspec/lib/mspec/runner/actions/timeout.rb +++ b/spec/mspec/lib/mspec/runner/actions/timeout.rb @@ -37,7 +37,7 @@ class TimeoutAction if elapsed > @timeout STDERR.puts "\n#{@current_state.description}" STDERR.flush - abort "Example took #{now - @started}s, which is longer than the timeout of #{@timeout}s" + abort "Example took longer than the configured timeout of #{@timeout}s" end end end diff --git a/spec/mspec/lib/mspec/runner/exception.rb b/spec/mspec/lib/mspec/runner/exception.rb index 0d9bb43105..aea6610cd3 100644 --- a/spec/mspec/lib/mspec/runner/exception.rb +++ b/spec/mspec/lib/mspec/runner/exception.rb @@ -6,6 +6,7 @@ class ExceptionState def initialize(state, location, exception) @exception = exception + @failure = exception.class == SpecExpectationNotMetError || exception.class == SpecExpectationNotFoundError @description = location ? "An exception occurred during: #{location}" : "" if state @@ -19,25 +20,26 @@ class ExceptionState end def failure? - [SpecExpectationNotMetError, SpecExpectationNotFoundError].any? { |e| @exception.is_a? e } + @failure end def message - if @exception.message.empty? - "<No message>" - elsif @exception.class == SpecExpectationNotMetError || - @exception.class == SpecExpectationNotFoundError - @exception.message + message = @exception.message + message = "<No message>" if message.empty? + + if @failure + message + elsif raise_error_message = @exception.instance_variable_get(:@mspec_raise_error_message) + raise_error_message.join("\n") else - "#{@exception.class}: #{@exception.message}" + "#{@exception.class}: #{message}" end end def backtrace - @backtrace_filter ||= MSpecScript.config[:backtrace_filter] + @backtrace_filter ||= MSpecScript.config[:backtrace_filter] || %r{(?:/bin/mspec|/lib/mspec/)} bt = @exception.backtrace || [] - bt.select { |line| $MSPEC_DEBUG or @backtrace_filter !~ line }.join("\n") end end diff --git a/spec/mspec/lib/mspec/utils/format.rb b/spec/mspec/lib/mspec/utils/format.rb index bb75e131de..425dd4d11c 100644 --- a/spec/mspec/lib/mspec/utils/format.rb +++ b/spec/mspec/lib/mspec/utils/format.rb @@ -13,7 +13,11 @@ end module MSpec def self.format(obj) - obj.pretty_inspect.chomp + if String === obj and obj.include?("\n") + "\n#{obj.inspect.gsub('\n', "\n")}" + else + obj.pretty_inspect.chomp + end rescue => e "#<#{obj.class}>(#pretty_inspect raised #{e.inspect})" end |