|
1 | 1 | (ns rewrite-clj.zip.insert-test
|
2 |
| - (:require [clojure.test :refer [deftest is]] |
3 |
| - [rewrite-clj.interop :as interop] |
4 |
| - [rewrite-clj.zip :as z])) |
5 |
| - |
6 |
| -(deftest t-whitespace-aware-insertion |
7 |
| - (doseq [[fmt m n f s] |
8 |
| - [["[%s]" z/next 0 z/insert-right "[1 2 3 4] x"] |
9 |
| - ["[%s]" z/next 1 z/insert-right "[1 x 2 3 4]"] |
10 |
| - ["[%s]" z/next 2 z/insert-right "[1 2 x 3 4]"] |
11 |
| - ["[%s]" z/next 3 z/insert-right "[1 2 3 x 4]"] |
12 |
| - ["[%s]" z/next 4 z/insert-right "[1 2 3 4 x]"] |
13 |
| - ["[%s]" z/next 0 z/insert-left "x [1 2 3 4]"] |
14 |
| - ["[%s]" z/next 1 z/insert-left "[x 1 2 3 4]"] |
15 |
| - ["[%s]" z/next 2 z/insert-left "[1 x 2 3 4]"] |
16 |
| - ["[%s]" z/next 3 z/insert-left "[1 2 x 3 4]"] |
17 |
| - ["[%s]" z/next 4 z/insert-left "[1 2 3 x 4]"] |
18 |
| - ["[%s]" z/next 0 z/insert-child "[x 1 2 3 4]"] |
19 |
| - ["[%s]" z/next 0 z/append-child "[1 2 3 4 x]"] |
20 |
| - ["[ %s]" z/next 0 z/insert-child "[x 1 2 3 4]"] |
21 |
| - ["[%s ]" z/next 0 z/append-child "[1 2 3 4 x]"] |
22 |
| - ["[%s]" z/next* 2 z/insert-right "[1 x 2 3 4]"] |
23 |
| - ["\n[%s]" z/leftmost* 1 z/insert-left "x\n[1 2 3 4]"] |
24 |
| - ["\n[%s]" z/leftmost* 1 z/insert-right "\nx [1 2 3 4]"]]] |
25 |
| - (let [elements (->> (z/of-string |
26 |
| - (interop/simple-format fmt "1 2 3 4")) |
27 |
| - (iterate m)) |
28 |
| - loc (nth elements n) |
29 |
| - loc' (f loc 'x)] |
30 |
| - (is (= (z/tag loc') (z/tag loc))) |
31 |
| - (is (= s (z/root-string loc')))))) |
| 2 | + (:require [clojure.test :refer [deftest is testing]] |
| 3 | + [rewrite-clj.zip :as z] |
| 4 | + [rewrite-clj.zip.test-helper :as th])) |
| 5 | + |
| 6 | +(def zipper-opts [{} {:track-position? true}]) |
| 7 | + |
| 8 | +;; special positional markers recognized by test-helper fns |
| 9 | +;; ⊚ - node location |
| 10 | +;; ◬ - root :forms node |
| 11 | + |
| 12 | +(deftest t-insert-right |
| 13 | + (doseq [zopts zipper-opts] |
| 14 | + (testing (str "zipper opts " zopts) |
| 15 | + (doseq [[in expected] |
| 16 | + [["⊚[1 2 3 4]" "⊚[1 2 3 4] x"] |
| 17 | + ["[⊚1 2 3 4]" "[⊚1 x 2 3 4]"] |
| 18 | + ["[1 ⊚2 3 4]" "[1 ⊚2 x 3 4]"] |
| 19 | + ["[1 2 ⊚3 4]" "[1 2 ⊚3 x 4]"] |
| 20 | + ["[1 2 3 ⊚4]" "[1 2 3 ⊚4 x]"] |
| 21 | + ["[1\n⊚ 2 3 4]" "[1\n⊚ x 2 3 4]"] |
| 22 | + ["⊚\n[1 2 3 4]" "⊚\nx [1 2 3 4]"] |
| 23 | + ["[1 2 3 4⊚;; comment\n]" "[1 2 3 4⊚;; comment\nx]"] |
| 24 | + ["⊚;; unterminated cmt" "⊚;; unterminated cmtx"] ;; an odd thing to do, but allowed |
| 25 | + ["⊚;; comment\n" "⊚;; comment\nx"]]] |
| 26 | + (let [zloc (th/of-locmarked-string in zopts)] |
| 27 | + (is (= expected (th/root-locmarked-string (z/insert-right zloc 'x))) |
| 28 | + in)))))) |
| 29 | + |
| 30 | +(deftest t-insert-rigth-contrived |
| 31 | + (doseq [zopts zipper-opts] |
| 32 | + (testing (str "zipper opts " zopts) |
| 33 | + (let [zloc (-> (th/of-locmarked-string "1⊚ 2" zopts) z/remove*)] |
| 34 | + (is (= "⊚12" (th/root-locmarked-string zloc)) "sanity pre-condition") |
| 35 | + (is (= "⊚1 x 2" (-> zloc |
| 36 | + (z/insert-right 'x) |
| 37 | + th/root-locmarked-string))))))) |
| 38 | + |
| 39 | +(deftest t-insert-left |
| 40 | + (doseq [zopts zipper-opts] |
| 41 | + (testing (str "zipper opts " zopts) |
| 42 | + (doseq [[in expected] |
| 43 | + [["⊚[1 2 3 4]" "x ⊚[1 2 3 4]"] |
| 44 | + ["[⊚1 2 3 4]" "[x ⊚1 2 3 4]"] |
| 45 | + ["[1 ⊚2 3 4]" "[1 x ⊚2 3 4]"] |
| 46 | + ["[1 2 ⊚3 4]" "[1 2 x ⊚3 4]"] |
| 47 | + ["[1 2 3 ⊚4]" "[1 2 3 x ⊚4]"] |
| 48 | + ["[1\n⊚ 2 3 4]" "[1\nx⊚ 2 3 4]"] |
| 49 | + ["⊚\n[1 2 3 4]" "x⊚\n[1 2 3 4]"] |
| 50 | + ["⊚;; comment\n" "x ⊚;; comment\n"] |
| 51 | + ["⊚;; unterminated cmt" "x ⊚;; unterminated cmt"]]] |
| 52 | + (let [zloc (th/of-locmarked-string in zopts)] |
| 53 | + (is (= expected (th/root-locmarked-string (z/insert-left zloc 'x))) |
| 54 | + in)))))) |
| 55 | + |
| 56 | +(deftest t-insert-left-contrived |
| 57 | + (doseq [zopts zipper-opts] |
| 58 | + (testing (str "zipper opts " zopts) |
| 59 | + (let [zloc (-> (th/of-locmarked-string "1⊚ 2" zopts) z/remove* z/right*)] |
| 60 | + (is (= "1⊚2" (th/root-locmarked-string zloc)) "sanity pre-condition") |
| 61 | + (is (= "1 x ⊚2" (-> zloc |
| 62 | + (z/insert-left 'x) |
| 63 | + th/root-locmarked-string))))))) |
| 64 | + |
| 65 | +(deftest t-insert-child |
| 66 | + (doseq [zopts zipper-opts] |
| 67 | + (testing (str "zipper opts " zopts) |
| 68 | + (doseq [[in expected] |
| 69 | + [["⊚[1 2 3 4]" "⊚[x 1 2 3 4]"] |
| 70 | + ["⊚[]" "⊚[x]"] |
| 71 | + ["⊚[1]" "⊚[x 1]"] |
| 72 | + ["⊚[ 1]" "⊚[x 1]"] |
| 73 | + ["⊚[ 1 ]" "⊚[x 1 ]"] |
| 74 | + ["⊚[ ]" "⊚[x ]"] |
| 75 | + ["⊚[ 1 2 3 4]" "⊚[x 1 2 3 4]"] |
| 76 | + ["⊚[;; comment\n1 2 3 4]" "⊚[x ;; comment\n1 2 3 4]"] |
| 77 | + ["◬;; unterminated cmt" "◬x ;; unterminated cmt"] |
| 78 | + ["◬;; comment\n" "◬x ;; comment\n"]]] |
| 79 | + (let [zloc (th/of-locmarked-string in zopts)] |
| 80 | + (is (= expected (th/root-locmarked-string (z/insert-child zloc 'x))) |
| 81 | + in)))))) |
| 82 | + |
| 83 | +(deftest t-append-child |
| 84 | + (doseq [zopts zipper-opts] |
| 85 | + (testing (str "zipper opts " zopts) |
| 86 | + (doseq [[in expected] |
| 87 | + [["⊚[1 2 3 4 ]" "⊚[1 2 3 4 x]"] |
| 88 | + ["⊚[]" "⊚[x]"] |
| 89 | + ["⊚[1]" "⊚[1 x]"] |
| 90 | + ["⊚[1 ]" "⊚[1 x]"] |
| 91 | + ["⊚[ 1 ]" "⊚[ 1 x]"] |
| 92 | + ["⊚[ ]" "⊚[ x]"] |
| 93 | + ["⊚[1 2 3 4;; comment\n]" "⊚[1 2 3 4;; comment\nx]"] |
| 94 | + ["◬;; unterminated cmt" "◬;; unterminated cmtx"] ;; odd to do but allowed |
| 95 | + ["◬#! unterminated cmt" "◬#! unterminated cmtx"] ;; try alternate comment syntax |
| 96 | + ["◬;; comment\n" "◬;; comment\nx"]]] |
| 97 | + (let [zloc (th/of-locmarked-string in zopts)] |
| 98 | + (is (= expected (th/root-locmarked-string (z/append-child zloc 'x))) |
| 99 | + in)))))) |
32 | 100 |
|
33 | 101 | (deftest t-different-node-types-that-allow-insertion
|
34 |
| - (doseq [[s depth result] |
35 |
| - [["[1 2]" 0 "[1 x 2 y]"] |
36 |
| - ["(1 2)" 0 "(1 x 2 y)"] |
37 |
| - ["#{1 2}" 0 "#{1 x 2 y}"] |
38 |
| - ["#(1 2)" 0 "#(1 x 2 y)"] |
39 |
| - ["'(1 2)" 1 "'(1 x 2 y)"] |
40 |
| - ["#=(1 2)" 1 "#=(1 x 2 y)"] |
41 |
| - ["#_(1 2)" 1 "#_(1 x 2 y)"] |
42 |
| - ["@(f 2)" 1 "@(f x 2 y)"]]] |
43 |
| - (let [loc (-> (iterate z/down (z/of-string s)) |
44 |
| - (nth (inc depth)) |
45 |
| - z/right |
46 |
| - (z/insert-left 'x) |
47 |
| - (z/insert-right 'y))] |
48 |
| - (is (= result (z/root-string loc)))))) |
| 102 | + (doseq [zopts zipper-opts] |
| 103 | + (testing (str "zipper opts " zopts) |
| 104 | + (doseq [[in expected] |
| 105 | + [["[1 ⊚2]" "[1 x ⊚2 y]"] |
| 106 | + ["(1 ⊚2)" "(1 x ⊚2 y)"] |
| 107 | + ["#{1 ⊚2}" "#{1 x ⊚2 y}"] |
| 108 | + ["#(1 ⊚2)" "#(1 x ⊚2 y)"] |
| 109 | + ["'(1 ⊚2)" "'(1 x ⊚2 y)"] |
| 110 | + ["#=(1 ⊚2)" "#=(1 x ⊚2 y)"] |
| 111 | + ["#_(1 ⊚2)" "#_(1 x ⊚2 y)"] |
| 112 | + ["@(f ⊚2)" "@(f x ⊚2 y)"]]] |
| 113 | + (let [zloc (th/of-locmarked-string in zopts)] |
| 114 | + (is (= expected (-> zloc |
| 115 | + (z/insert-left 'x) |
| 116 | + (z/insert-right 'y) |
| 117 | + (th/root-locmarked-string))) |
| 118 | + in)))))) |
0 commit comments