Class: Roby::Test::Spec

Inherits:
Minitest::Spec
  • Object
show all
Extended by:
DSL
Includes:
Assertions, MinitestHelpers, RunPlanners, TeardownPlans, Utilrb::Timepoints
Defined in:
lib/roby/test/spec.rb

Instance Attribute Summary collapse

Attributes included from TeardownPlans

#registered_plans

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

describe, included, roby_should_run, run_if, run_interactive, run_live, run_on_robot, run_simulated, run_single

Methods included from RunPlanners

planner_handler_for, #run_planners, setup_planning_handlers

Methods included from MinitestHelpers

#assert_raises, #capture_exceptions, #exception_details, #filter_execution_exceptions, #register_failure, #roby_exception_to_string, #roby_find_matching_exception, #to_s

Methods included from ExpectExecution

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

Methods included from TeardownPlans

#clear_registered_plans, #register_plan, #teardown_registered_plans

Methods included from Assertions

#__capture_log, #assert_adds_error, #assert_adds_framework_error, #assert_child_of, #assert_droby_compatible, #assert_event_becomes_unreachable, #assert_event_command_failed, #assert_event_emission, #assert_event_emission_failed, #assert_event_exception, #assert_event_is_unreachable, #assert_exception_can_be_pretty_printed, #assert_fatal_exception, #assert_free_event_command_failed, #assert_free_event_emission_failed, #assert_free_event_exception, #assert_free_event_exception_warning, #assert_handled_exception, #assert_logs_event, #assert_logs_exception_with_backtrace, #assert_nonfatal_exception, #assert_notifies_free_event_exception, #assert_relative_error, #assert_same_position, #assert_sets_equal, #assert_state_machine_transition, #assert_task_fails_to_start, #assert_task_quarantined, #capture_log, #create_exception_matcher, #droby_local_marshaller, #droby_remote_marshaller, #droby_to_remote, #droby_transfer, #refute_child_of, #roby_make_flexmock_exception_matcher, #validate_state_machine

Instance Attribute Details

#models_present_in_setupObject (readonly)

Set of models present during #setup

This is used to clear all the models created during the test in #teardown



53
54
55
# File 'lib/roby/test/spec.rb', line 53

def models_present_in_setup
  @models_present_in_setup
end

Class Method Details

.roby_plan_with(matcher, handler) ⇒ Object

Declare what #roby_run_planner should use to develop a given task during a test

The latest handler registered wins



177
178
179
# File 'lib/roby/test/spec.rb', line 177

def self.roby_plan_with(matcher, handler)
    RunPlanners.roby_plan_with(matcher, handler)
end

.test_methodsObject



37
38
39
40
41
42
43
# File 'lib/roby/test/spec.rb', line 37

def self.test_methods
    methods = super
    # Duplicate each method 'repeat' times
    methods.inject([]) do |list, m|
        list.concat([m] * Roby.app.test_repeat)
    end
end

Instance Method Details

#__full_name__Object



45
46
47
# File 'lib/roby/test/spec.rb', line 45

def __full_name__
    "#{self.class}##{name}"
end

#appObject



23
24
25
# File 'lib/roby/test/spec.rb', line 23

def app
    Roby.app
end

#clear_newly_defined_modelsObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/roby/test/spec.rb', line 89

def clear_newly_defined_models
    app.root_models.each do |root_model|
        ([root_model] + root_model.each_submodel.to_a).each do |m|
            if !models_present_in_setup.include?(m)
                m.permanent_model = false
                m.clear_model
            end
        end
    end
end

#engineObject



29
30
31
32
# File 'lib/roby/test/spec.rb', line 29

def engine
    Roby.warn_deprecated "#engine is deprecated, use #execution_engine instead"
    execution_engine
end

#execution_engineObject



33
34
35
# File 'lib/roby/test/spec.rb', line 33

def execution_engine
    app.execution_engine
end

#inhibit_fatal_messages(&block) ⇒ Object

Deprecated.

use capture_log instead



144
145
146
147
# File 'lib/roby/test/spec.rb', line 144

def inhibit_fatal_messages(&block)
    Roby.warn_deprecated "#{__method__} is deprecated, use capture_log instead"
    with_log_level(Roby, Logger::FATAL, &block)
end

#planObject



26
27
28
# File 'lib/roby/test/spec.rb', line 26

def plan
    app.plan
end

#process_events(timeout: 10, **options, &caller_block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/roby/test/spec.rb', line 100

def process_events(timeout: 10, **options, &caller_block)
    Roby.warn_deprecated "do not use #process_events. Use the expect_execution infrastructure instead"

    exceptions = Array.new
    first_pass = true
    while first_pass || execution_engine.has_waiting_work?
        first_pass = false

        execution_engine.join_all_waiting_work(timeout: timeout)
        execution_engine.start_new_cycle
        errors = execution_engine.process_events(**options, &caller_block)
        caller_block = nil
        exceptions.concat(errors.exceptions)
        execution_engine.cycle_end(Hash.new)
    end

    if !exceptions.empty?
        if exceptions.size == 1
            raise exceptions.first.exception
        else
            raise SynchronousEventProcessingMultipleErrors.new(exceptions.map(&:exception))
        end
    end
end

#process_events_until(timeout: 5, **options) ⇒ Object

Repeatedly process events until a condition is met

Yield Returns:

  • (Boolean)

    true if the condition is met, false otherwise



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/roby/test/spec.rb', line 128

def process_events_until(timeout: 5, **options)
    Roby.warn_deprecated "do not use #process_events. Use the expect_execution infrastructure with the 'achieve' expectation instead"

    start = Time.now
    while !yield
        now = Time.now
        remaining = timeout - (now - start)
        if remaining < 0
            flunk("failed to reach expected condition within #{timeout} seconds")
        end
        process_events(timeout: remaining, **options)
        sleep 0.01
    end
end

#roby_run_planner(root_task, recursive: true, **options) ⇒ Object

Deprecated.


167
168
169
# File 'lib/roby/test/spec.rb', line 167

def roby_run_planner(root_task, recursive: true, **options)
    run_planners(root_task, recursive: true, **options)
end

#runObject

Filters out the test suites that are not enabled by the current Roby configuration



183
184
185
186
187
188
189
190
# File 'lib/roby/test/spec.rb', line 183

def run
    time_it do
        capture_exceptions do
            self.class.roby_should_run(self, app)
            super
        end
    end
end

#setupObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/roby/test/spec.rb', line 55

def setup
    plan.execution_engine.display_exceptions = false
    # Mark every app-defined model as permanent, so that the tests can define
    # their own and get cleanup up properly on teardown
    @models_present_in_setup = Set.new
    app.root_models.each do |root_model|
        models_present_in_setup << root_model
        root_model.each_submodel do |m|
            models_present_in_setup << m
        end
    end
    register_plan(plan)

    super
end

#teardownObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/roby/test/spec.rb', line 71

def teardown
    Timecop.return

    begin
        super
    rescue ::Exception => e
        teardown_failure = e
    end

    teardown_registered_plans

ensure
    clear_registered_plans
    if teardown_failure
        raise teardown_failure
    end
end

#with_log_level(log_object, level) ⇒ Object

Deprecated.

use capture_log instead



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/roby/test/spec.rb', line 150

def with_log_level(log_object, level)
    Roby.warn_deprecated "#{__method__} is deprecated, use capture_log instead"
    if log_object.respond_to?(:logger)
        log_object = log_object.logger
    end
    current_level = log_object.level
    log_object.level = level

    yield

ensure
    if current_level
        log_object.level = current_level
    end
end