Questions tagged [test.check]

test.check is a Clojure property-based testing tool inspired by QuickCheck. The core idea of test.check is that instead of enumerating expected input and output for unit tests, developers write properties about their function that should hold true for all inputs. This lets them write concise, powerful tests.

test.check is a Clojure property-based testing tool inspired by QuickCheck. The core idea of test.check is that instead of enumerating expected input and output for unit tests, developers write properties about their function that should hold true for all inputs. This lets them write concise, powerful tests.

26 questions
5
votes
3 answers

test.check generate strings of a certain length

In using test.check I need a generator for strings of a certain length. Phone numbers, postal codes, social security numbers are all examples of this type of data. Although the examples appear to be only numbers, my question is for strings in…
M Smith
  • 1,988
  • 15
  • 28
4
votes
1 answer

How do I limit size of generated sample with `clojure.spec/+`?

clojure.spec/coll-of takes :gen-max option to limit the generated sample size. Is there an analog for clojure.spec/+?
Lambder
  • 2,953
  • 1
  • 26
  • 20
3
votes
1 answer

Clojure spec - override check generator for predicate

Is there a way to override generator for a core predicate function when calling clojure.spec.test.alpha/check? It is possible to override predicate generator by path inside s/gen: (gen/generate (s/gen (s/cat :s string?) {[:s] #(gen/return…
Nikola
  • 117
  • 7
3
votes
1 answer

Loops & state management in test.check

With the introduction of Spec, I try to write test.check generators for all of my functions. This is fine for simple data structures, but tends to become difficult with data structures that have parts that depend on each other. In other words,…
Maarten Truyens
  • 121
  • 2
  • 7
2
votes
3 answers

Spec: partially overriding generators in a map spec

Assuming I have already defined a spec from which I'd like to generate test data: (s/def :customer/id uuid?) (s/def :customer/given-name string?) (s/def :customer/surname string?) (s/def :customer/age pos?) (s/def ::customer (s/keys :req-un…
Tim Clemons
  • 6,231
  • 4
  • 25
  • 23
2
votes
3 answers

How do I generate random email addresses in test.check?

I'm trying to use gen/fmap with two random alphanumeric strings. Then I concatenate them with "@" and append ".com". But I'm struggling with the syntax. First attempt: (gen/fmap str (gen/string-alphanumeric) "@" (gen/string-alphanumeric)…
2
votes
1 answer

Need help understanding why Clojure spec test/check is failing the return validation when REPL doesn't fail

I've been playing around with Clojure Spec for testing and data generation and am seeing some strange behavior where the function works in unit tests and validation works in REPL but the generative testing with spec.test/check is failing. I've…
2
votes
1 answer

Clojure spec - test check OutOfMemoryError

I'm trying to do property-based testing for this simple function: (defn distinct-kw-keys [maps] (->> (map keys maps) (flatten) (filter keyword?) (distinct) (vec))) ... using fdef and check: (require…
Nikola
  • 117
  • 7
2
votes
1 answer

How can I generate random graphs with test.check?

I'm trying to generate a random graph in adjacency list form for the purposes of generative testing. An example graph would be: {:a #{:a :b}, :b #{:a :b}} (Adjacency lists are implemented as sets.) My first idea was this: (def vertex-gen…
Sebastian Oberhoff
  • 1,271
  • 1
  • 10
  • 16
2
votes
1 answer

Generating sorted data with test.check

I'd like to use test.check to generate sorted time series data of the form [ [timestamp value] [timestamp value] ..] where the timestamp, value -pairs are in ascending order by the timestamp. I can easily generate such data in random order with…
Janne
  • 3,647
  • 7
  • 28
  • 34
2
votes
1 answer

Generating structured maps with test.check

I'm playing around with test.check, and I'm testing a function which takes a map as an argument. These maps do have a defined structure, such as: {:name "Bob" :age 42 :email "bob@example.com" :admin true} Key point, there is a set of expected keys,…
Brad Koch
  • 19,267
  • 19
  • 110
  • 137
1
vote
1 answer

Clojure spec - s/or single branch generator

Is it possible to override a default spec generator so that data is always generated only for a single branch of the s/or composite spec? (s/def ::x (s/or :x-a nat-int? :x-b string?)) (gen/sample (s/gen ::x)) ;; generate strings only
Nikola
  • 117
  • 7
1
vote
2 answers

How to always generate data for optional keys in a spec?

If I have a spec like (clojure.spec/def ::person (clojure.spec/keys :req [::name ::address] :opt [::age])) And when I do (clojure.spec.gen/generate (clojure.spec/gen ::person)) Is there any way to tell the generator to always consider the optional…
Punit Naik
  • 515
  • 7
  • 26
1
vote
2 answers

How do I sample random subsequences in Clojure's test.check?

I'm trying to generate a random solvable instance of the Subset Sum Problem. Wikipedia states that the target value should always be zero, but it's also possible to specify the target value, which is what I'm doing here. So the idea is to create a…
Sebastian Oberhoff
  • 1,271
  • 1
  • 10
  • 16
1
vote
0 answers

How can I generate a single value with a given seed and size?

The test.check library provides a generate function that takes a generator and size and returns a single value from the generator with the given size, but it gets its seed nondeterministically. The library also provides a quick-check function that…
Sam Estep
  • 12,974
  • 2
  • 37
  • 75
1
2