Add specter tests

This commit is contained in:
Michiel Borkent 2022-03-18 19:35:44 +01:00
parent bd3adf029e
commit 7bfab37077
9 changed files with 1887 additions and 5 deletions

View file

@ -12,12 +12,9 @@ This release improves compatibility with several libraries: [loom](https://githu
To use specter in babashka, use the following coordinates:
``` clojure
{:deps {com.rpl/specter {:git/url "https://github.com/borkdude/specter"
:git/sha "8ba809a2cd35d3b6f8c5287e6bd3b4e06e42f6dc"}}}
{:deps {com.rpl/specter {:mvn/version "1.1.4"}}}
```
Hopefully the compatibility commit can be upstreamed back into specter at some point.
- Add `clojure.data.priority-map` as built-in library - this makes babashka compatible with [aysylu/loom](https://github.com/aysylu/loom)
- Add part of `clojure.tools.reader.reader-types` to support [hugsql.core](https://www.hugsql.org)
- [#1204](https://github.com/babashka/babashka/issues/1204) add property `babashka.config` to reflect `bb.edn` location ([@mknoszlig](https://github.com/mknoszlig))

View file

@ -129,7 +129,8 @@
cli-matic/cli-matic {:git/url "https://github.com/l3nz/cli-matic.git", :git/sha "9cd53ba7336363e3d06650dbad413b6f8b06e471"}
aysylu/loom {:mvn/version "1.0.2"}
com.layerware/hugsql-core {:mvn/version "0.5.1"}
com.github.seancorfield/expectations {:mvn/version "2.0.157"}}
com.github.seancorfield/expectations {:mvn/version "2.0.157"}
com.rpl/specter {:mvn/version "1.1.4"}}
:classpath-overrides {org.clojure/clojure nil
org.clojure/spec.alpha nil}}
:clj-nvd

View file

@ -21,6 +21,7 @@ com.github.seancorfield/expectations,https://github.com/clojure-expectations/clo
com.github.seancorfield/honeysql,https://github.com/seancorfield/honeysql
com.grammarly/omniconf,https://github.com/grammarly/omniconf
com.layerware/hugsql-core,
com.rpl/specter,https://github.com/redplanetlabs/specter
com.stuartsierra/component,https://github.com/stuartsierra/component
com.stuartsierra/dependency,https://github.com/stuartsierra/dependency
com.wsscode/cljc-misc,https://github.com/wilkerlucio/cljc-misc

1 maven-name git-url
21 com.github.seancorfield/honeysql https://github.com/seancorfield/honeysql
22 com.grammarly/omniconf https://github.com/grammarly/omniconf
23 com.layerware/hugsql-core
24 com.rpl/specter https://github.com/redplanetlabs/specter
25 com.stuartsierra/component https://github.com/stuartsierra/component
26 com.stuartsierra/dependency https://github.com/stuartsierra/dependency
27 com.wsscode/cljc-misc https://github.com/wilkerlucio/cljc-misc

View file

@ -111,4 +111,5 @@
aysylu/loom {:git-url "https://github.com/aysylu/loom", :test-namespaces (loom.test.network-simplex loom.test.label loom.test.alg-generic loom.test.compliance-tester loom.test.flow loom.test.alg loom.test.attr loom.test.graph loom.test.derived), :git-sha "d458f0c0dee9021983c64381b90a470f0178cc8e"}
com.layerware/hugsql-core {:test-namespaces (hugsql.babashka-test)}
com.github.seancorfield/expectations {:git-url "https://github.com/clojure-expectations/clojure-test", :test-namespaces (expectations.clojure.test-test), :git-sha "b30fefd97d9eb7d1f47e06956521f354cb926b03"}
com.rpl/specter {:git-url "https://github.com/redplanetlabs/specter", :test-namespaces (com.rpl.specter.cljs-test-helpers com.rpl.specter.test-helpers com.rpl.specter.core-test com.rpl.specter.zipper-test), :git-sha "67e86806020b9d02fbca8cdb1efad3002fc81a32"}
}

View file

@ -0,0 +1,13 @@
(ns com.rpl.specter.cljs-test-helpers)
;; it seems like gen/bind and gen/return are a monad (hence the names)
(defmacro for-all+ [bindings & body]
(let [parts (partition 2 bindings)
vars (vec (map first parts))
genned (reduce
(fn [curr [v code]]
`(clojure.test.check.generators/bind ~code (fn [~v] ~curr)))
`(clojure.test.check.generators/return ~vars)
(reverse parts))]
`(clojure.test.check.properties/for-all [~vars ~genned]
~@body)))

View file

@ -0,0 +1,7 @@
(ns com.rpl.specter.cljs-test-runner
(:require [doo.runner :refer-macros [doo-tests]]
[com.rpl.specter.core-test]
[com.rpl.specter.zipper-test]))
(doo-tests 'com.rpl.specter.core-test
'com.rpl.specter.zipper-test)

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,36 @@
(ns com.rpl.specter.test-helpers
(:require [clojure.test.check
[generators :as gen]
[properties :as prop]]
[clojure.test])
(:use [com.rpl.specter :only [select transform]]
[com.rpl.specter :only [select* transform*]]))
;; it seems like gen/bind and gen/return are a monad (hence the names)
;; this is only for clj (cljs version in different file)
(defmacro for-all+ [bindings & body]
(let [parts (partition 2 bindings)
vars (vec (map first parts))
genned (reduce
(fn [curr [v code]]
`(gen/bind ~code (fn [~v] ~curr)))
`(gen/return ~vars)
(reverse parts))]
`(prop/for-all [~vars ~genned]
~@body)))
(defmacro ic-test [params-decl apath transform-fn data params]
(let [platform (if (contains? &env :locals) :cljs :clj)
is-sym (if (= platform :clj) 'clojure.test/is 'cljs.test/is)]
`(let [icfnsel# (fn [~@params-decl] (select ~apath ~data))
icfntran# (fn [~@params-decl] (transform ~apath ~transform-fn ~data))
regfnsel# (fn [~@params-decl] (select* ~apath ~data))
regfntran# (fn [~@params-decl] (transform* ~apath ~transform-fn ~data))
params# (if (empty? ~params) [[]] ~params)]
(dotimes [_# 3]
(doseq [ps# params#]
(~is-sym (= (apply icfnsel# ps#) (apply regfnsel# ps#)))
(~is-sym (= (apply icfntran# ps#) (apply regfntran# ps#))))))))

View file

@ -0,0 +1,122 @@
(ns com.rpl.specter.zipper-test
#?(:cljs (:require-macros
[cljs.test :refer [is deftest]]
[clojure.test.check.clojure-test :refer [defspec]]
[com.rpl.specter.cljs-test-helpers :refer [for-all+]]
[com.rpl.specter
:refer [declarepath providepath select select-one select-one!
select-first transform setval replace-in]]))
(:use
#?(:clj [clojure.test :only [deftest is]])
#?(:clj [clojure.test.check.clojure-test :only [defspec]])
#?(:clj [com.rpl.specter.test-helpers :only [for-all+]])
#?(:clj [com.rpl.specter
:only [declarepath providepath select select-one select-one!
select-first transform setval replace-in]]))
(:require #?(:clj [clojure.test.check.generators :as gen])
#?(:clj [clojure.test.check.properties :as prop])
#?(:cljs [clojure.test.check :as tc])
#?(:cljs [clojure.test.check.generators :as gen])
#?(:cljs [clojure.test.check.properties :as prop :include-macros true])
[com.rpl.specter :as s]
[com.rpl.specter.zipper :as z]))
(defspec zipper-end-equivalency-test
(for-all+
[v (gen/not-empty (gen/vector gen/int))
i (gen/vector gen/int)]
(= (setval s/END i v)
(setval [z/VECTOR-ZIP z/DOWN z/RIGHTMOST z/INNER-RIGHT] i v))))
(deftest zipper-multi-insert-test
(is (= [1 2 :a :b 3 :a :b 4]
(setval [z/VECTOR-ZIP
z/DOWN
z/RIGHT
z/RIGHT
(s/multi-path z/INNER-RIGHT z/INNER-LEFT)]
[:a :b]
[1 2 3 4])
(setval [z/VECTOR-ZIP
z/DOWN
z/RIGHT
z/RIGHT
(s/multi-path z/INNER-LEFT z/INNER-RIGHT)]
[:a :b]
[1 2 3 4]))))
(deftest zipper-down-up-test
(is (= [1 [2 3 5] 6]
(transform [z/VECTOR-ZIP
z/DOWN
z/RIGHT
z/DOWN
z/RIGHT
z/RIGHT
(s/multi-path
s/STAY
[z/UP z/RIGHT])
z/NODE]
inc
[1 [2 3 4] 5]))))
(deftest next-terminate-test
(is (= [2 [3 4 [5]] 6]
(transform [z/VECTOR-ZIP z/NEXT-WALK z/NODE number?]
inc
[1 [2 3 [4]] 5])))
(is (= [1 [3 [[]] 5]]
(setval [z/VECTOR-ZIP
z/NEXT-WALK
(s/selected? z/NODE number? even?)
z/NODE-SEQ]
[]
[1 2 [3 [[4]] 5] 6]))))
(deftest zipper-nav-stop-test
(is (= [1]
(transform [z/VECTOR-ZIP z/UP z/NODE] inc [1])))
(is (= [1]
(transform [z/VECTOR-ZIP z/DOWN z/LEFT z/NODE] inc [1])))
(is (= [1]
(transform [z/VECTOR-ZIP z/DOWN z/RIGHT z/NODE] inc [1])))
(is (= []
(transform [z/VECTOR-ZIP z/DOWN z/NODE] inc []))))
(deftest find-first-test
(is (= [1 [3 [[4]] 5] 6]
(setval [z/VECTOR-ZIP
(z/find-first #(and (number? %) (even? %)))
z/NODE-SEQ]
[]
[1 2 [3 [[4]] 5] 6]))))
(deftest nodeseq-expand-test
(is (= [2 [2] [[4 4 4]] 4 4 4 6]
(transform [z/VECTOR-ZIP
z/NEXT-WALK
(s/selected? z/NODE number? odd?)
(s/collect-one z/NODE)
z/NODE-SEQ]
(fn [v _]
(repeat v (inc v)))
[1 [2] [[3]] 3 6]))))