diff options
| author | Andrey Orst <andreyorst@gmail.com> | 2020-11-13 22:54:12 +0300 |
|---|---|---|
| committer | Andrey Orst <andreyorst@gmail.com> | 2020-11-13 22:54:12 +0300 |
| commit | 1b309ac016d806d2f9b44540ed5020f5c60c4256 (patch) | |
| tree | fca18ab2e0153a1462da473cd2fe5c9cc0de6a69 /tests | |
| parent | 8493fa29b1848bf93e899e8930c6e8c1c723eece (diff) | |
fix(core): refactoring
- Rename `fn*` to `defn`, `fn&` to `fn+`.
- Do not use `fn+` in the core at all, provide it for convenience.
- Fix bug in `filter` due to incorrect `cons` implementation.
- Update `seq` and `eq` functions in macros
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/core.fnl | 18 | ||||
| -rw-r--r-- | tests/fn.fnl | 20 | ||||
| -rw-r--r-- | tests/macros.fnl | 2 | ||||
| -rw-r--r-- | tests/test.fnl | 25 |
4 files changed, 33 insertions, 32 deletions
diff --git a/tests/core.fnl b/tests/core.fnl index 992627d..ae7b3b4 100644 --- a/tests/core.fnl +++ b/tests/core.fnl @@ -54,12 +54,6 @@ (assert-ne a b)) (assert-eq [1 2 3] {1 1 2 2 3 3}) - - ;; TODO: decide if this is right or not. Looking from `seq' - ;; perspective, it is correct, as `(seq {4 1})' and `(seq [nil nil - ;; nil 1])' both yield `{4 1}'. From Lua's point this is not the - ;; same thing, for example because the sizes of these tables are - ;; different. (assert-eq {4 1} [nil nil nil 1])) (testing "eq metadata preservation" @@ -214,7 +208,7 @@ (assert-eq (table.concat (mapv string.upper "vaiv")) "VAIV")) (testing "reduce" - (fn* add + (defn add ([] 0) ([a] a) ([a b] (+ a b)) @@ -241,12 +235,12 @@ (fn [result input] (reducing result (f input))))) - (fn reduce- [f init [x & tbl]] - (if x (reduce- f (f init x) tbl) init)) + (fn -reduce [f init [x & tbl]] + (if x (-reduce f (f init x) tbl) init)) - (assert-eq (reduce add (range 10)) (reduce- add 0 (range 10))) + (assert-eq (reduce add (range 10)) (-reduce add 0 (range 10))) (assert-eq (reduce ((mapping inc) add) 0 (range 10)) - (reduce- ((mapping inc) add) 0 (range 10)))) + (-reduce ((mapping inc) add) 0 (range 10)))) (testing "filter" (assert-not (pcall filter)) @@ -346,7 +340,7 @@ (assert* ((complement #(= $1 $2)) 1 2))) (testing "apply" - (fn* add + (defn add ([x] x) ([x y] (+ x y)) ([x y & zs] diff --git a/tests/fn.fnl b/tests/fn.fnl index e508541..c7a3aa9 100644 --- a/tests/fn.fnl +++ b/tests/fn.fnl @@ -1,21 +1,21 @@ (require-macros :tests.test) (require-macros :cljlib-macros) -(deftest fn* - (testing "fn* meta" - (fn* f +(deftest defn + (testing "defn meta" + (defn f "docstring" [x] x) (assert-eq (meta f) (when-meta {:fnl/docstring "docstring" :fnl/arglist ["x"]})) - (fn* f + (defn f "docstring" ([x] x)) (assert-eq (meta f) (when-meta {:fnl/docstring "docstring" :fnl/arglist ["x"]})) - (fn* f + (defn f "docstring" ([x] x) ([x y] (+ x y))) @@ -23,7 +23,7 @@ :fnl/arglist ["\n [x]" "\n [x y]"]})) - (fn* f + (defn f "docstring" ([x] x) ([x y] (+ x y)) @@ -33,12 +33,12 @@ "\n [x y]" "\n [x y & z]"]})))) -(deftest fn& - (testing "fn& meta" - (fn& f "docstring" [x] x) +(deftest fn+ + (testing "fn+ meta" + (fn+ f "docstring" [x] x) (assert-eq (meta f) (when-meta {:fnl/docstring "docstring" :fnl/arglist ["x"]})) - (fn& f "docstring" [...] [...]) + (fn+ f "docstring" [...] [...]) (assert-eq (meta f) (when-meta {:fnl/docstring "docstring" :fnl/arglist ["..."]})))) diff --git a/tests/macros.fnl b/tests/macros.fnl index a9b41fe..402e42d 100644 --- a/tests/macros.fnl +++ b/tests/macros.fnl @@ -133,7 +133,7 @@ (assert-eq (meta g) (when-meta {:fnl/docstring "documentation"}))) (testing "defmulti with multiple arity" - (defmulti f (fn* ([x] x) ([x y] [x y]))) + (defmulti f (defn ([x] x) ([x y] [x y]))) (defmethod f :default ([_] :def) ([_ _] :def2)) (defmethod f :4 ([x] (.. x :2))) (defmethod f [:4 :2] ([x y] 42)) diff --git a/tests/test.fnl b/tests/test.fnl index d98e1fa..5dc40c1 100644 --- a/tests/test.fnl +++ b/tests/test.fnl @@ -1,10 +1,17 @@ (local test {}) (fn eq-fn [] - `(fn eq# [a# b#] - (if (and (= (type a#) :table) (= (type b#) :table)) - (let [oldmeta# (getmetatable b#)] - (setmetatable b# {:__index (fn [tbl# key#] + "Returns recursive equality function. + +This function is able to compare tables of any depth, even if one of +the tables uses tables as keys." + `(fn eq# [left# right#] + (if (and (= (type left#) :table) (= (type right#) :table)) + (let [oldmeta# (getmetatable right#)] + ;; In case if we'll get something like + ;; (eq {[1 2 3] {:a [1 2 3]}} {[1 2 3] {:a [1 2 3]}}) + ;; we have to do even deeper search + (setmetatable right# {:__index (fn [tbl# key#] (var res# nil) (each [k# v# (pairs tbl#)] (when (eq# k# key#) @@ -12,17 +19,17 @@ (lua :break))) res#)}) (var [res# count-a# count-b#] [true 0 0]) - (each [k# v# (pairs a#)] - (set res# (eq# v# (. b# k#))) + (each [k# v# (pairs left#)] + (set res# (eq# v# (. right# k#))) (set count-a# (+ count-a# 1)) (when (not res#) (lua :break))) (when res# - (each [_# _# (pairs b#)] + (each [_# _# (pairs right#)] (set count-b# (+ count-b# 1))) (set res# (= count-a# count-b#))) - (setmetatable b# oldmeta#) + (setmetatable right# oldmeta#) res#) - (= a# b#)))) + (= left# right#)))) (fn test.assert-eq [expr1 expr2 msg] |