-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm_tests2.clj
85 lines (70 loc) · 2.29 KB
/
fsm_tests2.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
(ns stateful-testing.fsm-tests2
(:require
[clojure.pprint :refer [pprint]]
[clojure.test :refer :all]
[clojure.test.check :as tc]
[clojure.test.check.generators :as gen]
[clojure.test.check.properties :as prop]
[clojure.test.check.rose-tree :as rose]
[stateful-testing.fsm-test-utils :as fsm]))
(def add-cmd
(reify
fsm/Command
(precondition [_ state]
true)
(postcondition [_ state cmd]
true)
(exec [_ state cmd]
(update-in state [:ids] conj (:id cmd)))
(generate [_ state]
(gen/fmap (partial zipmap [:type :id])
(gen/tuple (gen/return :add-cmd)
(gen/such-that #(not (contains? (:ids state) %))
gen/int))))))
(def delete-cmd
(reify
fsm/Command
(precondition [_ state]
(seq (:ids state)))
(postcondition [_ state cmd]
;;delete only valid if present in the set
(->> (:ids state)
(filter #(= % (:id cmd)))
seq))
(exec [_ state cmd]
(update-in state [:ids] (fn [ids]
(disj ids (:id cmd)))))
(generate [_ state]
(gen/fmap (partial zipmap [:type :id])
(gen/tuple (gen/return :delete-cmd)
(gen/elements (:ids state)))))))
(def clear-cmd
(reify
fsm/Command
(precondition [_ state]
true)
(postcondition [_ state cmd]
true)
(exec [_ state cmd]
(update-in state [:ids] (constantly #{})))
(generate [_ state]
(gen/fmap (partial zipmap [:type])
(gen/tuple (gen/return :clear-cmd))))))
(defn apply-commands
[commands]
(reduce (fn [ids {:keys [id type] :as cmd}]
(case type
:add-cmd (conj ids id)
:delete-cmd (set (remove #{id} ids))
:clear-cmd #{}))
#{}
commands))
(def commands-return-a-set
(prop/for-all [tx-log (fsm/cmd-seq {:ids #{}} {:add-cmd add-cmd
:delete-cmd delete-cmd
:clear-cmd clear-cmd})]
(set? (apply-commands tx-log))))
(deftest set-operations-pass
(let [result (tc/quick-check 100 commands-return-a-set)]
;(pprint result)
(is (true? (:result result)))))