From f7acb801949ea84dee463a5206eb3acdd8a9f961 Mon Sep 17 00:00:00 2001 From: Lee Read Date: Fri, 28 Jun 2024 18:41:49 -0400 Subject: [PATCH] test: migrate from clojure.test/are -> doseq (#288) Also add a clj-kondo discouraged-var linter to enforce are-less tests. Closes #287 --- .clj-kondo/config.edn | 3 +- test/rewrite_clj/custom_zipper/core_test.cljc | 86 +-- test/rewrite_clj/examples/cljx_test.cljc | 70 +- test/rewrite_clj/node/coercer_test.cljc | 242 +++--- test/rewrite_clj/node_test.cljc | 155 ++-- test/rewrite_clj/parser_test.cljc | 725 +++++++++--------- test/rewrite_clj/zip/base_test.cljc | 86 +-- test/rewrite_clj/zip/editz_test.cljc | 112 +-- test/rewrite_clj/zip/findz_test.cljc | 71 +- test/rewrite_clj/zip/insert_test.cljc | 76 +- test/rewrite_clj/zip/move_test.cljc | 38 +- test/rewrite_clj/zip/removez_test.cljc | 123 ++- test/rewrite_clj/zip/seqz_test.cljc | 98 ++- test/rewrite_clj/zip/walk_test.cljc | 31 +- test/rewrite_clj/zip/whitespace_test.cljc | 30 +- test/rewrite_clj/zip_test.cljc | 30 +- 16 files changed, 959 insertions(+), 1017 deletions(-) diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn index 4385b098..43333906 100644 --- a/.clj-kondo/config.edn +++ b/.clj-kondo/config.edn @@ -12,7 +12,8 @@ rewrite-clj.zip.subedit/edit->> clojure.core/->> rewrite-clj.custom-zipper.switchable/defn-switchable clojure.core/defn} :linters - {:redundant-str-call {:level :warning} + {:discouraged-var {clojure.test/are {:message "We're not fans of clojure.test/are, use doseq instead"}} + :redundant-str-call {:level :warning} :redundant-fn-wrapper {:level :warning} :unused-value {:level :warning} :aliased-namespace-symbol {:level :warning} diff --git a/test/rewrite_clj/custom_zipper/core_test.cljc b/test/rewrite_clj/custom_zipper/core_test.cljc index b4227b41..88c1008f 100644 --- a/test/rewrite_clj/custom_zipper/core_test.cljc +++ b/test/rewrite_clj/custom_zipper/core_test.cljc @@ -1,5 +1,5 @@ (ns rewrite-clj.custom-zipper.core-test - (:require [clojure.test :refer [deftest is are]] + (:require [clojure.test :refer [deftest is]] #?(:cljs [clojure.test.check :refer-macros [quick-check]]) [clojure.test.check.clojure-test :refer [defspec]] [clojure.test.check.generators :as gen] @@ -14,52 +14,52 @@ (is (= [1 1] (z/position (z/custom-zipper (node/comment-node "hello")))))) (deftest t-zdown-tracks-position-correctly - (are [?type ?pos] - (is (= ?pos - (-> (z/custom-zipper (?type [(node/token-node "hello")])) - z/down - z/position))) - node/forms-node [1 1] - node/fn-node [1 3] - node/quote-node [1 2])) + (doseq [[create-fn pos] + [[node/forms-node [1 1]] + [node/fn-node [1 3]] + [node/quote-node [1 2]]]] + (is (= pos + (-> (z/custom-zipper (create-fn [(node/token-node "hello")])) + z/down + z/position))))) (deftest t-zright-tracks-position-correctly - (are [?n ?pos] - (let [root (base/of-string "[hello \nworld]" {:track-position? true}) - zloc (nth (iterate z/next root) ?n)] - (is (= ?pos (z/position zloc)))) - 1 [1 2] - 2 [1 7] - 3 [1 8] - 4 [2 1])) + (doseq [[n pos] + [[1 [1 2]] + [2 [1 7]] + [3 [1 8]] + [4 [2 1]]]] + (let [root (base/of-string "[hello \nworld]" {:track-position? true}) + zloc (nth (iterate z/next root) n)] + (is (= pos (z/position zloc)))))) (deftest t-zrightmost-tracks-position-correctly (let [root (base/of-string "[hello world]" {:track-position? true})] (is (= [1 8] (-> root z/down z/rightmost z/position))))) (deftest t-zleft-tracks-position-correctly - (are [?n ?pos] - (let [root (base/of-string "[hello world]" {:track-position? true}) - zloc (nth (iterate z/left (z/rightmost (z/down root))) ?n)] - (is (= ?pos (z/position zloc)))) - 0 [1 8] - 1 [1 7] - 2 [1 2])) + (doseq [[n pos] + [[0 [1 8]] + [1 [1 7]] + [2 [1 2]]]] + (let [root (base/of-string "[hello world]" {:track-position? true}) + zloc (nth (iterate z/left (z/rightmost (z/down root))) n)] + (is (= pos (z/position zloc)))))) (deftest t-zup-tracks-position-correctly - (are [?n ?pos] - (let [bottom (-> (base/of-string "[x [y [1]]]" {:track-position? true}) - z/down - z/right z/right - z/down - z/right z/right - z/down) - zloc (nth (iterate z/up bottom) ?n)] - (is (= ?pos (z/position zloc)))) - 0 [1 8] - 1 [1 7] - 2 [1 4] - 3 [1 1])) + (doseq [[n pos] + [[0 [1 8]] + [1 [1 7]] + [2 [1 4]] + [3 [1 1]]]] + (let [bottom (-> (base/of-string "[x [y [1]]]" {:track-position? true}) + z/down + z/right z/right + z/down + z/right z/right + z/down) + zloc (nth (iterate z/up bottom) n)] + (is (= pos (z/position zloc)))))) (deftest t-zleftmost-tracks-position-correctly (is (= [1 2] @@ -89,12 +89,12 @@ z/position)))) (deftest t-zinsert-left-fixes-the-position - (are [?n ?pos] - (let [root (base/of-string "[hello world]" {:track-position? true}) - zloc (nth (iterate z/right (z/down root)) ?n)] - (is (= ?pos (z/position (z/insert-left zloc 'x))))) - 0 [1 3] - 1 [1 8])) + (doseq [[n pos] + [[0 [1 3]] + [1 [1 8]]]] + (let [root (base/of-string "[hello world]" {:track-position? true}) + zloc (nth (iterate z/right (z/down root)) n)] + (is (= pos (z/position (z/insert-left zloc 'x))))))) (def operations {:left z/left diff --git a/test/rewrite_clj/examples/cljx_test.cljc b/test/rewrite_clj/examples/cljx_test.cljc index dd1cef6a..39b45a48 100644 --- a/test/rewrite_clj/examples/cljx_test.cljc +++ b/test/rewrite_clj/examples/cljx_test.cljc @@ -1,5 +1,5 @@ (ns rewrite-clj.examples.cljx-test - (:require [clojure.test :refer [deftest is are]] + (:require [clojure.test :refer [deftest is]] [rewrite-clj.zip :as z])) ;; ## Reader Macro Detection @@ -17,13 +17,13 @@ (.startsWith nm "-"))))))))) (deftest t-cljx-macro-detection - (are [?data ?pred] - (let [loc (z/of-string ?data)] - (is (?pred (cljx-macro? loc)))) - "#+clj 123" identity - "#-clj 123" identity - "#clj 123" not - "123" not)) + (doseq [[data pred] + [["#+clj 123" identity] + ["#-clj 123" identity] + ["#clj 123" not] + ["123" not]]] + (let [loc (z/of-string data)] + (is (pred (cljx-macro? loc)))))) ;; ## Replace Form w/ Spaces @@ -74,14 +74,14 @@ (remove-reader-macro-symbol zloc)))) (deftest t-reader-macro-handling - (are [?data ?result] - (let [loc (z/of-string ?data) - rloc (handle-reader-macro #{"clj"} loc)] - (is (= ?result (z/root-string rloc)))) - "#+clj 123" " 123" - "#-clj 123" " " - "#+clx 123" " " - "#-clx 123" " 123")) + (doseq [[data result] + [["#+clj 123" " 123"] + ["#-clj 123" " "] + ["#+clx 123" " "] + ["#-clx 123" " 123"]]] + (let [loc (z/of-string data) + rloc (handle-reader-macro #{"clj"} loc)] + (is (= result (z/root-string rloc)))))) ;; ## cljx @@ -106,22 +106,22 @@ " [x]\n" " #+debug (println #-compact :debug 'inc x)\n" " (inc x))")] - (are [?profiles ?result] - (is (= ?result (cljx-string data ?profiles))) - #{} - (str "(defn debug-inc\n" - " [x]\n" - " \n" - " (inc x))") - - #{:debug} - (str "(defn debug-inc\n" - " [x]\n" - " (println :debug 'inc x)\n" - " (inc x))") - - #{:debug :compact} - (str "(defn debug-inc\n" - " [x]\n" - " (println 'inc x)\n" - " (inc x))")))) + (doseq [[profiles result] + [[#{} + (str "(defn debug-inc\n" + " [x]\n" + " \n" + " (inc x))")] + + [#{:debug} + (str "(defn debug-inc\n" + " [x]\n" + " (println :debug 'inc x)\n" + " (inc x))")] + + [#{:debug :compact} + (str "(defn debug-inc\n" + " [x]\n" + " (println 'inc x)\n" + " (inc x))")]]] + (is (= result (cljx-string data profiles)))))) diff --git a/test/rewrite_clj/node/coercer_test.cljc b/test/rewrite_clj/node/coercer_test.cljc index 15a28bc0..c48500de 100644 --- a/test/rewrite_clj/node/coercer_test.cljc +++ b/test/rewrite_clj/node/coercer_test.cljc @@ -1,5 +1,5 @@ (ns rewrite-clj.node.coercer-test - (:require [clojure.test :refer [deftest testing is are]] + (:require [clojure.test :refer [deftest testing is]] [rewrite-clj.node :as node] [rewrite-clj.node.protocols :as protocols] [rewrite-clj.node.regex :as regex] @@ -7,63 +7,62 @@ (deftest t-sexpr->node->sexpr-roundtrip (testing "simple cases roundtrip" - (are [?sexpr expected-tag expected-type] - (let [n (node/coerce ?sexpr)] - (is (node/node? n)) - (is (= ?sexpr (node/sexpr n))) - (is (string? (node/string n))) - (is (= expected-tag (node/tag n)) "tag") - (is (= expected-type (protocols/node-type n)) "node-type") - (is (not (meta n))) - (if (seq? ?sexpr) - (is (seq? (node/sexpr n))) - (is (= (type (node/sexpr n)) (type ?sexpr) )))) - - ;; numbers - - ;; note that we do have an integer-node, but rewrite-clj never parses to it - ;; so we never coerce to it either - 3 :token :token - 3N :token :token - 3.14 :token :token - 3.14M :token :token - 3e14 :token :token - - ;; ratios are not valid in cljs - #?@(:clj [3/4 :token :token]) - - ;; symbol/keyword/string/... - 'symbol :token :symbol - 'namespace/symbol :token :symbol - :keyword :token :keyword - :1.5.1 :token :keyword - ::keyword :token :keyword - ::1.5.1 :token :keyword - :namespace/keyword :token :keyword - - "" :token :string - "hello, over there!" :token :string - "multi\nline" :token :string - " " :token :string - "\n" :token :string - "\n\n" :token :string - "," :token :string - "inner\"quote" :token :string - "\\s+" :token :string - - ;; seqs - [] :vector :seq - [1 2 3] :vector :seq - () :list :seq - '() :list :seq - (list 1 2 3) :list :seq - #{} :set :seq - #{1 2 3} :set :seq - (cons 1 [2 3]) :list :seq - (lazy-seq [1 2 3]) :list :seq - - ;; date - #inst "2014-11-26T00:05:23" :token :token)) + (doseq [[sexpr expected-tag expected-type] + [;; numbers + + ;; note that we do have an integer-node, but rewrite-clj never parses to it + ;; so we never coerce to it either + [3 :token :token] + [3N :token :token] + [3.14 :token :token] + [3.14M :token :token] + [3e14 :token :token] + + #?@(:clj ;; ratios are not valid in cljs + [[3/4 :token :token]]) + + ;; symbol/keyword/string/... + ['symbol :token :symbol] + ['namespace/symbol :token :symbol] + [:keyword :token :keyword] + [:1.5.1 :token :keyword] + [::keyword :token :keyword] + [::1.5.1 :token :keyword] + [:namespace/keyword :token :keyword] + + ["" :token :string] + ["hello, over there!" :token :string] + ["multi\nline" :token :string] + [" " :token :string] + ["\n" :token :string] + ["\n\n" :token :string] + ["," :token :string] + ["inner\"quote" :token :string] + ["\\s+" :token :string] + + ;; seqs + [[] :vector :seq] + [[1 2 3] :vector :seq] + [() :list :seq] + ['() :list :seq] + [(list 1 2 3) :list :seq] + [#{} :set :seq] + [#{1 2 3} :set :seq] + [(cons 1 [2 3]) :list :seq] + [(lazy-seq [1 2 3]) :list :seq] + + ;; date + [#inst "2014-11-26T00:05:23" :token :token]]] + (let [n (node/coerce sexpr)] + (is (node/node? n)) + (is (= sexpr (node/sexpr n))) + (is (string? (node/string n))) + (is (= expected-tag (node/tag n)) "tag") + (is (= expected-type (protocols/node-type n)) "node-type") + (is (not (meta n))) + (if (seq? sexpr) + (is (seq? (node/sexpr n))) + (is (= (type (node/sexpr n)) (type sexpr))))))) (testing "multi-line string newline variants are preserved" (let [s "hey\nyou\rover\r\nthere" n (node/coerce s)] @@ -73,52 +72,47 @@ (testing "coerce string equals parsed string" (is (= (p/parse-string "\"hello\"") (node/coerce "hello"))))) -(deftest - t-quoted-list-reader-location-metadata-elided - (are [?sexpr expected-meta-keys] - (let [n (node/coerce ?sexpr)] - (is (node/node? n)) - (is (= expected-meta-keys (node/string n))) - (is (string? (node/string n))) - (is (= ?sexpr (node/sexpr n))) - (is (not (meta n))) - (is (= (type ?sexpr) (type (node/sexpr n))))) - '(1 2 3) "(1 2 3)" - '^:other-meta (4 5 6) "^{:other-meta true} (4 5 6)")) +(deftest t-quoted-list-reader-location-metadata-elided + (doseq [[sexpr expected-meta-keys] + [['(1 2 3) "(1 2 3)"] + ['^:other-meta (4 5 6) "^{:other-meta true} (4 5 6)"]]] + (let [n (node/coerce sexpr)] + (is (node/node? n)) + (is (= expected-meta-keys (node/string n))) + (is (string? (node/string n))) + (is (= sexpr (node/sexpr n))) + (is (not (meta n))) + (is (= (type sexpr) (type (node/sexpr n))))))) (deftest t-maps - (are [?sexpr] - (let [n (node/coerce ?sexpr)] - (is (node/node? n)) - (is (= :map (node/tag n))) - (is (= :seq (protocols/node-type n))) - (is (string? (node/string n))) - (is (= ?sexpr (node/sexpr n))) - ;; we do not restore to original map (hash-map or array-map), - ;; checking if we convert to any map is sufficient - (is (map? (node/sexpr n)))) - {} - {:a 1 :b 2} - (hash-map) - (hash-map :a 0 :b 1) - (array-map) - (array-map :d 4 :e 5))) - - + (doseq [sexpr [{} + {:a 1 :b 2} + (hash-map) + (hash-map :a 0 :b 1) + (array-map) + (array-map :d 4 :e 5)]] + (let [n (node/coerce sexpr)] + (is (node/node? n)) + (is (= :map (node/tag n))) + (is (= :seq (protocols/node-type n))) + (is (string? (node/string n))) + (is (= sexpr (node/sexpr n))) + ;; we do not restore to original map (hash-map or array-map), + ;; checking if we convert to any map is sufficient + (is (map? (node/sexpr n)))))) (deftest t-sexpr->node->sexpr-roundtrip-for-regex - (are [?in] - (let [n (node/coerce ?in)] - (is (node/node? n)) - (is (= :regex (node/tag n))) - (is (= :regex (protocols/node-type n))) - (is (string? (node/string n))) - (is (= (list 're-pattern (regex/pattern-string-for-regex ?in)) - (node/sexpr n)))) - #"abc" - #"a\nb\nc" - #"a\.\s*" - #"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")) + (doseq [in [#"abc" + #"a\nb\nc" + #"a\.\s*" + #"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"]] + (let [n (node/coerce in)] + (is (node/node? n)) + (is (= :regex (node/tag n))) + (is (= :regex (protocols/node-type n))) + (is (string? (node/string n))) + (is (= (list 're-pattern (regex/pattern-string-for-regex in)) + (node/sexpr n)))))) (deftest ^:skip-for-sci ;; sci, by design has its own var type, so skip this one for sci @@ -152,30 +146,30 @@ (deftest t-nodes-coerce-to-themselves (testing "parsed nodes" ;; lean on the parser to create node structures - (are [?s ?tag ?type] - (let [n (p/parse-string ?s)] - (is (= n (node/coerce n))) - (is (= ?tag (node/tag n))) - (is (= ?type (protocols/node-type n)))) - ";; comment" :comment :comment - "#! comment" :comment :comment - "#(+ 1 %)" :fn :fn - ":my-kw" :token :keyword - "^:m1 [1 2 3]" :meta :meta - "#:p1{:a 1 :b 2}" :namespaced-map :namespaced-map - "'a" :quote :quote - "#'var" :var :reader - "#=eval" :eval :reader - "@deref" :deref :deref - "#mymacro 44" :reader-macro :reader-macro - "#\"regex\"" :regex :regex - "[1 2 3]" :vector :seq - "42" :token :token - "sym" :token :symbol - "#_ 99" :uneval :uneval - " " :whitespace :whitespace - "," :comma :comma - "\n" :newline :newline)) + (doseq [[s tag type] + [[";; comment" :comment :comment] + ["#! comment" :comment :comment] + ["#(+ 1 %)" :fn :fn] + [":my-kw" :token :keyword] + ["^:m1 [1 2 3]" :meta :meta] + ["#:p1{:a 1 :b 2}" :namespaced-map :namespaced-map] + ["'a" :quote :quote] + ["#'var" :var :reader] + ["#=eval" :eval :reader] + ["@deref" :deref :deref] + ["#mymacro 44" :reader-macro :reader-macro] + ["#\"regex\"" :regex :regex] + ["[1 2 3]" :vector :seq] + ["42" :token :token] + ["sym" :token :symbol] + ["#_ 99" :uneval :uneval] + [" " :whitespace :whitespace] + ["," :comma :comma] + ["\n" :newline :newline]]] + (let [n (p/parse-string s)] + (is (= n (node/coerce n))) + (is (= tag (node/tag n))) + (is (= type (protocols/node-type n)))))) (testing "parsed forms nodes" (let [n (p/parse-string-all "(def a 1)")] (is (= n (node/coerce n))) diff --git a/test/rewrite_clj/node_test.cljc b/test/rewrite_clj/node_test.cljc index 833b3407..758427a8 100644 --- a/test/rewrite_clj/node_test.cljc +++ b/test/rewrite_clj/node_test.cljc @@ -1,47 +1,47 @@ (ns rewrite-clj.node-test "This test namespace originated from rewrite-cljs." - (:require [clojure.test :refer [deftest is are testing]] + (:require [clojure.test :refer [deftest is testing]] [rewrite-clj.node :as n] [rewrite-clj.node.protocols :as proto] [rewrite-clj.parser :as p])) (deftest nodes-convert-to-strings-and-sexpr-ability (testing "easily parseable" - (are [?in ?expected-tag ?expected-type ?expected-sexpr-able?] - (let [n (p/parse-string ?in)] - (is (= ?in (str n))) - (is (= ?expected-tag (n/tag n))) - (is (= ?expected-type (proto/node-type n))) - (is (= ?expected-sexpr-able? (n/sexpr-able? n)))) - "," :comma :comma false - "; comment" :comment :comment false - "#! comment" :comment :comment false - "@deref" :deref :deref true - "#(fn %1)" :fn :fn true - ":my-kw" :token :keyword true - "^:meta b" :meta :meta true - "#:prefix {:a 1}" :namespaced-map :namespaced-map true - "\n" :newline :newline false - "'quoted" :quote :quote true - "#booya 32" :reader-macro :reader-macro true - "#'myvar" :var :reader true - "#\"regex\"" :regex :regex true - "[1 2 3]" :vector :seq true - "\"string\"" :token :string true - "symbol" :token :symbol true - "43" :token :token true - "#_ nope" :uneval :uneval false - " " :whitespace :whitespace false)) + (doseq [[in expected-tag expected-type expected-sexpr-able?] + [["," :comma :comma false] + ["; comment" :comment :comment false] + ["#! comment" :comment :comment false] + ["@deref" :deref :deref true] + ["#(fn %1)" :fn :fn true] + [":my-kw" :token :keyword true] + ["^:meta b" :meta :meta true] + ["#:prefix {:a 1}" :namespaced-map :namespaced-map true] + ["\n" :newline :newline false] + ["'quoted" :quote :quote true] + ["#booya 32" :reader-macro :reader-macro true] + ["#'myvar" :var :reader true] + ["#\"regex\"" :regex :regex true] + ["[1 2 3]" :vector :seq true] + ["\"string\"" :token :string true] + ["symbol" :token :symbol true] + ["43" :token :token true] + ["#_ nope" :uneval :uneval false] + [" " :whitespace :whitespace false]]] + (let [n (p/parse-string in)] + (is (= in (str n))) + (is (= expected-tag (n/tag n))) + (is (= expected-type (proto/node-type n))) + (is (= expected-sexpr-able? (n/sexpr-able? n)))))) (testing "map qualifier" - (are [?auto-resolved ?prefix ?expected-str] - (let [n (n/map-qualifier-node ?auto-resolved ?prefix)] - (is (= ?expected-str (str n))) + (doseq [[auto-resolved prefix expected-str] + [[false "prefix" ":prefix"] + [true nil "::"] + [true "nsalias" "::nsalias"]]] + (let [n (n/map-qualifier-node auto-resolved prefix)] + (is (= expected-str (str n))) (is (= :map-qualifier (n/tag n))) (is (= :map-qualifier (proto/node-type n))) - (is (= true (n/sexpr-able? n)))) - false "prefix" ":prefix" - true nil "::" - true "nsalias" "::nsalias")) + (is (= true (n/sexpr-able? n)))))) (testing "integer" (let [n (n/integer-node 42)] (is (= :token (n/tag n))) @@ -53,7 +53,6 @@ (is (= "5 7" (str n))) (is (n/sexpr-able? n))))) - (deftest namespaced-keyword (is (= ":dill/dall" (n/string (n/keyword-node :dill/dall))))) @@ -123,59 +122,53 @@ map-qualifier-current-ns (n/map-qualifier-node true nil) map-qualifier-ns-alias (n/map-qualifier-node true "nsmap-alias")] (testing "qualified nodes are unaffected by resolver" - (are [?result ?node] - (do - (is (= ?result (-> ?node sexpr-default))) - (is (= ?result (-> ?node sexpr-custom)))) - :my-kw (n/keyword-node :my-kw) - 'my-sym (n/token-node 'my-sym) - :_/my-kw (n/keyword-node :_/my-kw) - '_/my-sym (n/token-node '_/my-sym) - :my-prefix/my-kw (n/keyword-node :my-prefix/my-kw) - 'my-prefix/my-sym (n/token-node 'my-prefix/my-sym))) + (doseq [[result node] + [[:my-kw (n/keyword-node :my-kw)] + ['my-sym (n/token-node 'my-sym)] + [:_/my-kw (n/keyword-node :_/my-kw)] + ['_/my-sym (n/token-node '_/my-sym)] + [:my-prefix/my-kw (n/keyword-node :my-prefix/my-kw)] + ['my-prefix/my-sym (n/token-node 'my-prefix/my-sym)]]] + (is (= result (-> node sexpr-default))) + (is (= result (-> node sexpr-custom))))) (testing "auto-resolve qualified key nodes are affected by resolver" - (are [?result-default ?result-custom ?node] - (do - (is (= ?result-default (-> ?node sexpr-default))) - (is (= ?result-custom (-> ?node sexpr-custom)))) - :?_current-ns_?/my-kw :my.current.ns/my-kw (n/keyword-node :my-kw true) - :??_my-alias_??/my-kw :my.aliased.ns/my-kw (n/keyword-node :my-alias/my-kw true))) + (doseq [[result-default result-custom node] + [[:?_current-ns_?/my-kw :my.current.ns/my-kw (n/keyword-node :my-kw true)] + [:??_my-alias_??/my-kw :my.aliased.ns/my-kw (n/keyword-node :my-alias/my-kw true)]]] + (is (= result-default (-> node sexpr-default))) + (is (= result-custom (-> node sexpr-custom))))) (testing "map qualified nodes can be affected by resolver" - (are [?result ?node] - (do - (is (= ?result (-> ?node (n/map-context-apply map-qualifier) sexpr-default))) - (is (= ?result (-> ?node (n/map-context-apply map-qualifier) sexpr-custom))) ) - :nsmap-prefix/my-kw (n/keyword-node :my-kw) - 'nsmap-prefix/my-sym (n/token-node 'my-sym))) + (doseq [[result node] + [[:nsmap-prefix/my-kw (n/keyword-node :my-kw)] + ['nsmap-prefix/my-sym (n/token-node 'my-sym)]]] + (is (= result (-> node (n/map-context-apply map-qualifier) sexpr-default))) + (is (= result (-> node (n/map-context-apply map-qualifier) sexpr-custom))))) (testing "map qualified auto-resolve current-ns nodes can be affected by resolver" - (are [?result-default ?result-custom ?node] - (do - (is (= ?result-default (-> ?node (n/map-context-apply map-qualifier-current-ns) sexpr-default))) - (is (= ?result-custom (-> ?node (n/map-context-apply map-qualifier-current-ns) sexpr-custom)))) - :?_current-ns_?/my-kw :my.current.ns/my-kw (n/keyword-node :my-kw) - '?_current-ns_?/my-sym 'my.current.ns/my-sym (n/token-node 'my-sym))) + (doseq [[result-default result-custom node] + [[:?_current-ns_?/my-kw :my.current.ns/my-kw (n/keyword-node :my-kw)] + ['?_current-ns_?/my-sym 'my.current.ns/my-sym (n/token-node 'my-sym)]]] + (is (= result-default (-> node (n/map-context-apply map-qualifier-current-ns) sexpr-default))) + (is (= result-custom (-> node (n/map-context-apply map-qualifier-current-ns) sexpr-custom))))) (testing "map qualified auto-resolve ns-alias nodes can be affected by resolver" - (are [?result-default ?result-custom ?node] - (do - (is (= ?result-default (-> ?node (n/map-context-apply map-qualifier-ns-alias) sexpr-default))) - (is (= ?result-custom (-> ?node (n/map-context-apply map-qualifier-ns-alias) sexpr-custom)))) - :??_nsmap-alias_??/my-kw :nsmap.aliased.ns/my-kw (n/keyword-node :my-kw) - '??_nsmap-alias_??/my-sym 'nsmap.aliased.ns/my-sym (n/token-node 'my-sym))) + (doseq [[result-default result-custom node] + [[:??_nsmap-alias_??/my-kw :nsmap.aliased.ns/my-kw (n/keyword-node :my-kw)] + ['??_nsmap-alias_??/my-sym 'nsmap.aliased.ns/my-sym (n/token-node 'my-sym)]]] + (is (= result-default (-> node (n/map-context-apply map-qualifier-ns-alias) sexpr-default))) + (is (= result-custom (-> node (n/map-context-apply map-qualifier-ns-alias) sexpr-custom))))) (testing "map qualified nodes that are unaffected by resolver" - (are [?result ?node] - (do - (is (= ?result (-> ?node (n/map-context-apply map-qualifier) sexpr-default))) - (is (= ?result (-> ?node (n/map-context-apply map-qualifier) sexpr-custom))) + (doseq [[result node] + [[:my-kw (n/keyword-node :_/my-kw)] + ['my-sym (n/token-node '_/my-sym)] + [:my-prefix/my-kw (n/keyword-node :my-prefix/my-kw)] + ['my-prefix/my-sym (n/token-node 'my-prefix/my-sym)]]] + (is (= result (-> node (n/map-context-apply map-qualifier) sexpr-default))) + (is (= result (-> node (n/map-context-apply map-qualifier) sexpr-custom))) - (is (= ?result (-> ?node (n/map-context-apply map-qualifier-current-ns) sexpr-default))) - (is (= ?result (-> ?node (n/map-context-apply map-qualifier-current-ns) sexpr-custom))) + (is (= result (-> node (n/map-context-apply map-qualifier-current-ns) sexpr-default))) + (is (= result (-> node (n/map-context-apply map-qualifier-current-ns) sexpr-custom))) - (is (= ?result (-> ?node (n/map-context-apply map-qualifier-ns-alias) sexpr-default))) - (is (= ?result (-> ?node (n/map-context-apply map-qualifier-ns-alias) sexpr-custom))) ) - :my-kw (n/keyword-node :_/my-kw) - 'my-sym (n/token-node '_/my-sym) - :my-prefix/my-kw (n/keyword-node :my-prefix/my-kw) - 'my-prefix/my-sym (n/token-node 'my-prefix/my-sym))) + (is (= result (-> node (n/map-context-apply map-qualifier-ns-alias) sexpr-default))) + (is (= result (-> node (n/map-context-apply map-qualifier-ns-alias) sexpr-custom))))) (testing "when auto-resolver returns nil, bare or already qualified kw is returned" (let [opts {:auto-resolve (fn [_alias])}] (is (= :my-kw (-> (n/keyword-node :my-kw true) (n/sexpr opts)))) @@ -187,7 +180,7 @@ (is (= :foo/my-kw (-> (n/keyword-node :foo/my-kw false) (assoc :map-qualifier {:auto-resolved? true :prefix "nsmap-alias"}) - (n/sexpr opts)))) )))) + (n/sexpr opts)))))))) (deftest t-sexpr-on-map-qualifier-node (testing "with default auto-resolve" diff --git a/test/rewrite_clj/parser_test.cljc b/test/rewrite_clj/parser_test.cljc index 52c64648..582b219f 100644 --- a/test/rewrite_clj/parser_test.cljc +++ b/test/rewrite_clj/parser_test.cljc @@ -2,7 +2,8 @@ :author "Yannick Scherer"} rewrite-clj.parser-test (:refer-clojure :exclude [read-string]) - (:require [clojure.test :refer [deftest is are]] + (:require [clojure.string :as string] + [clojure.test :refer [deftest is]] [clojure.tools.reader.edn :refer [read-string]] [rewrite-clj.node :as node] [rewrite-clj.parser :as p]) @@ -10,104 +11,105 @@ [java.io File]))) (deftest t-parsing-the-first-few-whitespaces - (are [?ws ?parsed] - (let [n (p/parse-string ?ws)] - (is (= :whitespace (node/tag n))) - (is (= ?parsed (node/string n)))) - " " " " - " \n " " ")) + (doseq [[ws parsed] + [[" " " "] + [" \n " " "]]] + (let [n (p/parse-string ws)] + (is (= :whitespace (node/tag n))) + (is (= parsed (node/string n)))))) (deftest t-parsing-whitespace-strings - (are [?ws ?children] - (let [n (p/parse-string-all ?ws)] - (is (= :forms (node/tag n))) - (is (= (.replace ?ws "\r\n" "\n") (node/string n))) - (is (= ?children (map (juxt node/tag node/string) (node/children n))))) - " \n " [[:whitespace " "] - [:newline "\n"] - [:whitespace " "]] - " \t \r\n \t " [[:whitespace " \t "] - [:newline "\n"] - [:whitespace " \t "]])) + (doseq [[ws children] + [[" \n " [[:whitespace " "] + [:newline "\n"] + [:whitespace " "]]] + [" \t \r\n \t " [[:whitespace " \t "] + [:newline "\n"] + [:whitespace " \t "]]]]] + + (let [n (p/parse-string-all ws)] + (is (= :forms (node/tag n))) + (is (= (string/replace ws "\r\n" "\n") (node/string n))) + (is (= children (map (juxt node/tag node/string) (node/children n))))))) #?(:clj (deftest t-parsing-unicode-whitespace-strings - (are [?ws ?children] - (let [n (p/parse-string-all ?ws)] - (is (= :forms (node/tag n))) - (is (= (.replace ?ws "\r\n" "\n") (node/string n))) - (is (= ?children (map (juxt node/tag node/string) (node/children n))))) - "\u2028" [[:whitespace "\u2028"]]))) + (let [ws "\u2028" + children [[:whitespace "\u2028"]] + n (p/parse-string-all ws)] + (is (= :forms (node/tag n))) + (is (= (string/replace ws "\r\n" "\n") (node/string n))) + (is (= children (map (juxt node/tag node/string) (node/children n))))))) (deftest t-parsing-simple-data - (are [?s ?r] - (let [n (p/parse-string ?s)] - (is (= :token (node/tag n))) - (is (= ?s (node/string n))) - (is (= ?r (node/sexpr n)))) - "0" 0 - "0.1" 0.1 - "12e10" 1.2e11 - "2r1100" 12 - "1N" 1N - ":key" :key - "\\\\" \\ - "\\a" \a - "\\space" \space - "\\'" \' - ":1.5" :1.5 - ":1.5.0" :1.5.0 - ":ns/key" :ns/key - ":key:key" :key:key - ":x'" :x' - "sym" 'sym - "sym#" 'sym# - "sym'" 'sym' - "sym'sym" 'sym'sym - "sym:sym" 'sym:sym - "\"string\"" "string")) + (doseq [[s r] + [["0" 0] + ["0.1" 0.1] + ["12e10" 1.2e11] + ["2r1100" 12] + ["1N" 1N] + [":key" :key] + ["\\\\" \\] + ["\\a" \a] + ["\\space" \space] + ["\\'" \'] + [":1.5" :1.5] + [":1.5.0" :1.5.0] + [":ns/key" :ns/key] + [":key:key" :key:key] + [":x'" :x'] + ["sym" 'sym] + ["sym#" 'sym#] + ["sym'" 'sym'] + ["sym'sym" 'sym'sym] + ["sym:sym" 'sym:sym] + ["\"string\"" "string"]]] + (let [n (p/parse-string s)] + (is (= :token (node/tag n))) + (is (= s (node/string n))) + (is (= r (node/sexpr n)))))) (deftest t-parsing-garden-selectors ;; https://github.com/noprompt/garden - (are [?s ?r] - (let [n (p/parse-string ?s) - r (node/sexpr n)] - (is (= ?s (node/string n))) - (is (= :token (node/tag n))) - (is (keyword? r)) - (is (= ?r r))) - ":&:hover" :&:hover - ;; clj clojure reader can't parse :&::before but we can create a keyword for it - ":&::before" (keyword "&::before"))) + (doseq [[s expected-r] + [[":&:hover" :&:hover] + ;; clj clojure reader can't parse :&::before but we can create a keyword for it + [":&::before" (keyword "&::before")]]] + (let [n (p/parse-string s) + r (node/sexpr n)] + (is (= s (node/string n))) + (is (= :token (node/tag n))) + (is (keyword? r)) + (is (= expected-r r))))) (deftest t-ratios - (are [?s ?r] - (let [n (p/parse-string ?s)] - (is (= :token (node/tag n))) - (is (= ?s (node/string n))) - (is (= ?r (node/sexpr n)))) - "3/4" #?(:clj 3/4 + (let [s "3/4" + r #?(:clj 3/4 ;; no ratios in cljs; they are evaluated on sexpr - :cljs 0.75))) + :cljs 0.75) + n (p/parse-string s)] + (is (= :token (node/tag n))) + (is (= s (node/string n))) + (is (= r (node/sexpr n))))) (deftest t-big-integers - (are [?s ?r] - (let [n (p/parse-string ?s)] - (is (= :token (node/tag n))) - (is (= ?s (node/string n))) - (is (= ?r (node/sexpr n)))) - "1234567890123456789012345678901234567890" 1234567890123456789012345678901234567890N)) + (let [s "1234567890123456789012345678901234567890" + r 1234567890123456789012345678901234567890N + n (p/parse-string s)] + (is (= :token (node/tag n))) + (is (= s (node/string n))) + (is (= r (node/sexpr n))))) (deftest t-parsing-symbolic-inf-values - (are [?s ?r] - (let [n (p/parse-string ?s)] - (is (= :token (node/tag n))) - (is (= ?s (node/string n))) - (is (= ?r (node/sexpr n)))) - "##Inf" #?(:cljs js/Number.POSITIVE_INFINITY - :default Double/POSITIVE_INFINITY) - "##-Inf" #?(:cljs js/Number.NEGATIVE_INFINITY - :default Double/NEGATIVE_INFINITY))) + (doseq [[s r] + [["##Inf" #?(:cljs js/Number.POSITIVE_INFINITY + :default Double/POSITIVE_INFINITY)] + ["##-Inf" #?(:cljs js/Number.NEGATIVE_INFINITY + :default Double/NEGATIVE_INFINITY)]]] + (let [n (p/parse-string s)] + (is (= :token (node/tag n))) + (is (= s (node/string n))) + (is (= r (node/sexpr n)))))) (deftest t-parsing-symbolic-NaN-value (let [n (p/parse-string "##NaN") @@ -118,29 +120,29 @@ :default (is (Double/isNaN e))))) (deftest t-parsing-reader-prefixed-data - (are [?s ?t ?ws ?sexpr] - (let [n (p/parse-string ?s) - children (node/children n) - c (map node/tag children)] - (is (= ?t (node/tag n))) - (is (= :token (last c))) - (is (= ?sexpr (node/sexpr n))) - (is (= 'sym (node/sexpr (last children)))) - (is (= ?ws (vec (butlast c))))) - "@sym" :deref [] '(deref sym) - "@ sym" :deref [:whitespace] '(deref sym) - "'sym" :quote [] '(quote sym) - "' sym" :quote [:whitespace] '(quote sym) - "`sym" :syntax-quote [] '(quote sym) - "` sym" :syntax-quote [:whitespace] '(quote sym) - "~sym" :unquote [] '(unquote sym) - "~ sym" :unquote [:whitespace] '(unquote sym) - "~@sym" :unquote-splicing [] '(unquote-splicing sym) - "~@ sym" :unquote-splicing [:whitespace] '(unquote-splicing sym) - "#=sym" :eval [] '(eval 'sym) - "#= sym" :eval [:whitespace] '(eval 'sym) - "#'sym" :var [] '(var sym) - "#'\nsym" :var [:newline] '(var sym))) + (doseq [[s t ws sexpr] + [["@sym" :deref [] '(deref sym)] + ["@ sym" :deref [:whitespace] '(deref sym)] + ["'sym" :quote [] '(quote sym)] + ["' sym" :quote [:whitespace] '(quote sym)] + ["`sym" :syntax-quote [] '(quote sym)] + ["` sym" :syntax-quote [:whitespace] '(quote sym)] + ["~sym" :unquote [] '(unquote sym)] + ["~ sym" :unquote [:whitespace] '(unquote sym)] + ["~@sym" :unquote-splicing [] '(unquote-splicing sym)] + ["~@ sym" :unquote-splicing [:whitespace] '(unquote-splicing sym)] + ["#=sym" :eval [] '(eval 'sym)] + ["#= sym" :eval [:whitespace] '(eval 'sym)] + ["#'sym" :var [] '(var sym)] + ["#'\nsym" :var [:newline] '(var sym)]]] + (let [n (p/parse-string s) + children (node/children n) + c (map node/tag children)] + (is (= t (node/tag n))) + (is (= :token (last c))) + (is (= sexpr (node/sexpr n))) + (is (= 'sym (node/sexpr (last children)))) + (is (= ws (vec (butlast c))))))) (deftest t-eval (let [n (p/parse-string "#=(+ 1 2)")] @@ -164,67 +166,67 @@ (is (thrown-with-msg? ExceptionInfo #"unsupported operation" (node/sexpr uneval))))) (deftest t-parsing-regular-expressions - (are [?s ?expected-sexpr] - (let [n (p/parse-string ?s)] + (doseq [[s expected-sexpr] + [["#\"regex\"" '(re-pattern "regex")] + ["#\"regex\\.\"" '(re-pattern "regex\\.")] + ["#\"[reg|k].x\"" '(re-pattern "[reg|k].x")] + ["#\"a\\nb\"" '(re-pattern "a\\nb")] + ["#\"a\nb\"" '(re-pattern "a\nb")] + + ["#\"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\"" + '(re-pattern "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")]]] + (let [n (p/parse-string s)] (is (= :regex (node/tag n))) - (is (= (count ?s) (node/length n))) - (is (= ?expected-sexpr (node/sexpr n)))) - "#\"regex\"" '(re-pattern "regex") - "#\"regex\\.\"" '(re-pattern "regex\\.") - "#\"[reg|k].x\"" '(re-pattern "[reg|k].x") - "#\"a\\nb\"" '(re-pattern "a\\nb") - "#\"a\nb\"" '(re-pattern "a\nb") - - "#\"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\"" - '(re-pattern "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"))) + (is (= (count s) (node/length n))) + (is (= expected-sexpr (node/sexpr n)))))) (deftest t-parsing-strings - (are [?s ?tag ?sexpr] - (let [n (p/parse-string ?s)] - (is (= ?tag (node/tag n))) - (is (= ?s (node/string n))) - (is (= ?sexpr (node/sexpr n)))) - "\"123\"" :token "123" - "\"123\\n456\"" :token "123\n456" - "\"123\n456\"" :multi-line "123\n456")) + (doseq [[s tag sexpr] + [["\"123\"" :token "123"] + ["\"123\\n456\"" :token "123\n456"] + ["\"123\n456\"" :multi-line "123\n456"]]] + (let [n (p/parse-string s)] + (is (= tag (node/tag n))) + (is (= s (node/string n))) + (is (= sexpr (node/sexpr n)))))) (deftest t-parsing-seqs - (are [?s ?t ?w ?c] - (let [n (p/parse-string ?s) + (doseq [[s t w c] + [["(1 2 3)" :list 2 3] + ["()" :list 0 0] + ["( )" :list 1 0] + ["() " :list 0 0] + ["[1 2 3]" :vector 2 3] + ["[]" :vector 0 0] + ["[ ]" :vector 1 0] + ["[] " :vector 0 0] + ["#{1 2 3}" :set 2 3] + ["#{}" :set 0 0] + ["#{ }" :set 1 0] + ["#{} " :set 0 0] + ["{:a 0 :b 1}" :map 3 4] + ["{}" :map 0 0] + ["{ }" :map 1 0] + ["{} " :map 0 0]]] + (let [n (p/parse-string s) children (node/children n) fq (frequencies (map node/tag children))] - (is (= ?t (node/tag n))) - (is (= (.trim ?s) (node/string n))) - (is (= (node/sexpr n) (read-string ?s))) - (is (= ?w (:whitespace fq 0))) - (is (= ?c (:token fq 0)))) - "(1 2 3)" :list 2 3 - "()" :list 0 0 - "( )" :list 1 0 - "() " :list 0 0 - "[1 2 3]" :vector 2 3 - "[]" :vector 0 0 - "[ ]" :vector 1 0 - "[] " :vector 0 0 - "#{1 2 3}" :set 2 3 - "#{}" :set 0 0 - "#{ }" :set 1 0 - "#{} " :set 0 0 - "{:a 0 :b 1}" :map 3 4 - "{}" :map 0 0 - "{ }" :map 1 0 - "{} " :map 0 0)) + (is (= t (node/tag n))) + (is (= (string/trim s) (node/string n))) + (is (= (node/sexpr n) (read-string s))) + (is (= w (:whitespace fq 0))) + (is (= c (:token fq 0)))))) (deftest t-parsing-invalid-maps ;; I don't know if this ability is intentional, but libraries ;; have come to rely on the behavior of parsing invalid maps. ;; Note: sexpr won't be possible on invalid Clojure - (are [?s ?t] - (let [n (p/parse-string ?s)] - (is (= ?t (node/tag n))) - (is (= ?s (node/string n)))) - "{:a}" :map - "{:r 1 :u}" :map)) + (doseq [[s t] + [["{:a}" :map] + ["{:r 1 :u}" :map]]] + (let [n (p/parse-string s)] + (is (= t (node/tag n))) + (is (= s (node/string n)))))) (deftest t-parsing-metadata (doseq [[meta-str expected-tag expected-meta-child-tag] @@ -408,203 +410,185 @@ (node/sexpr n))))) (deftest t-parsing-reader-macros - (are [?s ?t ?children] - (let [n (p/parse-string ?s)] - (is (= ?t (node/tag n))) - (is (= ?s (node/string n))) - (is (= ?children (map node/tag (node/children n))))) - "#'a" :var [:token] - "#=(+ 1 2)" :eval [:list] - "#macro 1" :reader-macro [:token :whitespace :token] - "#macro (* 2 3)" :reader-macro [:token :whitespace :list] - "#?(:clj bar)" :reader-macro [:token :list] - "#? (:clj bar)" :reader-macro [:token :whitespace :list] - "#?@ (:clj bar)" :reader-macro [:token :whitespace :list] - "#?foo baz" :reader-macro [:token :whitespace :token] - "#_abc" :uneval [:token] - "#_(+ 1 2)" :uneval [:list])) + (doseq [[s t children] + [["#'a" :var [:token]] + ["#=(+ 1 2)" :eval [:list]] + ["#macro 1" :reader-macro [:token :whitespace :token]] + ["#macro (* 2 3)" :reader-macro [:token :whitespace :list]] + ["#?(:clj bar)" :reader-macro [:token :list]] + ["#? (:clj bar)" :reader-macro [:token :whitespace :list]] + ["#?@ (:clj bar)" :reader-macro [:token :whitespace :list]] + ["#?foo baz" :reader-macro [:token :whitespace :token]] + ["#_abc" :uneval [:token]] + ["#_(+ 1 2)" :uneval [:list]]]] + (let [n (p/parse-string s)] + (is (= t (node/tag n))) + (is (= s (node/string n))) + (is (= children (map node/tag (node/children n))))))) (deftest t-parsing-anonymous-fn - (are [?s ?t ?sexpr-match ?children] - (let [n (p/parse-string ?s)] - (is (= ?t (node/tag n))) - (is (= ?s (node/string n))) - (is (re-matches ?sexpr-match (str (node/sexpr n)))) - (is (= ?children (map node/tag (node/children n))))) - "#(+ % 1)" - :fn #"\(fn\* \[p1_.*#\] \(\+ p1_.*# 1\)\)" - [:token :whitespace - :token :whitespace - :token] - - "#(+ %& %2 %1)" - :fn #"\(fn\* \[p1_.*# p2_.*# & rest_.*#\] \(\+ rest_.*# p2_.*# p1_.*#\)\)" - [:token :whitespace - :token :whitespace - :token :whitespace - :token])) + (doseq [[s t sexpr-match children] + [["#(+ % 1)" + :fn #"\(fn\* \[p1_.*#\] \(\+ p1_.*# 1\)\)" + [:token :whitespace + :token :whitespace + :token]] + ["#(+ %& %2 %1)" + :fn #"\(fn\* \[p1_.*# p2_.*# & rest_.*#\] \(\+ rest_.*# p2_.*# p1_.*#\)\)" + [:token :whitespace + :token :whitespace + :token :whitespace + :token]]]] + (let [n (p/parse-string s)] + (is (= t (node/tag n))) + (is (= s (node/string n))) + (is (re-matches sexpr-match (str (node/sexpr n)))) + (is (= children (map node/tag (node/children n))))))) (deftest t-parsing-comments - (are [?s] - (let [n (p/parse-string ?s)] - (is (node/printable-only? n)) - (is (= :comment (node/tag n))) - (is (= ?s (node/string n)))) - "; this is a comment\n" - ";; this is a comment\n" - "; this is a comment" - ";; this is a comment" - ";" - ";;" - ";\n" - ";;\n" - "#!shebang comment\n" - "#! this is a comment" - "#!\n")) + (doseq [s ["; this is a comment\n" + ";; this is a comment\n" + "; this is a comment" + ";; this is a comment" + ";" + ";;" + ";\n" + ";;\n" + "#!shebang comment\n" + "#! this is a comment" + "#!\n"]] + (let [n (p/parse-string s)] + (is (node/printable-only? n)) + (is (= :comment (node/tag n))) + (is (= s (node/string n)))))) (deftest t-parsing-auto-resolve-keywords - (are [?s ?sexpr-default ?sexpr-custom] - (let [n (p/parse-string ?s)] + (doseq [[s sexpr-default sexpr-custom] + [["::key" :?_current-ns_?/key :my.current.ns/key] + ["::xyz/key" :??_xyz_??/key :my.aliased.ns/key]]] + (let [n (p/parse-string s)] (is (= :token (node/tag n))) - (is (= ?s (node/string n))) - (is (= ?sexpr-default (node/sexpr n))) - (is (= ?sexpr-custom (node/sexpr n {:auto-resolve #(if (= :current %) + (is (= s (node/string n))) + (is (= sexpr-default (node/sexpr n))) + (is (= sexpr-custom (node/sexpr n {:auto-resolve #(if (= :current %) 'my.current.ns - (get {'xyz 'my.aliased.ns} % 'alias-unresolved))})))) - "::key" :?_current-ns_?/key :my.current.ns/key - "::xyz/key" :??_xyz_??/key :my.aliased.ns/key)) + (get {'xyz 'my.aliased.ns} % 'alias-unresolved))})))))) (deftest t-parsing-qualified-maps - (are [?s ?sexpr] - (let [n (p/parse-string ?s)] + (doseq [[s sexpr] + [["#:abc{:x 1, :y 1}" + {:abc/x 1, :abc/y 1}] + ["#:abc {:x 1, :y 1}" + {:abc/x 1, :abc/y 1}] + ["#:abc ,,, \n\n {:x 1 :y 2}" + {:abc/x 1, :abc/y 2}] + ["#:foo{:kw 1, :n/kw 2, :_/bare 3, 0 4}" + {:foo/kw 1, :n/kw 2, :bare 3, 0 4}] + ["#:abc{:a {:b 1}}" + {:abc/a {:b 1}}] + ["#:abc{:a #:def{:b 1}}" + {:abc/a {:def/b 1}}]]] + (let [n (p/parse-string s)] (is (= :namespaced-map (node/tag n))) - (is (= (count ?s) (node/length n))) - (is (= ?s (node/string n))) - (is (= ?sexpr (node/sexpr n)))) - "#:abc{:x 1, :y 1}" - {:abc/x 1, :abc/y 1} - - "#:abc {:x 1, :y 1}" - {:abc/x 1, :abc/y 1} - - "#:abc ,,, \n\n {:x 1 :y 2}" - {:abc/x 1, :abc/y 2} - - "#:foo{:kw 1, :n/kw 2, :_/bare 3, 0 4}" - {:foo/kw 1, :n/kw 2, :bare 3, 0 4} - - "#:abc{:a {:b 1}}" - {:abc/a {:b 1}} - - "#:abc{:a #:def{:b 1}}" - {:abc/a {:def/b 1}})) + (is (= (count s) (node/length n))) + (is (= s (node/string n))) + (is (= sexpr (node/sexpr n)))))) (deftest t-parsing-auto-resolve-current-ns-maps - (are [?s ?sexpr-default ?sexpr-custom] - (let [n (p/parse-string ?s)] - (is (= :namespaced-map (node/tag n))) - (is (= (count ?s) (node/length n))) - (is (= ?s (node/string n))) - (is (= ?sexpr-default (node/sexpr n))) - (is (= ?sexpr-custom (node/sexpr n {:auto-resolve #(if (= :current %) - 'booya.fooya - 'alias-unresolved)})))) - "#::{:x 1, :y 1}" - {:?_current-ns_?/x 1, :?_current-ns_?/y 1} - {:booya.fooya/x 1, :booya.fooya/y 1} - - "#:: {:x 1, :y 1}" - {:?_current-ns_?/x 1, :?_current-ns_?/y 1} - {:booya.fooya/x 1, :booya.fooya/y 1} - - "#:: \n,,\n,, {:x 1, :y 1}" - {:?_current-ns_?/x 1, :?_current-ns_?/y 1} - {:booya.fooya/x 1, :booya.fooya/y 1} - - "#::{:kw 1, :n/kw 2, :_/bare 3, 0 4}" - {:?_current-ns_?/kw 1, :n/kw 2, :bare 3, 0 4} - {:booya.fooya/kw 1, :n/kw 2, :bare 3, 0 4} - - "#::{:a {:b 1}}" - {:?_current-ns_?/a {:b 1}} - {:booya.fooya/a {:b 1}} - - "#::{:a #::{:b 1}}" - {:?_current-ns_?/a {:?_current-ns_?/b 1}} - {:booya.fooya/a {:booya.fooya/b 1}})) + (doseq [[s sexpr-default sexpr-custom] + [["#::{:x 1, :y 1}" + {:?_current-ns_?/x 1, :?_current-ns_?/y 1} + {:booya.fooya/x 1, :booya.fooya/y 1}] + ["#:: {:x 1, :y 1}" + {:?_current-ns_?/x 1, :?_current-ns_?/y 1} + {:booya.fooya/x 1, :booya.fooya/y 1}] + ["#:: \n,,\n,, {:x 1, :y 1}" + {:?_current-ns_?/x 1, :?_current-ns_?/y 1} + {:booya.fooya/x 1, :booya.fooya/y 1}] + ["#::{:kw 1, :n/kw 2, :_/bare 3, 0 4}" + {:?_current-ns_?/kw 1, :n/kw 2, :bare 3, 0 4} + {:booya.fooya/kw 1, :n/kw 2, :bare 3, 0 4}] + ["#::{:a {:b 1}}" + {:?_current-ns_?/a {:b 1}} + {:booya.fooya/a {:b 1}}] + ["#::{:a #::{:b 1}}" + {:?_current-ns_?/a {:?_current-ns_?/b 1}} + {:booya.fooya/a {:booya.fooya/b 1}}]]] + (let [n (p/parse-string s)] + (is (= :namespaced-map (node/tag n))) + (is (= (count s) (node/length n))) + (is (= s (node/string n))) + (is (= sexpr-default (node/sexpr n))) + (is (= sexpr-custom (node/sexpr n {:auto-resolve #(if (= :current %) + 'booya.fooya + 'alias-unresolved)})))))) (deftest parsing-auto-resolve-ns-alias-maps - (are [?s ?sexpr-default ?sexpr-custom] - (let [n (p/parse-string ?s)] - (is (= :namespaced-map (node/tag n))) - (is (= (count ?s) (node/length n))) - (is (= ?s (node/string n))) - (is (= ?sexpr-default (node/sexpr n))) - (is (= ?sexpr-custom (node/sexpr n {:auto-resolve #(if (= :current %) - 'my.current.ns - (get {'nsalias 'bing.bang - 'nsalias2 'woopa.doopa} % 'alias-unresolved))})))) - "#::nsalias{:x 1, :y 1}" - '{:??_nsalias_??/x 1, :??_nsalias_??/y 1} - '{:bing.bang/x 1, :bing.bang/y 1} - - "#::nsalias {:x 1, :y 1}" - '{:??_nsalias_??/x 1, :??_nsalias_??/y 1} - '{:bing.bang/x 1, :bing.bang/y 1} - - "#::nsalias ,,,,,,,,,,\n,,,,,,\n,,,,, {:x 1, :y 1}" - '{:??_nsalias_??/x 1, :??_nsalias_??/y 1} - '{:bing.bang/x 1, :bing.bang/y 1} - - "#::nsalias{:kw 1, :n/kw 2, :_/bare 3, 0 4}" - '{:??_nsalias_??/kw 1, :n/kw 2, :bare 3, 0 4} - '{:bing.bang/kw 1, :n/kw 2, :bare 3, 0 4} - - "#::nsalias{:a {:b 1}}" - '{:??_nsalias_??/a {:b 1}} - '{:bing.bang/a {:b 1}} - - "#::nsalias{:a #::nsalias2{:b 1}}" - '{:??_nsalias_??/a {:??_nsalias2_??/b 1}} - '{:bing.bang/a {:woopa.doopa/b 1}})) + (doseq [[s sexpr-default sexpr-custom] + [["#::nsalias{:x 1, :y 1}" + '{:??_nsalias_??/x 1, :??_nsalias_??/y 1} + '{:bing.bang/x 1, :bing.bang/y 1}] + ["#::nsalias {:x 1, :y 1}" + '{:??_nsalias_??/x 1, :??_nsalias_??/y 1} + '{:bing.bang/x 1, :bing.bang/y 1}] + ["#::nsalias ,,,,,,,,,,\n,,,,,,\n,,,,, {:x 1, :y 1}" + '{:??_nsalias_??/x 1, :??_nsalias_??/y 1} + '{:bing.bang/x 1, :bing.bang/y 1}] + ["#::nsalias{:kw 1, :n/kw 2, :_/bare 3, 0 4}" + '{:??_nsalias_??/kw 1, :n/kw 2, :bare 3, 0 4} + '{:bing.bang/kw 1, :n/kw 2, :bare 3, 0 4}] + ["#::nsalias{:a {:b 1}}" + '{:??_nsalias_??/a {:b 1}} + '{:bing.bang/a {:b 1}}] + ["#::nsalias{:a #::nsalias2{:b 1}}" + '{:??_nsalias_??/a {:??_nsalias2_??/b 1}} + '{:bing.bang/a {:woopa.doopa/b 1}}]]] + (let [n (p/parse-string s)] + (is (= :namespaced-map (node/tag n))) + (is (= (count s) (node/length n))) + (is (= s (node/string n))) + (is (= sexpr-default (node/sexpr n))) + (is (= sexpr-custom (node/sexpr n {:auto-resolve #(if (= :current %) + 'my.current.ns + (get {'nsalias 'bing.bang + 'nsalias2 'woopa.doopa} % 'alias-unresolved))})))))) (deftest t-parsing-exceptions - (are [?s ?p] - (is (thrown-with-msg? ExceptionInfo ?p (p/parse-string ?s))) - "#" #".*Unexpected EOF.*" - "#(" #".*Unexpected EOF.*" - "(def" #".*Unexpected EOF.*" - "[def" #".*Unexpected EOF.*" - "#{def" #".*Unexpected EOF.*" - "{:a 0" #".*Unexpected EOF.*" - "\"abc" #".*EOF.*" - "#\"abc" #".*Unexpected EOF.*" - "(def x 0]" #".*Unmatched delimiter.*" - "##wtf" #".*Invalid token: ##wtf" - "#=" #".*:eval node expects 1 value.*" - "#^" #".*:meta node expects 2 values.*" - "^:private" #".*:meta node expects 2 values.*" - "#^:private" #".*:meta node expects 2 values.*" - "#_" #".*:uneval node expects 1 value.*" - "#'" #".*:var node expects 1 value.*" - "#macro" #".*:reader-macro node expects 2 values.*" - "#:" #".*namespaced map expects a namespace*" - "#::" #".*namespaced map expects a map*" - "#::nsarg" #".*namespaced map expects a map*" - "#:{:a 1}" #".*namespaced map expects a namespace*" - "#::[a]" #".*namespaced map expects a map*" - "#:[a]" #".*namespaced map expects a namespace*" - "#:: token" #".*namespaced map expects a map*" - "#::alias [a]" #".*namespaced map expects a map*" - "#:prefix [a]" #".*namespaced map expects a map.*")) + (doseq [[s p] + [["#" #".*Unexpected EOF.*"] + ["#(" #".*Unexpected EOF.*"] + ["(def" #".*Unexpected EOF.*"] + ["[def" #".*Unexpected EOF.*"] + ["#{def" #".*Unexpected EOF.*"] + ["{:a 0" #".*Unexpected EOF.*"] + ["\"abc" #".*EOF.*"] + ["#\"abc" #".*Unexpected EOF.*"] + ["(def x 0]" #".*Unmatched delimiter.*"] + ["##wtf" #".*Invalid token: ##wtf"] + ["#=" #".*:eval node expects 1 value.*"] + ["#^" #".*:meta node expects 2 values.*"] + ["^:private" #".*:meta node expects 2 values.*"] + ["#^:private" #".*:meta node expects 2 values.*"] + ["#_" #".*:uneval node expects 1 value.*"] + ["#'" #".*:var node expects 1 value.*"] + ["#macro" #".*:reader-macro node expects 2 values.*"] + ["#:" #".*namespaced map expects a namespace*"] + ["#::" #".*namespaced map expects a map*"] + ["#::nsarg" #".*namespaced map expects a map*"] + ["#:{:a 1}" #".*namespaced map expects a namespace*"] + ["#::[a]" #".*namespaced map expects a map*"] + ["#:[a]" #".*namespaced map expects a namespace*"] + ["#:: token" #".*namespaced map expects a map*"] + ["#::alias [a]" #".*namespaced map expects a map*"] + ["#:prefix [a]" #".*namespaced map expects a map.*"]]] + (is (thrown-with-msg? ExceptionInfo p (p/parse-string s))))) (deftest t-sexpr-exceptions - (are [?s] - (is (thrown-with-msg? ExceptionInfo #"unsupported operation.*" (node/sexpr (p/parse-string ?s)))) - "#_42" ;; reader ignore/discard - ";; can't sexpr me!" ;; comment - " " ;; whitespace - )) + (doseq [s ["#_42" ;; reader ignore/discard + ";; can't sexpr me!" ;; comment + " " ;; whitespace + ]] + (is (thrown-with-msg? ExceptionInfo #"unsupported operation.*" (node/sexpr (p/parse-string s)))))) (deftest t-parsing-multiple-forms (let [s "1 2 3" @@ -665,20 +649,20 @@ " (println x))") positions (->> (p/parse-string-all s) (nodes-with-meta))] - (are [?pos ?end ?t ?s ?sexpr] - (let [{:keys [node end-pos]} (positions ?pos)] - (is (= ?t (node/tag node))) - (is (= ?s (node/string node))) - (is (= ?sexpr (node/sexpr node))) - (is (= ?end end-pos))) - [1 1] [3 15] :list s '(defn f [x] (println x)) - [1 2] [1 6] :token "defn" 'defn - [1 7] [1 8] :token "f" 'f - [2 3] [2 6] :vector "[x]" '[x] - [2 4] [2 5] :token "x" 'x - [3 3] [3 14] :list "(println x)" '(println x) - [3 4] [3 11] :token "println" 'println - [3 12] [3 13] :token "x" 'x)) + (doseq [[pos end t s sexpr] + [[[1 1] [3 15] :list s '(defn f [x] (println x))] + [[1 2] [1 6] :token "defn" 'defn] + [[1 7] [1 8] :token "f" 'f] + [[2 3] [2 6] :vector "[x]" '[x]] + [[2 4] [2 5] :token "x" 'x] + [[3 3] [3 14] :list "(println x)" '(println x)] + [[3 4] [3 11] :token "println" 'println] + [[3 12] [3 13] :token "x" 'x]]] + (let [{:keys [node end-pos]} (positions pos)] + (is (= t (node/tag node))) + (is (= s (node/string node))) + (is (= sexpr (node/sexpr node))) + (is (= end end-pos))))) ;; root node (let [s (str ;1234567890 @@ -692,35 +676,30 @@ (is (= [3 5] end-pos)))) (deftest t-os-specific-line-endings - (are [?in ?expected] - (let [str-actual (-> ?in p/parse-string-all node/string)] - (is (= ?expected str-actual) "from string") + (doseq [[in expected] + [["heya\r\nplaya\r\n" + "heya\nplaya\n"] + [";; comment\r\n(+ 1 2 3)\r\n" + ";; comment\n(+ 1 2 3)\n"] + ["1\r2\r\n3\r\f4" + "1\n2\n3\n4"] + ["\n\n\n\n" + "\n\n\n\n"] + ["\r\r\r\r\r" + "\n\n\n\n\n"] + ["\r\n\r\n\r\n\r\n\r\n\r\n" + "\n\n\n\n\n\n"] + [;1 2 3 4 5 6 7 + "\r\n\r\r\f\r\n\r\r\n\r" + "\n\n\n\n\n\n\n"] + ]] + (let [str-actual (-> in p/parse-string-all node/string)] + (is (= expected str-actual) "from string") #?(:clj - (is (= ?expected (let [t-file (File/createTempFile "rewrite-clj-parse-test" ".clj")] + (is (= expected (let [t-file (File/createTempFile "rewrite-clj-parse-test" ".clj")] (.deleteOnExit t-file) - (spit t-file ?in) - (-> t-file p/parse-file-all node/string))) "from file"))) - "heya\r\nplaya\r\n" - "heya\nplaya\n" - - ";; comment\r\n(+ 1 2 3)\r\n" - ";; comment\n(+ 1 2 3)\n" - - "1\r2\r\n3\r\f4" - "1\n2\n3\n4" - - "\n\n\n\n" - "\n\n\n\n" - - "\r\r\r\r\r" - "\n\n\n\n\n" - - "\r\n\r\n\r\n\r\n\r\n\r\n" - "\n\n\n\n\n\n" - - ;1 2 3 4 5 6 7 - "\r\n\r\r\f\r\n\r\r\n\r" - "\n\n\n\n\n\n\n")) + (spit t-file in) + (-> t-file p/parse-file-all node/string))) "from file"))))) (deftest t-position-in-ex-data (let [ex (try (p/parse-string "(defn foo [)") diff --git a/test/rewrite_clj/zip/base_test.cljc b/test/rewrite_clj/zip/base_test.cljc index 44473dfc..ec29c5bc 100644 --- a/test/rewrite_clj/zip/base_test.cljc +++ b/test/rewrite_clj/zip/base_test.cljc @@ -1,5 +1,5 @@ (ns rewrite-clj.zip.base-test - (:require [clojure.test :refer [deftest is are]] + (:require [clojure.test :refer [deftest is]] [rewrite-clj.node :as node] [rewrite-clj.zip :as z])) @@ -64,55 +64,49 @@ (deftest t-zipper-creation-for-whitespace-only-nodes - (are [?ws ?s] - (let [n (node/forms-node [?ws]) + (doseq [[ws s] + [[(node/spaces 3) " "] + [(node/comment-node "foo") ";foo"]]] + (let [n (node/forms-node [ws]) loc (z/of-node n)] (is (= :forms (z/tag loc))) (is (= nil (z/sexpr loc))) - (is (= ?s (z/string loc))) - (is (= ?s (z/root-string loc)))) - (node/spaces 3) " " - (node/comment-node "foo") ";foo")) + (is (= s (z/string loc))) + (is (= s (z/root-string loc)))))) (deftest t-length-calculation - (are [?s] - (let [s ?s - loc (z/of-string s)] - (is (= (count s) (z/length loc)))) - "0" - "^:private x" - "[1 2 [3 4] #{5}]" - "{:a 0, :b 1, ::c 3, :ns/x}" - "#inst \"2014\"" - "#_(+ 2 3)" - "@(deref x)" - "#=(+ 1 2)")) + (doseq [s ["0" + "^:private x" + "[1 2 [3 4] #{5}]" + "{:a 0, :b 1, ::c 3, :ns/x}" + "#inst \"2014\"" + "#_(+ 2 3)" + "@(deref x)" + "#=(+ 1 2)"]] + (let [loc (z/of-string s)] + (is (= (count s) (z/length loc)))))) (deftest t-sexpr-applies-auto-resolve-opts-to-nested-elements - (are [?sexpr-default ?child-sexprs-default ?sexpr-custom ?child-sexprs-custom] - (let [s "{:x [[[::a]] #{::myalias2/b} #::myalias{:x 1 :y 2}]}" - zloc (z/of-string s) - opts {:auto-resolve #(if (= :current %) - 'my.current.ns - (get {'myalias 'my.aliased.ns} % - (symbol (str % "-unresolved"))))} - zloc-custom-opts (z/of-string s opts)] - (is (= ?sexpr-default (z/sexpr zloc))) - (is (= ?child-sexprs-default (z/child-sexprs zloc))) - (is (= ?sexpr-custom (z/sexpr zloc-custom-opts))) - (is (= ?child-sexprs-custom (z/child-sexprs zloc-custom-opts)))) - {:x [[[:?_current-ns_?/a]] - #{:??_myalias2_??/b} - {:??_myalias_??/x 1, :??_myalias_??/y 2}]} - - '(:x [[[:?_current-ns_?/a]] - #{:??_myalias2_??/b} - {:??_myalias_??/x 1, :??_myalias_??/y 2}]) - - {:x [[[:my.current.ns/a]] - #{:myalias2-unresolved/b} - {:my.aliased.ns/x 1, :my.aliased.ns/y 2}]} - - '(:x [[[:my.current.ns/a]] - #{:myalias2-unresolved/b} - {:my.aliased.ns/x 1, :my.aliased.ns/y 2}]))) + (let [sexpr-default {:x [[[:?_current-ns_?/a]] + #{:??_myalias2_??/b} + {:??_myalias_??/x 1, :??_myalias_??/y 2}]} + child-sexprs-default '(:x [[[:?_current-ns_?/a]] + #{:??_myalias2_??/b} + {:??_myalias_??/x 1, :??_myalias_??/y 2}]) + sexpr-custom {:x [[[:my.current.ns/a]] + #{:myalias2-unresolved/b} + {:my.aliased.ns/x 1, :my.aliased.ns/y 2}]} + child-sexprs-custom '(:x [[[:my.current.ns/a]] + #{:myalias2-unresolved/b} + {:my.aliased.ns/x 1, :my.aliased.ns/y 2}]) + s "{:x [[[::a]] #{::myalias2/b} #::myalias{:x 1 :y 2}]}" + zloc (z/of-string s) + opts {:auto-resolve #(if (= :current %) + 'my.current.ns + (get {'myalias 'my.aliased.ns} % + (symbol (str % "-unresolved"))))} + zloc-custom-opts (z/of-string s opts)] + (is (= sexpr-default (z/sexpr zloc))) + (is (= child-sexprs-default (z/child-sexprs zloc))) + (is (= sexpr-custom (z/sexpr zloc-custom-opts))) + (is (= child-sexprs-custom (z/child-sexprs zloc-custom-opts))))) diff --git a/test/rewrite_clj/zip/editz_test.cljc b/test/rewrite_clj/zip/editz_test.cljc index 2dd90716..4ea3b149 100644 --- a/test/rewrite_clj/zip/editz_test.cljc +++ b/test/rewrite_clj/zip/editz_test.cljc @@ -1,5 +1,5 @@ (ns rewrite-clj.zip.editz-test - (:require [clojure.test :refer [deftest testing is are]] + (:require [clojure.test :refer [deftest testing is]] [rewrite-clj.node :as node] [rewrite-clj.zip :as z])) @@ -7,20 +7,20 @@ (let [root (z/of-string "[1 \"2\" :3]") elements (iterate z/next root)] (testing "edit" - (are [?n ?f ?s] - (let [loc (nth elements ?n) - loc' (z/edit loc ?f)] - (is (= ?s (z/root-string loc')))) - 0 #(subvec % 1) "[\"2\" :3]" - 1 #(str % "_x") "[\"1_x\" \"2\" :3]" - 2 #(keyword % "k") "[1 :2/k :3]")) + (doseq [[n f s] + [[0 #(subvec % 1) "[\"2\" :3]"] + [1 #(str % "_x") "[\"1_x\" \"2\" :3]"] + [2 #(keyword % "k") "[1 :2/k :3]"]]] + (let [loc (nth elements n) + loc' (z/edit loc f)] + (is (= s (z/root-string loc')))))) (testing "replace" - (are [?n ?v ?s] - (let [loc (nth elements ?n) - loc' (z/replace loc ?v)] - (is (= ?s (z/root-string loc')))) - 0 [1] "[1]" - 1 #{0} "[#{0} \"2\" :3]")))) + (doseq [[n v s] + [[0 [1] "[1]"] + [1 #{0} "[#{0} \"2\" :3]"]]] + (let [loc (nth elements n) + loc' (z/replace loc v)] + (is (= s (z/root-string loc')))))))) (deftest t-edit-with-args (is (= "[1 102 3]" (-> "[1 2 3]" @@ -53,54 +53,54 @@ (deftest t-splice-operations (let [root (z/of-string "[1 [ ] [2 3] [ 4 ]]") elements (iterate z/next root)] - (are [?n ?s ?e] - (let [loc (nth elements ?n) - loc' (z/splice loc)] - (is (= ?s (z/root-string loc'))) - (is (= ?e (z/sexpr loc')))) - 0 "1 [ ] [2 3] [ 4 ]" 1 - 1 "[1 [ ] [2 3] [ 4 ]]" 1 - 2 "[1 [2 3] [ 4 ]]" 1 - 3 "[1 [ ] 2 3 [ 4 ]]" 2 - 6 "[1 [ ] [2 3] 4]" 4))) + (doseq [[n s e] + [[0 "1 [ ] [2 3] [ 4 ]" 1] + [1 "[1 [ ] [2 3] [ 4 ]]" 1] + [2 "[1 [2 3] [ 4 ]]" 1] + [3 "[1 [ ] 2 3 [ 4 ]]" 2] + [6 "[1 [ ] [2 3] 4]" 4]]] + (let [loc (nth elements n) + loc' (z/splice loc)] + (is (= s (z/root-string loc'))) + (is (= e (z/sexpr loc'))))))) (deftest t-splicing-with-comment - (are [?data ?s] - (let [v (z/of-string ?data) - loc (-> v z/down z/right z/splice)] - (is (= ?s (z/root-string loc)))) - "[1 [2\n;; comment\n3]]" "[1 2\n;; comment\n3]" - "[1 [;;comment\n3]]" "[1 ;;comment\n3]" - "[1 [;;comment\n]]" "[1 ;;comment\n]" - "[1 [2\n;;comment\n]]" "[1 2\n;;comment\n]")) + (doseq [[data s] + [["[1 [2\n;; comment\n3]]" "[1 2\n;; comment\n3]"] + ["[1 [;;comment\n3]]" "[1 ;;comment\n3]"] + ["[1 [;;comment\n]]" "[1 ;;comment\n]"] + ["[1 [2\n;;comment\n]]" "[1 2\n;;comment\n]"]]] + (let [v (z/of-string data) + loc (-> v z/down z/right z/splice)] + (is (= s (z/root-string loc)))))) (deftest t-replacement-using-a-hand-crafted-node - (are [?node ?s] - (let [root (z/of-string "[1 2 3]")] - (is (= ?s - (-> root - z/next - (z/replace ?node) - z/root-string)))) - (node/token-node 255 "16rff") "[16rff 2 3]" - (node/integer-node 255 16) "[0xff 2 3]" - (node/integer-node 255 8) "[0377 2 3]" - (node/integer-node 9 2) "[2r1001 2 3]")) + (doseq [[node s] + [[(node/token-node 255 "16rff") "[16rff 2 3]"] + [(node/integer-node 255 16) "[0xff 2 3]"] + [(node/integer-node 255 8) "[0377 2 3]"] + [(node/integer-node 9 2) "[2r1001 2 3]"]]] + (let [root (z/of-string "[1 2 3]")] + (is (= s + (-> root + z/next + (z/replace node) + z/root-string)))))) (deftest t-prefix - (are [?in ?prefix ?expected] - (let [zin (z/of-string ?in) - zmod (z/prefix zin ?prefix) - out (z/root-string zmod)] - (is (= ?expected out))) - "\"one\"" "123-" "\"123-one\"" - "\"a\nbb\ncc\ndd\"" "456-" "\"456-a\nbb\ncc\ndd\"")) + (doseq [[in prefix expected] + [["\"one\"" "123-" "\"123-one\""] + ["\"a\nbb\ncc\ndd\"" "456-" "\"456-a\nbb\ncc\ndd\""]]] + (let [zin (z/of-string in) + zmod (z/prefix zin prefix) + out (z/root-string zmod)] + (is (= expected out))))) (deftest t-suffix - (are [?in ?suffix ?expected] - (let [zin (z/of-string ?in) - zmod (z/suffix zin ?suffix) + (doseq [[in suffix expected] + [["\"one\"" "-123" "\"one-123\""] + ["\"a\nbb\ncc\ndd\"" "-456" "\"a\nbb\ncc\ndd-456\""]]] + (let [zin (z/of-string in) + zmod (z/suffix zin suffix) out (z/root-string zmod)] - (is (= ?expected out))) - "\"one\"" "-123" "\"one-123\"" - "\"a\nbb\ncc\ndd\"" "-456" "\"a\nbb\ncc\ndd-456\"")) + (is (= expected out))))) diff --git a/test/rewrite_clj/zip/findz_test.cljc b/test/rewrite_clj/zip/findz_test.cljc index 83d2a103..fa455403 100644 --- a/test/rewrite_clj/zip/findz_test.cljc +++ b/test/rewrite_clj/zip/findz_test.cljc @@ -1,5 +1,5 @@ (ns rewrite-clj.zip.findz-test - (:require [clojure.test :refer [deftest testing is are]] + (:require [clojure.test :refer [deftest testing is]] [rewrite-clj.zip :as z]) #?(:clj (:import clojure.lang.ExceptionInfo))) @@ -161,43 +161,42 @@ (is (= :my.current.ns/a (z/sexpr loc))))))) (deftest t-find-last-by-pos - (are [?for-position ?expected] - ;; row 1 2 3 - ;; col 12345678901 12345 1234567890 - (let [sample "(defn hi-fn\n [x]\n (+ x 1))" - actual (-> sample - (z/of-string {:track-position? true}) - (z/find-last-by-pos ?for-position) - z/string)] - (is (= ?expected actual))) - [1 1] "(defn hi-fn\n [x]\n (+ x 1))" - [3 10] "(defn hi-fn\n [x]\n (+ x 1))" - [1 6] " " - [1 7] "hi-fn" - [1 10] "hi-fn" - [1 11] "hi-fn" - [2 4] "x" - [2 5] "[x]" - {:row 2 :col 5} "[x]" ;; original cljs syntax still works - [3 8] "1" - [3 9] "(+ x 1)" - ;; at and end of row - [1 12] "\n" - ;; past and end of row - [1 200] "\n" - ;; past end of sample - [3 11] nil - [400 400] nil)) + (doseq [[for-position expected] + [[[1 1] "(defn hi-fn\n [x]\n (+ x 1))"] + [[3 10] "(defn hi-fn\n [x]\n (+ x 1))"] + [[1 6] " "] + [[1 7] "hi-fn"] + [[1 10] "hi-fn"] + [[1 11] "hi-fn"] + [[2 4] "x"] + [[2 5] "[x]"] + [{:row 2 :col 5} "[x]"] ;; original cljs syntax still works + [[3 8] "1"] + [[3 9] "(+ x 1)"] + ;; at and end of row + [[1 12] "\n"] + ;; past and end of row + [[1 200] "\n"] + ;; past end of sample + [[3 11] nil] + [[400 400] nil]]] + ;; row 1 2 3 + ;; col 12345678901 12345 1234567890 + (let [sample "(defn hi-fn\n [x]\n (+ x 1))" + actual (-> sample + (z/of-string {:track-position? true}) + (z/find-last-by-pos for-position) + z/string)] + (is (= expected actual))))) (deftest t-find-last-by-pos-invalid - (are [?for-position] - (let [sample (z/of-string "(def b 42)" {:track-position? true})] - (is (thrown-with-msg? ExceptionInfo #"zipper row and col positions are ones-based" - (z/find-last-by-pos sample ?for-position)))) - [0 0] - [3 0] - [0 10] - [-100 -200])) + (doseq [for-position [[0 0] + [3 0] + [0 10] + [-100 -200]]] + (let [sample (z/of-string "(def b 42)" {:track-position? true})] + (is (thrown-with-msg? ExceptionInfo #"zipper row and col positions are ones-based" + (z/find-last-by-pos sample for-position)))))) (deftest find-tag-by-pos (is (= "[4 5 6]" (-> "[1 2 3 [4 5 6]]" diff --git a/test/rewrite_clj/zip/insert_test.cljc b/test/rewrite_clj/zip/insert_test.cljc index 0fe69f06..380f54b5 100644 --- a/test/rewrite_clj/zip/insert_test.cljc +++ b/test/rewrite_clj/zip/insert_test.cljc @@ -1,48 +1,48 @@ (ns rewrite-clj.zip.insert-test - (:require [clojure.test :refer [deftest is are]] + (:require [clojure.test :refer [deftest is]] [rewrite-clj.interop :as interop] [rewrite-clj.zip :as z])) (deftest t-whitespace-aware-insertion - (are [?fmt ?m ?n ?f ?s] - (let [elements (->> (z/of-string - (interop/simple-format ?fmt "1 2 3 4")) - (iterate ?m)) - loc (nth elements ?n) - loc' (?f loc 'x)] - (is (= (z/tag loc') (z/tag loc))) - (is (= ?s (z/root-string loc')))) - "[%s]" z/next 0 z/insert-right "[1 2 3 4] x" - "[%s]" z/next 1 z/insert-right "[1 x 2 3 4]" - "[%s]" z/next 2 z/insert-right "[1 2 x 3 4]" - "[%s]" z/next 3 z/insert-right "[1 2 3 x 4]" - "[%s]" z/next 4 z/insert-right "[1 2 3 4 x]" - "[%s]" z/next 0 z/insert-left "x [1 2 3 4]" - "[%s]" z/next 1 z/insert-left "[x 1 2 3 4]" - "[%s]" z/next 2 z/insert-left "[1 x 2 3 4]" - "[%s]" z/next 3 z/insert-left "[1 2 x 3 4]" - "[%s]" z/next 4 z/insert-left "[1 2 3 x 4]" - "[%s]" z/next 0 z/insert-child "[x 1 2 3 4]" - "[%s]" z/next 0 z/append-child "[1 2 3 4 x]" - "[ %s]" z/next 0 z/insert-child "[x 1 2 3 4]" - "[%s ]" z/next 0 z/append-child "[1 2 3 4 x]" - "[%s]" z/next* 2 z/insert-right "[1 x 2 3 4]" - "\n[%s]" z/leftmost* 1 z/insert-left "x\n[1 2 3 4]" - "\n[%s]" z/leftmost* 1 z/insert-right "\nx [1 2 3 4]")) + (doseq [[fmt m n f s] + [["[%s]" z/next 0 z/insert-right "[1 2 3 4] x"] + ["[%s]" z/next 1 z/insert-right "[1 x 2 3 4]"] + ["[%s]" z/next 2 z/insert-right "[1 2 x 3 4]"] + ["[%s]" z/next 3 z/insert-right "[1 2 3 x 4]"] + ["[%s]" z/next 4 z/insert-right "[1 2 3 4 x]"] + ["[%s]" z/next 0 z/insert-left "x [1 2 3 4]"] + ["[%s]" z/next 1 z/insert-left "[x 1 2 3 4]"] + ["[%s]" z/next 2 z/insert-left "[1 x 2 3 4]"] + ["[%s]" z/next 3 z/insert-left "[1 2 x 3 4]"] + ["[%s]" z/next 4 z/insert-left "[1 2 3 x 4]"] + ["[%s]" z/next 0 z/insert-child "[x 1 2 3 4]"] + ["[%s]" z/next 0 z/append-child "[1 2 3 4 x]"] + ["[ %s]" z/next 0 z/insert-child "[x 1 2 3 4]"] + ["[%s ]" z/next 0 z/append-child "[1 2 3 4 x]"] + ["[%s]" z/next* 2 z/insert-right "[1 x 2 3 4]"] + ["\n[%s]" z/leftmost* 1 z/insert-left "x\n[1 2 3 4]"] + ["\n[%s]" z/leftmost* 1 z/insert-right "\nx [1 2 3 4]"]]] + (let [elements (->> (z/of-string + (interop/simple-format fmt "1 2 3 4")) + (iterate m)) + loc (nth elements n) + loc' (f loc 'x)] + (is (= (z/tag loc') (z/tag loc))) + (is (= s (z/root-string loc')))))) (deftest t-different-node-types-that-allow-insertion - (are [?s ?depth ?result] - (let [loc (-> (iterate z/down (z/of-string ?s)) - (nth (inc ?depth)) + (doseq [[s depth result] + [["[1 2]" 0 "[1 x 2 y]"] + ["(1 2)" 0 "(1 x 2 y)"] + ["#{1 2}" 0 "#{1 x 2 y}"] + ["#(1 2)" 0 "#(1 x 2 y)"] + ["'(1 2)" 1 "'(1 x 2 y)"] + ["#=(1 2)" 1 "#=(1 x 2 y)"] + ["#_(1 2)" 1 "#_(1 x 2 y)"] + ["@(f 2)" 1 "@(f x 2 y)"]]] + (let [loc (-> (iterate z/down (z/of-string s)) + (nth (inc depth)) z/right (z/insert-left 'x) (z/insert-right 'y))] - (is (= ?result (z/root-string loc)))) - "[1 2]" 0 "[1 x 2 y]" - "(1 2)" 0 "(1 x 2 y)" - "#{1 2}" 0 "#{1 x 2 y}" - "#(1 2)" 0 "#(1 x 2 y)" - "'(1 2)" 1 "'(1 x 2 y)" - "#=(1 2)" 1 "#=(1 x 2 y)" - "#_(1 2)" 1 "#_(1 x 2 y)" - "@(f 2)" 1 "@(f x 2 y)")) + (is (= result (z/root-string loc)))))) diff --git a/test/rewrite_clj/zip/move_test.cljc b/test/rewrite_clj/zip/move_test.cljc index 7542d58b..799f217b 100644 --- a/test/rewrite_clj/zip/move_test.cljc +++ b/test/rewrite_clj/zip/move_test.cljc @@ -1,25 +1,25 @@ (ns rewrite-clj.zip.move-test - (:require [clojure.test :refer [deftest is are]] + (:require [clojure.test :refer [deftest is]] [rewrite-clj.zip :as z])) - (deftest t-whitespace-aware-movement - (let [root (z/of-string "[ 1 [2 3] 4]")] - (are [?ops ?sexpr ?pred] - (let [loc ((apply comp (reverse ?ops)) root)] - (is (= ?sexpr (z/sexpr loc))) - (is (?pred loc))) - [z/down] 1 identity - [z/next] 1 (complement z/end?) - [z/next z/next] [2 3] (complement z/end?) - [z/down z/right z/right] 4 (complement z/end?) - [z/down z/rightmost] 4 z/rightmost? - [z/down z/rightmost z/rightmost] 4 z/rightmost? - [z/down z/leftmost] 1 z/leftmost? - [z/down z/right z/leftmost] 1 z/leftmost? - [z/down z/next z/next z/up] [2 3] identity - [z/down z/next z/next z/up z/up] [1 [2 3] 4] #(and (z/leftmost? %) (z/rightmost? %)) - [z/down z/rightmost z/prev] 3 identity - [z/down z/rightmost z/next] 4 z/end?))) +(deftest t-whitespace-aware-movement + (let [root (z/of-string "[ 1 [2 3] 4]")] + (doseq [[ops sexpr pred] + [[[z/down] 1 identity] + [[z/next] 1 (complement z/end?)] + [[z/next z/next] [2 3] (complement z/end?)] + [[z/down z/right z/right] 4 (complement z/end?)] + [[z/down z/rightmost] 4 z/rightmost?] + [[z/down z/rightmost z/rightmost] 4 z/rightmost] + [[z/down z/leftmost] 1 z/leftmost] + [[z/down z/right z/leftmost] 1 z/leftmost?] + [[z/down z/next z/next z/up] [2 3] identity] + [[z/down z/next z/next z/up z/up] [1 [2 3] 4] #(and (z/leftmost? %) (z/rightmost? %))] + [[z/down z/rightmost z/prev] 3 identity] + [[z/down z/rightmost z/next] 4 z/end?]]] + (let [loc ((apply comp (reverse ops)) root)] + (is (= sexpr (z/sexpr loc))) + (is (pred loc)))))) (deftest t-moving-into-an-empty-inner-node (let [zloc (z/of-string "[]")] diff --git a/test/rewrite_clj/zip/removez_test.cljc b/test/rewrite_clj/zip/removez_test.cljc index 68249d56..a42c6947 100644 --- a/test/rewrite_clj/zip/removez_test.cljc +++ b/test/rewrite_clj/zip/removez_test.cljc @@ -1,33 +1,33 @@ (ns rewrite-clj.zip.removez-test - (:require [clojure.test :refer [deftest is are]] + (:require [clojure.test :refer [deftest is]] [rewrite-clj.zip :as z])) (deftest t-whitespace-aware-removal - (are [?in ?n ?expected-out ?expected-out-preserve-newline] - (let [elements (->> (z/of-string ?in) - (iterate z/next)) - loc (nth elements ?n)] - (is (= ?expected-out - (-> loc z/remove z/root-string)) - "remove") - (is (= ?expected-out-preserve-newline - (-> loc z/remove-preserve-newline z/root-string)) - "remove-preserve-newline")) - "[1 2 3 4]" 0 "" "" - "[1 2 3 4]" 1 "[2 3 4]" "[2 3 4]" - "[1 2 3 4]" 2 "[1 3 4]" "[1 3 4]" - "[1 2 3 4]" 3 "[1 2 4]" "[1 2 4]" - "[1 2 3 4]" 4 "[1 2 3]" "[1 2 3]" - "[ 1 2 3 4]" 1 "[2 3 4]" "[2 3 4]" - "[1 2 3 4 ]" 4 "[1 2 3]" "[1 2 3]" - "[1]" 1 "[]" "[]" - "[ 1 ]" 1 "[]" "[]" - "[\n \n 1 \n \n]" 1 "[]" "[\n \n\n \n]" - "[\n \n 1 \n \n 2 \n\n]" 2 "[\n \n 1]" "[\n \n 1 \n \n\n\n]" - "[;; c\n1]" 1 "[;; c\n]" "[;; c\n]" - "[1\n;; c\n2]" 1 "[;; c\n2]" "[\n;; c\n2]" - "[1\n;; c\n2]" 2 "[1\n;; c\n]" "[1\n;; c\n]" - "[1\n;; c\n2]" 1 "[;; c\n2]" "[\n;; c\n2]")) + (doseq [[in n expected-out expected-out-preserve-newline] + [["[1 2 3 4]" 0 "" ""] + ["[1 2 3 4]" 1 "[2 3 4]" "[2 3 4]"] + ["[1 2 3 4]" 2 "[1 3 4]" "[1 3 4]"] + ["[1 2 3 4]" 3 "[1 2 4]" "[1 2 4]"] + ["[1 2 3 4]" 4 "[1 2 3]" "[1 2 3]"] + ["[ 1 2 3 4]" 1 "[2 3 4]" "[2 3 4]"] + ["[1 2 3 4 ]" 4 "[1 2 3]" "[1 2 3]"] + ["[1]" 1 "[]" "[]"] + ["[ 1 ]" 1 "[]" "[]"] + ["[\n \n 1 \n \n]" 1 "[]" "[\n \n\n \n]"] + ["[\n \n 1 \n \n 2 \n\n]" 2 "[\n \n 1]" "[\n \n 1 \n \n\n\n]"] + ["[;; c\n1]" 1 "[;; c\n]" "[;; c\n]"] + ["[1\n;; c\n2]" 1 "[;; c\n2]" "[\n;; c\n2]"] + ["[1\n;; c\n2]" 2 "[1\n;; c\n]" "[1\n;; c\n]"] + ["[1\n;; c\n2]" 1 "[;; c\n2]" "[\n;; c\n2]"]]] + (let [elements (->> (z/of-string in) + (iterate z/next)) + loc (nth elements n)] + (is (= expected-out + (-> loc z/remove z/root-string)) + "remove") + (is (= expected-out-preserve-newline + (-> loc z/remove-preserve-newline z/root-string)) + "remove-preserve-newline")))) (deftest t-more-whitespace (let [root (z/of-string @@ -45,45 +45,36 @@ (is (= "; comment\n" (z/root-string loc))))) (deftest t-removing-at-end-of-input-preserves-an-existing-newline-at-end-of-input - (are [?in ?expected-out ?expected-out-preserve-newline] - (let [zloc (->> ?in + (doseq [[in expected-out expected-out-preserve-newline] + [["(def a 1) (del-me b 2)" + "(def a 1)" + "(def a 1)"] + ["(def a 1) (del-me b 2)\n" + "(def a 1)\n" + "(def a 1)\n"] + ["(def a 1)\n(del-me b 2)" + "(def a 1)" + "(def a 1)\n"] + ["(def a 1)\n\n\n(del-me b 2)" + "(def a 1)" + "(def a 1)\n\n\n"] + ["(def a 1) (del-me b 2)\n\n\n" + "(def a 1)\n" + "(def a 1)\n\n\n"] + ["(def a 1)\n\n\n(del-me b 2) \n\n\n" + "(def a 1)\n" + "(def a 1)\n\n\n\n\n\n"] + ["(def a 1)\n\n\n(def b 2)\n\n\n(del-me c 3)" + "(def a 1)\n\n\n(def b 2)" + "(def a 1)\n\n\n(def b 2)\n\n\n"] + ["(def a 1)\n\n\n(def b 2)\n\n\n(del-me c 3) \n \n" + "(def a 1)\n\n\n(def b 2)\n" + "(def a 1)\n\n\n(def b 2)\n\n\n\n \n"] + ["(def a 1)\n\n(del-me b 2)\n;; a comment\n" + "(def a 1)\n;; a comment\n" + "(def a 1)\n\n\n;; a comment\n"]]] + (let [zloc (->> in z/of-string z/rightmost)] - (is (= ?expected-out (->> zloc z/remove z/root-string)) "remove") - (is (= ?expected-out-preserve-newline (->> zloc z/remove-preserve-newline z/root-string)) "remove-preserve-newline") - true) - "(def a 1) (del-me b 2)" - "(def a 1)" - "(def a 1)" - - "(def a 1) (del-me b 2)\n" - "(def a 1)\n" - "(def a 1)\n" - - "(def a 1)\n(del-me b 2)" - "(def a 1)" - "(def a 1)\n" - - "(def a 1)\n\n\n(del-me b 2)" - "(def a 1)" - "(def a 1)\n\n\n" - - "(def a 1) (del-me b 2)\n\n\n" - "(def a 1)\n" - "(def a 1)\n\n\n" - - "(def a 1)\n\n\n(del-me b 2) \n\n\n" - "(def a 1)\n" - "(def a 1)\n\n\n\n\n\n" - - "(def a 1)\n\n\n(def b 2)\n\n\n(del-me c 3)" - "(def a 1)\n\n\n(def b 2)" - "(def a 1)\n\n\n(def b 2)\n\n\n" - - "(def a 1)\n\n\n(def b 2)\n\n\n(del-me c 3) \n \n" - "(def a 1)\n\n\n(def b 2)\n" - "(def a 1)\n\n\n(def b 2)\n\n\n\n \n" - - "(def a 1)\n\n(del-me b 2)\n;; a comment\n" - "(def a 1)\n;; a comment\n" - "(def a 1)\n\n\n;; a comment\n")) + (is (= expected-out (->> zloc z/remove z/root-string)) "remove") + (is (= expected-out-preserve-newline (->> zloc z/remove-preserve-newline z/root-string)) "remove-preserve-newline")))) diff --git a/test/rewrite_clj/zip/seqz_test.cljc b/test/rewrite_clj/zip/seqz_test.cljc index 765a4fbc..95884cf5 100644 --- a/test/rewrite_clj/zip/seqz_test.cljc +++ b/test/rewrite_clj/zip/seqz_test.cljc @@ -1,70 +1,64 @@ (ns rewrite-clj.zip.seqz-test - (:require [clojure.test :refer [deftest testing is are]] + (:require [clojure.test :refer [deftest testing is]] [rewrite-clj.zip :as z])) (deftest t-predicates - (are [?s ?p] - (do - (is (-> ?s z/of-string z/seq?)) - (is (-> ?s z/of-string ?p))) - "[1 2 3]" z/vector? - "{:a 1}" z/map? - "#:my-prefix {:a 1}" z/namespaced-map? - "(+ 2 3)" z/list? - "#{1 2}" z/set?)) + (doseq [[s p] + [["[1 2 3]" z/vector?] + ["{:a 1}" z/map?] + ["#:my-prefix {:a 1}" z/namespaced-map?] + ["(+ 2 3)" z/list?] + ["#{1 2}" z/set?]]] + (is (-> s z/of-string z/seq?)) + (is (-> s z/of-string p)))) (deftest t-seq (testing "map and map-vals are equivalent and update values in maps" - (are [?sin ?sout] - (do - (is (= ?sout (->> ?sin - z/of-string - (z/map-vals #(z/edit % inc)) - z/string))) - (is (= ?sout (->> ?sin - z/of-string - (z/map #(z/edit % inc)) - z/string)))) - "{:a 0, :b 1}" - "{:a 1, :b 2}" - - "#::my-ns-alias{:x 42, ::y 17}" - "#::my-ns-alias{:x 43, ::y 18}")) - + (doseq [[sin sout] + [["{:a 0, :b 1}" + "{:a 1, :b 2}"] + ["#::my-ns-alias{:x 42, ::y 17}" + "#::my-ns-alias{:x 43, ::y 18}"]]] + (is (= sout (->> sin + z/of-string + (z/map-vals #(z/edit % inc)) + z/string))) + (is (= sout (->> sin + z/of-string + (z/map #(z/edit % inc)) + z/string))))) (testing "map-keys works for maps" - (are [?sin ?sout] - (is (= ?sout (->> ?sin - z/of-string - (z/map-keys #(z/edit % name)) - z/string))) - "{:a 0, :b 1}" - "{\"a\" 0, \"b\" 1}")) + (let [sin "{:a 0, :b 1}" + sout "{\"a\" 0, \"b\" 1}"] + (is (= sout (->> sin + z/of-string + (z/map-keys #(z/edit % name)) + z/string))))) (testing "map works for seqs and preserves whitespace" (is (= "[ 5\n6\n7]" (->> "[ 1\n2\n3]" z/of-string (z/map #(z/edit % + 4)) z/string)))) (testing "get on maps and vectors" - (are [?sin ?key ?expected-value] - (is (= ?expected-value (-> ?sin - z/of-string - (z/get ?key) - z/sexpr))) - "{:a 0 :b 1}" :a 0 - "{:a 3, :b 4}" :b 4 - "{:x 10 :y 20}" :z nil - "[1 2 3]" 1 2)) - + (doseq [[sin key expected-value] + [["{:a 0 :b 1}" :a 0] + ["{:a 3, :b 4}" :b 4] + ["{:x 10 :y 20}" :z nil] + ["[1 2 3]" 1 2]]] + (is (= expected-value (-> sin + z/of-string + (z/get key) + z/sexpr))))) (testing "assoc map and vector" - (are [?sin ?key ?value ?sout] - (is (= ?sout (-> ?sin - z/of-string - (z/assoc ?key ?value) - z/string))) - "{:a 0, :b 1}" :a 3 "{:a 3, :b 1}" - "{:a 0, :b 1}" :c 2 "{:a 0, :b 1 :c 2}" - "{}" :x 0 "{:x 0}" - "[1 2 3]" 2 703 "[1 2 703]")) + (doseq [[sin key value sout] + [["{:a 0, :b 1}" :a 3 "{:a 3, :b 1}"] + ["{:a 0, :b 1}" :c 2 "{:a 0, :b 1 :c 2}"] + ["{}" :x 0 "{:x 0}"] + ["[1 2 3]" 2 703 "[1 2 703]"]]] + (is (= sout (-> sin + z/of-string + (z/assoc key value) + z/string))))) (testing "out of bounds assoc on vector should throw" (is (thrown? #?(:clj IndexOutOfBoundsException :cljs js/Error) (-> "[5 10 15]" z/of-string (z/get 5)))))) diff --git a/test/rewrite_clj/zip/walk_test.cljc b/test/rewrite_clj/zip/walk_test.cljc index 77400005..b168bba6 100644 --- a/test/rewrite_clj/zip/walk_test.cljc +++ b/test/rewrite_clj/zip/walk_test.cljc @@ -1,5 +1,5 @@ (ns rewrite-clj.zip.walk-test - (:require [clojure.test :refer [deftest testing is are]] + (:require [clojure.test :refer [deftest testing is]] [rewrite-clj.zip :as z])) (defn- walk-order-tester [walk-fn s] @@ -144,23 +144,20 @@ z/root-string))) (deftest t-zipper-wee-walks - (are [?sample] - (do - (is (= ?sample (walker z/postwalk ?sample)) "postwalk") - (is (= ?sample (walker z/prewalk ?sample)) "prewalk")) - "" - ";; comment" - "1" - "[1]")) + (doseq [sample ["" + ";; comment" + "1" + "[1]"]] + (is (= sample (walker z/postwalk sample)) "postwalk") + (is (= sample (walker z/prewalk sample)) "prewalk"))) #?(:clj (deftest t-zipper-tree-larger-walks - (are [?larger-sample] - (let [s (slurp ?larger-sample)] + (doseq [larger-sample + [;; 11876 lines + "https://raw.githubusercontent.com/clojure/clojurescript/fa4b8d853be08120cb864782e4ea48826b9d757e/src/main/cljs/cljs/core.cljs" + ;; 4745 lines + "https://raw.githubusercontent.com/clojure/clojurescript/fa4b8d853be08120cb864782e4ea48826b9d757e/src/main/clojure/cljs/analyzer.cljc"]] + (let [s (slurp larger-sample)] (is (= s (walker z/postwalk s)) "postwalk") - (is (= s (walker z/prewalk s)) "prewalk")) - - ;; 11876 lines - "https://raw.githubusercontent.com/clojure/clojurescript/fa4b8d853be08120cb864782e4ea48826b9d757e/src/main/cljs/cljs/core.cljs" - ;; 4745 lines - "https://raw.githubusercontent.com/clojure/clojurescript/fa4b8d853be08120cb864782e4ea48826b9d757e/src/main/clojure/cljs/analyzer.cljc"))) + (is (= s (walker z/prewalk s)) "prewalk"))))) diff --git a/test/rewrite_clj/zip/whitespace_test.cljc b/test/rewrite_clj/zip/whitespace_test.cljc index c0c6bf74..e09fbdcc 100644 --- a/test/rewrite_clj/zip/whitespace_test.cljc +++ b/test/rewrite_clj/zip/whitespace_test.cljc @@ -1,5 +1,5 @@ (ns rewrite-clj.zip.whitespace-test - (:require [clojure.test :refer [deftest is testing are]] + (:require [clojure.test :refer [deftest is testing]] [rewrite-clj.node :as node] [rewrite-clj.zip :as z])) @@ -27,18 +27,18 @@ (is (= :token (node/tag n))) (is (= 0 (node/sexpr n))))) (testing "prepending appending spaces" - (are [?left-fn ?right-fn] - (do (let [n (-> loc z/down* z/rightmost* (?left-fn 3))] - (is (= " \n0 1 ;comment" (z/root-string n)))) - (let [n (-> loc z/down* z/rightmost* (?right-fn 3))] - (is (= " \n0 1;comment " (z/root-string n))))) - z/prepend-space z/append-space - z/insert-space-left z/insert-space-right)) + (doseq [[left-fn right-fn] + [[z/prepend-space z/append-space] + [z/insert-space-left z/insert-space-right]]] + (let [n (-> loc z/down* z/rightmost* (left-fn 3))] + (is (= " \n0 1 ;comment" (z/root-string n)))) + (let [n (-> loc z/down* z/rightmost* (right-fn 3))] + (is (= " \n0 1;comment " (z/root-string n)))))) (testing "prepending appending linebreaks" - (are [?left-fn ?right-fn] - (do (let [n (-> loc z/down* z/rightmost* (?left-fn 3))] - (is (= " \n0 1\n\n\n;comment" (z/root-string n)))) - (let [n (-> loc z/down* z/rightmost* (?right-fn 3))] - (is (= " \n0 1;comment\n\n\n" (z/root-string n))))) - z/prepend-newline z/append-newline - z/insert-newline-left z/insert-newline-right)))) + (doseq [[left-fn right-fn] + [[z/prepend-newline z/append-newline] + [z/insert-newline-left z/insert-newline-right]]] + (let [n (-> loc z/down* z/rightmost* (left-fn 3))] + (is (= " \n0 1\n\n\n;comment" (z/root-string n)))) + (let [n (-> loc z/down* z/rightmost* (right-fn 3))] + (is (= " \n0 1;comment\n\n\n" (z/root-string n)))))))) diff --git a/test/rewrite_clj/zip_test.cljc b/test/rewrite_clj/zip_test.cljc index d4ceb6db..a02912f0 100644 --- a/test/rewrite_clj/zip_test.cljc +++ b/test/rewrite_clj/zip_test.cljc @@ -1,7 +1,7 @@ (ns rewrite-clj.zip-test "This test namespace originated from rewrite-cljs." (:require [clojure.string :as string] - [clojure.test :refer [deftest testing is are]] + [clojure.test :refer [deftest testing is]] [rewrite-clj.node :as n] [rewrite-clj.zip :as z])) @@ -45,20 +45,20 @@ (let [[start end] (z/position-span zloc)] (assoc acc start {:node (z/node zloc) :end-pos end}))) {}))] - (are [?pos ?end ?t ?s ?sexpr] - (let [{:keys [node end-pos]} (positions ?pos)] - (is (= ?t (n/tag node))) - (is (= ?s (n/string node))) - (is (= ?sexpr (n/sexpr node))) - (is (= ?end end-pos))) - [1 1] [3 15] :list s '(defn f [x] (println x)) - [1 2] [1 6] :token "defn" 'defn - [1 7] [1 8] :token "f" 'f - [2 3] [2 6] :vector "[x]" '[x] - [2 4] [2 5] :token "x" 'x - [3 3] [3 14] :list "(println x)" '(println x) - [3 4] [3 11] :token "println" 'println - [3 12] [3 13] :token "x" 'x)) + (doseq [[pos end t s sexpr] + [[[1 1] [3 15] :list s '(defn f [x] (println x))] + [[1 2] [1 6] :token "defn" 'defn] + [[1 7] [1 8] :token "f" 'f] + [[2 3] [2 6] :vector "[x]" '[x]] + [[2 4] [2 5] :token "x" 'x] + [[3 3] [3 14] :list "(println x)" '(println x)] + [[3 4] [3 11] :token "println" 'println] + [[3 12] [3 13] :token "x" 'x]]] + (let [{:keys [node end-pos]} (positions pos)] + (is (= t (n/tag node))) + (is (= s (n/string node))) + (is (= sexpr (n/sexpr node))) + (is (= end end-pos))))) ;; root node (let [s (str ;1234567890