-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1086 from frenchy64/distributive
Distributive schemas
- Loading branch information
Showing
6 changed files
with
264 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
set -xe | ||
|
||
clojure -M:jar && clojure -M:install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
(ns malli.distributive-test | ||
(:require [clojure.test :refer [are deftest is testing]] | ||
[malli.core :as m] | ||
[malli.impl.util :as miu] | ||
[malli.generator :as mg] | ||
[malli.registry :as mr] | ||
[malli.transform :as mt] | ||
[malli.util :as mu])) | ||
|
||
(def options {:registry (merge (mu/schemas) (m/default-schemas))}) | ||
|
||
(defn dist [s] | ||
(m/form (m/deref s options))) | ||
|
||
(defn valid? [?schema value] (m/validate ?schema value options)) | ||
|
||
(deftest distributive-multi-test | ||
(is (= (dist | ||
[:merge | ||
[:map [:x :int]] | ||
[:multi {:dispatch :y} | ||
[1 [:map [:y [:= 1]]]] | ||
[2 [:map [:y [:= 2]]]]]]) | ||
[:multi {:dispatch :y} | ||
[1 [:map [:x :int] [:y [:= 1]]]] | ||
[2 [:map [:x :int] [:y [:= 2]]]]])) | ||
(is (= (dist | ||
[:merge | ||
[:map [:x :int]] | ||
[:schema | ||
[:multi {:dispatch :y} | ||
[1 [:map [:y [:= 1]]]] | ||
[2 [:map [:y [:= 2]]]]]]]) | ||
[:multi {:dispatch :y} | ||
[1 [:map [:x :int] [:y [:= 1]]]] | ||
[2 [:map [:x :int] [:y [:= 2]]]]])) | ||
(is (= (dist | ||
[:merge | ||
[:multi {:dispatch :y} | ||
[1 [:map [:y [:= 1]]]] | ||
[2 [:map [:y [:= 2]]]]] | ||
[:map [:x :int]]]) | ||
[:multi {:dispatch :y} | ||
[1 [:map [:y [:= 1]] [:x :int]]] | ||
[2 [:map [:y [:= 2]] [:x :int]]]])) | ||
(is (= (dist | ||
[:merge | ||
[:multi {:dispatch :y} | ||
[1 [:map [:y [:= 1]]]] | ||
[2 [:map [:y [:= 2]]]]] | ||
[:map [:x :int]] | ||
[:map [:z :int]]]) | ||
(dist | ||
[:merge | ||
[:merge | ||
[:multi {:dispatch :y} | ||
[1 [:map [:y [:= 1]]]] | ||
[2 [:map [:y [:= 2]]]]] | ||
[:map [:x :int]]] | ||
[:map [:z :int]]]) | ||
[:multi {:dispatch :y} | ||
[1 [:map [:y [:= 1]] [:x :int] [:z :int]]] | ||
[2 [:map [:y [:= 2]] [:x :int] [:z :int]]]])) | ||
(is (= (dist | ||
[:merge | ||
[:multi {:dispatch :y} | ||
[1 [:map [:y [:= 1]]]] | ||
[2 [:map [:y [:= 2]]]]] | ||
[:map [:x :int]] | ||
[:map [:z :int]] | ||
[:multi {:dispatch :y} | ||
[3 [:map [:y [:= 3]]]] | ||
[4 [:map [:y [:= 4]]]]]]) | ||
(dist | ||
[:merge | ||
[:merge | ||
[:merge | ||
[:multi {:dispatch :y} | ||
[1 [:map [:y [:= 1]]]] | ||
[2 [:map [:y [:= 2]]]]] | ||
[:map [:x :int]]] | ||
[:map [:z :int]]] | ||
[:multi {:dispatch :y} | ||
[3 [:map [:y [:= 3]]]] | ||
[4 [:map [:y [:= 4]]]]]]) | ||
[:multi {:dispatch :y} | ||
[1 [:multi {:dispatch :y} | ||
[3 [:map [:y [:= 3]] [:x :int] [:z :int]]] | ||
[4 [:map [:y [:= 4]] [:x :int] [:z :int]]]]] | ||
[2 [:multi {:dispatch :y} | ||
[3 [:map [:y [:= 3]] [:x :int] [:z :int]]] | ||
[4 [:map [:y [:= 4]] [:x :int] [:z :int]]]]]])) | ||
(is (= (dist | ||
[:merge | ||
[:multi {:dispatch :y} | ||
[1 [:map [:y [:= 1]]]] | ||
[2 [:map [:y [:= 2]]]]] | ||
[:map [:x :int]] | ||
[:map [:z :int]] | ||
[:multi {:dispatch :a} | ||
[3 [:map [:a [:= 3]]]] | ||
[4 [:map [:a [:= 4]]]]]]) | ||
[:multi {:dispatch :y} | ||
[1 [:multi {:dispatch :a} | ||
[3 [:map [:y [:= 1]] [:x :int] [:z :int] [:a [:= 3]]]] | ||
[4 [:map [:y [:= 1]] [:x :int] [:z :int] [:a [:= 4]]]]]] | ||
[2 [:multi {:dispatch :a} | ||
[3 [:map [:y [:= 2]] [:x :int] [:z :int] [:a [:= 3]]]] | ||
[4 [:map [:y [:= 2]] [:x :int] [:z :int] [:a [:= 4]]]]]]])) | ||
(is (= (dist | ||
[:merge | ||
[:multi {:dispatch :y} | ||
[1 [:map [:y [:= 1]]]] | ||
[2 [:map [:y [:= 2]]]]] | ||
[:multi {:dispatch :y} | ||
[3 [:map [:y [:= 3]]]] | ||
[4 [:map [:y [:= 4]]]]]]) | ||
[:multi {:dispatch :y} | ||
[1 [:multi {:dispatch :y} | ||
[3 [:map [:y [:= 3]]]] | ||
[4 [:map [:y [:= 4]]]]]] | ||
[2 [:multi {:dispatch :y} | ||
[3 [:map [:y [:= 3]]]] | ||
[4 [:map [:y [:= 4]]]]]]])) | ||
(is (= (dist | ||
[:merge | ||
[:multi {:dispatch :y} | ||
[1 [:map [:y [:= 1]]]] | ||
[2 [:map [:y [:= 2]]]]] | ||
[:multi {:dispatch :z} | ||
[3 [:map [:z [:= 3]]]] | ||
[4 [:map [:z [:= 4]]]]]]) | ||
[:multi {:dispatch :y} | ||
[1 [:multi {:dispatch :z} | ||
[3 [:map [:y [:= 1]] [:z [:= 3]]]] | ||
[4 [:map [:y [:= 1]] [:z [:= 4]]]]]] | ||
[2 [:multi {:dispatch :z} | ||
[3 [:map [:y [:= 2]] [:z [:= 3]]]] | ||
[4 [:map [:y [:= 2]] [:z [:= 4]]]]]]]))) | ||
|
||
(deftest parse-distributive-multi-test | ||
(is (= [1 [3 {:y 1, :z 3}]] | ||
(m/parse | ||
[:merge | ||
[:multi {:dispatch :y} | ||
[1 [:map [:y [:= 1]]]] | ||
[2 [:map [:y [:= 2]]]]] | ||
[:multi {:dispatch :z} | ||
[3 [:map [:z [:= 3]]]] | ||
[4 [:map [:z [:= 4]]]]]] | ||
{:y 1 :z 3} | ||
options)))) | ||
|
||
(deftest gen-distributive-multi-test | ||
(is (= [{:y 1, :z 3} {:y 2, :z 4} {:y 2, :z 3} {:y 2, :z 3} {:y 1, :z 4} | ||
{:y 1, :z 3} {:y 1, :z 3} {:y 1, :z 3} {:y 1, :z 3} {:y 2, :z 4}] | ||
(mg/sample | ||
[:merge | ||
[:multi {:dispatch :y} | ||
[1 [:map [:y [:= 1]]]] | ||
[2 [:map [:y [:= 2]]]]] | ||
[:multi {:dispatch :z} | ||
[3 [:map [:z [:= 3]]]] | ||
[4 [:map [:z [:= 4]]]]]] | ||
(assoc options :seed 0))))) |