spec/unit/events_spec.rb in finite_machine-0.6.1 vs spec/unit/events_spec.rb in finite_machine-0.7.0

- old
+ new

@@ -289,17 +289,56 @@ event :drive, :engine_on => :running, if: -> { return false } event :stop, :any => :neutral } callbacks { - on_enter(:drive) { } - on_exit(:stop) { } + on_before(:drive) { } + on_after(:stop) { } } end expect(fsm.current).to eql(:neutral) expect(fsm.start).to eql(FiniteMachine::SUCCEEDED) expect(fsm.drive).to eql(FiniteMachine::CANCELLED) expect(fsm.stop).to eql(FiniteMachine::SUCCEEDED) expect(fsm.stop).to eql(FiniteMachine::NOTRANSITION) + end + + it "allows for self transition events" do + digits = [] + callbacks = [] + phone = FiniteMachine.define do + initial :on_hook + + events { + event :digit, :on_hook => :dialing + event :digit, :dialing => :dialing + event :off_hook, :dialing => :alerting + } + + callbacks { + on_before_digit { |event, digit| digits << digit} + on_before_off_hook { |event| callbacks << "dialing #{digits.join}" } + } + end + + expect(phone.current).to eq(:on_hook) + phone.digit(9) + expect(phone.current).to eq(:dialing) + phone.digit(1) + expect(phone.current).to eq(:dialing) + phone.digit(1) + expect(phone.current).to eq(:dialing) + phone.off_hook + expect(phone.current).to eq(:alerting) + expect(digits).to match_array(digits) + expect(callbacks).to match_array(["dialing 911"]) + end + + it "detects dangerous event names" do + expect { FiniteMachine.define do + events { + event :transition, :a => :b + } + end }.to raise_error(FiniteMachine::AlreadyDefinedError) end end