aboutsummaryrefslogtreecommitdiffstats
path: root/spec/lib/logic_spec.moon
blob: 5b714653626a72cc1c3d34e84525c81740e618c1 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import TestPilot from require 'spec.test_setup'

describe "logic", ->
  test = TestPilot '', '(import* testing logic)\n'

  describe "==", ->
    it "can compare any type", -> COPILOT\eval_once '
      (expect= true (== 1 1))
      (expect= false (== 1 2))
      (expect= false (== 1 "hello"))
      (expect= true (== "hello" "hello"))
      (expect= true (== [1 2 3] [1 2 3]))
      (expect= false (== [1 2 3] [1 2 1]))
      (expect= false (== [1 2 3] [1 2]))
      (expect= false (== [1 2 3] [1 2 3 4]))
      (expect= true (==
          {"a" 1 "b" true "c" ["test"]}
          {"a" 1 "b" true "c" ["test"]}))
      (expect= false (==
          {"a" 1 "b" false "c" ["test"]}
          {"a" 1 "b" true "c" ["test"]}))
      (expect= false (==
          {"a" 1 "b" true "c" ["test" "toast"]}
          {"a" 1 "b" true "c" ["test"]}))
      (expect= false (==
          {"a" 1 "b" true}
          {"a" 1 "b" true "c" ["test"]}))
      (expect= false (==
          {"a" 1 "b" true}
          {"a" 1}))
      (expect= true (== print print))
      (expect= false (== print ==))
    '

    it "is aliased as eq", -> COPILOT\eval_once '
      (expect= true (== eq ==))
    '

  describe "!=", ->
    it "can compare any type", -> COPILOT\eval_once '
      (expect= false (!= 1 1))
      (expect= true (!= 1 2))
      (expect= true (!= 1 "hello"))
      (expect= false (!= "hello" "hello"))
      (expect= false (!= [1 2 3] [1 2 3]))
      (expect= true (!= [1 2 3] [1 2 1]))
      (expect= true (!= [1 2 3] [1 2]))
      (expect= true (!= [1 2 3] [1 2 3 4]))
      (expect= false (!=
          {"a" 1 "b" true "c" ["test"]}
          {"a" 1 "b" true "c" ["test"]}))
      (expect= true (!=
          {"a" 1 "b" false "c" ["test"]}
          {"a" 1 "b" true "c" ["test"]}))
      (expect= true (!=
          {"a" 1 "b" true "c" ["test" "toast"]}
          {"a" 1 "b" true "c" ["test"]}))
      (expect= true (!=
          {"a" 1 "b" true}
          {"a" 1 "b" true "c" ["test"]}))
      (expect= true (!=
          {"a" 1 "b" true}
          {"a" 1}))
      (expect= false (!= print print))
      (expect= true (!= print ==))
    '

    it "is aliased as not-eq", -> COPILOT\eval_once '
      (expect= true (== not-eq !=))
    '

  describe "bool", ->
    it "coerces numbers", -> COPILOT\eval_once '
      (expect= false (bool 0))
      (expect= true (bool 1))
      (expect= true (bool -1))
      (expect= true (bool 1024))
    '

    it "accepts booleans", -> COPILOT\eval_once '
      (expect= false (bool false))
      (expect= true (bool true))
    '

  describe "not", ->
    it "accepts booleans", -> COPILOT\eval_once '
      (expect= true (not false))
      (expect= false (not true))
    '

    it "coerces numbers", -> COPILOT\eval_once '
      (expect= true (not 0))
      (expect= false (not 1))
      (expect= false (not -1))
      (expect= false (not 1024))
    '

  describe "or", ->
    it "accepts any number of mixed arguments", -> COPILOT\eval_once '
      (expect= false (or false 0))
      (expect= true (or 1 0))
      (expect= false (or 0 false 0 0 0 0))
      (expect= true (or 0 0 0 true 0 0))
      (expect= true (or 0 true 0 1 0 0))
    '

  describe "and", ->
    it "accepts any number of mixed arguments", -> COPILOT\eval_once '
      (expect= false (and false 1))
      (expect= true (and 1 true))
      (expect= false (and false 0))
      (expect= false (and 1 1 true 0))
      (expect= true (and 1 1 true true 1))
      (expect= true (and 1 1 1))
    '

  describe "<", ->
    it "is aliased as asc?", -> COPILOT\eval_once '
      (expect= < asc?)
    '

    it "compares numbers as expected", -> COPILOT\eval_once '
      (assert (< -2 5))
      (assert (not (< 3 1)))
      (assert (not (< 1 1)))
    '

    it "can handle multiple arguments", -> COPILOT\eval_once '
      (assert (< 1 2 3))
      (assert (not (< 2 3.5 3 4)))
      (assert (not (< 3 2 1)))
    '

  describe "<=", ->
    it "compares numbers as expected", -> COPILOT\eval_once '
      (assert (<= 1 1))
      (assert (<= -2 2))
      (assert (not (<= 3 2)))
    '

    it "can handle multiple arguments", -> COPILOT\eval_once '
      (assert (<= 1 2 3))
      (assert (<= 1 2 2 3))
      (assert (not (<= 2 3 2.5 4)))
      (assert (not (<= 3 2 2 1)))
    '

  describe ">", ->
    it "is aliased as desc?", -> COPILOT\eval_once '
      (expect= > desc?)
    '

    it "compares numbers as expected", -> COPILOT\eval_once '
      (assert (> 3 1))
      (assert (not (> -2 5)))
      (assert (not (> 1 1)))
    '

    it "can handle multiple arguments", -> COPILOT\eval_once '
      (assert (> 3 2 1))
      (assert (not (> 5 4 3 3.5 2)))
      (assert (not (> 1 2 3)))
    '

  describe ">=", ->
    it "compares numbers as expected", -> COPILOT\eval_once '
      (assert (>= 1 1))
      (assert (>= 5 1))
      (assert (not (>= 2 3)))
    '

    it "can handle multiple arguments", -> COPILOT\eval_once '
      (assert (>= 3 2 1))
      (assert (>= 3 2 2 1))
      (assert (not (>= 5 4 3 3 3.5 2)))
      (assert (not (>= 1 2 2 3)))
    '

  describe "<, <=, >, >=", ->
    each = (fn) ->
      fn '<'
      fn '<='
      fn '>'
      fn '>='

    it "need at least two arguments", ->
      each =>
        err = assert.has.error -> COPILOT\eval_once "(#{@})"
        assert.matches "couldn't match arguments", err

        err = assert.has.error -> COPILOT\eval_once "(#{@} 1)"
        assert.matches "couldn't match arguments", err

    it "only work on numbers", ->
      each =>
        err = assert.has.error -> COPILOT\eval_once "(#{@} 1 'b')"
        assert.matches "couldn't match arguments", err

        err = assert.has.error -> COPILOT\eval_once "(#{@} 'c' 'd')"
        assert.matches "couldn't match arguments", err

        err = assert.has.error -> COPILOT\eval_once "(#{@} true 3)"
        assert.matches "couldn't match arguments", err