Test cases for state machine macro

This commit is contained in:
Jeremy Penner 2013-04-05 21:16:51 -04:00
parent a239d83dfa
commit 907b45a8ef

31
test/hottub/test/core.clj Normal file
View file

@ -0,0 +1,31 @@
(ns hottub.test.core
(:use [hottub.core])
(:use [clojure.test])
(:require [hottub.stm :as stm]
[hottub.gs :as gs]
hottub.stm.input))
(defn is-active [stm input]
(contains? (:mousebuttons input) :left))
(stm/defstm ::click
[(::stm/enter {:returns-value false} [stm]
(let [input-now (:input (first (gs/q :name :input)))
stm (stm/post-event stm (assoc input-now :type :input))]
stm))
(:input #(not (is-active %1 %2)))]
[(:input is-active)]
[(:input #(not (is-active %1 %2)))])
(defn run-with-mouse [stmid value mousebuttonfirst & mousebuttons]
(gs/with-gs gs/gs-empty
(gs/add-index :name)
(gs/set-entity (gs/gen-id) {:name :input :input {:mousebuttons mousebuttonfirst}})
(apply stm/run (stm/start stmid value) (map (fn [mousebutton] {:type :input :mousebuttons mousebutton}) mousebuttons))))
(deftest input-stm
(are [stm state] (= (stm/state stm) state)
(run-with-mouse ::click nil #{:left}) :start
(run-with-mouse ::click nil #{}) :start1
(run-with-mouse ::click nil #{} #{:left}) :start2
(run-with-mouse ::click nil #{} #{} #{:left} #{:left} #{}) nil))