Skip to content

Commit 83df4a7

Browse files
authored
Merge pull request #1109 from tomjkidd/master
test: Add tests to show multi usage
2 parents 3cb28db + a811985 commit 83df4a7

File tree

1 file changed

+67
-2
lines changed

1 file changed

+67
-2
lines changed

test/malli/util_test.cljc

+67-2
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,24 @@
509509
(is (true? (m/validate
510510
(mu/update schema :b identity)
511511
{:a 1
512-
:c "a string"}))))))
512+
:c "a string"}))))
513+
(testing "Multi schema can update its individual dispatch schemas"
514+
(is (mu/equals (-> (m/schema [:multi {:dispatch :type}
515+
[:sized [:map
516+
[:type keyword?]
517+
[:size int?]]]
518+
[:human [:map
519+
[:type keyword?]
520+
[:name string?]
521+
[:address [:map [:country keyword?]]]]]])
522+
(mu/update :sized mu/select-keys [:size])
523+
(mu/update :human mu/select-keys [:name :address]))
524+
[:multi {:dispatch :type}
525+
[:sized [:map
526+
[:size int?]]]
527+
[:human [:map
528+
[:name string?]
529+
[:address [:map [:country keyword?]]]]]])))))
513530

514531
(deftest assoc-in-test
515532
(is (mu/equals (mu/assoc-in (m/schema [:vector int?]) [0] string?) [:vector string?]))
@@ -523,7 +540,55 @@
523540
(is (mu/equals (mu/assoc-in (m/schema [:map]) [:a :b :c :d] int?)
524541
[:map [:a [:map [:b [:map [:c [:map [:d int?]]]]]]]]))
525542
(is (mu/equals (mu/assoc-in (m/schema [:ref {:registry {::a int?, ::b string?}} ::a]) [0] ::b) [:ref {:registry {::a int?, ::b string?}} ::b]))
526-
(is (mu/equals (mu/assoc-in (m/schema [:schema int?]) [0] string?) [:schema string?])))
543+
(is (mu/equals (mu/assoc-in (m/schema [:schema int?]) [0] string?) [:schema string?]))
544+
545+
(testing "Multi schema can use assoc-in to introduce new dispatch schemas"
546+
(is (mu/equals (-> (m/schema [:multi {:dispatch :type}
547+
[:sized [:map
548+
[:type keyword?]
549+
[:size int?]]]
550+
[:human [:map
551+
[:type keyword?]
552+
[:name string?]
553+
[:address [:map [:country keyword?]]]]]])
554+
(mu/assoc-in [:robot] [:map
555+
[:type keyword?]
556+
[:serial-number string?]
557+
[:charging-station [:map [:id string?]]]]))
558+
[:multi {:dispatch :type}
559+
[:sized [:map
560+
[:type keyword?]
561+
[:size int?]]]
562+
[:human [:map
563+
[:type keyword?]
564+
[:name string?]
565+
[:address [:map [:country keyword?]]]]]
566+
[:robot [:map
567+
[:type keyword?]
568+
[:serial-number string?]
569+
[:charging-station [:map [:id string?]]]]]])))
570+
(testing "Multi schema can use assoc-in to extend existing schemas"
571+
(is (mu/equals (-> (m/schema [:multi {:dispatch :type}
572+
[:sized [:map
573+
[:type keyword?]
574+
[:size int?]]]
575+
[:human [:map
576+
[:type keyword?]
577+
[:name string?]
578+
[:address [:map [:country keyword?]]]]]])
579+
(mu/assoc-in [:sized :unit] string?)
580+
(mu/assoc-in [:human :address :state] string?))
581+
[:multi {:dispatch :type}
582+
[:sized [:map
583+
[:type keyword?]
584+
[:size int?]
585+
[:unit string?]]]
586+
[:human [:map
587+
[:type keyword?]
588+
[:name string?]
589+
[:address [:map
590+
[:country keyword?]
591+
[:state string?]]]]]]))))
527592

528593
(deftest update-in-test
529594
(is (mu/equals (mu/update-in (m/schema [:vector int?]) [0] (constantly string?)) [:vector string?]))

0 commit comments

Comments
 (0)