Codebase list test-check-clojure / 1460e24
properties.{[clj cljs] -> cljc} Nicolas Berger authored 8 years ago Gary Fredericks committed 8 years ago
3 changed file(s) with 60 addition(s) and 96 deletion(s). Raw diff Collapse all Expand all
+0
-60
src/main/clojure/clojure/test/check/properties.clj less more
0 ; Copyright (c) Rich Hickey, Reid Draper, and contributors.
1 ; All rights reserved.
2 ; The use and distribution terms for this software are covered by the
3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4 ; which can be found in the file epl-v10.html at the root of this distribution.
5 ; By using this software in any fashion, you are agreeing to be bound by
6 ; the terms of this license.
7 ; You must not remove this notice, or any other, from this software.
8
9 (ns clojure.test.check.properties
10 (:require [clojure.test.check.generators :as gen]))
11
12 (defn- apply-gen
13 [function]
14 (fn [args]
15 (let [result (try (apply function args)
16 (catch java.lang.ThreadDeath t (throw t))
17 (catch Throwable t t))]
18 {:result result
19 :function function
20 :args args})))
21
22 (defn for-all*
23 "Creates a property (properties are also generators). A property
24 is a generator that generates the result of applying the function
25 under test with the realized arguments. Once realized, the arguments
26 will be applied to `function` with `apply`.
27
28 Example:
29
30 (for-all* [gen/int gen/int] (fn [a b] (>= (+ a b) a)))
31 "
32 [args function]
33 (gen/fmap
34 (apply-gen function)
35 (apply gen/tuple args)))
36
37 (defn- binding-vars
38 [bindings]
39 (map first (partition 2 bindings)))
40
41 (defn- binding-gens
42 [bindings]
43 (map second (partition 2 bindings)))
44
45 (defmacro for-all
46 "Macro sugar for `for-all*`. `for-all` lets you name the parameter
47 and use them in expression, without wrapping them in a lambda. Like
48 `for-all*`, it returns a property.
49
50 Examples
51
52 (for-all [a gen/int
53 b gen/int]
54 (>= (+ a b) a))
55 "
56 [bindings & body]
57 `(for-all* ~(vec (binding-gens bindings))
58 (fn [~@(binding-vars bindings)]
59 ~@body)))
0 ; Copyright (c) Rich Hickey, Reid Draper, and contributors.
1 ; All rights reserved.
2 ; The use and distribution terms for this software are covered by the
3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4 ; which can be found in the file epl-v10.html at the root of this distribution.
5 ; By using this software in any fashion, you are agreeing to be bound by
6 ; the terms of this license.
7 ; You must not remove this notice, or any other, from this software.
8
9 (ns clojure.test.check.properties
10 (:require [clojure.test.check.generators :as gen]))
11
12 (defn- apply-gen
13 [function]
14 (fn [args]
15 (let [result (try (apply function args)
16 #?(:clj (catch java.lang.ThreadDeath t (throw t)))
17 (catch #?(:clj Throwable :cljs :default) t t))]
18 {:result result
19 :function function
20 :args args})))
21
22 (defn for-all*
23 "Creates a property (properties are also generators). A property
24 is a generator that generates the result of applying the function
25 under test with the realized arguments. Once realized, the arguments
26 will be applied to `function` with `apply`.
27
28 Example:
29
30 (for-all* [gen/int gen/int] (fn [a b] (>= (+ a b) a)))
31 "
32 [args function]
33 (gen/fmap
34 (apply-gen function)
35 (apply gen/tuple args)))
36
37 (defn- binding-vars
38 [bindings]
39 (map first (partition 2 bindings)))
40
41 (defn- binding-gens
42 [bindings]
43 (map second (partition 2 bindings)))
44
45 (defmacro for-all
46 "Macro sugar for `for-all*`. `for-all` lets you name the parameter
47 and use them in expression, without wrapping them in a lambda. Like
48 `for-all*`, it returns a property.
49
50 Examples
51
52 (for-all [a gen/int
53 b gen/int]
54 (>= (+ a b) a))
55 "
56 [bindings & body]
57 `(for-all* ~(vec (binding-gens bindings))
58 (fn [~@(binding-vars bindings)]
59 ~@body)))
+0
-36
src/main/clojure/clojure/test/check/properties.cljs less more
0 ; Copyright (c) Rich Hickey, Reid Draper, and contributors.
1 ; All rights reserved.
2 ; The use and distribution terms for this software are covered by the
3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4 ; which can be found in the file epl-v10.html at the root of this distribution.
5 ; By using this software in any fashion, you are agreeing to be bound by
6 ; the terms of this license.
7 ; You must not remove this notice, or any other, from this software.
8
9 (ns clojure.test.check.properties
10 (:require-macros clojure.test.check.properties)
11 (:require [clojure.test.check.generators :as gen]))
12
13 (defn- apply-gen
14 [function]
15 (fn [args]
16 (let [result (try (apply function args)
17 (catch :default t t))]
18 {:result result
19 :function function
20 :args args})))
21
22 (defn for-all*
23 "Creates a property (properties are also generators). A property
24 is a generator that generates the result of applying the function
25 under test with the realized arguments. Once realized, the arguments
26 will be applied to `function` with `apply`.
27
28 Example:
29
30 (for-all* [gen/int gen/int] (fn [a b] (>= (+ a b) a)))
31 "
32 [args function]
33 (gen/fmap
34 (apply-gen function)
35 (apply gen/tuple args)))