Module: Roby::Test::Assertions

Included in:
Self, Spec, TestCase
Defined in:
lib/roby/test/assertions.rb

Defined Under Namespace

Classes: FlexmockExceptionMatcher, FlexmockExceptionTasks

Deprecated assertions replaced by expect_execution collapse

Instance Method Summary collapse

Instance Method Details

#__capture_log(mock, level) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/roby/test/assertions.rb', line 81

def __capture_log(mock, level)
    Roby.disable_colors

    object_logger =
        if mock.respond_to?(:logger)
            mock.logger
        else
            mock
        end

    capture = []
    original_level = object_logger.level
    level_value = Logger.const_get(level.upcase)
    object_logger.level = level_value if original_level > level_value

    mock.should_receive(level)
        .and_return do |msg|
            if msg.respond_to?(:to_str)
                capture << msg
            else
                mock.invoke_original(level) do
                    capture << msg.call
                    break
                end
            end
        end

    yield
    capture
ensure
    Roby.enable_colors_if_available
    object_logger.level = original_level
end

#assert_adds_error(matcher, original_exception: nil, failure_point: PlanObject, &block) ⇒ Object

Deprecated.

use #expect_execution { … } .to { have_error_matching … } instead



540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/roby/test/assertions.rb', line 540

def assert_adds_error(matcher,
                      original_exception: nil,
                      failure_point: PlanObject, &block)
    Roby.warn_deprecated "#{__method__} is deprecated, use "\
                         "#expect_execution { ... }.to "\
                         "{ have_error_matching ... } instead"

    matcher = create_exception_matcher(
        matcher,
        original_exception: original_exception,
        failure_point: failure_point
    )
    expect_execution(&block)
        .to { have_error_matching matcher }
end

#assert_adds_framework_error(matcher, &block) ⇒ Object

Deprecated.

use #expect_execution { … } .to { have_framework_error_matching … } instead



558
559
560
561
562
563
564
# File 'lib/roby/test/assertions.rb', line 558

def assert_adds_framework_error(matcher, &block)
    Roby.warn_deprecated "#{__method__} is deprecated, use "\
                         "#expect_execution { ... }.to "\
                         "{ have_framework_error_matching ... } instead"
    expect_execution(&block)
        .to { have_framework_error_matching matcher }
end

#assert_child_of(parent, child, relation, *info) ⇒ Object

Asserts that two tasks are in a parent-child relationship in a specific relation

Parameters:

  • parent (Roby::Task)

    the parent task

  • child (Roby::Task)

    the child task

  • relation (Relations::Models::Graph)

    the relation

  • info (#===)

    optional object used to match the edge information. Leave empty if it should not be matched. Note that giving ‘nil’ will match against nil



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/roby/test/assertions.rb', line 139

def assert_child_of(parent, child, relation, *info)
    assert_same parent.relation_graphs, child.relation_graphs,
                "#{parent} and #{child} cannot be related as they "\
                "are not acting on the same relation graphs"
    graph = parent.relation_graph_for(relation)
    assert graph.has_vertex?(parent),
           "#{parent} and #{child} canot be related in #{relation}"\
           " as the former is not in the graph"
    assert graph.has_vertex?(child),
           "#{parent} and #{child} canot be related in #{relation}"\
           " as the latter is not in the graph"
    assert parent.child_object?(child, relation),
           "#{child} is not a child of #{parent} in #{relation}"
    unless info.empty?
        assert_equal info.first, parent[child, relation], "info differs"
    end
end

#assert_droby_compatible(object, local_marshaller: droby_local_marshaller, remote_marshaller: droby_remote_marshaller, bidirectional: false) ⇒ Object

Asserts that an object can marshalled an unmarshalled by the DRoby protocol

It does not verify that the resulting objects are equal as they usually are not

Parameters:

  • object (Object)

    the object to test against

  • local_marshaller (DRoby::Marshal) (defaults to: droby_local_marshaller)

    the local marshaller, which will be used to marshal ‘object’

  • remote_marshaller (DRoby::Marshal) (defaults to: droby_remote_marshaller)

    the remote marshaller, which will be used to unmarshal the marshalled version of ‘object’

Returns:

  • (Object)

    the ‘remote’ object created from the unmarshalled droby representation



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/roby/test/assertions.rb', line 235

def assert_droby_compatible(
    object, local_marshaller: droby_local_marshaller,
    remote_marshaller: droby_remote_marshaller,
    bidirectional: false
)
    remote_object = droby_transfer(
        object,
        local_marshaller: local_marshaller,
        remote_marshaller: remote_marshaller
    )

    return remote_object unless bidirectional

    local_object = droby_transfer(
        remote_object,
        local_marshaller: remote_marshaller,
        remote_marshaller: local_marshaller
    )
    [remote_object, local_object]
end

#assert_event_becomes_unreachable(generator, timeout: 5, &block) ⇒ Object

Deprecated.

use #expect_execution { … } .to { become_unreachable generator }



322
323
324
325
326
327
328
329
# File 'lib/roby/test/assertions.rb', line 322

def assert_event_becomes_unreachable(generator, timeout: 5, &block)
    Roby.warn_deprecated "#{__method__} is deprecated, use "\
                         "#expect_execution { ... }.to "\
                         "{ become_unreachable generator } instead"
    expect_execution(&block)
        .timeout(timeout)
        .to { become_unreachable generator }
end

#assert_event_command_failed(exception = CommandFailed, original_exception: nil, failure_point: EventGenerator, execution_engine: nil, &block) ⇒ Object

Deprecated.

use expect_execution { … } .to { have_error_matching CommandFailed.match… }



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/roby/test/assertions.rb', line 385

def assert_event_command_failed(exception = CommandFailed,
                                original_exception: nil,
                                failure_point: EventGenerator,
                                execution_engine: nil, &block)
    Roby.warn_deprecated "#{__method__} is deprecated, use "\
                         "#expect_execution { ... }.to "\
                         "{ have_error_matching CommandFailed.match... } "\
                         "instead"

    assert_event_exception(
        exception,
        original_exception: original_exception,
        failure_point: failure_point,
        execution_engine: execution_engine, &block
    )
end

#assert_event_emission(positive = [], negative = [], _msg = nil, timeout = 5, enable_scheduler: nil, garbage_collect_pass: true) ⇒ Object

Deprecated.

use #expect_execution { … }.to { emit event } instead



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/roby/test/assertions.rb', line 294

def assert_event_emission(
    positive = [], negative = [], _msg = nil, timeout = 5,
    enable_scheduler: nil, garbage_collect_pass: true
)
    Roby.warn_deprecated "#{__method__} is deprecated, use "\
                         "#expect_execution { ... }.to { emit event } instead"
    expect_execution { yield if block_given? }
        .timeout(timeout)
        .scheduler(enable_scheduler)
        .garbage_collect(garbage_collect_pass)
        .to do
            not_emit(*Array(negative))
            emit(*Array(positive))
        end
end

#assert_event_emission_failed(exception = EmissionFailed, original_exception: nil, failure_point: EventGenerator, execution_engine: nil, &block) ⇒ Object

Deprecated.

use expect_execution { … } .to { have_error_matching EmissionFailed.match.… }



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/roby/test/assertions.rb', line 348

def assert_event_emission_failed(exception = EmissionFailed,
                                 original_exception: nil,
                                 failure_point: EventGenerator,
                                 execution_engine: nil, &block)
    Roby.warn_deprecated "#{__method__} is deprecated, use "\
                         "#expect_execution { ... }.to "\
                         "{ have_error_matching EmissionFailed.match... } "\
                         "instead"
    assert_event_exception(
        exception,
        original_exception: original_exception,
        failure_point: failure_point,
        execution_engine: execution_engine, &block
    )
end

#assert_event_exception(matcher, original_exception: nil, failure_point: EventGenerator) ⇒ Object

Deprecated.

use expect_execution { … }.to { have_error_matching … }



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/roby/test/assertions.rb', line 419

def assert_event_exception(matcher,
                           original_exception: nil,
                           failure_point: EventGenerator)
    Roby.warn_deprecated "#{__method__} is deprecated, use "\
                         "#expect_execution { ... }.to "\
                         "{ have_error_matching ... } instead"

    matcher = create_exception_matcher(
        matcher,
        original_exception: original_exception,
        failure_point: failure_point
    )
    expect_execution { yield if block_given? }
        .to { have_error_matching matcher }
end

#assert_event_is_unreachable(event, reason: nil) ⇒ Object

Verifies that a given event is unreachable, optionally checking its unreachability reason

Returns:

  • the unreachability reason



119
120
121
122
123
124
125
126
127
128
# File 'lib/roby/test/assertions.rb', line 119

def assert_event_is_unreachable(event, reason: nil)
    assert event.unreachable?,
           "#{event} was expected to be unreachable but is not"
    if reason
        assert(reason === event.unreachability_reason,
               "the unreachability of #{event} was expected to "\
               "match #{reason} but it is #{event.unreachability_reason}")
    end
    event.unreachability_reason
end

#assert_exception_can_be_pretty_printed(e) ⇒ Object

Checks that an exception’s #pretty_print method passes without exceptions



46
47
48
49
50
# File 'lib/roby/test/assertions.rb', line 46

def assert_exception_can_be_pretty_printed(e)
    # verify that the exception can be pretty-printed, all Roby
    # exceptions should
    PP.pp(e, "".dup)
end

#assert_fatal_exception(matcher, failure_point: Task, original_exception: nil, tasks: [], kill_tasks: tasks, garbage_collect: false) ⇒ Object

Deprecated.

use expect_execution { … }.to { have_error_matching … }



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/roby/test/assertions.rb', line 454

def assert_fatal_exception(matcher,
                           failure_point: Task,
                           original_exception: nil,
                           tasks: [],
                           kill_tasks: tasks,
                           garbage_collect: false)
    Roby.warn_deprecated "#{__method__} is deprecated, "\
                         "use #expect_execution { ... }.to "\
                         "{ have_error_matching ... } instead"
    matcher = create_exception_matcher(
        matcher,
        original_exception: original_exception,
        failure_point: failure_point
    )
    expect_execution { yield if block_given? }
        .garbage_collect(garbage_collect)
        .to { have_error_matching matcher }
        .exception
end

#assert_free_event_command_failed(exception = CommandFailed, original_exception: nil, failure_point: EventGenerator, execution_engine: nil, &block) ⇒ Object

Deprecated.

use expect_execution { … } .to { have_error_matching CommandFailed.match… }



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/roby/test/assertions.rb', line 366

def assert_free_event_command_failed(exception = CommandFailed,
                                     original_exception: nil,
                                     failure_point: EventGenerator,
                                     execution_engine: nil, &block)
    Roby.warn_deprecated "#{__method__} is deprecated, use "\
                         "#expect_execution { ... }.to "\
                         "{ have_error_matching CommandFailed.match... } "\
                         "instead"

    assert_event_exception(
        exception,
        original_exception: original_exception,
        failure_point: failure_point,
        execution_engine: execution_engine, &block
    )
end

#assert_free_event_emission_failed(exception = EmissionFailed, original_exception: nil, failure_point: EventGenerator, &block) ⇒ Object

Deprecated.

use expect_execution { … }.to { have_error_matching … }



332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/roby/test/assertions.rb', line 332

def assert_free_event_emission_failed(exception = EmissionFailed,
                                      original_exception: nil,
                                      failure_point: EventGenerator, &block)
    Roby.warn_deprecated(
        "#{__method__} is deprecated, use instead expect_execution { ... }"\
        ".to { have_error_matching ... }"
    )
    assert_event_emission_failed(
        exception,
        original_exception: original_exception,
        failure_point: failure_point, &block
    )
end

#assert_free_event_exception(matcher, original_exception: nil, failure_point: EventGenerator, execution_engine: nil, &block) ⇒ Object

Deprecated.

use expect_execution { … }.to { have_error_matching … }



403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/roby/test/assertions.rb', line 403

def assert_free_event_exception(matcher,
                                original_exception: nil,
                                failure_point: EventGenerator,
                                execution_engine: nil, &block)
    Roby.warn_deprecated "#{__method__} is deprecated, use "\
                         "#expect_execution { ... }.to "\
                         "{ have_error_matching ... } instead"

    assert_event_exception(
        matcher,
        original_exception: original_exception,
        failure_point: failure_point,
        execution_engine: execution_engine, &block)
end

#assert_free_event_exception_warning(&block) ⇒ Object

Deprecated.


517
518
519
520
521
522
523
# File 'lib/roby/test/assertions.rb', line 517

def assert_free_event_exception_warning(&block)
    Roby.warn_deprecated "#{__method__} is deprecated, and has no "\
                         "replacements. It is not needed when using "\
                         "the expect_execution harness"
    messages = capture_log(execution_engine, :warn, &block)
    assert_equal ["1 free event exceptions"], messages
end

#assert_handled_exception(matcher, failure_point: Task, original_exception: nil, tasks: []) ⇒ Object

Deprecated.

use #expect_execution { … } .to { have_handled_error_matching … }



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/roby/test/assertions.rb', line 476

def assert_handled_exception(matcher,
                             failure_point: Task,
                             original_exception: nil,
                             tasks: [])
    Roby.warn_deprecated "#{__method__} is deprecated, use "\
                         "#expect_execution { ... }.to "\
                         "{ have_handled_error_matching ... } instead"
    matcher = create_exception_matcher(
        matcher,
        original_exception: original_exception,
        failure_point: failure_point
    )
    expect_execution { yield if block_given? }
        .to { have_handled_error_matching matcher }
end

#assert_logs_event(event_name, *args) ⇒ Object

Expects the event logger to receive the given message

The assertion is validated on teardown



259
260
261
# File 'lib/roby/test/assertions.rb', line 259

def assert_logs_event(event_name, *args)
    @expected_events << [event_name, args]
end

#assert_logs_exception_with_backtrace(exception_m, logger, level) ⇒ Object

Deprecated.


510
511
512
513
514
# File 'lib/roby/test/assertions.rb', line 510

def assert_logs_exception_with_backtrace(exception_m, logger, level)
    flexmock(Roby).should_receive(:log_exception_with_backtrace)
                  .once
                  .with(exception_m, logger, level)
end

#assert_nonfatal_exception(matcher, failure_point: Task, original_exception: nil, tasks: [], &block) ⇒ Object

Deprecated.

use expect_execution { … }.to { have_error_matching … }



493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/roby/test/assertions.rb', line 493

def assert_nonfatal_exception(matcher,
                              failure_point: Task,
                              original_exception: nil,
                              tasks: [], &block)
    Roby.warn_deprecated "#{__method__} is deprecated, use "\
                         "#expect_execution { ... }.to "\
                         "{ have_error_matching ... } instead"
    matcher = create_exception_matcher(
        matcher,
        original_exception: original_exception,
        failure_point: failure_point
    )
    expect_execution
        .to { have_error_matching matcher }
end

#assert_notifies_free_event_exception(matcher, failure_point: nil) ⇒ Object

Deprecated.


526
527
528
529
530
531
532
533
534
535
536
# File 'lib/roby/test/assertions.rb', line 526

def assert_notifies_free_event_exception(matcher, failure_point: nil)
    Roby.warn_deprecated "#{__method__} is deprecated, and has no "\
                         "replacements. It is not needed when using "\
                         "the expect_execution harness"
    flexmock(execution_engine)
        .should_receive(:notify_exception)
        .with(
            ExecutionEngine::EXCEPTION_FREE_EVENT,
            *roby_make_flexmock_exception_matcher(matcher, [failure_point])
        ).once
end

#assert_pp(expected, object) ⇒ Object

Checks the result of pretty-printing an object

The method will ignore non-empty blank lines as output of the pretty-print, and an empty line at the end. The reason is that pretty-print will add spaces at the nest level after a breakable, which is hard (if not impossible) to represent when using an editor that cleans trailing whitespaces



674
675
676
677
678
679
680
681
682
683
684
# File 'lib/roby/test/assertions.rb', line 674

def assert_pp(expected, object)
    actual = PP.pp(object, "".dup).chomp
    actual = actual.split("\n").map do |line|
        if line =~ /^\s+$/
            ""
        else
            line
        end
    end.join("\n")
    assert_equal expected, actual
end

#assert_relative_error(expected, found, error, msg = "") ⇒ Object

This assertion fails if the relative error between found and expectedis more than error



170
171
172
173
174
175
176
177
178
# File 'lib/roby/test/assertions.rb', line 170

def assert_relative_error(expected, found, error, msg = "")
    if expected == 0
        assert_in_delta(0, found, error,
                        "comparing #{found} to #{expected} in #{msg}")
    else
        assert_in_delta(0, (found - expected) / expected, error,
                        "comparing #{found} to #{expected} in #{msg}")
    end
end

#assert_same_position(expected, found, dl = 0.01, dt = 0.01, msg = "") ⇒ Object

This assertion fails if found and expected are more than dl meters apart in the x, y and z coordinates, or dt radians apart in angles



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

def assert_same_position(expected, found, dl = 0.01, dt = 0.01, msg = "")
    assert_relative_error(expected.x, found.x, dl, msg)
    assert_relative_error(expected.y, found.y, dl, msg)
    assert_relative_error(expected.z, found.z, dl, msg)
    assert_relative_error(expected.yaw, found.yaw, dt, msg)
    assert_relative_error(expected.pitch, found.pitch, dt, msg)
    assert_relative_error(expected.roll, found.roll, dt, msg)
end

#assert_sets_equal(expected, actual) ⇒ Object

Better equality test for sets

It displays the difference between the two sets



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/roby/test/assertions.rb', line 55

def assert_sets_equal(expected, actual)
    if !(diff = (expected - actual)).empty?
        flunk("expects two sets to be equal, but #{expected} is "\
              "missing #{diff.size} expected elements:\n  "\
              "#{diff.to_a.map(&:to_s).join(', ')}")
    elsif !(diff = (actual - expected)).empty?
        flunk("expects two sets to be equal, but #{actual} has "\
              "#{diff.size} more elements than expected:\n  "\
              "#{diff.to_a.map(&:to_s).join(', ')}")
    end
end

#assert_state_machine_transition(state_machine_task, to_state: Regexp.new, timeout: 5, start: true) ⇒ Object

Deprecated.


630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
# File 'lib/roby/test/assertions.rb', line 630

def assert_state_machine_transition(state_machine_task,
                                    to_state: Regexp.new,
                                    timeout: 5,
                                    start: true)
    Roby.warn_deprecated "#{__method__} is deprecated, use "\
                         "#validate_state_machine instead"
    state_machines =
        state_machine_task
        .each_coordination_object
        .find_all { |obj| obj.kind_of?(Coordination::ActionStateMachine) }
    if state_machines.empty?
        raise ArgumentError, "#{state_machine_task} has no state machines"
    end

    if to_state.respond_to?(:to_str) && !to_state.end_with?("_state")
        to_state = "#{to_state}_state"
    end

    done = false
    state_machines.each do |m|
        m.on_transition do |_, new_state|
            done = true if to_state === new_state.name
        end
    end
    yield if block_given?
    process_events_until(timeout: timeout, garbage_collect_pass: false) do
        done
    end
    roby_run_planner(state_machine_task)
    if start
        assert_event_emission(
            state_machine_task.current_task_child.start_event
        )
    end
    state_machine_task.current_task_child
end

#assert_task_fails_to_start(task, matcher, failure_point: task.start_event, original_exception: nil, tasks: []) ⇒ Object

Deprecated.

use expect_execution { … }.to { fail_to_start … }



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/roby/test/assertions.rb', line 436

def assert_task_fails_to_start(task, matcher,
                               failure_point: task.start_event,
                               original_exception: nil,
                               tasks: [])
    Roby.warn_deprecated "#{__method__} is deprecated, use "\
                         "#expect_execution { ... }.to "\
                         "{ fail_to_start ... } instead"
    matcher = create_exception_matcher(
        matcher,
        original_exception: original_exception,
        failure_point: failure_point
    )
    expect_execution { yield if block_given? }
        .to { fail_to_start task, matcher }
    task.failure_reason
end

#assert_task_quarantined(task, timeout: 5, &block) ⇒ Object

Deprecated.

use #expect_execution { … }.to { quarantine task }



311
312
313
314
315
316
317
318
# File 'lib/roby/test/assertions.rb', line 311

def assert_task_quarantined(task, timeout: 5, &block)
    Roby.warn_deprecated "#{__method__} is deprecated, use "\
                         "#expect_execution { ... }.to { quarantine task } "\
                         "instead"
    expect_execution(&block)
        .timeout(timeout)
        .to { quarantine task }
end

#capture_log(object, level, &block) ⇒ Array<String>

Capture log output from one logger and returns it

Note that it currently does not “de-shares” loggers

Parameters:

  • a (Logger, #logger)

    logger object, or an object that holds one

  • level (Symbol)

    the name of the logging method (e.g. :warn)

Returns:

  • (Array<String>)


75
76
77
78
79
# File 'lib/roby/test/assertions.rb', line 75

def capture_log(object, level, &block)
    FlexMock.use(object) do |mock|
        __capture_log(mock, level, &block)
    end
end

#create_exception_matcher(localized_error_type, original_exception: nil, failure_point: nil) ⇒ #===

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Helper method that creates a matching object for localized errors

Parameters:

  • localized_error_type (LocalizedError)

    the error model to match

  • original_exception (Exception, nil) (defaults to: nil)

    an original exception to match, for exceptions that transform exceptions into other (e.g. CodeError)

  • failure_point (Task, EventGenerator) (defaults to: nil)

    the exceptions’ failure point

Returns:

  • (#===)

    an object that can match an execution exception



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/roby/test/assertions.rb', line 277

def create_exception_matcher(localized_error_type,
                             original_exception: nil,
                             failure_point: nil)
    matcher = localized_error_type.match
    matcher.with_original_exception(original_exception) if original_exception
    if matcher.respond_to?(:with_ruby_exception) && matcher.ruby_exception_class == Exception
        if original_exception
            matcher.with_ruby_exception(original_exception)
        else
            matcher.without_ruby_exception
        end
    end
    matcher.with_origin(failure_point) if failure_point
    matcher
end

#droby_local_marshallerObject



192
193
194
# File 'lib/roby/test/assertions.rb', line 192

def droby_local_marshaller
    @droby_local_marshaller ||= DRoby::Marshal.new
end

#droby_remote_marshallerObject



196
197
198
# File 'lib/roby/test/assertions.rb', line 196

def droby_remote_marshaller
    @droby_remote_marshaller ||= DRoby::Marshal.new
end

#droby_to_remote(object, local_marshaller: droby_local_marshaller) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/roby/test/assertions.rb', line 200

def droby_to_remote(object, local_marshaller: droby_local_marshaller)
    droby = local_marshaller.dump(object)
    dumped =
        begin Marshal.dump(droby)
        rescue Exception => e
            require "roby/droby/logfile/writer"
            obj, exception = Roby::DRoby::Logfile::Writer
                             .find_invalid_marshalling_object(droby)
            raise e, "#{obj} cannot be marshalled: "\
                     "#{exception.message}", exception.backtrace
        end
    Marshal.load(dumped)
end

#droby_transfer(object, local_marshaller: droby_local_marshaller, remote_marshaller: droby_remote_marshaller) ⇒ Object



214
215
216
217
218
219
# File 'lib/roby/test/assertions.rb', line 214

def droby_transfer(object,
                   local_marshaller: droby_local_marshaller,
                   remote_marshaller: droby_remote_marshaller)
    loaded = droby_to_remote(object, local_marshaller: local_marshaller)
    remote_marshaller.local_object(loaded)
end

#find_state_machine_capture(task, capture_name) ⇒ (Coordination::Models::Capture, Coordination::ActionStateMachine)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds a capture with the given name in the state machine(s) attached to a task



732
733
734
735
736
737
738
739
740
741
# File 'lib/roby/test/assertions.rb', line 732

def find_state_machine_capture(task, capture_name)
    task.each_coordination_object do |object|
        next unless object.model.respond_to?(:each_capture)

        object.model.each_capture do |c, _|
            return [c, object] if c.name == capture_name
        end
    end
    nil
end

#refute_child_of(parent, child, relation) ⇒ Object

Asserts that two tasks are not a parent-child relationship in a specific relation



159
160
161
162
163
164
165
166
# File 'lib/roby/test/assertions.rb', line 159

def refute_child_of(parent, child, relation)
    assert_same parent.relation_graphs, child.relation_graphs,
                "#{parent} and #{child} cannot be related as they "\
                "are not acting on the same relation graphs"
    graph = parent.relation_graph_for(relation)
    refute(graph.has_vertex?(parent) && graph.has_vertex?(child) &&
           parent.child_object?(child, relation))
end

#roby_make_flexmock_exception_matcher(matcher, tasks) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Helper method that creates exception matchers that provide better error messages, for the benefit of the exception assertions



622
623
624
625
# File 'lib/roby/test/assertions.rb', line 622

def roby_make_flexmock_exception_matcher(matcher, tasks)
    [FlexmockExceptionMatcher.new(matcher.to_execution_exception_matcher),
     FlexmockExceptionTasks.new(tasks.to_set)]
end

#roby_normalize_exception_message(msg) ⇒ Object

Remove variations from an exception message that are run-dependent

This allows to validate the formatation of a message within unit tests



746
747
748
749
750
751
752
# File 'lib/roby/test/assertions.rb', line 746

def roby_normalize_exception_message(msg)
    msg.gsub(/id:\s*"?\d+"?/, "id:XX")
       .gsub(/\[[\d:.]* @\d+\]/, "[XX]")
       .gsub(/^\s+$/, "")
       .gsub(/\.rb:\d+:/, ".rb:XXX:")
       .gsub(%r{/.*tools/roby/}, "") # paths may be absolute or relative
end

#run_state_machine_capture(task, capture_name, context: [], event: nil) ⇒ Object

Run a capture block and return the result

This is typically used in an action interface spec this way:

task = my_action(**arguments)
task = run_planners(task)
result = run_capture(task, 'capture_name', context: current_pose)

Note that a capture’s name is the name of the local variable it is assigned to. For instance, in the following example, the capture’s name is ‘c’

action_state_machine 'test' do
   task = state(something)
   c = capture(task.success_event) do |e|
   end

Parameters:

  • task (Roby::Task)

    the roby task that is supporting the capture

  • capture_name (String)

    the name of the capture, which is the name of the local variable it is assigned to

  • context (Object) (defaults to: [])

    the context of the event passed to the capture, in the common case where the capture only reads the event context. Use either ‘context’ or ‘event’, but not both.

  • event (Roby::Event) (defaults to: nil)

    the event that should be passed to the capture. Use either ‘context’ or ‘event’, but not both



711
712
713
714
715
716
717
718
719
720
721
722
723
724
# File 'lib/roby/test/assertions.rb', line 711

def run_state_machine_capture(task, capture_name, context: [], event: nil)
    if event && (!context.kind_of?(Array) || !context.empty?)
        raise ArgumentError, "cannot pass both context and event"
    end

    (capture, state_machine) = find_state_machine_capture(task, capture_name)
    unless capture
        raise ArgumentError, "no capture named '#{capture_name}' in any "\
            "state machine associated with #{task}"
    end

    event ||= Struct.new(:context).new(Array(context))
    capture.filter(state_machine, event)
end

#setupObject



6
7
8
9
# File 'lib/roby/test/assertions.rb', line 6

def setup
    @expected_events = []
    super
end

#teardownObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/roby/test/assertions.rb', line 11

def teardown
    @expected_events.each do |m, args|
        unless plan.event_logger.has_received_event?(m, *args)
            received_events_s =
                plan
                .event_logger.received_events
                .find_all { |ev_name, _| ev_name.to_s !~ /timegroup/ }
                .map do |ev_name, _time, ev_args|
                    "#{ev_name}(#{ev_args.map(&:to_s).join(', ')})"
                end
                .join('\n  ')

            flunk("expected to receive a log event "\
                  "#{m}(#{args.map(&:to_s).join(', ')}) but did not. "\
                  "Received:\n  #{received_events_s}")
        end
    end
    super
end

#validate_state_machine(task_or_action, &block) ⇒ Object

Perform tests on an action state machine

Parameters:



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

def validate_state_machine(task_or_action, &block)
    machine = ValidateStateMachine.new(self, task_or_action)
    unless task_or_action.respond_to?(:running?) && task_or_action.running?
        machine.start
    end
    machine.evaluate(&block)
end