Module: Roby::Test::MinitestHelpers

Includes:
ExpectExecution
Included in:
Minitest::Test, Spec
Defined in:
lib/roby/test/minitest_helpers.rb

Overview

Handlers for minitest-based tests

They mainly “tune” the default minitest behaviour to match some of the Roby idioms as e.g. using pretty-print to format exception messages

Constant Summary

Constants included from ExpectExecution

ExpectExecution::SETUP_METHODS

Instance Attribute Summary

Attributes included from ExpectExecution

#expect_execution_default_timeout

Instance Method Summary collapse

Methods included from ExpectExecution

#add_expectations, #execute, #execute_one_cycle, #expect_execution, #reset_current_expect_execution, #setup_current_expect_execution

Instance Method Details

#assert_raises(*exp, display_exceptions: false, return_original_exception: false, &block) ⇒ Object



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/roby/test/minitest_helpers.rb', line 33

def assert_raises(*exp, display_exceptions: false, return_original_exception: false, &block)
    if plan.executable?
        # Avoid having it displayed by the execution engine. We're going
        # to display any unexpected exception anyways
        display_exceptions_enabled, plan.execution_engine.display_exceptions =
            plan.execution_engine.display_exceptions?, display_exceptions
    end

    msg = exp.pop if String === exp.last

    matchers = exp.dup
    exp = exp.map do |e|
        if e.kind_of?(Queries::LocalizedErrorMatcher)
            e.model
        else
            e
        end
    end

    # The caller expects a non-Roby exception. It is going to be
    # wrapped in a LocalizedError, so make sure we properly
    # process it
    begin
        yield
    rescue *exp => e
        if matchers.any? { |m| m === e }
            assert_exception_can_be_pretty_printed(e)
            return e
        else
            flunk("#{matchers.map(&:to_s).join(', ')} exceptions expected, not #{e.class}")
        end
    rescue ::Exception => root_e # rubocop:disable Naming/RescuedExceptionsVariableName
        assert_exception_can_be_pretty_printed(root_e)
        all = Roby.flatten_exception(root_e)
        if actual_e = all.find { |e| matchers.any? { |expected_e| expected_e === e } }
            if return_original_exception
                return actual_e, root_e
            else
                return actual_e
            end
        end
        actually_caught = roby_exception_to_string(*all)
        flunk("#{exp.map(&:to_s).join(', ')} exceptions expected, not #{root_e.class} #{actually_caught}")
    end
    flunk("#{exp.map(&:to_s).join(', ')} exceptions expected but received nothing")
ensure
    if plan.executable?
        plan.execution_engine.display_exceptions =
            display_exceptions_enabled
    end
end

#register_failure(e) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/roby/test/minitest_helpers.rb', line 100

def register_failure(e)
    case e
    when Assertion
        self.failures << e
    else
        self.failures << Minitest::UnexpectedError.new(e)
    end
end

#roby_exception_to_string(*queue) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/roby/test/minitest_helpers.rb', line 85

def roby_exception_to_string(*queue)
    msg = String.new
    seen = Set.new
    while e = queue.shift
        next if seen.include?(e)

        seen << e
        e_bt = Minitest.filter_backtrace(e.backtrace).join "\n    "
        msg << "\n\n" << Roby.format_exception(e).join("\n") + "\n    #{e_bt}"

        queue.concat(e.original_exceptions) if e.respond_to?(:original_exceptions)
    end
    msg
end

#roby_find_matching_exception(expected, exception) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/roby/test/minitest_helpers.rb', line 14

def roby_find_matching_exception(expected, exception)
    queue = [exception]
    seen  = Set.new
    until queue.empty?
        e = queue.shift
        next if seen.include?(e)

        seen << e
        if expected.any? { |expected_e| e.kind_of?(expected_e) }
            return e
        end

        if e.respond_to?(:original_exceptions)
            queue.concat(e.original_exceptions)
        end
    end
    nil
end

#sanitize_exception(e) ⇒ Object

Overloaded from minitest

Minitest requires assertions to be marshallable, which this method checks. This is unnecessarily restrictive for us, so we reimplement this method to bypass that check

It means that we can’t use minitest’s client/server tests, but that’s not something we do.



117
118
119
# File 'lib/roby/test/minitest_helpers.rb', line 117

def sanitize_exception(e)
    e
end