From 1e5023fe6f90e32906f684380ca1db8d4f68d4a3 Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Sat, 2 Jan 2021 23:53:38 -0500 Subject: [PATCH] Refactor tests to support from-spec --- src/core.janet | 10 +++++----- src/generators.janet | 8 ++++---- src/loaders.janet | 20 ++++++++++---------- test/core.janet | 4 ++-- test/loaders.janet | 23 +++++++++++++++++------ 5 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/core.janet b/src/core.janet index 2a733c6..2683217 100644 --- a/src/core.janet +++ b/src/core.janet @@ -60,7 +60,7 @@ (threads/print "Reading " spec-name " data spec...") (setdyn :error-context {:spec-name spec-name}) (loaders/from-spec (set-defaults spec) spec-name))] - + (threads/distribute jobs thread-init))) (defn produce-writer-specs @@ -73,10 +73,10 @@ [site data env] (let [site-pairs (pairs site) jobs (seq [[spec-name spec] :in site-pairs] - (threads/print "Reading " spec-name " site spec...") - (generators/from-spec (set-defaults spec) - spec-name - data))] + (threads/print "Reading " spec-name " site spec...") + (generators/from-spec (set-defaults spec) + spec-name + data))] (let [segments (-> (threads/distribute jobs thread-init) (values))] (array/concat ;segments)))) diff --git a/src/generators.janet b/src/generators.janet index b2f4378..01c20a7 100644 --- a/src/generators.janet +++ b/src/generators.janet @@ -21,7 +21,7 @@ ([err fib] (propagate (error/renderer-error err f args-type) fib)))) -(defn render-each-generator +(defn- render-each-generator [data spec-name filter site-selector path-generator renderer] (fn [parent] (set-cxt! spec-name) @@ -34,7 +34,7 @@ (array/push res [:write path contents])))) (:send parent [:res spec-name res]))) -(defn copy-each-generator +(defn- copy-each-generator [data spec-name filter site-selector path-generator] (fn [parent] (set-cxt! spec-name) @@ -49,7 +49,7 @@ (array/push res [:copy from to]))) (:send parent [:res spec-name res]))) -(defn render-generator +(defn- render-generator [data spec-name path-generator renderer] (fn [parent] (set-cxt! spec-name) @@ -62,7 +62,7 @@ (array/push res [:write path contents])) (:send parent [:res spec-name res]))) -(defn copy-some-generator +(defn- copy-some-generator [data spec-name site-selector path-generator] (fn [parent] (set-cxt! spec-name) diff --git a/src/loaders.janet b/src/loaders.janet index a1e5221..8bb4005 100644 --- a/src/loaders.janet +++ b/src/loaders.janet @@ -8,9 +8,9 @@ (defn- make-attrs [parser filename &opt file-contents] (let [base-attrs @{:path filename :contents file-contents}] (try (parser file-contents base-attrs) - ([err fib] (propagate (error/attrs-error err parser) fib))))) + ([err fib] (propagate (error/attrs-error err parser) fib))))) -(defn from-file-spec-loader +(defn- from-file-spec-loader ``` A loader that takes either a single file spec or a sequence of file specs and sends back a list of attributes. @@ -38,7 +38,7 @@ _ (error/loader-spec-error spec-name loader))] (:send parent [:res spec-name res])))) -(defn from-path-loader +(defn- from-path-loader ``` A loader that takes a literal file path and generates file attributes. @@ -52,7 +52,7 @@ res (make-attrs parser path file-contents)] (:send parent [:res spec-name res])))) -(defn bare-attr-loader +(defn- bare-attr-loader ``` A loader that takes an attributes literal and returns it. ``` @@ -69,17 +69,17 @@ (match spec ({:src loader :attrs parser} (function? loader)) (from-file-spec-loader spec-name - loader - parser - transform-f) - + loader + parser + transform-f) + ({:src path :attrs parser} (string? path)) (from-path-loader spec-name path parser) - + {:attrs attrs} (bare-attr-loader spec-name attrs) - + _ (error/data-error spec)))) diff --git a/test/core.janet b/test/core.janet index 471207a..dfe2dff 100644 --- a/test/core.janet +++ b/test/core.janet @@ -11,10 +11,10 @@ (test-temple) (test-bagatto) (:send parent :tests-pass))) - + (assert-thrown (test-temple)) (assert-thrown (test-bagatto)) - + (core/thread-init run) (is (= (thread/receive) :tests-pass))) diff --git a/test/loaders.janet b/test/loaders.janet index 9d0c2f8..ed95633 100644 --- a/test/loaders.janet +++ b/test/loaders.janet @@ -7,13 +7,17 @@ @{:send (fn [self res] (put self :res res))}) (deftest path-loader - (let [loader (loaders/from-path-loader "path test" "test/support/test.txt" bagatto/parse-base) + (let [loader (loaders/from-spec {:src "test/support/test.txt" + :attrs bagatto/parse-base} + "path test") [_ _ {:path path :contents contents}] ((loader (parent)) :res)] (is (= "test/support/test.txt" path)) (is (== @"test file contents\n" contents)))) (deftest non-existent - (let [loader (loaders/from-path-loader "path test" "test/support/not-there.txt" bagatto/parse-base)] + (let [loader (loaders/from-spec {:src "test/support/not-there.txt" + :attrs bagatto/parse-base} + "path test")] (assert-thrown (loader (parent))))) (deftest invalid-loader @@ -22,24 +26,31 @@ (deftest loader-arity (let [single-spec (fn [x] :wrong) - loader (loaders/from-file-spec-loader "spec test" single-spec bagatto/parse-base identity)] + loader (loaders/from-spec {:src single-spec + :attrs bagatto/parse-base} + "spec test")] (assert-thrown (loader (parent))))) (deftest some-spec-loader (let [single-spec (fn [] {:some ["spec-path" "spec-contents"]}) - loader (loaders/from-file-spec-loader "spec test" single-spec bagatto/parse-base identity) + loader (loaders/from-spec {:src single-spec + :attrs bagatto/parse-base} + "spec test") [_ _ {:path path :contents contents}] ((loader (parent)) :res)] (is (= "spec-path" path)) (is (= "spec-contents" contents)))) (deftest each-spec-loader (let [multi-spec (fn [] {:each ["path only" ["spec-path" "spec-contents"]]}) - loader (loaders/from-file-spec-loader "spec test" multi-spec bagatto/parse-base identity) + loader (loaders/from-spec {:src multi-spec + :attrs bagatto/parse-base} + "spec test") [_ _ res] ((loader (parent)) :res)] (is (== @[@{:path "path only"} @{:path "spec-path" :contents "spec-contents"}] res)))) (deftest attr-loader - (let [loader (loaders/bare-attr-loader "attr loader" {:test-attr "test value"}) + (let [loader (loaders/from-spec {:attrs {:test-attr "test value"}} + "attr loader") [_ _ res] ((loader (parent)) :res)] (is (== {:test-attr "test value"} res)))) -- 2.45.2