File: callback_with_bound_method_test.rb

package info (click to toggle)
ruby-state-machines 0.100.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,600 kB
  • sloc: ruby: 18,854; sh: 17; makefile: 4
file content (38 lines) | stat: -rw-r--r-- 984 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true

require 'test_helper'

class CallbackWithBoundMethodTest < StateMachinesTest
  def setup
    @object = Object.new
  end

  def test_should_call_method_within_the_context_of_the_object_for_block_methods
    context = nil
    callback = StateMachines::Callback.new(:before, do: ->(*args) { context = [self] + args }, bind_to_object: true)
    callback.call(@object, {}, 1, 2, 3)

    assert_equal [@object, 1, 2, 3], context
  end

  def test_should_ignore_option_for_symbolic_methods
    class << @object
      attr_reader :context

      def after_ignite(*args)
        @context = args
      end
    end

    callback = StateMachines::Callback.new(:before, do: :after_ignite, bind_to_object: true)
    callback.call(@object)

    assert_empty @object.context
  end

  def test_should_ignore_option_for_string_methods
    callback = StateMachines::Callback.new(:before, do: '[1, 2, 3]', bind_to_object: true)

    assert callback.call(@object)
  end
end