Skip to content

Instantly share code, notes, and snippets.

@Sose

Sose/test.cljs Secret

Last active May 26, 2021
Embed
What would you like to do?
(def script [[:start 0 0 0] [:let :x 10] [:let :y 20] [:let :x 30]])
;; turtle/step-turtle is a function of type [turtle, script] -> turtle
;; "a turtle" is the whole environment needed to step or run a script
(deftest turtle-let
(testing "turtle let"
(let [initial-turtle (turtle/turtle-at-start script)
x-defined (turtle/step-turtle initial-turtle script)
xy-defined (turtle/step-turtle x-defined script)
x-redefined (turtle/step-turtle xy-defined script)]
(is (= {:x 10} (get x-defined :defs)))
(is (= {:x 10 :y 20} (get xy-defined :defs)))
(is (= {:x 30 :y 20} (get x-redefined :defs))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; alternative? looks pretty ugly
(def script [[:start 0 0 0] [:let :x 10] [:let :y 20] [:let :x 30]])
(def defs-at-step [{:x 10} {:x 10 :y 20} {:x 30 :y 20}])
(deftest turtle-let2
(testing "turtle let 2"
(loop [turtle (turtle/turtle-at-start script)
[defs & rest-defs] defs-at-step]
(let [newturtle (turtle/step-turtle turtle script)
[i] (:script-index turtle)]
(if (>= i (count script))
nil
(do (is (= defs (get newturtle :defs)))
(recur newturtle rest-defs)))))))
;;;;;;;;;;;;;;;;;;;;;;;;
;; edit: step 3
(def script [[:start 0 0 0] [:let :x 10] [:let :y 20] [:let :x 30]])
(def defs-at-step [{:x 10} {:x 10 :y 20} {:x 30 :y 20}])
(defn test-at-steps [script getter corrects]
(loop [turtle (turtle/turtle-at-start script)
[c & cs] corrects]
(let [newturtle (turtle/step-turtle turtle script)
[i] (:script-index turtle)]
(if (>= i (count script))
nil
(do (is (= c (getter newturtle)))
(recur newturtle cs))))))
(deftest turtle-let
(testing "turtle let"
(test-at-steps script :defs defs-at-step)))
;;;;;;;;;;;;;;;;;;;;
; version 4
(defn test-at-steps2 [script getter corrects]
(let [turtle (turtle/turtle-at-start script)
all-ts (->> turtle
(iterate #(turtle/step-turtle % script))
(drop 1)
(take (count script)))]
(dorun (map #(is (= %1 %2)) corrects (map getter all-ts)))))
(deftest turtle-let2
(testing "turtle let2"
(test-at-steps2 script :defs defs-at-step)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment