~subsetpark/bagatto

1e5023fe6f90e32906f684380ca1db8d4f68d4a3 — Zach Smith 6 months ago 05cfd98
Refactor tests to support from-spec
5 files changed, 38 insertions(+), 27 deletions(-)

M src/core.janet
M src/generators.janet
M src/loaders.janet
M test/core.janet
M test/loaders.janet
M src/core.janet => src/core.janet +5 -5
@@ 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))))

M src/generators.janet => src/generators.janet +4 -4
@@ 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)

M src/loaders.janet => src/loaders.janet +10 -10
@@ 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))))

M test/core.janet => test/core.janet +2 -2
@@ 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)))


M test/loaders.janet => test/loaders.janet +17 -6
@@ 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))))