|
1 | 1 | (ns rewrite-clj.custom-zipper.core-test
|
2 | 2 | (:require [clojure.test.check
|
3 |
| - [generators :as gen] |
4 |
| - [properties :as prop]] |
5 |
| - [midje.sweet :refer :all] |
| 3 | + [generators :as gen] |
| 4 | + [properties :as prop]] |
| 5 | + [clojure.test :refer :all] |
6 | 6 | [rewrite-clj.node :as node]
|
7 | 7 | [rewrite-clj.node.generators :as g]
|
8 |
| - [rewrite-clj.test-helpers :refer :all] |
| 8 | + [clojure.test.check.clojure-test :refer :all] |
9 | 9 | [rewrite-clj.zip
|
10 |
| - [base :as base] |
11 |
| - [whitespace :as ws]] |
| 10 | + [base :as base] |
| 11 | + [whitespace :as ws]] |
12 | 12 | [rewrite-clj.custom-zipper
|
13 | 13 | [core :as z]
|
14 | 14 | [utils :as u]]))
|
15 | 15 |
|
16 |
| -(fact "zipper starts with position [1 1]" |
17 |
| - (z/position (z/custom-zipper (node/comment-node "hello"))) => [1 1]) |
18 |
| - |
19 |
| -(tabular |
20 |
| - (fact "z/down tracks position correctly" |
21 |
| - (-> (z/custom-zipper (?type [(node/token-node "hello")])) |
22 |
| - z/down |
23 |
| - z/position) => ?pos) |
24 |
| - ?type ?pos |
25 |
| - node/forms-node [1 1] |
26 |
| - node/fn-node [1 3] |
27 |
| - node/quote-node [1 2]) |
28 |
| - |
29 |
| -(tabular |
30 |
| - (fact "z/right tracks position correctly" |
31 |
| - (let [root (base/of-string "[hello \nworld]" {:track-position? true}) |
32 |
| - zloc (nth (iterate z/next root) ?n)] |
33 |
| - (z/position zloc) => ?pos)) |
34 |
| - ?n ?pos |
35 |
| - 1 [1 2] |
36 |
| - 2 [1 7] |
37 |
| - 3 [1 8] |
38 |
| - 4 [2 1]) |
39 |
| - |
40 |
| -(fact "z/rightmost tracks position correctly" |
41 |
| - (let [root (base/of-string "[hello world]" {:track-position? true})] |
42 |
| - (-> root z/down z/rightmost z/position) => [1 8])) |
43 |
| - |
44 |
| -(tabular |
45 |
| - (fact "z/left tracks position correctly" |
46 |
| - (let [root (base/of-string "[hello world]" {:track-position? true}) |
47 |
| - zloc (nth (iterate z/left (z/rightmost (z/down root))) ?n)] |
48 |
| - (z/position zloc) => ?pos)) |
49 |
| - ?n ?pos |
50 |
| - 0 [1 8] |
51 |
| - 1 [1 7] |
52 |
| - 2 [1 2]) |
53 |
| - |
54 |
| -(tabular |
55 |
| - (fact "z/up tracks position correctly" |
56 |
| - (let [bottom (-> (base/of-string "[x [y [1]]]" {:track-position? true}) |
57 |
| - z/down |
58 |
| - z/right z/right |
59 |
| - z/down |
60 |
| - z/right z/right |
61 |
| - z/down) |
62 |
| - zloc (nth (iterate z/up bottom) ?n)] |
63 |
| - (z/position zloc) => ?pos)) |
64 |
| - ?n ?pos |
65 |
| - 0 [1 8] |
66 |
| - 1 [1 7] |
67 |
| - 2 [1 4] |
68 |
| - 3 [1 1]) |
69 |
| - |
70 |
| -(fact "z/leftmost tracks position correctly" |
71 |
| - (-> (base/of-string "[hello world]" {:track-position? true}) |
72 |
| - z/down |
73 |
| - z/right z/right |
74 |
| - z/leftmost |
75 |
| - z/position) => [1 2]) |
76 |
| - |
77 |
| -(fact "z/remove tracks position correctly" |
78 |
| - (let [root (base/of-string "[hello world]" {:track-position? true})] |
79 |
| - (-> root z/down z/remove z/position) => [1 1] |
80 |
| - (-> root z/down z/right z/remove z/position) => [1 2])) |
81 |
| - |
82 |
| -(fact "z/replace doesn't change the current position" |
83 |
| - (-> (base/of-string "[hello world]" {:track-position? true}) |
84 |
| - z/down |
85 |
| - (z/replace 'x) |
86 |
| - z/position) => [1 2]) |
87 |
| - |
88 |
| -(fact "z/insert-right doesn't change the current position" |
89 |
| - (-> (base/of-string "[hello world]" {:track-position? true}) |
90 |
| - z/down |
91 |
| - (z/insert-right 'x) |
92 |
| - z/position) => [1 2]) |
93 |
| - |
94 |
| -(tabular |
95 |
| - (fact "z/insert-left fixes the position" |
96 |
| - (let [root (base/of-string "[hello world]" {:track-position? true}) |
97 |
| - zloc (nth (iterate z/right (z/down root)) ?n)] |
98 |
| - (z/position (z/insert-left zloc 'x)) => ?pos)) |
99 |
| - ?n ?pos |
100 |
| - 0 [1 3] |
101 |
| - 1 [1 8]) |
| 16 | +(deftest t-zipper-starts-with-position-1-1 |
| 17 | + (is (= [1 1] (z/position (z/custom-zipper (node/comment-node "hello")))))) |
| 18 | + |
| 19 | +(deftest t-zdown-tracks-position-correctly |
| 20 | + (are [?type ?pos] |
| 21 | + (is (= ?pos |
| 22 | + (-> (z/custom-zipper (?type [(node/token-node "hello")])) |
| 23 | + z/down |
| 24 | + z/position))) |
| 25 | + node/forms-node [1 1] |
| 26 | + node/fn-node [1 3] |
| 27 | + node/quote-node [1 2])) |
| 28 | + |
| 29 | +(deftest t-zright-tracks-position-correctly |
| 30 | + (are [?n ?pos] |
| 31 | + (let [root (base/of-string "[hello \nworld]" {:track-position? true}) |
| 32 | + zloc (nth (iterate z/next root) ?n)] |
| 33 | + (is (= ?pos (z/position zloc)))) |
| 34 | + 1 [1 2] |
| 35 | + 2 [1 7] |
| 36 | + 3 [1 8] |
| 37 | + 4 [2 1])) |
| 38 | + |
| 39 | +(deftest t-zrightmost-tracks-position-correctly |
| 40 | + (let [root (base/of-string "[hello world]" {:track-position? true})] |
| 41 | + (is (= [1 8] (-> root z/down z/rightmost z/position))))) |
| 42 | + |
| 43 | +(deftest t-zleft-tracks-position-correctly |
| 44 | + (are [?n ?pos] |
| 45 | + (let [root (base/of-string "[hello world]" {:track-position? true}) |
| 46 | + zloc (nth (iterate z/left (z/rightmost (z/down root))) ?n)] |
| 47 | + (is (= ?pos (z/position zloc)))) |
| 48 | + 0 [1 8] |
| 49 | + 1 [1 7] |
| 50 | + 2 [1 2])) |
| 51 | + |
| 52 | +(deftest t-zup-tracks-position-correctly |
| 53 | + (are [?n ?pos] |
| 54 | + (let [bottom (-> (base/of-string "[x [y [1]]]" {:track-position? true}) |
| 55 | + z/down |
| 56 | + z/right z/right |
| 57 | + z/down |
| 58 | + z/right z/right |
| 59 | + z/down) |
| 60 | + zloc (nth (iterate z/up bottom) ?n)] |
| 61 | + (is (= ?pos (z/position zloc)))) |
| 62 | + 0 [1 8] |
| 63 | + 1 [1 7] |
| 64 | + 2 [1 4] |
| 65 | + 3 [1 1])) |
| 66 | + |
| 67 | +(deftest t-zleftmost-tracks-position-correctly |
| 68 | + (is (= [1 2] |
| 69 | + (-> (base/of-string "[hello world]" {:track-position? true}) |
| 70 | + z/down |
| 71 | + z/right z/right |
| 72 | + z/leftmost |
| 73 | + z/position)))) |
| 74 | + |
| 75 | +(deftest t-zremove-tracks-position-correctly |
| 76 | + (let [root (base/of-string "[hello world]" {:track-position? true})] |
| 77 | + (is (= [1 1] (-> root z/down z/remove z/position))) |
| 78 | + (is (= [1 2] (-> root z/down z/right z/remove z/position))))) |
| 79 | + |
| 80 | +(deftest t-zreplace-doesnt-change-the-current-position |
| 81 | + (is (= [1 2] |
| 82 | + (-> (base/of-string "[hello world]" {:track-position? true}) |
| 83 | + z/down |
| 84 | + (z/replace 'x) |
| 85 | + z/position)))) |
| 86 | + |
| 87 | +(deftest t-zinsert-right-doesnt-change-the-current-position |
| 88 | + (is (= [1 2] |
| 89 | + (-> (base/of-string "[hello world]" {:track-position? true}) |
| 90 | + z/down |
| 91 | + (z/insert-right 'x) |
| 92 | + z/position)))) |
| 93 | + |
| 94 | +(deftest t-zinsert-left-fixes-the-position |
| 95 | + (are [?n ?pos] |
| 96 | + (let [root (base/of-string "[hello world]" {:track-position? true}) |
| 97 | + zloc (nth (iterate z/right (z/down root)) ?n)] |
| 98 | + (is (= ?pos (z/position (z/insert-left zloc 'x))))) |
| 99 | + 0 [1 3] |
| 100 | + 1 [1 8])) |
102 | 101 |
|
103 | 102 | (def operations
|
104 | 103 | {:left z/left
|
|
162 | 161 | :else
|
163 | 162 | (first (node/string (z/node zloc)))))
|
164 | 163 |
|
165 |
| -(property "zipper position always matches row and column in root-string" |
| 164 | +(defspec t-zipper-position-always-matches-row-and-column-in-root-string |
166 | 165 | (prop/for-all [node (g/node)
|
167 | 166 | operations (gen/vector (gen/elements (keys operations)) 1 8)]
|
168 |
| - (let [zloc (apply-operations |
169 |
| - (base/edn* node {:track-position? true}) |
170 |
| - operations)] |
171 |
| - (= (char-here zloc) |
172 |
| - (char-at-position (base/root-string zloc) (z/position zloc)))))) |
| 167 | + (let [zloc (apply-operations |
| 168 | + (base/edn* node {:track-position? true}) |
| 169 | + operations)] |
| 170 | + (= (char-here zloc) |
| 171 | + (char-at-position (base/root-string zloc) (z/position zloc)))))) |
0 commit comments