~subsetpark/bagatto

ref: thread-refactor bagatto/test/loaders.janet -rw-r--r-- 1.7 KiB
8b5548eb — Zach Smith Format 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
(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-path-loader "path test" "test/support/test.txt" bagatto/parse-base)
        [_ _ {: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)]
    (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-file-spec-loader "spec test" single-spec bagatto/parse-base identity)]
    (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)
        [_ _ {: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)
        [_ _ 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"})
        [_ _ res] ((loader (parent)) :res)]
    (is (== {:test-attr "test value"} res))))

(run-tests!)