Update rewrite-clj tests
This commit is contained in:
parent
ad4935077d
commit
6467316dc8
1 changed files with 70 additions and 72 deletions
|
|
@ -1,67 +1,77 @@
|
||||||
(ns rewrite-clj.node.coercer-test
|
(ns rewrite-clj.node.coercer-test
|
||||||
(:require [clojure.test :refer [deftest testing is are]]
|
(:require [clojure.test :refer [deftest testing is are]]
|
||||||
[rewrite-clj.node :as node]
|
[rewrite-clj.node :as node]
|
||||||
|
[rewrite-clj.node.protocols :as protocols]
|
||||||
|
[rewrite-clj.node.regex :as regex]
|
||||||
[rewrite-clj.parser :as p]))
|
[rewrite-clj.parser :as p]))
|
||||||
|
|
||||||
(deftest t-sexpr->node->sexpr-roundtrip
|
(deftest t-sexpr->node->sexpr-roundtrip
|
||||||
(testing "simple cases roundtrip"
|
(testing "simple cases roundtrip"
|
||||||
(are [?sexpr expected-tag ]
|
(are [?sexpr expected-tag expected-type]
|
||||||
(let [n (node/coerce ?sexpr)]
|
(let [n (node/coerce ?sexpr)]
|
||||||
(is (node/node? n))
|
(is (node/node? n))
|
||||||
(is (= ?sexpr (node/sexpr n)))
|
(is (= ?sexpr (node/sexpr n)))
|
||||||
(is (string? (node/string n)))
|
(is (string? (node/string n)))
|
||||||
(is (= expected-tag (node/tag n)) "tag")
|
(is (= expected-tag (node/tag n)) "tag")
|
||||||
|
#_(is (= expected-type (protocols/node-type n)) "node-type")
|
||||||
(is (not (meta n)))
|
(is (not (meta n)))
|
||||||
(is (= (type ?sexpr) (type (node/sexpr n)))))
|
(if (seq? ?sexpr)
|
||||||
|
(is (seq? (node/sexpr n)))
|
||||||
|
(is (= (type (node/sexpr n)) (type ?sexpr) ))))
|
||||||
|
|
||||||
;; numbers
|
;; numbers
|
||||||
|
|
||||||
;; note that we do have an integer-node, but rewrite-clj never parses to it
|
;; note that we do have an integer-node, but rewrite-clj never parses to it
|
||||||
;; so we never coerce to it either
|
;; so we never coerce to it either
|
||||||
3 :token ;;:token
|
3 :token :token
|
||||||
3N :token ;;:token
|
3N :token :token
|
||||||
3.14 :token ;;:token
|
3.14 :token :token
|
||||||
3.14M :token ;;:token
|
3.14M :token :token
|
||||||
3e14 :token ;;:token
|
3e14 :token :token
|
||||||
|
|
||||||
;; ratios are not valid in cljs
|
;; ratios are not valid in cljs
|
||||||
#?@(:clj [3/4 :token ;;:token
|
#?@(:clj [3/4 :token :token])
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
;; symbol/keyword/string/...
|
;; symbol/keyword/string/...
|
||||||
'symbol :token ;;:symbol
|
'symbol :token :symbol
|
||||||
'namespace/symbol :token ;;:symbol
|
'namespace/symbol :token :symbol
|
||||||
:keyword :token ;;:keyword
|
:keyword :token :keyword
|
||||||
:1.5.1 :token ;;:keyword
|
:1.5.1 :token :keyword
|
||||||
::keyword :token ;;:keyword
|
::keyword :token :keyword
|
||||||
::1.5.1 :token ;;:keyword
|
::1.5.1 :token :keyword
|
||||||
:namespace/keyword :token ;;:keyword
|
:namespace/keyword :token :keyword
|
||||||
|
|
||||||
"" :token ;;:string
|
"" :token :string
|
||||||
"hello, over there!" :token ;;:string
|
"hello, over there!" :token :string
|
||||||
"multi\nline" :multi-line ;;:string
|
"multi\nline" :token :string
|
||||||
" " :token ;;:string
|
" " :token :string
|
||||||
"\n" :multi-line ;;:string
|
"\n" :token :string
|
||||||
"\n\n" :multi-line ;;:string
|
"\n\n" :token :string
|
||||||
"," :token ;;:string
|
"," :token :string
|
||||||
|
"inner\"quote" :token :string
|
||||||
|
"\\s+" :token :string
|
||||||
|
|
||||||
;; seqs
|
;; seqs
|
||||||
[] :vector ;;:seq
|
[] :vector :seq
|
||||||
[1 2 3] :vector ;;:seq
|
[1 2 3] :vector :seq
|
||||||
() :list ;;:seq
|
() :list :seq
|
||||||
'() :list ;;:seq
|
'() :list :seq
|
||||||
(list 1 2 3) :list ;;:seq
|
(list 1 2 3) :list :seq
|
||||||
#{} :set ;;:seq
|
#{} :set :seq
|
||||||
#{1 2 3} :set ;;:seq
|
#{1 2 3} :set :seq
|
||||||
|
(cons 1 [2 3]) :list :seq
|
||||||
|
(lazy-seq [1 2 3]) :list :seq
|
||||||
|
|
||||||
;; date
|
;; date
|
||||||
#inst "2014-11-26T00:05:23" :token ;; :token
|
#inst "2014-11-26T00:05:23" :token :token))
|
||||||
))
|
(testing "multi-line string newline variants are preserved"
|
||||||
(testing "multi-line string newline variants are normalized"
|
|
||||||
(let [s "hey\nyou\rover\r\nthere"
|
(let [s "hey\nyou\rover\r\nthere"
|
||||||
n (node/coerce s)]
|
n (node/coerce s)]
|
||||||
(is (= "hey\nyou\nover\nthere" (node/sexpr n))))))
|
(is (= s (node/sexpr n)))))
|
||||||
|
(testing "coerce string roundtrip"
|
||||||
|
(is (= "\"hey \\\" man\"" (-> "hey \" man" node/coerce node/string))))
|
||||||
|
(testing "coerce string equals parsed string"
|
||||||
|
(is (= (p/parse-string "\"hello\"") (node/coerce "hello")))))
|
||||||
|
|
||||||
(deftest
|
(deftest
|
||||||
t-quoted-list-reader-location-metadata-elided
|
t-quoted-list-reader-location-metadata-elided
|
||||||
|
|
@ -81,7 +91,7 @@
|
||||||
(let [n (node/coerce ?sexpr)]
|
(let [n (node/coerce ?sexpr)]
|
||||||
(is (node/node? n))
|
(is (node/node? n))
|
||||||
(is (= :map (node/tag n)))
|
(is (= :map (node/tag n)))
|
||||||
;; (is (= :seq protocols/node-type n))
|
#_(is (= :seq (protocols/node-type n)))
|
||||||
(is (string? (node/string n)))
|
(is (string? (node/string n)))
|
||||||
(is (= ?sexpr (node/sexpr n)))
|
(is (= ?sexpr (node/sexpr n)))
|
||||||
;; we do not restore to original map (hash-map or array-map),
|
;; we do not restore to original map (hash-map or array-map),
|
||||||
|
|
@ -94,27 +104,16 @@
|
||||||
(array-map)
|
(array-map)
|
||||||
(array-map :d 4 :e 5)))
|
(array-map :d 4 :e 5)))
|
||||||
|
|
||||||
(deftest t-namespaced-maps-coerce-to-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)))
|
|
||||||
(is (map? (node/sexpr n))))
|
|
||||||
#:prefix {:a 1 :b 2}
|
|
||||||
#::{:c 3 :d 4}
|
|
||||||
#::p{:e 5 :f 6}))
|
|
||||||
|
|
||||||
(deftest t-sexpr->node->sexpr-roundtrip-for-regex
|
(deftest t-sexpr->node->sexpr-roundtrip-for-regex
|
||||||
(are [?in]
|
(are [?in]
|
||||||
(let [n (node/coerce ?in)]
|
(let [n (node/coerce ?in)]
|
||||||
(is (node/node? n))
|
(is (node/node? n))
|
||||||
(is (= :regex (node/tag n)))
|
(is (= :regex (node/tag n)))
|
||||||
;; (is (= :regex (protocols/node-type n)))
|
#_(is (= :regex (protocols/node-type n)))
|
||||||
(is (string? (node/string n)))
|
(is (string? (node/string n)))
|
||||||
#_(is (= (list 're-pattern (regex/pattern-string-for-regex ?in))
|
(is (= (list 're-pattern (regex/pattern-string-for-regex ?in))
|
||||||
(node/sexpr n))))
|
(node/sexpr n))))
|
||||||
#"abc"
|
#"abc"
|
||||||
#"a\nb\nc"
|
#"a\nb\nc"
|
||||||
|
|
@ -127,7 +126,7 @@
|
||||||
(let [n (node/coerce #'identity)]
|
(let [n (node/coerce #'identity)]
|
||||||
(is (node/node? n))
|
(is (node/node? n))
|
||||||
(is (= :var (node/tag n)))
|
(is (= :var (node/tag n)))
|
||||||
#_(is (= :reader (protocols/node-type n)))
|
(is (= :reader (protocols/node-type n)))
|
||||||
(is (= '(var #?(:clj clojure.core/identity :cljs cljs.core/identity)) (node/sexpr n)))))
|
(is (= '(var #?(:clj clojure.core/identity :cljs cljs.core/identity)) (node/sexpr n)))))
|
||||||
|
|
||||||
(deftest t-nil
|
(deftest t-nil
|
||||||
|
|
@ -153,31 +152,30 @@
|
||||||
(deftest t-nodes-coerce-to-themselves
|
(deftest t-nodes-coerce-to-themselves
|
||||||
(testing "parsed nodes"
|
(testing "parsed nodes"
|
||||||
;; lean on the parser to create node structures
|
;; lean on the parser to create node structures
|
||||||
(are [?s ?tag #_?type]
|
(are [?s ?tag ?type]
|
||||||
(let [n (p/parse-string ?s)]
|
(let [n (p/parse-string ?s)]
|
||||||
(is (= n (node/coerce n)))
|
(is (= n (node/coerce n)))
|
||||||
(is (= ?tag (node/tag n)))
|
(is (= ?tag (node/tag n)))
|
||||||
#_(is (= ?type (protocols/node-type n))))
|
#_(is (= ?type (protocols/node-type n))))
|
||||||
";; comment" :comment ;;:comment
|
";; comment" :comment :comment
|
||||||
"#! comment" :comment ;;:comment
|
"#! comment" :comment :comment
|
||||||
"#(+ 1 %)" :fn ;;:fn
|
"#(+ 1 %)" :fn :fn
|
||||||
":my-kw" :token ;;:keyword
|
":my-kw" :token :keyword
|
||||||
"^:m1 [1 2 3]" :meta ;;:meta
|
"^:m1 [1 2 3]" :meta :meta
|
||||||
"#:p1{:a 1 :b 2}" :namespaced-map ;;:namespaced-map
|
"#:p1{:a 1 :b 2}" :namespaced-map :namespaced-map
|
||||||
"'a" :quote ;;:quote
|
"'a" :quote :quote
|
||||||
"#'var" :var ;;:reader
|
"#'var" :var :reader
|
||||||
"#=eval" :eval ;;:reader
|
"#=eval" :eval :reader
|
||||||
"@deref" :deref ;;:deref
|
"@deref" :deref :deref
|
||||||
"#mymacro 44" :reader-macro ;;:reader-macro
|
"#mymacro 44" :reader-macro :reader-macro
|
||||||
"#\"regex\"" :regex ;;:regex
|
"#\"regex\"" :regex :regex
|
||||||
"[1 2 3]" :vector ;;:seq
|
"[1 2 3]" :vector :seq
|
||||||
"42" :token ;;:token
|
"42" :token :token
|
||||||
"sym" :token ;;:symbol
|
"sym" :token :symbol
|
||||||
"#_ 99" :uneval ;;:uneval
|
"#_ 99" :uneval :uneval
|
||||||
" " :whitespace ;;:whitespace
|
" " :whitespace :whitespace
|
||||||
"," :comma ;;:comma
|
"," :comma :comma
|
||||||
"\n" :newline ;;:newline
|
"\n" :newline :newline))
|
||||||
))
|
|
||||||
(testing "parsed forms nodes"
|
(testing "parsed forms nodes"
|
||||||
(let [n (p/parse-string-all "(def a 1)")]
|
(let [n (p/parse-string-all "(def a 1)")]
|
||||||
(is (= n (node/coerce n)))
|
(is (= n (node/coerce n)))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue