Skip to content

Commit

Permalink
Add letsc!/defsc!; Switch tests to letsc!/defsc!
Browse files Browse the repository at this point in the history
  • Loading branch information
KGOH committed Mar 6, 2024
1 parent 3a29ad5 commit 786530c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/ps/sc.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@
`(eval `(sc.api/letsc ~(:selected-ep-id @letsc-select-state) ~~@body)))


(defmacro letsc! [& body]
`(eval `(sc.api/letsc ~(or (:selected-ep-id @letsc-select-state) (sc.api/last-ep-id)) ~~@body)))


(defn letsc-select-specific! [selected-ep-id]
(mark-selected-ep-id-value (save-letsc-select! selected-ep-id)))

Expand Down Expand Up @@ -227,6 +231,10 @@
`(eval `(defsc ~~*ns* ~(save-last-defsc-ep-id! (:selected-ep-id @letsc-select-state)))))


(defmacro defsc! []
`(eval `(defsc ~~*ns* ~(save-last-defsc-ep-id! (or (:selected-ep-id @letsc-select-state) (sc.api/last-ep-id))))))


(defmacro undefsc
"sc.api/undefsc modified to restore original values"
[intern-ns ep-or-cs-id]
Expand Down
45 changes: 30 additions & 15 deletions test/ps/sc_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@


(t/deftest last-ep-id-test
(sc.api/dispose-all!)
(sut/drop-letsc-select!)

(defn bar [strs]
(mapv (fn [s]
(mapv #(do (sc.api/spy)
Expand All @@ -19,17 +22,17 @@
strs))


(t/testing "letsc-last"
(t/testing "letsc!"
(bar ["baz" "quux"])

(t/is (= ["baz" "quux"] (sut/letsc-last 'strs)))
(t/is (= "quux" (sut/letsc-last 's))))
(t/is (= ["baz" "quux"] (sut/letsc! 'strs)))
(t/is (= "quux" (sut/letsc! 's))))


(t/testing "defsc-last"
(t/testing "defsc!"
(bar ["baz1" "quux1"])

(sut/defsc-last)
(sut/defsc!)

(t/is (= ["baz1" "quux1"] (eval `strs)))
(t/is (= "quux1" (eval `s)))
Expand All @@ -38,7 +41,7 @@
(t/testing "last ep id is calculated at the call time"
(bar ["baz2" "quux2"])

(sut/defsc-last)
(sut/defsc!)

(t/is (= ["baz2" "quux2"] (eval `strs)))
(t/is (= "quux2" (eval `s))))
Expand Down Expand Up @@ -83,6 +86,15 @@
(sut/letsc-all '[i x]))))

(t/testing "select ep id"
(t/testing "last is selected by default"
(t/is (= 1 (sut/letsc! 'x)))
(t/is (= 5 (sut/letsc! 'i)))

(sut/defsc!)
(t/is (= 1 (eval `x)))
(t/is (= 5 (eval `i)))
(sut/undefsc-lastdef))

(t/is (= [5 16 8 4 2 [1]]
(sut/letsc-select-start! 'x)))

Expand All @@ -94,6 +106,9 @@
(sut/letsc-select-prev!)))

(t/testing "start on the same expr does nothing"
(t/is (= [5 16 8 4 [2] 1]
(sut/letsc-select-start! 'x)))

(t/is (= [5 16 8 4 [2] 1]
(sut/letsc-select-start! 'x))))

Expand Down Expand Up @@ -145,11 +160,11 @@
(sut/letsc-select-next!)))

(t/testing "letsc selected"
(t/is (= 8 (sut/letsc-selected 'x)))
(t/is (= 2 (sut/letsc-selected 'i))))
(t/is (= 8 (sut/letsc! 'x)))
(t/is (= 2 (sut/letsc! 'i))))

(t/testing "defsc selected"
(sut/defsc-selected)
(sut/defsc!)

(t/is (= 8 (eval `x)))
(t/is (= 2 (eval `i)))
Expand All @@ -176,19 +191,19 @@

(t/is (= {`x 1 `y 2 `z 3} (f1 1 2)))
(sut/undefsc-lastdef)
(t/is (every? this-ns? (sut/defsc-last)))
(t/is (every? this-ns? (sut/defsc!)))
(t/is (= {`x 1 `y 2 `z 3} (f1 (eval `x) (eval `y))))

(t/is (= {`x 4 `y 5 `z 6} (f2 4 5 6)))
(sut/undefsc-lastdef)
(t/is (every? this-ns? (sut/defsc-last)))
(t/is (every? this-ns? (sut/defsc!)))
(t/is (contains? @sut/original-var-values `z))
(t/is (= {`x 4 `y 5 `z 6} (f2 (eval `x) (eval `y) (eval `z))))

(t/is (= {`x 7 `y 8 `z 6} (f1 7 8)))
(sut/undefsc-lastdef)
(t/is (= {`x 9 `y 10 `z 3} (f1 9 10)))
(t/is (every? this-ns? (sut/defsc-last)))
(t/is (every? this-ns? (sut/defsc!)))
(t/is (= {`x 9 `y 10 `z 3} (f1 (eval `x) (eval `y))))

(sut/undefsc-lastdef)
Expand Down Expand Up @@ -216,14 +231,14 @@
(zipmap `[x2 z2] [x2 z2]))

(t/is (= {`x2 1 `y2 2 `z2 -3 `zz2 4} (f1 1 2 4)))
(sut/defsc-last)
(sut/defsc!)
(t/is (= 1 (eval `x2)))
(t/is (= 2 (eval `y2)))
(t/is (= -3 (eval `z2)))
(t/is (= 4 (eval `zz2)))

(t/is (= {`x2 5 `z2 6} (f2 5 6)))
(sut/defsc-last)
(sut/defsc!)
(t/is (= 5 (eval `x2)))
(t/is (= 6 (eval `z2)))
(t/is (= 2 (eval `y2)))
Expand All @@ -235,4 +250,4 @@
(t/is (= -2 (eval `y2)))

(t/is (undefined-sym? `zz2))
(t/is (undefined-sym? `x2)))
(t/is (undefined-sym? `x2)))

0 comments on commit 786530c

Please sign in to comment.