aboutsummaryrefslogtreecommitdiffstats
path: root/spec/match_spec.moon
blob: 52911db4d9626364c2ea4ea6fcd187cc4f8a9b4e (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import val, evt from require 'alv.base.match'
import Result, ValueStream, EventStream from require 'alv'

mk_val = (type, const) ->
  value = ValueStream type
  with Result :value
    .side_inputs = { 'fake' } unless const

mk_evt = (type, const) ->
  value = EventStream type
  with Result :value
    .side_inputs = { 'fake' } unless const

describe 'val and evt', ->
  describe 'type-less shorthand', ->
    it 'matches metatype', ->
      str = mk_val 'str'
      num = mk_val 'num'
      assert.is.equal str, val!\match { str }
      assert.is.equal num, val!\match { num }
      assert.has.error -> evt!\match { str }
      assert.has.error -> evt!\match { num }

      str = mk_evt 'str'
      num = mk_evt 'num'
      assert.is.equal str, evt!\match { str }
      assert.is.equal num, evt!\match { num }
      assert.has.error -> val!\match { str }
      assert.has.error -> val!\match { num }

    it 'is in recall mode', ->
      value = val!
      event = evt!
      two_equal_values = value + value
      two_equal_events = event + event

      str1 = mk_val 'str'
      str2 = mk_val 'str'
      num = mk_val 'num'
      assert.is.same { str1, str2 }, two_equal_values\match { str1, str2 }
      assert.is.same { str2, str1 }, two_equal_values\match { str2, str1 }
      assert.is.same { num, num }, two_equal_values\match { num, num }
      assert.has.error -> two_equal_values\match { str1, num }
      assert.has.error -> two_equal_values\match { num, str2 }
      assert.has.error -> two_equal_events\match { str1, str2 }

      str1 = mk_evt 'str'
      str2 = mk_evt 'str'
      num = mk_evt 'num'
      assert.is.same { str1, str2 }, two_equal_events\match { str1, str2 }
      assert.is.same { str2, str1 }, two_equal_events\match { str2, str1 }
      assert.is.same { num, num }, two_equal_events\match { num, num }
      assert.has.error -> two_equal_events\match { str1, num }
      assert.has.error -> two_equal_events\match { num, str2 }
      assert.has.error -> two_equal_values\match { str1, str2 }

    it 'stringifies well', ->
      assert.is.equal 'event!', tostring evt!
      assert.is.equal 'value', tostring val!

  describe 'typed shorthand', ->
    it 'matches by metatype', ->
      str = mk_val 'str'
      num = mk_val 'num'
      assert.is.equal str, val.str\match { str }
      assert.is.equal num, val.num\match { num }
      assert.has.error -> evt.str\match { str }
      assert.has.error -> evt.num\match { num }

      str = mk_evt 'str'
      num = mk_evt 'num'
      assert.is.equal str, evt.str\match { str }
      assert.is.equal num, evt.num\match { num }
      assert.has.error -> val.str\match { str }
      assert.has.error -> val.num\match { num }

    it 'matches by type', ->
      str = mk_val 'str'
      num = mk_val 'num'
      assert.is.equal str, val.str\match { str }
      assert.is.equal num, val.num\match { num }
      assert.has.error -> val.num\match { str }
      assert.has.error -> val.str\match { num }

      str = mk_evt 'str'
      num = mk_evt 'num'
      assert.is.equal str, evt.str\match { str }
      assert.is.equal num, evt.num\match { num }
      assert.has.error -> evt.num\match { str }
      assert.has.error -> evt.str\match { num }

    it 'stringifies well', ->
      assert.is.equal 'str!', tostring evt.str
      assert.is.equal 'num!', tostring evt.num
      assert.is.equal 'str', tostring val.str
      assert.is.equal 'num', tostring val.num

describe 'choice', ->
  str = mk_val 'str'
  num = mk_val 'num'
  bool = mk_val 'bool'
  choice = val.str / val.num

  it 'matches either type', ->
    assert.is.equal str, choice\match { str }
    assert.is.equal num, choice\match { num }
    assert.has.error -> choice\match { bool }

  it 'can recall the choice', ->
    same = choice!
    assert.is.equal num, same\match { num }

    same = same + same
    assert.is.same { str, str }, same\match { str, str }
    assert.is.same { num, num }, same\match { num, num }
    assert.has.error -> same\match { str, num }
    assert.has.error -> same\match { num, str }
    assert.has.error -> same\match { bool, bool }

  it 'makes inner types recall', ->
    same = (val! / evt!)!
    same = same + same
    assert.is.same { str, str }, same\match { str, str }
    assert.is.same { num, num }, same\match { num, num }
    assert.is.same { bool, bool }, same\match { bool, bool }
    assert.has.error -> same\match { str, num }
    assert.has.error -> same\match { num, str }

  it 'stringifies well', ->
    assert.is.equal '(str | num)', tostring choice

describe 'sequence', ->
  str = mk_val 'str'
  num = mk_val 'num'
  bool = mk_evt 'bool'
  seq = val.str + val.num + evt.bool

  it 'matches all types in order', ->
    assert.is.same { str, num, bool }, seq\match { str, num, bool }

  it 'can assign non-numeric keys', ->
    named = seq\named 'str', 'num', 'bool'
    assert.is.same { :str, :num, :bool }, named\match { str, num, bool }
    assert.is.same { str, num, bool }, seq\match { str, num, bool }

  it 'fails if too little arguments', ->
    assert.has.error -> seq\match { str, num }

  it 'fails if too many arguments', ->
    assert.has.error -> seq\match { str, num, bool, bool }

  it 'can handle optional children', ->
    opt = -val.str + val.num
    assert.is.same { str, num }, opt\match { str, num }
    assert.is.same { nil, num }, opt\match { num }
    assert.has.error -> opt\match { str, str, num }
    assert.has.error -> opt\match { str, num, num }

  it 'can handle repeat children', ->
    rep = val.str + val.num*2
    assert.is.same { str, {num} }, rep\match { str, num }
    assert.is.same { str, {num,num} }, rep\match { str, num, num }
    assert.has.error -> rep\match { str }
    assert.has.error -> rep\match { str, num, num, num }

  it 'stringifies well', ->
    assert.is.equal '(str num bool!)', tostring seq

describe 'repeat', ->
  str = mk_val 'str'
  num = mk_val 'num'

  times = (n, arg) -> return for i=1,n do arg

  it '*x is [1,x[', ->
    rep = val.str*3
    assert.has.error -> rep\match (times 0, str)
    assert.is.same (times 1, str), rep\match (times 1, str)
    assert.is.same (times 2, str), rep\match (times 2, str)
    assert.is.same (times 3, str), rep\match (times 3, str)
    assert.has.error -> rep\match (times 4, str)
    assert.has.error -> rep\match (times 3, num)

  it '*0 is [1,[', ->
    rep = val.str*0
    assert.has.error -> rep\match (times 0, str)
    assert.is.same (times 1, str), rep\match (times 1, str)
    assert.is.same (times 2, str), rep\match (times 2, str)
    assert.is.same (times 20, str), rep\match (times 20, str)
    assert.has.error -> rep\match (times 3, num)

  it '^x is [0,x[', ->
    rep = val.str^3
    assert.is.same {}, rep\match {}
    assert.is.same (times 1, str), rep\match (times 1, str)
    assert.is.same (times 2, str), rep\match (times 2, str)
    assert.is.same (times 3, str), rep\match (times 3, str)
    assert.has.error -> rep\match (times 4, str)
    assert.has.error -> rep\match (times 3, num)

  it '^0 is [0,[', ->
    rep = val.str^0
    assert.is.same {}, rep\match {}
    assert.is.same (times 1, str), rep\match (times 1, str)
    assert.is.same (times 2, str), rep\match (times 2, str)
    assert.is.same (times 20, str), rep\match (times 20, str)
    assert.has.error -> rep\match (times 3, num)

  it ':rep(min, max) does anything else', ->
    rep = val.str\rep 2, 2
    assert.has.error -> rep\match {}
    assert.has.error -> rep\match (times 1, str)
    assert.is.same (times 2, str), rep\match (times 2, str)
    assert.has.error -> rep\match (times 3, str)
    assert.has.error -> rep\match (times 2, num)

  it 'stringifies well', ->
    assert.is.equal 'str{1-3}', tostring val.str*3
    assert.is.equal 'str{1-*}', tostring val.str*0
    assert.is.equal 'str{0-*}', tostring val.str^0
    assert.is.equal 'str{2-2}', tostring val.str\rep 2, 2

describe 'complex nesting', ->
  bang = mk_evt 'bang'
  str = mk_val 'str'
  num = mk_val 'num'
  pattern = -evt.bang + val.num*4 + (val.str + (val.num / val.str))\named('key', 'val')^0

  it 'just works', ->
    assert.is.same { bang, { num, num }, {} }, pattern\match { bang, num, num }
    assert.is.same { nil, { num }, { { key: str, val: num }, { key: str, val: str } } },
                   pattern\match { num, str, num, str, str }
    assert.has.error -> pattern\match { num, str }
    assert.has.error -> pattern\match { bang, num, num, num, num, num, num }
    assert.has.error -> pattern\match { bang, bang, num }
    assert.has.error -> pattern\match { num, str, num, str }
    assert.has.error -> pattern\match { num, str, num, str, mk_val 'bool' }

  it 'stringifies well', ->
    assert.is.equal '(bang!? num{1-4} (str (num | str)){0-*})', tostring pattern