-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmavoscript.html
361 lines (343 loc) · 8.33 KB
/
mavoscript.html
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MavoScript Tests</title>
<link rel="stylesheet" href="https://get.mavo.io/mavo.css" />
<link rel="stylesheet" href="style.css" />
<style>
td {
font: bold 100%/1.5 Consolas, Monaco, monospace;
}
table[data-test="expressionRewrite"][data-columns="3"] td:nth-child(n + 2) {
white-space: pre;
overflow: auto;
}
</style>
<script src="https://get.mavo.io/mavo.js"></script>
<script src="mavotest.js"></script>
</head>
<body>
<h1>MavoScript Tests</h1>
<section>
<h1>Expression Rewrite</h1>
<p>These tests check if MavoScript is rewritten to JS correctly</p>
<table class="reftest" data-test="expressionRewrite" data-columns="3">
<tr>
<td>1 + 1</td>
<td>$fn.addition(1, 1)</td>
</tr>
<tr title="Operator flattening">
<td>1 + 2 + 3</td>
<td>$fn.addition(1, 2, 3)</td>
</tr>
<tr>
<td>!foo</td>
<td>$fn.not(foo)</td>
</tr>
<!-- <tr title="Currently failing">
<td>not foo</td>
<td>$fn.not(foo)</td>
</tr> -->
<tr title="Do not parse as not operator">
<td>notes</td>
<td>notes</td>
</tr>
<tr>
<td>-1</td>
<td>$fn.minus(1)</td>
</tr>
<tr>
<td>+1</td>
<td>$fn.plus(1)</td>
</tr>
<tr>
<td>if(2, 3, 4)</td>
<td>$fn.iff(2, 3, 4)</td>
</tr>
<tr>
<td>foo and 5 or baz</td>
<td>$fn.or($fn.and(foo, 5), baz)</td>
</tr>
<tr title="Do not parse as and operator">
<td>bands</td>
<td>bands</td>
</tr>
<tr>
<td>"foo"</td>
</tr>
<tr>
<td>if(foo >= 5, 1, 2)</td>
<td>$fn.iff($fn.gte(foo, 5), 1, 2)</td>
</tr>
<tr>
<td>foo == "bar"</td>
<td>$fn.eq(foo, "bar")</td>
</tr>
<tr title="Single character equals">
<td>foo = 5</td>
<td>$fn.eq(foo, 5)</td>
</tr>
<tr>
<td>foo > bar</td>
<td>$fn.gt(foo, bar)</td>
</tr>
<tr title="3 operand comparison">
<td>foo > bar > baz</td>
<td>$fn.gt(foo, bar, baz)</td>
</tr>
<tr title="3 operand equality">
<td>foo = bar = baz</td>
<td>$fn.eq(foo, bar, baz)</td>
</tr>
<tr title="4 operand equality">
<td>foo = bar = baz = yolo</td>
<td>$fn.eq(foo, bar, baz, yolo)</td>
</tr>
<tr title="4 operand equality">
<td>foo = bar = baz == yolo</td>
<td>$fn.eq(foo, bar, baz, yolo)</td>
</tr>
<tr title="4 operand heterogeneous comparison">
<td>foo > bar >= baz < yolo</td>
<td>$fn.compare(foo, "gt", bar, "gte", baz, "lt", yolo)</td>
</tr>
<tr title="4 operand heterogeneous equality and comparison">
<td>foo > bar = baz <= yolo</td>
<td>$fn.compare(foo, "gt", bar, "eq", baz, "lte", yolo)</td>
</tr>
<tr>
<td>foo != bar</td>
<td>$fn.neq(foo, bar)</td>
</tr>
<tr>
<td>foo != bar != baz</td>
<td>$fn.neq(foo, bar, baz)</td>
</tr>
<tr>
<td>foo & bar & baz</td>
<td>$fn.concatenate(foo, bar, baz)</td>
</tr>
<tr>
<td>foo.bar</td>
<td>$fn.get(foo, "bar")</td>
</tr>
<tr>
<td>foo.bar.baz[0]</td>
<td>$fn.get(foo, "bar", "baz", 0)</td>
</tr>
<tr>
<td>foo.bar()</td>
<td>foo.bar()</td>
</tr>
<tr>
<td>foo[bar]</td>
<td>$fn.get(foo, bar)</td>
</tr>
<tr>
<td>foo["bar"]</td>
<td>$fn.get(foo, "bar")</td>
</tr>
<tr>
<td>foo.bar + 1</td>
<td>$fn.addition($fn.get(foo, "bar"), 1)</td>
</tr>
<tr>
<td>foo: "bar"</td>
<td>$fn.keyvalue("foo", "bar")</td>
</tr>
<tr>
<td>"foo": "bar"</td>
<td>$fn.keyvalue("foo", "bar")</td>
</tr>
<tr>
<td>"foo": "bar" : "baz"</td>
<td>$fn.keyvalue("foo", "bar", "baz")</td>
</tr>
<tr>
<td>this.prop</td>
<td>$fn.get($this, "prop")</td>
</tr>
<tr>
<td>task where done</td>
<td>$fn.filter(task, <script>print(Mavo.Script.serializeScopeCall(["task", "done"]))</script>)</td>
</tr>
<tr>
<td>person where age > 6</td>
<td>$fn.filter(person, <script>print(Mavo.Script.serializeScopeCall(["person", "$fn.gt(age, 6)"]))</script>)</td>
</tr>
<tr>
<td>person where age > 6 where gender = 'female'</td>
<td>$fn.filter(person, <script>print(Mavo.Script.serializeScopeCall(["person", "$fn.gt(age, 6)"]) + ", ", Mavo.Script.serializeScopeCall(["person", "$fn.eq(gender, 'female')"]))</script>)</td>
</tr>
<tr>
<td>1 .. 5</td>
<td>$fn.range(1, 5)</td>
</tr>
<tr title="Regular JS" data-error>
<td>foo.filter(a => a + 5)</td>
</tr>
<tr title="Regular JS" data-error>
<td>/* js */ 5+5</td>
</tr>
<tr title="Context-sensitive functions">
<td>duration(1)</td>
<td>$fn.duration.call($this, 1)</td>
</tr>
</table>
</section>
<section>
<h1>Function Rewrite</h1>
<p>These tests check if functions are rewritten to JS correctly</p>
<table class="reftest" data-test="expressionRewrite" data-columns="3">
<tr title="Normal function">
<td>count(foo)</td>
<td>$fn.count(foo)</td>
</tr>
<tr title="Normal function, explicit $fn">
<td>$fn.range(1, 5)</td>
<td>$fn.range(1, 5)</td>
</tr>
<tr title="Rewrite function that clashes with prototype function">
<td>split(solution)</td>
<td>$fn.split(solution)</td>
</tr>
<tr title="Member">
<td>foo.slice()</td>
</tr>
<tr title="Global">
<td>CSS.supports("filter")</td>
</tr>
<tr title="Multiple levels">
<td>document.body.firstChild.contains(foo)</td>
</tr>
</table>
</section>
<section mv-app mv-source='data:application/json,{"url": "https://mavo.io"}'>
<h1>Expression resolution</h1>
<script>
window.foo = 5;
</script>
<table class="reftest" id="traptests">
<tr title="Unquoted strings">
<td>[foobar]</td>
<td>foobar</td>
</tr>
<tr title="Global variables">
<td>[foo]</td>
<td>5</td>
</tr>
<tr title="Functions">
<td>[addition(1, 2)]</td>
<td>3</td>
</tr>
<tr title="Case insenstive functions">
<td>[SUBTRACT(1, 2)]</td>
<td>-1</td>
</tr>
<tr title="Unquoted strings that are function names">
<td>[Average]</td>
<td>Average</td>
</tr>
<tr title="Same as above, different casing">
<td>[AVERAGE]</td>
<td>AVERAGE</td>
</tr>
<tr title="Unquoted strings that are action names">
<td>[set]</td>
<td>set</td>
</tr>
<tr title="Math function">
<td>[pow(2, 2)]</td>
<td>4</td>
</tr>
<tr title="Math function case insensitive">
<td>[POW(2, 2)]</td>
<td>4</td>
</tr>
<tr title="Math constant">
<td>[PI & ""]</td>
<td>3.141592653589793</td>
</tr>
<tr title="Math constant, lowercase">
<td>[pi & ""]</td>
<td>3.141592653589793</td>
</tr>
<tr title="Unquoted strings that are Math function names">
<td>[pow]</td>
<td>pow</td>
</tr>
<tr title="Property name with same name as function">
<td>
<span property="year">2000</span>
[year($today)]
</td>
<td>2000 <script>document.write(new Date().getFullYear())</script></td>
</tr>
<tr title="Property in data with same name as function">
<td>
"[url('foo')]"
</td>
<td>""</td>
</tr>
<tr title="Member expressions should still work">
<td>[Math.pow(2,2)]</td>
<td>4</td>
</tr>
<tr title="Member expressions should still work">
<td><meta property="array" mv-value="[1.5, 2.5, 3.5]" /> [array.map(Math.round)]</td>
<td>2, 3, 4</td>
</tr>
</table>
<script>
for (let test of $$("#traptests tr")) {
$.create("td", {
textContent: test.cells[0].textContent,
before: test.cells[0],
"mv-expressions": "none"
});
}
</script>
</section>
<section>
<h1>where</h1>
<pre id="where-data">{
"col1": [{"name": "name1", "foo": "foobar1"}, {"name": "name2", "foo": "foo2"}],
"col2": [{"name": "name2", "bar": "foobar1"}, {"name": "name3", "bar": "bar2", "sub": {"prop": "foobar1"}}]
}</pre>
<table class="reftest" mv-app mv-storage="#where-data">
<tr>
<td>
<div mv-multiple="col1">
<span property="name">Name Text</span>
<span property="foo">Foo Text</span>
</div>
</td>
<td>
</td>
</tr>
<tr mv-multiple="col2" title="Descendant, see #484" class="ignore">
<td>
<span property="name">Name Text</span>
<span property="bar">Bar Text</span>
<span property="sub"><span property="prop"> </span></span>
</td>
<td>
<p>Test child: [json(col1 where foo = bar)]
<p>Test descendant: [json(col1 where foo = prop)]
</td>
<td>
<p>Test child: [json(filter(col1, col1.foo = bar))]
<p>Test descendant: [json(filter(col1, col1.foo = prop))]
</td>
</tr>
</table>
</section>
<script>
function expressionRewrite(test, result, expected) {
var r = result.textContent = Mavo.Script.rewrite(test.textContent);
return Test.equals(r, expected.textContent);
}
</script>
</body>
</html>