Skip to content

Instantly share code, notes, and snippets.

@bendisposto
Created February 18, 2015 15:30
Show Gist options
  • Save bendisposto/c2a839b7219c04880e68 to your computer and use it in GitHub Desktop.
Save bendisposto/c2a839b7219c04880e68 to your computer and use it in GitHub Desktop.
vl_5_2_2015 Teil5: Quickcheck + Schemas
(ns comb.core
(:require [clojure.test.check :as tc]
[clojure.test.check.generators :as gen]
[clojure.test.check.properties :as prop]
[clojure.test :as t]
[schema.core :as s]
[schema-gen.core :as sg]))
(comment
(def DataSet [[(s/one s/Int "x") (s/one s/Int "y") (s/one s/Int "z")]])
(def d [[1 2 3] [0 0 0] [-1 2 6]])
(s/validate DataSet d)
(s/validate DataSet [[1 2 3] [0 0.3 7] [0 0]])
(defn append [c x y z]
(conj c [x y z]))
;; Contract
(s/defn append :- DataSet [c :- DataSet x :- s/Num y :- s/Num z :- s/Num]
(conj c [x y z]))
(s/with-fn-validation (append d 6 7 8))
(s/with-fn-validation (append [1 2 3] 6 7 8))
;; Testen
;; Wie sieht der Generator aus?
(def input-gen (gen/vector (gen/vector gen/int 3)))
(gen/sample input-gen 10)
(def append-prop (prop/for-all [x gen/int
y gen/int
z gen/int
c input-gen]
(s/with-fn-validation (append c x y z))))
(tc/quick-check 1000 append-prop)
;; Vergleichen wir mal Schema und Generator:
;; (def DataSet [[(s/one s/Int "x") (s/one s/Int "y") (s/one s/Int "z")]])
;; (def input-gen (gen/vector (gen/vector gen/int 3)))
;; Schon sehr ähnlich, oder?
(def input-gen2 (sg/schema->gen DataSet))
(gen/sample input-gen2 14)
(def Address {:street s/Str :code s/Int :city s/Str})
(gen/sample (sg/schema->gen Address) 5)
;; Was gibt es sonst noch?
)
(defproject comb "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.7.0-alpha5"]
[schema-gen "0.1.3"]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment