_评论者:reiddraper_
抱歉延迟了,这是我一直在工作的草稿:
diff --git a/src/main/clojure/clojure/test/check/properties.clj b/src/main/clojure/clojure/test/check/properties.clj
index 99b5222..139ae9a 100644
--- a/src/main/clojure/clojure/test/check/properties.clj
+++ b/src/main/clojure/clojure/test/check/properties.clj
@@ -8,13 +8,47 @@
; 您必须保留此声明,或其他任何声明,在本软件中。
(ns clojure.test.check.properties
+ (:import clojure.test.check.generators.Generator)
(:require [clojure.test.check.generators :as gen]))
+(defrecord Result [result pass? message stamps])
+
+(defprotocol ToResult
+ (to-result [a]))
+
+(extend java.lang.Object
+ ToResult
+ {:to-result (fn [b]
+ ;;; 此处不检查捕获的异常
+ (->Result b (not (false? b)) nil nil))})
+
+(extend nil
+ ToResult
+ {:to-result (fn [b]
+ (->Result b false nil nil))})
+
+(extend java.lang.Boolean
+ ToResult
+ {:to-result (fn [b]
+ (->Result b b nil nil))})
+
+(extend Generator
+ ToResult
+ {:to-result identity})
+
+(extend Result
+ ToResult
+ {:to-result identity})
+
+(defn message
+ [m property]
+ (assoc property :message m))
+
- (defn- apply-gen
- [function]
- (fn [args]
- (let [result (try (apply function args) (catch Throwable t t))]
- {:result result
+ (let [result (to-result (try (apply function args) (catch Throwable t t)))]
+ {:result (:result result)
- :function function
- :args args})
@@ -29,9 +63,18 @@
- (for-all* [gen/int gen/int] (fn [a b] (>= (+ a b) a)))
- "
- [args function]
- (gen/fmap
- (apply-gen function)
- (apply gen/tuple args)))
+ (gen/bind
+ (apply gen/tuple args)
+ (fn [a]
+ (let [result ((apply-gen function) a)]
+ (cond (gen/generator? result) (gen/fmap (fn [r] (println "foo") (update-in r :args #(conj % a))) result)
+ ;;; 注意:在我离开这段代码之前,这里快速做一下笔记,
+ ;; 这个 :else 被调用是因为我们正在将结果
+ ;;; 包装在一个带有关联的 {:result ...} 映射中。
+ ;;; 我想我们应该有两个结果类型,一个结果是属性本身返回的,
+ ;;; 另一个结果将 'args' 放在这个结果之上。
+ :else (do (println "bar") (gen/return result)))))
+ ))
- (defn binding-vars
[绑定]