~subsetpark/ec

ref: 8585fc08ca3344402bbc0099c0782917395cab0c ec/test/parser.janet -rw-r--r-- 1.3 KiB
8585fc08 — Zach Smith Add lockfile 11 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
58
59
60
61
62
(import /src/calc)
(import /src/parser)

(import /src/operations :prefix "o/")
(import /src/adverbs :prefix "a/")

(use testament)

(deftest brackets
  (let [[parsed] (parser/parse "[]")]
    (is (== [0] (parsed :shape)))
    (is (== [] (parsed :data)))))

(deftest num
  (let [[int] (parser/parse "5")
        [float] (parser/parse "5.0")
        [neg] (parser/parse "-5")
        [underscored] (parser/parse "5_000")]
    
    (is (= 5 (int :value)))
    (is (calc/Int? int))
    
    (is (= 5 (float :value)))
    (is (calc/Float? float))
    
    (is (= -5 (neg :value)))
    (is (calc/Int? neg))
    
    (is (= 5000 (underscored :value)))
    (is (calc/Int? underscored))))

(deftest word
  (let [[add dist] (parser/parse "+ /")]
    (is (= add o/add))
    (is (calc/Operation? add))

    (is (= dist a/distribute-dyad))
    (is (calc/Adverb? dist))))

(deftest pushable
  (let [s (:new calc/Stack)
        parsed (parser/parse "4 5 +")]
    (calc/push-all s parsed)
    
    (is (= 1 (length (s :data))))
    (def res (first (s :data)))

    (is (= 9 (res :value)))
    (is (calc/Float? res))))

(deftest push-adverb
  (let [s (:new calc/Stack)
        parsed (parser/parse "0 [1 2 3] [+] /")]
    (calc/push-all s parsed)    
    
    (is (= 1 (length (s :data))))
    (def res (first (s :data)))

    (is (= 6 (res :value)))
    (is (calc/Float? res))))

(run-tests!)