File: generators_test.clj

package info (click to toggle)
prismatic-schema-clojure 1.2.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 460 kB
  • sloc: sh: 40; makefile: 17
file content (62 lines) | stat: -rw-r--r-- 2,096 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(ns schema.experimental.generators-test
  (:use clojure.test)
  (:require
   [clojure.test.check.properties :as properties]
   [clojure.test.check.generators :as check-generators]
   [clojure.test.check.clojure-test :as check-clojure-test]
   [schema.core :as s]
   [schema.experimental.generators :as generators]))


(def OGInner
  {(s/required-key "l") [s/Int]
   s/Keyword s/Str})

(def OGInner2
  {:c OGInner
   :d s/Str})

(def OGSchema
  {:a [s/Str]
   :b OGInner2})

(def FinalSchema
  {:a (s/eq ["bob"])
   :b {:c (s/conditional (fn [{:strs [l]}] (and (every? even? l) (seq l))) OGInner)
       :d (s/eq "mary")}})

(deftest sample-test
  (let [res (generators/sample
             20 OGSchema
             {[s/Str] (generators/always ["bob"])
              s/Int ((generators/fmap #(inc (* % 2))) check-generators/int)}
             {[s/Int] (comp (generators/such-that seq)
                            (generators/fmap (partial mapv inc)))
              OGInner2 (generators/merged {:d "mary"})})]
    (is (= (count res) 20))
    (is (s/validate [FinalSchema] res))))

(deftest simple-leaf-generators-smoke-test
  (doseq [leaf-schema [double float long int short char byte boolean
                       Double Float Long Integer Short Character Byte Boolean
                       doubles floats longs ints shorts chars bytes booleans
                       s/Str String s/Bool s/Num s/Int s/Keyword s/Symbol s/Inst
                       Object s/Any s/Uuid (s/eq "foo") (s/enum :a :b :c)]]
    (testing (str leaf-schema)
      (is (= 10 (count (generators/sample 10 leaf-schema)))))))

(def FancySeq
  "A sequence that starts with a String, followed by an optional Keyword,
   followed by any number of Numbers."
  [(s/one s/Str "s")
   (s/optional s/Keyword "k")
   s/Num])

(deftest fancy-seq-smoke-test
  "Catch issues wit a fancier schema with optional keys and such."
  (is (= 100 (count (generators/sample 100 FancySeq)))))

(check-clojure-test/defspec spec-test
  100
  (properties/for-all [x (generators/generator OGSchema)]
                      (not (s/check OGSchema x))))