From 3d4ea333c823503107683c6f528a2c885f47e51c Mon Sep 17 00:00:00 2001 From: Stuart Halloway Date: Sat, 9 Feb 2013 10:40:00 -0500 Subject: [PATCH 1/3] adopt latest test.generative --- pom.xml | 2 +- test/clojure/test_clojure/numbers.clj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index d66f32f..be85dbf 100644 --- a/pom.xml +++ b/pom.xml @@ -48,7 +48,7 @@ org.clojure test.generative - 0.2.0 + 0.4.0 test diff --git a/test/clojure/test_clojure/numbers.clj b/test/clojure/test_clojure/numbers.clj index d00b3f4..a040cc2 100644 --- a/test/clojure/test_clojure/numbers.clj +++ b/test/clojure/test_clojure/numbers.clj @@ -15,7 +15,7 @@ (:use clojure.test [clojure.test.generative :exclude (is)] clojure.template) - (:require [clojure.test.generative.generators :as gen])) + (:require [clojure.data.generators :as gen])) ; TODO: -- 1.7.3.5 From 3f87fa7f1af41b4cfe8ea796c777e81ae60ef792 Mon Sep 17 00:00:00 2001 From: Stuart Halloway Date: Sat, 9 Feb 2013 10:41:31 -0500 Subject: [PATCH 2/3] some read and edn/read tests --- test/clojure/test_clojure/edn.clj | 38 ++++++++++ test/clojure/test_clojure/generators.clj | 114 ++++++++++++++++++++++++++++++ test/clojure/test_clojure/reader.clj | 39 ++++++++++- 3 files changed, 190 insertions(+), 1 deletions(-) create mode 100644 test/clojure/test_clojure/edn.clj create mode 100644 test/clojure/test_clojure/generators.clj diff --git a/test/clojure/test_clojure/edn.clj b/test/clojure/test_clojure/edn.clj new file mode 100644 index 0000000..4d5996c --- /dev/null +++ b/test/clojure/test_clojure/edn.clj @@ -0,0 +1,38 @@ +; Copyright (c) Rich Hickey. All rights reserved. +; The use and distribution terms for this software are covered by the +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +; which can be found in the file epl-v10.html at the root of this distribution. +; By using this software in any fashion, you are agreeing to be bound by +; the terms of this license. +; You must not remove this notice, or any other, from this software. + +; Author: Stuart Halloway + + +(ns clojure.test-clojure.edn + (:require [clojure.test.generative :refer (defspec)] + [clojure.test-clojure.generators :as cgen] + [clojure.edn :as edn])) + +(defn roundtrip + "Print an object and read it back as edn. Returns rather than throws + any exceptions." + [o] + (binding [*print-length* nil + *print-dup* nil + *print-level* nil] + (try + (-> o pr-str edn/read-string) + (catch Throwable t t)))) + +(defspec types-that-should-roundtrip + roundtrip + [^{:tag cgen/ednable} o] + (when-not (= o %) + (throw (ex-info "Value cannot roundtrip, see ex-data" {:printed o :read %})))) + +(defspec types-that-should-not-roundtrip + roundtrip + [^{:tag cgen/non-ednable} o] + (when-not (instance? Throwable %) + (throw (ex-info "edn/read should have thrown, see ex-data" {:printed o :read %})))) diff --git a/test/clojure/test_clojure/generators.clj b/test/clojure/test_clojure/generators.clj new file mode 100644 index 0000000..b42756a --- /dev/null +++ b/test/clojure/test_clojure/generators.clj @@ -0,0 +1,114 @@ +(ns clojure.test-clojure.generators + (:require [clojure.data.generators :as gen]) + (:refer-clojure :exclude [namespace])) + +(defn var-value-source + "Generates a scalar suitable for an initial var value." + [] + (let [v (gen/scalar)] + (if (symbol? v) + `(quote ~v) + v))) + +(defn var-source + [n] + `(def ~(symbol (str "var" n)) + ~(var-value-source))) + +(defn record-source + [n] + (let [rname (str "ExampleRecord" "-" n) + fldct (gen/geometric 0.1)] + `(defrecord ~(symbol rname) ~(vec (map #(symbol (str "f" %)) (range fldct)))))) + +(defn generate-namespaces + "Returns a map with :nses, :vars, :records" + [{:keys [nses vars-per-ns records-per-ns]}] + (let [nses (mapv #(create-ns (symbol (str "clojure.generated.ns" %))) + (range nses)) + _ (doseq [ns nses] (binding [*ns* ns] (refer 'clojure.core))) + make-in-ns (fn [ns src] (binding [*ns* ns] (eval src))) + vars (->> (mapcat + (fn [ns] + (map + #(make-in-ns ns (var-source %)) + (range vars-per-ns))) + nses) + (into [])) + records (->> (mapcat + (fn [ns] + (map + #(make-in-ns ns (record-source %)) + (range records-per-ns))) + nses) + (into []))] + {:nses nses + :vars vars + :records records})) + +(def shared-generation + (delay (generate-namespaces {:nses 5 :vars-per-ns 5 :records-per-ns 5}))) + +(defn namespace + [] + (gen/rand-nth (:nses @shared-generation))) + +(defn var + [] + (gen/rand-nth (:vars @shared-generation))) + +(defn record + [] + (gen/rand-nth (:records @shared-generation))) + +(def ednable-scalars + [(constantly nil) + gen/byte + gen/long + gen/boolean + #_gen/printable-ascii-char ;; waiting on character reader fix + gen/string + gen/symbol + gen/keyword + gen/uuid + gen/date + gen/ratio + gen/bigint + gen/bigdec]) + +(defn- call-through + "Recursively call x until it doesn't return a function." + [x] + (if (fn? x) + (recur (x)) + x)) + +(defn ednable-scalar + [] + (call-through (rand-nth ednable-scalars))) + +(def ednable-collections + [[gen/vec [ednable-scalars]] + [gen/set [ednable-scalars]] + [gen/hash-map [ednable-scalars ednable-scalars]]]) + +(defn ednable-collection + [] + (let [[coll args] (rand-nth ednable-collections)] + (apply coll (map rand-nth args)))) + +(defn ednable + [] + (gen/one-of ednable-scalar ednable-collection)) + +(defn non-ednable + "Generate something that can be printed with *print-dup*, but + cannot be read back via edn/read." + [] + (gen/one-of namespace var)) + +(defn dup-readable + "Generate something that requires print-dup to be printed in + a roundtrippable way." + [] + (gen/one-of namespace var)) diff --git a/test/clojure/test_clojure/reader.clj b/test/clojure/test_clojure/reader.clj index 806b8ab..3ca4a86 100644 --- a/test/clojure/test_clojure/reader.clj +++ b/test/clojure/test_clojure/reader.clj @@ -21,7 +21,9 @@ (:use [clojure.instant :only [read-instant-date read-instant-calendar read-instant-timestamp]]) - (:require clojure.walk) + (:require clojure.walk + [clojure.test.generative :refer (defspec)] + [clojure.test-clojure.generators :as cgen]) (:import [clojure.lang BigInt Ratio] java.io.File java.util.TimeZone)) @@ -571,3 +573,38 @@ Exception #"No reader function for tag foo" "#foo [1 2]" Exception #"No reader function for tag bar/foo" "#bar/foo [1 2]" Exception #"No reader function for tag bar.baz/foo" "#bar.baz/foo [1 2]")))) + + +(defn roundtrip + "Print an object and read it back. Returns rather than throws + any exceptions." + [o] + (binding [*print-length* nil + *print-dup* nil + *print-level* nil] + (try + (-> o pr-str read-string) + (catch Throwable t t)))) + +(defn roundtrip-dup + "Print an object with print-dup and read it back. + Returns rather than throws any exceptions." + [o] + (binding [*print-length* nil + *print-dup* true + *print-level* nil] + (try + (-> o pr-str read-string) + (catch Throwable t t)))) + +(defspec types-that-should-roundtrip + roundtrip + [^anything o] + (when-not (= o %) + (throw (ex-info "Value cannot roundtrip, see ex-data" {:printed o :read %})))) + +(defspec types-that-need-dup-to-roundtrip + roundtrip-dup + [^{:tag cgen/dup-readable} o] + (when-not (= o %) + (throw (ex-info "Value cannot roundtrip, see ex-data" {:printed o :read %})))) -- 1.7.3.5 From dcc04abca14966083b5039b4535bacde5eddcff7 Mon Sep 17 00:00:00 2001 From: Stuart Halloway Date: Sat, 9 Feb 2013 10:42:06 -0500 Subject: [PATCH 3/3] run the generative tests for 60 seconds, insted of 10 --- src/script/run_tests.clj | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) diff --git a/src/script/run_tests.clj b/src/script/run_tests.clj index dde62d7..192a34e 100755 --- a/src/script/run_tests.clj +++ b/src/script/run_tests.clj @@ -1,2 +1,3 @@ +(System/setProperty "clojure.test.generative.msec" "60000") (require '[clojure.test.generative.runner :as runner]) (runner/-main "test") -- 1.7.3.5