~subsetpark/bagatto

ref: HEAD bagatto/test/loaders.janet -rw-r--r-- 2.1 KiB
4fc04b3a — Zach Smith Add render some 2 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
57
(import testament :prefix "" :exit true)

(import /bagatto)
(import /src/loaders)

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

(deftest path-loader
  (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-spec {:src "test/support/not-there.txt"
                                   :attrs bagatto/parse-base}
                                  "path test")]
    (assert-thrown (loader (parent)))))

(deftest invalid-loader
  (let [loader (fn [] :wrong)]
    (assert-thrown (loader (parent)))))

(deftest loader-arity
  (let [single-spec (fn [x] :wrong)
        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-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-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/from-spec {:attrs {:test-attr "test value"}}
                                  "attr loader")
        [_ _ res] ((loader (parent)) :res)]
    (is (== {:test-attr "test value"} res))))

(run-tests!)