|
1 | 1 | (ns rewrite-clj.custom-zipper.utils-test
|
2 |
| - (:require [clojure.test :refer [deftest testing is are]] |
3 |
| - [rewrite-clj.custom-zipper.core :as z] |
| 2 | + (:require [clojure.test :refer [deftest testing is]] |
| 3 | + [rewrite-clj.custom-zipper.core :as zraw] |
4 | 4 | [rewrite-clj.custom-zipper.utils :as u]
|
5 | 5 | [rewrite-clj.node :as node]
|
6 | 6 | [rewrite-clj.zip.base :as base])
|
7 | 7 | #?(:clj (:import clojure.lang.ExceptionInfo)))
|
8 | 8 |
|
9 | 9 | (deftest t-remove-sibling
|
10 |
| - (let [a (node/token-node 'a) |
11 |
| - b (node/token-node 'b) |
12 |
| - c (node/token-node 'c) |
13 |
| - d (node/token-node 'd) |
14 |
| - loc (z/down (base/of-node* (node/forms-node [a b c d])))] |
15 |
| - (testing "remove-right" |
16 |
| - (let [loc' (u/remove-right loc)] |
17 |
| - (is (= 'a (base/sexpr loc'))) |
18 |
| - (is (= "acd" (base/root-string loc'))))) |
19 |
| - (testing "remove-left" |
20 |
| - (let [loc' (-> loc z/right z/right u/remove-left)] |
21 |
| - (is (= 'c (base/sexpr loc'))) |
22 |
| - (is (= "acd" (base/root-string loc'))))) |
23 |
| - (testing "remove-and-move-right" |
24 |
| - (let [loc' (u/remove-and-move-right (z/right loc))] |
25 |
| - (is (= 'c (base/sexpr loc'))) |
26 |
| - (is (= "acd" (base/root-string loc'))))) |
27 |
| - (testing "remove-and-move-left" |
28 |
| - (let [loc' (-> loc z/right u/remove-and-move-left)] |
29 |
| - (is (= 'a (base/sexpr loc'))) |
30 |
| - (is (= "acd" (base/root-string loc'))))))) |
| 10 | + (doseq [opts [{} {:track-position? true}]] |
| 11 | + (let [a (node/token-node 'a) |
| 12 | + b (node/token-node 'b) |
| 13 | + c (node/token-node 'c) |
| 14 | + d (node/token-node 'd) |
| 15 | + loc (zraw/down (base/of-node* (node/forms-node [a b c d]) opts))] |
| 16 | + (testing "remove-right" |
| 17 | + (let [loc' (u/remove-right loc)] |
| 18 | + (is (= 'a (base/sexpr loc'))) |
| 19 | + (is (= "acd" (base/root-string loc'))))) |
| 20 | + (testing "remove-left" |
| 21 | + (let [loc' (-> loc zraw/right zraw/right u/remove-left)] |
| 22 | + (is (= 'c (base/sexpr loc'))) |
| 23 | + (is (= "acd" (base/root-string loc'))))) |
| 24 | + (testing "remove-and-move-right" |
| 25 | + (let [loc' (u/remove-and-move-right (zraw/right loc))] |
| 26 | + (is (= 'c (base/sexpr loc'))) |
| 27 | + (is (= "acd" (base/root-string loc'))))) |
| 28 | + (testing "remove-and-move-left" |
| 29 | + (let [loc' (-> loc zraw/right u/remove-and-move-left)] |
| 30 | + (is (= 'a (base/sexpr loc'))) |
| 31 | + (is (= "acd" (base/root-string loc')))))))) |
31 | 32 |
|
32 | 33 | (deftest t-remove-and-move-up
|
33 |
| - (let [root (base/of-string "[a [b c d]]")] |
34 |
| - (are [?n ?sexpr ?root-string] |
35 |
| - (let [zloc (nth (iterate z/next root) ?n) |
36 |
| - zloc' (u/remove-and-move-up zloc)] |
37 |
| - (is (= ?sexpr (base/sexpr zloc'))) |
38 |
| - (is (= ?root-string (base/root-string zloc')))) |
39 |
| - 4 '[c d] "[a [ c d]]" |
40 |
| - 6 '[b d] "[a [b d]]"))) |
| 34 | + (doseq [opts [{} {:track-position? true}] |
| 35 | + [s next-count expected-sexpr expected-root-string] |
| 36 | + [["[a [b c d]]" 4 '[c d] "[a [ c d]]"] |
| 37 | + ["[a [b c d]]" 6 '[b d] "[a [b d]]"] |
| 38 | + ["((x) 1)" 1 '(1) "( 1)"] |
| 39 | + ["((x)1)" 3 '((x)) "((x))"]]] |
| 40 | + (testing (str "opts " opts) |
| 41 | + (let [root (base/of-string s opts) |
| 42 | + zloc (nth (iterate zraw/next root) next-count) |
| 43 | + zloc' (u/remove-and-move-up zloc) ] |
| 44 | + (is (= expected-sexpr (base/sexpr zloc'))) |
| 45 | + (is (= expected-root-string (base/root-string zloc'))))))) |
41 | 46 |
|
42 | 47 | (deftest t-remove-and-move-up-throws
|
43 |
| - ;; I can't tell you why, but shadow-cljs will sometimes, under certain vesions of node and |
| 48 | + ;; I can't tell you why, but shadow-cljs will sometimes, under certain versions of node and |
44 | 49 | ;; other variables I do not understand culminate in a:
|
45 | 50 | ;; #object[RangeError RangeError: Maximum call stack size exceeded]
|
46 | 51 | ;; when I change this test to use (thrown-with-msg? ...)
|
47 |
| - (let [zloc (base/of-string "[a [b c d]]")] |
48 |
| - (is (= "cannot remove at top" (try |
49 |
| - (u/remove-and-move-up zloc) |
50 |
| - (catch ExceptionInfo e |
51 |
| - ;; ex-message is only avail in 1.10 and we support clj >= 1.8 |
52 |
| - #?(:clj (.getMessage e) :cljs (.-message e)))))))) |
| 52 | + (doseq [opts [{} {:track-position? true}]] |
| 53 | + (let [zloc (base/of-string "[a [b c d]]" opts)] |
| 54 | + (is (= "cannot remove at top" (try |
| 55 | + (u/remove-and-move-up zloc) |
| 56 | + (catch ExceptionInfo e |
| 57 | + ;; ex-message is only avail in 1.10 and we support clj >= 1.8 |
| 58 | + #?(:clj (.getMessage e) :cljs (.-message e))))) |
| 59 | + opts)))) |
53 | 60 |
|
54 | 61 | (deftest t-remove-and-move-left-tracks-current-position-correctly
|
55 |
| - (are [?n ?pos] |
56 |
| - (let [root (base/of-string "[a bb ccc]" {:track-position? true}) |
57 |
| - zloc (nth (iterate z/next root) ?n)] |
58 |
| - (is (= ?pos (z/position (u/remove-and-move-left zloc))))) |
59 |
| - 3 [1 3] |
60 |
| - 5 [1 6] |
61 |
| - 2 [1 2])) |
| 62 | + (doseq [[next-count expected-pos expected-root-string] |
| 63 | + [[3 [1 3] "[a ccc]"] |
| 64 | + [5 [1 6] "[a bb ]"] |
| 65 | + [2 [1 2] "[abb ccc]"]]] |
| 66 | + (let [root (base/of-string "[a bb ccc]" {:track-position? true}) |
| 67 | + zloc (nth (iterate zraw/next root) next-count) |
| 68 | + zloc (u/remove-and-move-left zloc)] |
| 69 | + (is (= expected-pos (zraw/position zloc))) |
| 70 | + (is (= expected-root-string (base/root-string zloc)))))) |
62 | 71 |
|
63 | 72 | (deftest t-remove-and-move-right-does-not-affect-position
|
64 |
| - (are [?n ?pos] |
65 |
| - (let [root (base/of-string "[a bb ccc]" {:track-position? true}) |
66 |
| - zloc (nth (iterate z/next root) ?n)] |
67 |
| - (is (= ?pos (z/position (u/remove-and-move-right zloc))))) |
68 |
| - 3 [1 4] |
69 |
| - 1 [1 2] |
70 |
| - 2 [1 3])) |
| 73 | + (doseq [[next-count expected-pos expected-root-string] |
| 74 | + [[3 [1 4] "[a ccc]"] |
| 75 | + [1 [1 2] "[ bb ccc]"] |
| 76 | + [2 [1 3] "[abb ccc]"]]] |
| 77 | + (let [root (base/of-string "[a bb ccc]" {:track-position? true}) |
| 78 | + zloc (nth (iterate zraw/next root) next-count) |
| 79 | + zloc (u/remove-and-move-right zloc)] |
| 80 | + (is (= expected-pos (zraw/position zloc))) |
| 81 | + (is (= expected-root-string (base/root-string zloc)))))) |
71 | 82 |
|
72 | 83 | (deftest t-remove-left-tracks-current-position-correctly
|
73 |
| - (are [?n ?pos] |
74 |
| - (let [root (base/of-string "[a bb ccc]" {:track-position? true}) |
75 |
| - zloc (nth (iterate z/next root) ?n)] |
76 |
| - (is (= ?pos (z/position (u/remove-left zloc))))) |
77 |
| - 3 [1 3] |
78 |
| - 5 [1 6])) |
| 84 | + (doseq [[next-count expected-pos expected-root-string] |
| 85 | + [[3 [1 3] "[abb ccc]"] |
| 86 | + [5 [1 6] "[a bbccc]"]]] |
| 87 | + (let [root (base/of-string "[a bb ccc]" {:track-position? true}) |
| 88 | + zloc (nth (iterate zraw/next root) next-count) |
| 89 | + zloc (u/remove-left zloc)] |
| 90 | + (is (= expected-pos (zraw/position zloc))) |
| 91 | + (is (= expected-root-string (base/root-string zloc)))))) |
79 | 92 |
|
80 | 93 | (deftest t-remove-and-move-up-tracks-current-position-correctly
|
81 |
| - (are [?n ?pos] |
82 |
| - (let [root (base/of-string "[a1 [bb4 ccc6]]" {:track-position? true}) |
83 |
| - zloc (nth (iterate z/next root) ?n)] |
84 |
| - (is (= ?pos (z/position (u/remove-and-move-up zloc))))) |
85 |
| - 4 [1 5] |
86 |
| - 6 [1 5])) |
| 94 | + (doseq [[s next-count expected-pos expected-string expected-root-string] |
| 95 | + [["[a1 [bb4 ccc6]]" 4 [1 5] "[ ccc6]" "[a1 [ ccc6]]"] |
| 96 | + ["[a1 [bb4 ccc6]]" 6 [1 5] "[bb4 ]" "[a1 [bb4 ]]"] |
| 97 | + ["((x) 1)" 1 [1 1] "( 1)" "( 1)"] |
| 98 | + ["((x)1)" 3 [1 1] "((x))" "((x))"]]] |
| 99 | + (let [root (base/of-string s {:track-position? true}) |
| 100 | + zloc (nth (iterate zraw/next root) next-count) |
| 101 | + zloc (u/remove-and-move-up zloc)] |
| 102 | + (is (= expected-pos (zraw/position zloc))) |
| 103 | + (is (= expected-string (base/string zloc))) |
| 104 | + (is (= expected-root-string (base/root-string zloc)))))) |
0 commit comments