From 907b45a8effc4f2ea523c05a63b1a9604e42c1cd Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Fri, 5 Apr 2013 21:16:51 -0400 Subject: [PATCH] Test cases for state machine macro --- test/hottub/test/core.clj | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 test/hottub/test/core.clj diff --git a/test/hottub/test/core.clj b/test/hottub/test/core.clj new file mode 100644 index 0000000..cdeb844 --- /dev/null +++ b/test/hottub/test/core.clj @@ -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))