summaryrefslogtreecommitdiff
path: root/tests/macros.fnl
diff options
context:
space:
mode:
authorAndrey Listopadov <andreyorst@gmail.com>2022-08-21 18:03:25 +0000
committerAndrey Listopadov <andreyorst@gmail.com>2022-08-21 18:03:25 +0000
commit9bbe5ddf93c7c8b17a73318bc89dd1330f4f3f59 (patch)
tree7d358804b1bcb5ab4f1368d2d60eb2993f4de926 /tests/macros.fnl
parent58f91092e2831421aa88be36e9dfa6dd153fd212 (diff)
release v1.0.0
Almost complete rewrite of the library, complete with lazy sequences, immutable tables, transients, transducers, better equality semantics, and more correct code generation in macros.
Diffstat (limited to 'tests/macros.fnl')
-rw-r--r--tests/macros.fnl165
1 files changed, 22 insertions, 143 deletions
diff --git a/tests/macros.fnl b/tests/macros.fnl
index b142d4e..66cd158 100644
--- a/tests/macros.fnl
+++ b/tests/macros.fnl
@@ -1,63 +1,10 @@
(require-macros :fennel-test)
(require-macros :init-macros)
+(local (meta? fennel) (pcall require :fennel))
-(deftest test-into
- (testing "into"
- (assert-eq (into [] nil) [])
- (assert-eq (into nil nil) nil)
- (assert-eq (into nil [1 2 3]) [1 2 3])
-
- (assert-eq (into [] []) [])
- (assert-eq (into [1 2 3] []) [1 2 3])
- (assert-eq (into [1 2 3] [4 5 6]) [1 2 3 4 5 6])
-
- (assert-eq (into {} {}) {})
- (assert-eq (into {:a 1} {}) {:a 1})
- (assert-eq (into {:a 1} {:b 2}) {:a 1 :b 2})
-
- ;; different bodies are being used at compile time so worth testing
- (assert-eq (into [] {}) [])
- (assert-eq (into {} []) [])
- (assert-eq (. (getmetatable (into [] {})) :cljlib/type) :seq)
- (assert-eq (. (getmetatable (into {} [])) :cljlib/type) :table)
- (let [a []] (assert-eq (. (getmetatable (into a a)) :cljlib/type) :seq))
-
- ;; can't transform table with more than one key-value pair, as order
- ;; is undeterminitive
- (assert-eq (into [] {:a 1}) [[:a 1]])
- (assert-eq (into [[:b 2]] {:a 1}) [[:b 2] [:a 1]])
- (assert-eq (into [[:c 3]] {}) [[:c 3]])
-
- (assert-eq (into {} [[:c 3] [:a 1] [:b 2]]) {:a 1 :b 2 :c 3})
- (assert-eq (into {:d 4} [[:c 3] [:a 1] [:b 2]]) {:a 1 :b 2 :c 3 :d 4})
- (assert-eq (into {:a 0 :b 0 :c 0} [[:c 3] [:a 1] [:b 2]]) {:a 1 :b 2 :c 3})
-
- (let [a (fn [] {:a 1})
- b (fn [] [[:b 2]])]
- (assert-eq (into (a) (b)) {:a 1 :b 2})
- (assert-eq (into (b) (a)) [[:b 2] [:a 1]])
- (let [c []]
- (assert-eq (into c (b)) [[:b 2]]))
- (let [c []]
- (assert-eq (into c (a)) [[:a 1]]))
- (let [c []]
- (assert-eq (into (b) c) (b))
- (assert-eq (into (a) c) (a))))
-
- (let [a {}
- b []]
- (assert-eq (into a [1 2 3]) [1 2 3])
- (assert-eq (into b [1 2 3]) [1 2 3]))
- (let [a {}
- b []]
- (assert-eq (into b {:a 1}) [[:a 1]]))
-
- (let [a {}
- b []]
- (assert-eq (into a "vaiv") ["v" "a" "i" "v"])
- (when _G.utf8 (assert-eq (into b "ваыв") ["в" "а" "ы" "в"])))
- (assert-eq (into [] "vaiv") ["v" "a" "i" "v"])
- (when _G.utf8 (assert-eq (into [] "ваыв") ["в" "а" "ы" "в"]))))
+(fn meta [x]
+ {:fnl/docstring (fennel.metadata:get x :fnl/docstring)
+ :fnl/arglist (fennel.metadata:get x :fnl/arglist)})
(deftest test-let-variants
(testing "when-let"
@@ -125,13 +72,6 @@
(defmethod f :my-default [_] 42)
(assert-eq (f 10) 42))
-
- (testing "defmulti docstring"
- (defmulti f "documentation" (fn [x] x))
- (assert-eq (meta f) {:fnl/docstring "documentation"})
- (defmulti g "documentation" (fn [x] x) :default 0)
- (assert-eq (meta g) {:fnl/docstring "documentation"}))
-
(testing "defmulti with multiple arity"
(defmulti f (fn* ([x] x) ([x y] [x y])))
(defmethod f :default ([_] :def) ([_ _] :def2))
@@ -143,67 +83,6 @@
(assert-eq (f :4) :42)
(assert-eq (f :4 :2) 42)))
-(deftest test-def-macros
- (testing "def"
- (def {:mutable true} a 10)
- (assert-eq a 10)
- (set a 20)
- (assert-eq a 20)
- (def a {})
- (assert-eq a {})
- (def a.b 10)
- (assert-eq a.b 10)
- (assert-eq b 10)
- (def :mutable c 10)
- (set c 15)
- (assert-eq c 15))
-
- (testing "defonce"
- (defonce {:mutable true} a 10)
- (assert-eq a 10)
- (defonce a {})
- (assert-eq a 10)
- (defonce b {})
- (defonce b.a 10)
- (assert-eq b.a 10)
- (assert-eq a 10)))
-
-(deftest test-meta
- (testing "with-meta"
- (assert-eq (meta (with-meta :a {:k :v})) {:k :v}))
-
- (testing "def meta"
- (def {:doc "x"} x 10)
- (assert-eq (meta x) {:fnl/docstring "x"})
- (def {:doc "x" :mutable true} x 10)
- (assert-eq (meta x) {:fnl/docstring "x"}))
-
- (testing "defonce meta table"
- (defonce {:doc "x"} x 10)
- (assert-eq (meta x) {:fnl/docstring "x"})
- (defonce {:doc "y"} x 20)
- (assert-eq (meta x) {:fnl/docstring "x"})
- (defonce {:doc "y" :mutable true} y 20)
- (assert-eq (meta y) {:fnl/docstring "y"})))
-
-(deftest test-empty
- (testing "empty map"
- (assert-eq (empty {}) {})
- (assert-eq (getmetatable (empty {})) {:cljlib/type :table})
- (let [a {:a 1 :b 2}]
- (assert-eq (empty a) {})
- (assert-eq (getmetatable (empty a)) {:cljlib/type :table}))
- (let [a {}]
- (assert-eq (empty a) [])
- (assert-eq (getmetatable (empty a)) {:cljlib/type :empty})))
-
- (testing "empty seq"
- (assert-eq (empty []) {})
- (assert-eq (getmetatable (empty [])) {:cljlib/type :seq})
- (let [a [:a 1 :b 2]]
- (assert-eq (empty a) [])
- (assert-eq (getmetatable (empty a)) {:cljlib/type :seq}))))
-
(deftest test-try
(testing "try"
(assert-eq (try (+ 1 2 3)) 6)
@@ -213,35 +92,35 @@
(testing "catch-all"
(assert-eq (try
- (error "10")
- (catch _ :pass))
+ (error "10")
+ (catch _ :pass))
:pass)
(assert-eq (try
- (error [10])
- (catch err err))
+ (error [10])
+ (catch err err))
[10]))
(testing "finally"
(let [tbl []]
(try
- (try
- (finally (table.insert tbl 1)))
- (try
- (error 10)
- (catch _ (table.insert tbl 2))
- (finally (table.insert tbl 3)))
- (try
- (error 20)
- (finally (table.insert tbl 4)))
- (catch _ (table.insert tbl 5))
- (catch 20 (table.insert tbl 6))
- (finally (table.insert tbl 7)))
+ (try
+ (finally (table.insert tbl 1)))
+ (try
+ (error 10)
+ (catch _ (table.insert tbl 2))
+ (finally (table.insert tbl 3)))
+ (try
+ (error 20)
+ (finally (table.insert tbl 4)))
+ (catch _ (table.insert tbl 5))
+ (catch 20 (table.insert tbl 6))
+ (finally (table.insert tbl 7)))
(assert-eq tbl [1 2 3 4 5 7])))
(testing "runtime error"
(assert-eq 0 (try
- (/ 1 nil)
- (catch _ 0))))
+ (/ 1 nil)
+ (catch _ 0))))
(testing "multi-value results"
(assert-eq 3 (select :# (try (values 1 2 3))))