|
1 | 1 | (ns fsm-clj.core-test
|
2 | 2 | (:require [clojure.test :refer :all]
|
3 |
| - [fsm-clj.core :as fsm])) |
| 3 | + [fsm-clj.core :refer :all])) |
| 4 | + |
| 5 | +(defsm simple-turnstile |
| 6 | + [[:locked -> :unlocked when :coin] |
| 7 | + [:unlocked -> :locked when :push]]) |
| 8 | +(deftest simple-transitions |
| 9 | + (testing "Basic state transition" |
| 10 | + (is (-> (simple-turnstile) |
| 11 | + (send-event :coin) |
| 12 | + :state |
| 13 | + (= :unlocked))))) |
| 14 | + |
| 15 | +(defn inc-amount [acc _] (inc acc)) |
| 16 | +(defsm turnstile-with-accumulator |
| 17 | + [[:locked -> :unlocked when :coin action `inc-amount] |
| 18 | + [:unlocked -> :locked when :push]]) |
| 19 | +(deftest accumulators+actions |
| 20 | + (testing "The accumulator is properly calculated" |
| 21 | + (is (-> (turnstile-with-accumulator 0) |
| 22 | + (send-event :coin) |
| 23 | + (send-event :push) |
| 24 | + (send-event :coin) |
| 25 | + :value |
| 26 | + (= 2))))) |
| 27 | + |
| 28 | +(defn add-amount [acc amount] (+ acc amount)) |
| 29 | +(defsm turnstile-with-accumulator2 |
| 30 | + [[:locked -> :unlocked when :coin action `add-amount] |
| 31 | + [:unlocked -> :locked when :push]]) |
| 32 | +(deftest events-with-payload |
| 33 | + (testing "" |
| 34 | + (is (-> (turnstile-with-accumulator2 0) |
| 35 | + (send-event :coin 50) |
| 36 | + (send-event :push) |
| 37 | + (send-event :coin 100) |
| 38 | + :value |
| 39 | + (= 150))))) |
| 40 | + |
| 41 | +(defn guard-handler [_state amount] |
| 42 | + (>= amount 50)) |
| 43 | +(defsm turnstile-with-guard |
| 44 | + [[:locked -> :unlocked when :coin action `add-amount guard `guard-handler] |
| 45 | + [:unlocked -> :locked when :push]]) |
| 46 | + |
| 47 | +(deftest guard |
| 48 | + (testing "An amount < than 50 should not unlock the turnstile" |
| 49 | + (is (-> (turnstile-with-guard 0) |
| 50 | + (send-event :coin 25) |
| 51 | + :state |
| 52 | + (= :locked)))) |
| 53 | + (testing "An Amount greater or equal 50 should unlock the turnstile" |
| 54 | + (is (-> (turnstile-with-guard 0) |
| 55 | + (send-event :coin 50) |
| 56 | + :state |
| 57 | + (= :unlocked))))) |
| 58 | + |
| 59 | + |
| 60 | +;; generic traffic light fsm tests |
| 61 | + |
4 | 62 |
|
5 | 63 | (defn inc-handler [acc message]
|
6 | 64 | (or message (inc acc)))
|
7 |
| - |
8 | 65 | (defn foo-guard [_ message]
|
9 | 66 | (>= message 10))
|
10 |
| - |
11 |
| -(fsm/defsm traffic-light |
| 67 | +(defsm traffic-light |
12 | 68 | [[:green -> :yellow when :to-yellow action `inc-handler]
|
13 | 69 | [:yellow -> :red when :to-red action `inc-handler]
|
14 | 70 | [:red -> :green when :to-green guard `foo-guard]])
|
|
32 | 88 | (is (-> fsm :state (= :green)))))
|
33 | 89 |
|
34 | 90 | (testing "A valid transition should change the state"
|
35 |
| - (is (-> traffic-light-fsm (fsm/send-event :to-yellow) :state (= :yellow)))) |
| 91 | + (is (-> traffic-light-fsm (send-event :to-yellow) :state (= :yellow)))) |
36 | 92 |
|
37 | 93 | (testing "An invalid transition should not change the state"
|
38 |
| - (is (-> traffic-light-fsm (fsm/send-event :to-red) :state (= :green)))) |
| 94 | + (is (-> traffic-light-fsm (send-event :to-red) :state (= :green)))) |
39 | 95 |
|
40 | 96 | (testing "The transition action should be executed"
|
41 | 97 | (is (-> traffic-light-fsm
|
42 |
| - (fsm/send-event :to-yellow) |
43 |
| - (fsm/send-event :to-red) |
| 98 | + (send-event :to-yellow) |
| 99 | + (send-event :to-red) |
44 | 100 | :value
|
45 | 101 | (= 2))))
|
46 | 102 |
|
47 | 103 | (testing "We can pass message to transition in send-event"
|
48 | 104 | (is (-> traffic-light-fsm
|
49 |
| - (fsm/send-event :to-yellow) |
50 |
| - (fsm/send-event :to-red -1) |
| 105 | + (send-event :to-yellow) |
| 106 | + (send-event :to-red -1) |
51 | 107 | :value
|
52 | 108 | (= -1))))
|
53 | 109 |
|
54 | 110 | (testing "Guard is executed before action and prevent the state to change"
|
55 | 111 | (is (-> traffic-light-fsm
|
56 |
| - (fsm/send-event :to-yellow) |
57 |
| - (fsm/send-event :to-red) |
58 |
| - (fsm/send-event :to-green 8) |
| 112 | + (send-event :to-yellow) |
| 113 | + (send-event :to-red) |
| 114 | + (send-event :to-green 8) |
59 | 115 | :state
|
60 | 116 | (= :red)))))
|
61 | 117 |
|
0 commit comments