Sha256: 1d1906ed71cb02ef4477f046d3e678798a9624be4e67a78db004d790dd8019cd

Contents?: true

Size: 1.56 KB

Versions: 14

Compression:

Stored size: 1.56 KB

Contents

require_relative '../../test_helper'

class StateContextProxyWithUnlessConditionTest < StateMachinesTest
  def setup
    @klass = Class.new(Validateable)
    machine = StateMachines::Machine.new(@klass, initial: :parked)
    state = machine.state :parked

    @state_context = StateMachines::StateContext.new(state)
    @object = @klass.new

    @condition_result = nil
    @options = @state_context.validate(unless: lambda { @condition_result })[0]
  end

  def test_should_have_if_option
    refute_nil @options[:if]
  end

  def test_should_be_false_if_state_is_different
    @object.state = nil
    refute @options[:if].call(@object)
  end

  def test_should_be_false_if_original_condition_is_true
    @condition_result = true
    refute @options[:if].call(@object)
  end

  def test_should_be_true_if_state_matches_and_original_condition_is_false
    @condition_result = false
    assert @options[:if].call(@object)
  end

  def test_should_evaluate_symbol_condition
    @klass.class_eval do
      attr_accessor :callback
    end

    options = @state_context.validate(unless: :callback)[0]

    object = @klass.new
    object.callback = true
    refute options[:if].call(object)

    object.callback = false
    assert options[:if].call(object)
  end

  def test_should_evaluate_string_condition
    @klass.class_eval do
      attr_accessor :callback
    end

    options = @state_context.validate(unless: '@callback')[0]

    object = @klass.new
    object.callback = true
    refute options[:if].call(object)

    object.callback = false
    assert options[:if].call(object)
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
state_machines-0.5.0 test/unit/state_context/state_context_proxy_with_unless_condition_test.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/state_machines-0.2.2/test/unit/state_context/state_context_proxy_with_unless_condition_test.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/state_machines-0.2.2/test/unit/state_context/state_context_proxy_with_unless_condition_test.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/state_machines-0.2.2/test/unit/state_context/state_context_proxy_with_unless_condition_test.rb
state_machines-0.4.0 test/unit/state_context/state_context_proxy_with_unless_condition_test.rb
state_machines-0.3.0 test/unit/state_context/state_context_proxy_with_unless_condition_test.rb
state_machines-0.2.2 test/unit/state_context/state_context_proxy_with_unless_condition_test.rb
state_machines-0.2.1 test/unit/state_context/state_context_proxy_with_unless_condition_test.rb
state_machines-0.2.0 test/unit/state_context/state_context_proxy_with_unless_condition_test.rb
state_machines-0.1.4 test/unit/state_context/state_context_proxy_with_unless_condition_test.rb
state_machines-0.1.3 test/unit/state_context/state_context_proxy_with_unless_condition_test.rb
state_machines-0.1.2 test/unit/state_context/state_context_proxy_with_unless_condition_test.rb
state_machines-0.1.1 test/unit/state_context/state_context_proxy_with_unless_condition_test.rb
state_machines-0.1.0 test/unit/state_context/state_context_proxy_with_unless_condition_test.rb