~subsetpark/bagatto

ref: import-syntax-update bagatto/test/generators.janet -rw-r--r-- 2.1 KiB
34918929 — Zach Smith Add ignore file 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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!)