|
| 1 | +require File.join(File.dirname(__FILE__), 'test_helper') |
| 2 | + |
| 3 | +$VERBOSE = false |
| 4 | +require 'workflow' |
| 5 | +require 'mocha/minitest' |
| 6 | + |
| 7 | +class ConditionalsTest < Minitest::Test |
| 8 | + |
| 9 | + test 'can_<fire_event>? with conditions' do |
| 10 | + c = Class.new do |
| 11 | + include Workflow |
| 12 | + workflow do |
| 13 | + state :off do |
| 14 | + event :turn_on, :transitions_to => :on, :if => :sufficient_battery_level? |
| 15 | + event :turn_on, :transitions_to => :low_battery, :if => proc { |obj| obj.battery > 0 } |
| 16 | + end |
| 17 | + state :on |
| 18 | + state :low_battery |
| 19 | + end |
| 20 | + attr_reader :battery |
| 21 | + def initialize(battery) |
| 22 | + @battery = battery |
| 23 | + end |
| 24 | + |
| 25 | + def sufficient_battery_level? |
| 26 | + @battery > 10 |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + device = c.new 0 |
| 31 | + assert_equal false, device.can_turn_on? |
| 32 | + |
| 33 | + device = c.new 5 |
| 34 | + assert device.can_turn_on? |
| 35 | + device.turn_on! |
| 36 | + assert device.low_battery? |
| 37 | + assert_equal false, device.on? |
| 38 | + |
| 39 | + device = c.new 50 |
| 40 | + assert device.can_turn_on? |
| 41 | + device.turn_on! |
| 42 | + assert device.on? |
| 43 | + end |
| 44 | + |
| 45 | + test 'gh-227 allow event arguments in conditions - test with proc' do |
| 46 | + c = Class.new do |
| 47 | + include Workflow |
| 48 | + # define more advanced workflow, where event methods allow arguments |
| 49 | + workflow do |
| 50 | + state :off do |
| 51 | + # turn_on accepts additional argument `power_adapter` |
| 52 | + event :turn_on, :transitions_to => :on, :if => :sufficient_battery_level? |
| 53 | + event :turn_on, :transitions_to => :low_battery # otherwise |
| 54 | + end |
| 55 | + state :on |
| 56 | + state :low_battery |
| 57 | + end |
| 58 | + attr_reader :battery |
| 59 | + def initialize(battery) |
| 60 | + @battery = battery |
| 61 | + end |
| 62 | + |
| 63 | + def sufficient_battery_level?(power_adapter=false) |
| 64 | + power_adapter || @battery > 10 |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + # test for conditions in a proc |
| 69 | + device = c.new 5 |
| 70 | + device.turn_on!(true) # case with event arguments to be taken into account |
| 71 | + assert device.on? |
| 72 | + end |
| 73 | + |
| 74 | + test 'gh-227 allow event arguments in conditions - test with a method' do |
| 75 | + c = Class.new do |
| 76 | + include Workflow |
| 77 | + # define more advanced workflow, where event methods allow arguments |
| 78 | + workflow do |
| 79 | + state :off do |
| 80 | + # turn_on accepts additional argument `power_adapter` |
| 81 | + event :turn_on, :transitions_to => :on, :if => proc { |obj, power_adapter| power_adapter || obj.battery > 10 } |
| 82 | + event :turn_on, :transitions_to => :low_battery # otherwise |
| 83 | + end |
| 84 | + state :on |
| 85 | + state :low_battery |
| 86 | + end |
| 87 | + attr_reader :battery |
| 88 | + def initialize(battery) |
| 89 | + @battery = battery |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + device = c.new 5 |
| 94 | + device.turn_on!(true) # case with event arguments to be taken into account |
| 95 | + assert device.on? |
| 96 | + end |
| 97 | + |
| 98 | +end |
| 99 | + |
0 commit comments