~subsetpark/bagatto

fb138c64fa51b371d03e1ea66cd88e64607063d8 — Zach Smith 6 months ago 1e5023f loader-refactor
unit tests for generators
4 files changed, 99 insertions(+), 23 deletions(-)

M project.janet
M src/error.janet
M src/generators.janet
M test/generators.janet
M project.janet => project.janet +4 -4
@@ 13,10 13,10 @@
(def *static-build* (= (or (os/getenv "BAG_STATIC_BUILD") "0") "1"))

(declare-executable
 :name "bag"
 :entry "main.janet"
 :lflags (if *static-build* ["-static"] [])
 :install true)
  :name "bag"
  :entry "main.janet"
  :lflags (if *static-build* ["-static"] [])
  :install true)

(declare-source
  :source ["bagatto.janet" "repl.janet"])

M src/error.janet => src/error.janet +20 -1
@@ 104,7 104,7 @@ Specification can be one of the following:
  (string/format
    ```

Received invalid loader for data spec : %q
Received invalid loader for data spec: %q

Got: function %s, arity %q;



@@ 116,3 116,22 @@ Loader should return either:
    spec-name
    (f-name f)
    ((disasm f) :arity)))

(defn copy-error
  [spec-name selector item]
  (error (string/format
           ```
          
Attempted to generate a copy instruction for site spec: %q

In data at %q, expected :

{:path <from path>}

Got:

%q
          ```
           spec-name
           selector
           item)))

M src/generators.janet => src/generators.janet +22 -18
@@ 34,32 34,34 @@
          (array/push res [:write path contents]))))
    (:send parent [:res spec-name res])))

(defn- copy-each-generator
  [data spec-name filter site-selector path-generator]
(defn- render-generator
  [data spec-name path-generator renderer]
  (fn [parent]
    (set-cxt! spec-name)

    (threads/print "Generating paths for " spec-name "...")
    (threads/print "Rendering " spec-name "...")
    (def res @[])

    (loop [item :in (data site-selector)]
      (if-let [_should-read (filter data item)
               from (item :path)
               to (apply-path path-generator [data item] :di)]
        (array/push res [:copy from to])))
    (if-let [path (apply-path path-generator [data] :d)
             contents (apply-renderer renderer [data] :d)]
      (array/push res [:write path contents]))
    (:send parent [:res spec-name res])))

(defn- render-generator
  [data spec-name path-generator renderer]
(defn- copy-each-generator
  [data spec-name filter site-selector path-generator]
  (fn [parent]
    (set-cxt! spec-name)

    (threads/print "Rendering " spec-name "...")
    (threads/print "Generating paths for " spec-name "...")
    (def res @[])

    (if-let [path (apply-path path-generator [data] :d)
             contents (apply-renderer renderer [data] :d)]
      (array/push res [:write path contents]))
    (loop [item :in (data site-selector)]
      (when (filter data item)
        (unless (item :path)
          (error/copy-error spec-name site-selector item))
        (let [from (item :path)
              to (apply-path path-generator [data item] :di)]
          (array/push res [:copy from to]))))
    (:send parent [:res spec-name res])))

(defn- copy-some-generator


@@ 70,10 72,12 @@
    (threads/print "Generating path for " spec-name "...")
    (def res @[])

    (if-let [item (data site-selector)
             from (item :path)
             to (apply-path path-generator [data] :d)]
      (array/push res [:copy from to]))
    (let [item (data site-selector)]
      (unless (and item (item :path))
        (error/copy-error spec-name site-selector item))
      (let [from (item :path)
            to (apply-path path-generator [data] :d)]
        (array/push res [:copy from to])))
    (:send parent [:res spec-name res])))

(defn from-spec

M test/generators.janet => test/generators.janet +53 -0
@@ 1,3 1,56 @@
(import testament :prefix "" :exit true)

(import src/generators)

(defn- parent []
  @{:send (fn [self res] (put self :res res))})

(defn path-builder [data item] (string "test/out/" (item :topic)))
(defn exclaim [data item] (string (item :topic) "!"))

(defn base-path [data] "test/out/world.html")
(defn hello-world [data] (string "Hello " (get-in data [:world :contents]) "!"))

(def sample-data {:pages [{:topic "test-topic"}]})
(def path-data {:pages [{:topic "test-topic" :path "/pages/page.md"}]})
(def base-data {:world {:contents "world!"}})
(def some-data {:world {:path "/pages/world.md"}})

(deftest render-each-generator
  (let [spec {:each :pages
              :dest path-builder
              :out exclaim}
        generator (generators/from-spec spec "render each test" sample-data)
        [_ _ out] ((generator (parent)) :res)]
    (is (== @[[:write "test/out/test-topic" "test-topic!"]] out))))

(deftest copy-each-generator
  (let [spec {:each :pages
              :dest path-builder}
        generator (generators/from-spec spec "copy each test" sample-data)]
    (assert-thrown ((generator (parent)))))
  (let [spec {:each :pages
              :dest path-builder}
        generator (generators/from-spec spec "copy each test" path-data)
        [_ _ out] ((generator (parent)) :res)]
    (is (== @[[:copy "/pages/page.md" "test/out/test-topic"]] out))))

(deftest render-generator
  (let [spec {:dest base-path
              :out hello-world}
        generator (generators/from-spec spec "render generator" base-data)
        [_ _ out] ((generator (parent)) :res)]
    (is (== @[[:write "test/out/world.html" "Hello world!!"]] out))))

(deftest copy-some-generator
  (let [spec {:some :world
              :dest base-path}
        generator (generators/from-spec spec "copy some test" base-data)]
    (assert-thrown ((generator (parent)))))
  (let [spec {:some :world
              :dest base-path}
        generator (generators/from-spec spec "copy some test" some-data)
        [_ _ out] ((generator (parent)) :res)]
    (is (== @[[:copy "/pages/world.md" "test/out/world.html"]] out))))

(run-tests!)