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
|
import Pattern, match from require 'core.pattern'
import Result, Value from require 'core'
-- wrap in non-const result
wrap = (value) ->
with Result :value
.side_inputs = { 'fake' }
-- wrap in const result
wrap_const = (value) -> Result :value
describe 'Type Pattern', ->
num = wrap Value.num 1
str = wrap Value.str 'hello'
special = wrap Value 'midi/sysex-message'
c_num = wrap_const Value.num 1
c_str = wrap_const Value.str 'hello'
describe 'simple types', ->
it 'matches self type', ->
pat = Pattern 'num'
assert.is.true pat\matches num
assert.is.true pat\matches c_num
assert.is.false pat\matches str
assert.is.false pat\matches c_str
assert.is.false pat\matches nil
it 'can contain special symbols', ->
pat = Pattern 'midi/sysex-message'
assert.is.true pat\matches special
assert.is.false pat\matches num
assert.is.false pat\matches str
assert.is.false pat\matches nil
it 'can match \'any\' type', ->
pat = Pattern 'any'
assert.is.true pat\matches num
assert.is.true pat\matches str
assert.is.true pat\matches special
assert.is.true pat\matches c_num
assert.is.true pat\matches c_str
assert.is.false pat\matches nil
it 'checks const-ness', ->
pat = Pattern '=any'
assert.is.true pat.const
assert.is.true pat\matches c_num
assert.is.true pat\matches c_str
assert.is.false pat\matches num
assert.is.false pat\matches str
assert.is.false pat\matches nil
pat = Pattern '=num'
assert.is.true pat.const
assert.is.true pat\matches c_num
assert.is.false pat\matches c_str
assert.is.false pat\matches num
assert.is.false pat\matches str
assert.is.false pat\matches nil
describe 'can capture', ->
it 'matches', ->
pat = Pattern 'str'
stream = {str, num}
assert.is.equal str, pat\match stream
assert.is.same {num}, stream
it 'errors if not given', ->
pat = Pattern 'str'
stream = {}
assert.has.error -> pat\match stream
stream = {num, str}
assert.has.error -> pat\match stream
assert.is.same {num, str}, stream
describe 'optional types', ->
pat = Pattern 'num?'
it 'at the end', ->
stream = {}
assert.is.false, pat\match stream
it 'in the middle', ->
stream = {str}
assert.is.false, pat\match stream
describe 'splats', ->
pat = Pattern '*num'
it 'parses', ->
assert.is.true pat.splat
assert.is.equal 'num', pat.type
it 'at the end', ->
stream = {}
assert.has.error -> pat\match stream
assert.is.same {}, stream
stream = {num}
assert.is.same {num}, pat\match stream
assert.is.same {}, stream
stream = {num, num}
assert.is.same {num, num}, pat\match stream
assert.is.same {}, stream
it 'in the middle', ->
stream = {str}
assert.has.error -> pat\match stream
assert.is.same {str}, stream
stream = {num, str}
assert.is.same {num}, pat\match stream
assert.is.same {str}, stream
stream = {num, num, str}
assert.is.same {num, num}, pat\match stream
assert.is.same {str}, stream
describe 'optional splats', ->
pat = Pattern '*num?'
it 'parses', ->
assert.is.true pat.splat
assert.is.true pat.opt
assert.is.equal 'num', pat.type
it 'at the end', ->
stream = {}
assert.is.same {}, pat\match stream
assert.is.same {}, stream
stream = {num}
assert.is.same {num}, pat\match stream
assert.is.same {}, stream
stream = {num, num}
assert.is.same {num, num}, pat\match stream
assert.is.same {}, stream
it 'in the middle', ->
stream = {str}
assert.is.same {}, pat\match stream
assert.is.same {str}, stream
stream = {num, str}
assert.is.same {num}, pat\match stream
assert.is.same {str}, stream
stream = {num, num, str}
assert.is.same {num, num}, pat\match stream
assert.is.same {str}, stream
describe 'match', ->
num = wrap Value.num 1
str = wrap Value.str 'hello'
c_num = wrap_const Value.num 1
c_str = wrap_const Value.str 'hello'
it 'matches lists', ->
assert.is.same {num, num, str}, match 'num num str', {num, num, str}
assert.is.same {num, str}, match 'num str', {num, str}
assert.is.same {c_num, str}, match '=num str', {c_num, str}
it 'throws type errors', ->
assert.has.error -> match 'str num str', {num, num, str}
assert.has.error -> match 'num str num', {num, str}
assert.has.error -> match '=num str', {num, str}
it 'throws extra arg errors', ->
assert.has.error -> match 'num str', {num, str, str}
assert.has.error -> match {}, {num}
assert.has.error -> match '*num', {num, num, str}
it 'matches optional arguments', ->
assert.is.same {str, num, str}, match 'str num? str', {str, num, str}
assert.is.same {str, nil, str}, match 'str num? str', {str, str}
assert.is.same {str, nil}, match 'str num?', {str}
it 'matches splats', ->
assert.is.same {{c_str, str, str}, num}, match '*str num', {c_str, str, str, num}
assert.is.same {c_str, {str, str}}, match 'any? *str', {c_str, str, str}
assert.has.error -> match '*str num', {num}
assert.has.error -> match 'any? *str', {str}
it 'matches optional splats', ->
assert.is.same {{c_str, str, str}, num}, match '*str? num', {c_str, str, str, num}
assert.is.same {c_str, {str, str}}, match 'any? *str?', {c_str, str, str}
assert.is.same {{}, num}, match '*str? num', {num}
assert.is.same {str, {}}, match 'any? *str?', {str}
|