Back
with-redefs-fn (clj)
(source)function
(with-redefs-fn binding-map func)
Temporarily redefines Vars during a call to func. Each val of
binding-map will replace the root value of its key which must be
a Var. After func is called with no args, the root values of all
the Vars will be set back to their old values. These temporary
changes will be visible in all threads. Useful for mocking out
functions during testing.
Examples
rbuchmann/samak
(ns samak.caravan-test
(:require [samak.caravan :as sut]
[samak.api :as api]
[samak.stdlib :as pipes]
[samak.oasis :as oasis]
[samak.core :as core]
[samak.runtime :as rt]
[samak.test-programs :as test-programs]
[samak.code-db :as db]
[samak.utils :as utils]
[samak.trace :as trace]
#?(:clj [clojure.core.async :as a :refer [<! chan go go-loop]]
:cljs [clojure.core.async :as a :refer [<! chan go go-loop]])
#?(:clj [clojure.test :as t :refer [deftest is]]
:cljs [cljs.test :as t :include-macros true])))
#_(deftest should-add-cell
(let [db (db/create-empty-db)
builtin (db/parse-tree->db! db (map #(api/defexp % (api/builtin %)) builtins))]
(sut/init db)
(sut/repl-eval all-things-samak)
(with-redefs-fn {#'sut/add-node (fn [s f]
(is (= "test" s))
(let [args (get-in f [:samak.nodes/rhs :samak.nodes/arguments])
i (get-in args [1 :samak.nodes/node])]
(is (= 2 (count args)))
(is (= :samak.nodes/integer (:samak.nodes/type i)))))}
#((sut/add-cell) {:sym "test" :cell 1 :type :integer}))))
#_(deftest should-add-map-cell
(let [db (db/create-empty-db)
builtin (db/parse-tree->db! db (map #(api/defexp % (api/builtin %)) builtins))]
(sut/init db)
(sut/repl-eval all-things-samak)
(with-redefs-fn {#'sut/add-node (fn [s f]
(is (= "test" s))
(let [m (get-in f [:samak.nodes/rhs :samak.nodes/arguments 0 :samak.nodes/node])
assert-map (is (= :samak.nodes/map (:samak.nodes/type m)))]
(is (= 2 (count (:samak.nodes/mapkv-pairs m))))
(is (= :samak.nodes/integer (get-in m [:samak.nodes/mapkv-pairs 1 :samak.nodes/mapvalue :samak.nodes/type])))))}
#((sut/add-cell) {:sym "test" :cell 2 :type :integer}))))
#_(deftest should-add-map-cell-from-key
(let [db (db/create-empty-db)
builtin (db/parse-tree->db! db (map #(api/defexp % (api/builtin %)) builtins))]
(sut/init db)
(sut/repl-eval all-things-samak)
(with-redefs-fn {#'sut/add-node (fn [s f]
(is (= "test" s))
(let [m (get-in f [:samak.nodes/rhs :samak.nodes/arguments 0 :samak.nodes/node])
assert-map (is (= :samak.nodes/map (:samak.nodes/type m)))]
(is (= 2 (count (:samak.nodes/mapkv-pairs m))))
(is (= :samak.nodes/integer (get-in m [:samak.nodes/mapkv-pairs 1 :samak.nodes/mapvalue :samak.nodes/type])))))}
#((sut/add-cell) {:sym "test" :cell 3 :type :integer}))))
#_(deftest should-add-map-cell-from-value
(let [db (db/create-empty-db)
builtin (db/parse-tree->db! db (map #(api/defexp % (api/builtin %)) builtins))]
(sut/init db)
(sut/repl-eval all-things-samak)
(with-redefs-fn {#'sut/add-node (fn [s f]
(is (= "test" s))
(let [m (get-in f [:samak.nodes/rhs :samak.nodes/arguments 0 :samak.nodes/node])
assert-map (is (= :samak.nodes/map (:samak.nodes/type m)))]
(is (= 2 (count (:samak.nodes/mapkv-pairs m))))
(is (= :samak.nodes/integer (get-in m [:samak.nodes/mapkv-pairs 1 :samak.nodes/mapvalue :samak.nodes/type])))))}
#((sut/add-cell) {:sym "test" :cell 4 :type :integer}))))
#_(deftest should-cut-cell
(let [db (db/create-empty-db)
builtin (db/parse-tree->db! db (map #(api/defexp % (api/builtin %)) builtins))]
(sut/init db)
(sut/repl-eval all-things-samak)
(with-redefs-fn {#'sut/add-node (fn [s f]
(is (= "test" s))
(let [m (get-in f [:samak.nodes/rhs :samak.nodes/arguments 0 :samak.nodes/node])
l (get-in m [:samak.nodes/mapkv-pairs 0 :samak.nodes/mapvalue])
assert-list (is (= :samak.nodes/vector (:samak.nodes/type l)))]
(is (= 5 (count (:samak.nodes/children l))))))}
#(is (= :done ((sut/cut-cell) {:sym "test" :cell-idx 5}))))))
#_(deftest should-load-network
(let [syms (merge {'pipes/ui pipes/debug
'pipes/mouse pipes/debug
'pipes/keyboard pipes/debug
'pipes/layout pipes/debug}
core/samak-symbols)
rt (rt/make-runtime syms)
oasis-rt (reduce rt/eval-expression! rt (oasis/start))]
;; (println (str "db: " oasis-rt))
(sut/init oasis-rt)
(with-redefs-fn {#'sut/add-pipe (fn [p] ;; (println (str "pipe: " p))
)
#'sut/add-node (fn [sym ast] ;; (println (str "pipe: " p))
)}
#(is (= 2 (count (keys (sut/load-oasis)))))))
)