Skip to content

Commit ba650a4

Browse files
committed
Unit tests
1 parent 4dcc3e9 commit ba650a4

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

test/fsm_clj/core_test.clj

+45-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,48 @@
11
(ns fsm-clj.core-test
22
(:require [clojure.test :refer :all]
3-
[fsm-clj.core :refer :all]))
3+
[fsm-clj.core :as fsm]))
44

5-
(deftest a-test
6-
(testing "FIXME, I fail."
7-
(is (= 0 1))))
5+
(defn inc-handler [acc message]
6+
(or message (inc acc)))
7+
8+
(fsm/defsm traffic-light
9+
[[:green -> :yellow when :to-yellow handler `inc-handler]
10+
[:yellow -> :red when :to-red handler `inc-handler]
11+
[:red -> :green when :to-green handler `inc-handler]
12+
[:red -> :yellow when :to-yellow handler `inc-handler]])
13+
14+
(def traffic-light-fsm (traffic-light 0))
15+
16+
(deftest fsm-test
17+
(testing "By default first transition state is the start state"
18+
(is (-> traffic-light-fsm :state (= :green))))
19+
20+
(testing "We can set an accumulator at fsm creation"
21+
(let [fsm (traffic-light 10)]
22+
(is (-> fsm :acc (= 10)))))
23+
24+
(testing "We can set initial state at fsm creation"
25+
(let [fsm (traffic-light 0 :red)]
26+
(is (-> fsm :state (= :red)))))
27+
28+
(testing "A valid transition should change the state"
29+
(is (-> traffic-light-fsm (fsm/send-event :to-yellow) :state (= :yellow))))
30+
31+
(testing "An invalid transition should not change the state"
32+
(is (-> traffic-light-fsm (fsm/send-event :to-red) :state (= :green))))
33+
34+
(testing "The transition handler should be executed"
35+
(is (-> traffic-light-fsm
36+
(fsm/send-event :to-yellow)
37+
(fsm/send-event :to-red)
38+
:acc
39+
(= 2))))
40+
41+
(testing "We can pass message to transition handler"
42+
(is (-> traffic-light-fsm
43+
(fsm/send-event :to-yellow)
44+
(fsm/send-event :to-red -1)
45+
:acc
46+
(= -1))))
47+
48+
)

0 commit comments

Comments
 (0)