Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to specify generator size and seed #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/schema_generators/generators.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"Experimental support for compiling schemas to test.check generators."
(:require
[clojure.test.check.generators :as generators]
[clojure.test.check.rose-tree :as rose]
[schema.spec.core :as spec :include-macros true]
schema.spec.collection
schema.spec.leaf
Expand Down Expand Up @@ -223,3 +224,17 @@
"Sample a single element of low to moderate size."
[& generator-args]
(generators/generate (apply generator generator-args) 10))

(s/defn generate-alt :- s/Any
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a poor name choice. Any suggestions while I'm trying to come up with something better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do perhaps
(s/defn generate-deterministic [size seed & generator-args] (generators/generate (apply generator generator-args) size seed))

Although really I would prefer to just get out of this game entirely (and eventually remove sample and generate from this library entirely). This is really a library about making generators, and it was probably a mistake to ever provide wrappers for using them rather than encouraging just using test.check directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rational of only handling generator creation logic in this lib sounds good. I'll implement these generation changes on my side

"Sample a single element

Providing the same random seed will give the same result"
([schema random-seed] (generate-alt schema random-seed 10))
([schema random-seed size] (generate-alt schema random-seed size {} {}))
([schema :- Schema
random-seed
size :- s/Int
leaf-generators :- LeafGenerators
wrappers :- GeneratorWrappers]
(let [gen (generator schema leaf-generators wrappers)]
(rose/root (generators/call-gen gen random-seed size)))))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unfortunate to depend on internal clojure.test logic (like with (rose/root (generators/call-gen ..))), but it is pretty contained, so I think it is worth it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like test.check exposes these as arguments to generators/generate, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In recent releases, yes.

13 changes: 13 additions & 0 deletions test/schema_generators/generators_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[clojure.test.check]
[clojure.test.check.properties :as properties :include-macros true]
[clojure.test.check.generators :as check-generators]
[clojure.test.check.random :as random]
[clojure.test.check.clojure-test #?@(:clj [:refer [defspec]]
:cljs [:refer-macros [defspec]])]
[schema.core :as s :include-macros true]
Expand Down Expand Up @@ -64,3 +65,15 @@
50
(properties/for-all [x (generators/generator OGSchema)]
(not (s/check OGSchema x))))

(deftest generate-alt-test
(let [random-seed (random/make-random)
other-seed (random/make-random)
res-a (generators/generate-alt OGSchema random-seed 15)
res-b (generators/generate-alt OGSchema random-seed 15)
res-c (generators/generate-alt OGSchema random-seed 10)
res-d (generators/generate-alt OGSchema other-seed 15)]
(is (= res-a res-b) "samples generated with same seed and size are the same")
(is (not (= res-a res-c)) "samples generated with same seed and diff. size are diff.")
(is (not (= res-a res-d)) "samples generated with diff. seed and same size are diff.")
(is (s/validate OGSchema res-a))))