Skip to content

Commit 8cede71

Browse files
committed
Unit tests improvements + code fmt
1 parent 06c9f7a commit 8cede71

File tree

3 files changed

+81
-25
lines changed

3 files changed

+81
-25
lines changed

doc/documentation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ For example, let's say that when a coin transition is triggered we should pass t
8181

8282
#### Guard
8383

84-
Guard is a function responsible to determine if a state should change or not.
84+
Guard is responsible to determine if a state should change or not.
8585

8686
Supposing that the minimum coin value to unlock the turnstile should be 50.
8787

@@ -90,7 +90,7 @@ Supposing that the minimum coin value to unlock the turnstile should be 50.
9090
(defn inc-amount [acc amount] (+ acc amount))
9191

9292
(defn guard-handler [_state amount]
93-
(when (>= amount 50)))
93+
(>= amount 50))
9494

9595
(defsm turnstile
9696
[[:locked -> :unlocked when :coin action `inc-amount guard `guard-handler]
@@ -101,5 +101,5 @@ Supposing that the minimum coin value to unlock the turnstile should be 50.
101101
:state) ;; => :lock
102102
```
103103

104-
The guard handler must be a function with two parameters, being the first the current state and the second the message
104+
The guard handler must be a function with two parameters, being the first the accumulator and the second the message
105105
sent by `send-event`.

src/fsm_clj/core.clj

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
(s/def ::action (s/cat :_ #{'action} :handler any?))
66
(s/def ::guard (s/cat :_ #{'guard} :handler any?))
77
(s/def ::transition
8-
(s/cat
9-
:state keyword?
10-
:_-> #{'->}
11-
:target keyword?
12-
:_when #{'when}
13-
:event keyword?
14-
:opts (s/* (s/alt :action ::action :guard ::guard))))
8+
(s/cat
9+
:state keyword?
10+
:_-> #{'->}
11+
:target keyword?
12+
:_when #{'when}
13+
:event keyword?
14+
:opts (s/* (s/alt :action ::action :guard ::guard))))
1515

1616
(defn- set-state [fsm state]
1717
(if ((->> fsm :transitions keys (into #{})) state)
@@ -25,8 +25,8 @@
2525

2626
(defmacro eval-fn [opts attr]
2727
`(if (-> ~opts ~attr nil?)
28-
(fn [acc# _#] (if (= ~attr :guard) true acc#))
29-
@(-> ~opts ~attr :handler eval resolve)))
28+
(fn [acc# _#] (if (= ~attr :guard) true acc#))
29+
@(-> ~opts ~attr :handler eval resolve)))
3030

3131
(defn- parse-fsm-transition [transition]
3232
(let [parsed (s/conform ::transition transition)

test/fsm_clj/core_test.clj

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,70 @@
11
(ns fsm-clj.core-test
22
(: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+
462

563
(defn inc-handler [acc message]
664
(or message (inc acc)))
7-
865
(defn foo-guard [_ message]
966
(>= message 10))
10-
11-
(fsm/defsm traffic-light
67+
(defsm traffic-light
1268
[[:green -> :yellow when :to-yellow action `inc-handler]
1369
[:yellow -> :red when :to-red action `inc-handler]
1470
[:red -> :green when :to-green guard `foo-guard]])
@@ -32,30 +88,30 @@
3288
(is (-> fsm :state (= :green)))))
3389

3490
(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))))
3692

3793
(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))))
3995

4096
(testing "The transition action should be executed"
4197
(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)
44100
:value
45101
(= 2))))
46102

47103
(testing "We can pass message to transition in send-event"
48104
(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)
51107
:value
52108
(= -1))))
53109

54110
(testing "Guard is executed before action and prevent the state to change"
55111
(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)
59115
:state
60116
(= :red)))))
61117

0 commit comments

Comments
 (0)