diff --git a/.github/workflows/gen-tests.yml b/.github/workflows/gen-tests.yml new file mode 100644 index 0000000..d62ba90 --- /dev/null +++ b/.github/workflows/gen-tests.yml @@ -0,0 +1,28 @@ +--- +name: Run generative tests + +on: + push: + branches: [main] + schedule: + - cron: "0 0 1 * *" # every month + +jobs: + gen-tests: + name: Run generative tests + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Setup latest Java + uses: actions/setup-java@v3.11.0 + with: + distribution: zulu + java-version: 21 + - name: Setup Clojure + uses: DeLaGuardo/setup-clojure@bc7570e912b028bbcc22f457adec7fdf98e2f4ed # 12.5 + with: + cli: 1.10.1.693 + - name: Run tests + run: CLOJURE=clojure-11 bin/run-gen-tests diff --git a/bin/kaocha b/bin/kaocha index 0ffaf81..a73a695 100755 --- a/bin/kaocha +++ b/bin/kaocha @@ -1,3 +1,3 @@ #!/usr/bin/env bash # Should work if the env var is empty -clojure -A:"$CLOJURE" -M:test -m kaocha.runner "$@" +clojure -A:"$CLOJURE" -M:test -m kaocha.runner --skip-meta :generative "$@" diff --git a/bin/run-gen-tests b/bin/run-gen-tests new file mode 100755 index 0000000..b51b8d9 --- /dev/null +++ b/bin/run-gen-tests @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +# Should work if the env var is empty +clojure -A:"$CLOJURE" -M:test -m kaocha.runner --focus oksa.generator-test "$@" diff --git a/test/oksa/generator_test.cljc b/test/oksa/generator_test.cljc new file mode 100644 index 0000000..cfbfc6b --- /dev/null +++ b/test/oksa/generator_test.cljc @@ -0,0 +1,36 @@ +(ns ^:generative oksa.generator-test + (:require [#?(:clj clojure.test + :cljs cljs.test) :as t] + [malli.generator :as mg] + [malli.core :as m] + [oksa.parse] + [oksa.test-util :refer [unparse-and-validate]])) + +#?(:bb nil + :clj (require '[kaocha.config] + '[kaocha.testable])) + +(def document-schema (oksa.parse/-graphql-dsl-lang {:oksa/strict true} :oksa.parse/Document)) +(def document-parser (m/parser document-schema)) + +#?(:bb nil + :clj (t/deftest generator-test + (let [seed (or (:kaocha.plugin.randomize/seed kaocha.testable/*config*) + (.nextLong (java.util.Random.))) + samples (loop [seed seed + samples nil + tries 10] + (cond (some? samples) samples + (zero? tries) (throw (ex-info "could not generate examples" {})) + :else (recur + (inc seed) + (try (mg/generate document-schema {:size 10 :seed seed}) + (catch Exception _ nil)) + (dec tries))))] + (prn ::seed seed) + (doseq [sample samples] + (t/is (true? (or (= :malli.core/invalid (document-parser sample)) + (string? (try (unparse-and-validate sample) + (catch Throwable e + (prn (ex-info "invalid sample" {:sample sample} e)) + nil)))))))))) \ No newline at end of file