squiffy-runtime 7.0.0-beta.7 → 7.0.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/confirmDialog.d.ts +8 -0
  2. package/dist/events.d.ts +10 -0
  3. package/dist/every.d.ts +12 -0
  4. package/dist/every.test.d.ts +1 -0
  5. package/dist/playerSettings.d.ts +17 -0
  6. package/dist/preview-html.js +1 -1
  7. package/dist/settings.test.d.ts +1 -0
  8. package/dist/settingsDialog.d.ts +17 -0
  9. package/dist/squiffy.page.css +4 -1
  10. package/dist/squiffy.preview.html +673 -83
  11. package/dist/squiffy.runtime.css +531 -1
  12. package/dist/squiffy.runtime.d.ts +5 -1
  13. package/dist/squiffy.runtime.global.js +106 -85
  14. package/dist/squiffy.runtime.global.js.map +1 -1
  15. package/dist/squiffy.runtime.js +4743 -4157
  16. package/dist/squiffy.runtime.js.map +1 -1
  17. package/dist/state.d.ts +1 -0
  18. package/dist/style.test.d.ts +1 -0
  19. package/dist/textProcessor.d.ts +8 -0
  20. package/dist/transitions.test.d.ts +1 -0
  21. package/dist/types.d.ts +43 -0
  22. package/dist/uiLinks.test.d.ts +1 -0
  23. package/dist/utils.d.ts +1 -0
  24. package/dist/utils.test.d.ts +1 -0
  25. package/package.json +3 -3
  26. package/src/animation.test.ts +44 -0
  27. package/src/animation.ts +7 -3
  28. package/src/confirmDialog.ts +66 -0
  29. package/src/events.ts +17 -0
  30. package/src/every.test.ts +25 -0
  31. package/src/every.ts +94 -0
  32. package/src/panels.test.ts +24 -0
  33. package/src/playerSettings.ts +128 -0
  34. package/src/plugins/animate.ts +9 -0
  35. package/src/plugins/live.ts +3 -1
  36. package/src/plugins/plugins.test.ts +17 -0
  37. package/src/preview.test.ts +53 -3
  38. package/src/preview.ts +60 -1
  39. package/src/settings.test.ts +233 -0
  40. package/src/settingsDialog.ts +160 -0
  41. package/src/squiffy.page.css +4 -1
  42. package/src/squiffy.runtime.css +531 -1
  43. package/src/squiffy.runtime.test.ts +613 -16
  44. package/src/squiffy.runtime.ts +372 -19
  45. package/src/state.ts +16 -1
  46. package/src/style.test.ts +89 -0
  47. package/src/textProcessor.ts +141 -6
  48. package/src/transitions.test.ts +302 -0
  49. package/src/types.ts +56 -0
  50. package/src/uiLinks.test.ts +156 -0
  51. package/src/utils.test.ts +60 -0
  52. package/src/utils.ts +44 -14
  53. package/dist/infixExpressions.d.ts +0 -7
  54. package/src/infixExpressions.test.ts +0 -516
  55. package/src/infixExpressions.ts +0 -672
  56. /package/dist/{infixExpressions.test.d.ts → animation.test.d.ts} +0 -0
@@ -1,516 +0,0 @@
1
- import { expect, test, describe } from "vitest";
2
- import { rewriteDisplayExpression, rewriteInfixArguments, rewriteInfixEmbed, rewriteInfixCondition, rewriteInfixExpressions, rewriteValueExpression, splitSetters } from "./infixExpressions.js";
3
-
4
- describe("rewriteInfixCondition", () => {
5
- test.each([
6
- ["gender = \"male\"", "(eq gender \"male\")"],
7
- ["gender == \"male\"", "(eq gender \"male\")"],
8
- ["gender != \"male\"", "(ne gender \"male\")"],
9
- ["score > 100", "(gt score 100)"],
10
- ["score < 100", "(lt score 100)"],
11
- ["score >= 100", "(gte score 100)"],
12
- ["score <= 100", "(lte score 100)"],
13
- ["a and b", "(and a b)"],
14
- ["a && b", "(and a b)"],
15
- ["a or b", "(or a b)"],
16
- ["a || b", "(or a b)"],
17
- ["not a", "(not a)"],
18
- ["!a", "(not a)"],
19
- ["! a", "(not a)"],
20
- ["not not a", "(not (not a))"],
21
- ])("operator: %s", (input, expected) => {
22
- expect(rewriteInfixCondition(input)).toBe(expected);
23
- });
24
-
25
- test.each([
26
- ["score>100", "(gt score 100)"],
27
- ["gender=\"male\"", "(eq gender \"male\")"],
28
- ["a&&b||c", "(or (and a b) c)"],
29
- ["score>=-5", "(gte score -5)"],
30
- ])("no spaces: %s", (input, expected) => {
31
- expect(rewriteInfixCondition(input)).toBe(expected);
32
- });
33
-
34
- test.each([
35
- ["a and b and c", "(and a b c)"],
36
- ["a or b or c", "(or a b c)"],
37
- ["a or b and c", "(or a (and b c))"],
38
- ["a and b or c", "(or (and a b) c)"],
39
- ["score > 100 and has_key", "(and (gt score 100) has_key)"],
40
- ["x = 1 or y != 2 and z", "(or (eq x 1) (and (ne y 2) z))"],
41
- ["not a and b", "(and (not a) b)"],
42
- ["not a = b", "(not (eq a b))"],
43
- ["not gender = \"male\"", "(not (eq gender \"male\"))"],
44
- ["not score > 100 and has_key", "(and (not (gt score 100)) has_key)"],
45
- ["!a = b", "(not (eq a b))"],
46
- ["not a = b or not c", "(or (not (eq a b)) (not c))"],
47
- ["(not a) = b", "(eq (not a) b)"],
48
- ["a = (not b)", "(eq a (not b))"],
49
- ["not (seen \"intro\") or count >= 3", "(or (not (seen \"intro\")) (gte count 3))"],
50
- ])("precedence: %s", (input, expected) => {
51
- expect(rewriteInfixCondition(input)).toBe(expected);
52
- });
53
-
54
- test.each([
55
- ["(a or b) and c", "(and (or a b) c)"],
56
- ["not (a = 1)", "(not (eq a 1))"],
57
- ["((a))", null],
58
- ["((a or b)) and c", "(and (or a b) c)"],
59
- ["(score > 100) = true", "(eq (gt score 100) true)"],
60
- ])("grouping: %s", (input, expected) => {
61
- expect(rewriteInfixCondition(input)).toBe(expected);
62
- });
63
-
64
- test.each([
65
- ["name = 'Bob'", "(eq name 'Bob')"],
66
- ["name = \"a > b and c\"", "(eq name \"a > b and c\")"],
67
- ["done = true", "(eq done true)"],
68
- ["x = null", "(eq x null)"],
69
- ["score > 1.5", "(gt score 1.5)"],
70
- ["player.score > 10", "(gt player.score 10)"],
71
- ["[my score] > 10", "(gt [my score] 10)"],
72
- ["@index > 0", "(gt @index 0)"],
73
- ["(length \"rounds\") > 0", "(gt (length \"rounds\") 0)"],
74
- ["(contains \"inventory\" \"key\") and door = \"open\"", "(and (contains \"inventory\" \"key\") (eq door \"open\"))"],
75
- ["(and a b) or c", "(or (and a b) c)"],
76
- ["(at (array \"a\" \"b\")) or x", "(or (at (array \"a\" \"b\")) x)"],
77
- ["(get (concat a b)) = 1", "(eq (get (concat a b)) 1)"],
78
- ["(lookup obj key=1) = 2", "(eq (lookup obj key=1) 2)"],
79
- ["(seen (x)) and y", "(and (seen x) y)"],
80
- ])("operands: %s", (input, expected) => {
81
- expect(rewriteInfixCondition(input)).toBe(expected);
82
- });
83
-
84
- test.each([
85
- "has_key",
86
- " has_key ",
87
- "(eq gender \"male\")",
88
- "(and (gt score 100) has_key)",
89
- "(seen \"intro\")",
90
- "(at (array \"start\" \"other\"))",
91
- "x includeZero=true",
92
- "(gt (length \"rounds\") 0)",
93
- "\"string\"",
94
- "42",
95
- ])("passes through prefix syntax: %s", (input) => {
96
- expect(rewriteInfixCondition(input)).toBeNull();
97
- });
98
-
99
- test("not with prefix syntax is equivalent", () => {
100
- expect(rewriteInfixCondition("(not game_over)")).toBe("(not game_over)");
101
- });
102
-
103
- test("infix inside a helper's arguments", () => {
104
- expect(rewriteInfixCondition("(foo (a > 1))")).toBe("(foo (gt a 1))");
105
- });
106
-
107
- test.each([
108
- ["score >", "expected a value after '>'"],
109
- ["a and", "expected a value after 'and'"],
110
- ["a or", "expected a value after 'or'"],
111
- ["not", "expected a value after 'not'"],
112
- ["> 100", "expected a value before '>'"],
113
- ["and b", "expected a value before 'and'"],
114
- ["(a > 1", "expected ')' but found end of expression"],
115
- ["a > 1)", "unexpected ')'"],
116
- ["a > 1 b", "unexpected 'b'"],
117
- ["1 < x < 10", "comparisons can't be chained"],
118
- ["a = = b", "expected a value before '='"],
119
- ["a > () ", "unexpected ')'"],
120
- ["a = not b", "'not' must be inside parentheses here, e.g. (not value)"],
121
- ["a = !b", "'!' must be inside parentheses here, e.g. (!value)"],
122
- ])("error: %s", (input, message) => {
123
- expect(() => rewriteInfixCondition(input)).toThrow(message);
124
- });
125
-
126
- test.each([
127
- ["a + b > 1", "(gt (add a b) 1)"],
128
- ["a - b > 1", "(gt (sub a b) 1)"],
129
- ["a * b > 1", "(gt (mul a b) 1)"],
130
- ["a / b > 1", "(gt (div a b) 1)"],
131
- ["a % b = 1", "(eq (mod a b) 1)"],
132
- ["-a < 0", "(lt (sub 0 a) 0)"],
133
- ["- a < 0", "(lt (sub 0 a) 0)"],
134
- ["gold >= price * 2", "(gte gold (mul price 2))"],
135
- ])("arithmetic operator: %s", (input, expected) => {
136
- expect(rewriteInfixCondition(input)).toBe(expected);
137
- });
138
-
139
- test.each([
140
- ["a + b * c = 1", "(eq (add a (mul b c)) 1)"],
141
- ["a * b + c = 1", "(eq (add (mul a b) c) 1)"],
142
- ["a - b - c = 1", "(eq (sub (sub a b) c) 1)"],
143
- ["a / b / c = 1", "(eq (div (div a b) c) 1)"],
144
- ["a % b * c = 1", "(eq (mul (mod a b) c) 1)"],
145
- ["(a + b) * c = 1", "(eq (mul (add a b) c) 1)"],
146
- ["a * (b + c) = 1", "(eq (mul a (add b c)) 1)"],
147
- ["-a * b = 1", "(eq (mul (sub 0 a) b) 1)"],
148
- ["-(a + b) = 1", "(eq (sub 0 (add a b)) 1)"],
149
- ["a - -b = 1", "(eq (sub a (sub 0 b)) 1)"],
150
- ["a + b > c and not d - 1 = 0", "(and (gt (add a b) c) (not (eq (sub d 1) 0)))"],
151
- ["a > b + 1 or c", "(or (gt a (add b 1)) c)"],
152
- ["(length \"items\") * 2 > 3", "(gt (mul (length \"items\") 2) 3)"],
153
- ])("arithmetic precedence: %s", (input, expected) => {
154
- expect(rewriteInfixCondition(input)).toBe(expected);
155
- });
156
-
157
- test.each([
158
- ["a+b>1", "(gt (add a b) 1)"],
159
- ["a*b>1", "(gt (mul a b) 1)"],
160
- ["a%b>1", "(gt (mod a b) 1)"],
161
- ["a -1 > 0", "(gt (sub a 1) 0)"],
162
- ["a - 1 > 0", "(gt (sub a 1) 0)"],
163
- ["a -b > 0", "(gt (sub a b) 0)"],
164
- ["a /b > 0", "(gt (div a b) 0)"],
165
- ["a * -1 > 0", "(gt (mul a -1) 0)"],
166
- ["a > -1", "(gt a -1)"],
167
- ["1.5 * a > 0", "(gt (mul 1.5 a) 0)"],
168
- ])("arithmetic spacing: %s", (input, expected) => {
169
- expect(rewriteInfixCondition(input)).toBe(expected);
170
- });
171
-
172
- test.each([
173
- ["my-score > 1", "(gt my-score 1)"],
174
- ["a-b > 1", "(gt a-b 1)"],
175
- ["../score > 1", "(gt ../score 1)"],
176
- ["player/score > 1", "(gt player/score 1)"],
177
- ["[gold + silver] > 1", "(gt [gold + silver] 1)"],
178
- ["name = \"a + b\"", "(eq name \"a + b\")"],
179
- ])("- and / inside names and paths: %s", (input, expected) => {
180
- expect(rewriteInfixCondition(input)).toBe(expected);
181
- });
182
-
183
- test.each([
184
- "my-score",
185
- "(gt x -1)",
186
- "(foo a -1)",
187
- "(x -1)",
188
- ])("passes through prefix syntax with -: %s", (input) => {
189
- expect(rewriteInfixCondition(input)).toBeNull();
190
- });
191
-
192
- test.each([
193
- ["a +", "expected a value after '+'"],
194
- ["a * > 1", "expected a value before '>'"],
195
- ["* a", "expected a value before '*'"],
196
- ["a + + b", "expected a value before '+'"],
197
- ["-", "expected a value after '-'"],
198
- ["(a + b", "expected ')' but found end of expression"],
199
- ])("arithmetic error: %s", (input, message) => {
200
- expect(() => rewriteInfixCondition(input)).toThrow(message);
201
- });
202
-
203
- test("error message includes the condition", () => {
204
- expect(() => rewriteInfixCondition(" score > ")).toThrow("Invalid condition \"score >\": expected a value after '>'");
205
- });
206
-
207
- test("malformed prefix syntax is left for Handlebars to report", () => {
208
- expect(rewriteInfixCondition("a b")).toBeNull();
209
- expect(rewriteInfixCondition("(eq a")).toBeNull();
210
- expect(rewriteInfixCondition("\"unterminated")).toBeNull();
211
- });
212
- });
213
-
214
- describe("rewriteInfixArguments", () => {
215
- test.each([
216
- ["\"gold\" gold + price * 2", "\"gold\" (add gold (mul price 2))"],
217
- ["\"gold\" (gold + price) * 2", "\"gold\" (mul (add gold price) 2)"],
218
- ["\"name\" name + \" the Brave\"", "\"name\" (add name \" the Brave\")"],
219
- ["\"x\" -y", "\"x\" (sub 0 y)"],
220
- ["\"x\" a > b", "\"x\" (gt a b)"],
221
- ["\"x\" a and b", "\"x\" (and a b)"],
222
- ["\"x\" (length \"items\") - 1", "\"x\" (sub (length \"items\") 1)"],
223
- ])("%s", (input, expected) => {
224
- expect(rewriteInfixArguments(input)).toBe(expected);
225
- });
226
-
227
- test.each([
228
- "\"score\" 100",
229
- "\"score\" -1",
230
- "\"name\" \"Alice\"",
231
- "\"items\" (array \"a\" \"b\")",
232
- "\"x\" (add a b)",
233
- "\"counter\"",
234
- "\"x\" my-score",
235
- ])("passes through prefix syntax: %s", (input) => {
236
- expect(rewriteInfixArguments(input)).toBeNull();
237
- });
238
-
239
- test.each([
240
- ["\"gold\" gold +", "Invalid expression \"\"gold\" gold +\": expected a value after '+'"],
241
- ["\"gold\" gold + 1 x", "unexpected 'x'"],
242
- ["\"gold\" + 1", "expected a value before '+'"],
243
- ])("error: %s", (input, message) => {
244
- expect(() => rewriteInfixArguments(input)).toThrow(message);
245
- });
246
- });
247
-
248
- describe("list helpers take expressions", () => {
249
- test("rewrites {{append}}, {{prepend}} and {{remove}}", () => {
250
- expect(rewriteInfixExpressions('{{append "a" { msec: m, type: 1 }}} {{prepend "a" n + 1}} {{remove "a" x}}'))
251
- .toBe('{{append "a" (__object "msec" m "type" 1)}} {{prepend "a" (add n 1)}} {{remove "a" x}}');
252
- });
253
- });
254
-
255
- describe("rewriteInfixEmbed (#450)", () => {
256
- test.each([
257
- ['"say" hp=hp - 1', '"say" hp=(sub hp 1)'],
258
- ['"say" who="Ann" rich=gold > 100', '"say" who="Ann" rich=(gt gold 100)'],
259
- ['"say" items=[1, 2] hero={ hp: 10 }', '"say" items=(array 1 2) hero=(__object "hp" 10)'],
260
- ['"say" first=inventory[0]', '"say" first=(lookup inventory 0)'],
261
- ])("%s", (input, expected) => {
262
- expect(rewriteInfixEmbed(input)).toBe(expected);
263
- });
264
-
265
- test.each([
266
- '"say"',
267
- '"say" who="Ann" mood=mood',
268
- '"say" n=(add n 1)',
269
- ])("leaves %s alone", input => {
270
- expect(rewriteInfixEmbed(input)).toBeNull();
271
- });
272
-
273
- test("rewrites inline and block embeds in text", () => {
274
- expect(rewriteInfixExpressions('{{embed "a" n=n + 1}} {{#embed "b" ok=not done}}x{{/embed}}'))
275
- .toBe('{{embed "a" n=(add n 1)}} {{#embed "b" ok=(not done)}}x{{/embed}}');
276
- });
277
- });
278
-
279
- describe("rewriteDisplayExpression", () => {
280
- test.each([
281
- ["price * 2", "mul price 2"],
282
- ["gold + price * 2", "add gold (mul price 2)"],
283
- ["a > b", "gt a b"],
284
- ["gold", "gold"],
285
- [" (gold) ", "gold"],
286
- ["(length \"items\")", "length \"items\""],
287
- ["-gold", "sub 0 gold"],
288
- ])("%s", (input, expected) => {
289
- expect(rewriteDisplayExpression(input)).toBe(expected);
290
- });
291
-
292
- test.each([
293
- ["", "expected a value"],
294
- ["a +", "expected a value after '+'"],
295
- ["a b", "unexpected 'b'"],
296
- ["5", "expected an attribute or a calculation"],
297
- ["\"text\"", "expected an attribute or a calculation"],
298
- ["\"unterminated", "unterminated string"],
299
- ])("error: %s", (input, message) => {
300
- expect(() => rewriteDisplayExpression(input)).toThrow(message);
301
- });
302
- });
303
-
304
- describe("rewriteInfixExpressions", () => {
305
- test("#if", () => {
306
- expect(rewriteInfixExpressions("{{#if score > 100 and has_key}}yes{{/if}}"))
307
- .toBe("{{#if (and (gt score 100) has_key)}}yes{{/if}}");
308
- });
309
-
310
- test("#unless", () => {
311
- expect(rewriteInfixExpressions("{{#unless not (seen \"intro\") or count >= 3}}x{{/unless}}"))
312
- .toBe("{{#unless (or (not (seen \"intro\")) (gte count 3))}}x{{/unless}}");
313
- });
314
-
315
- test("else if / else unless", () => {
316
- expect(rewriteInfixExpressions("{{#if a = 1}}one{{else if a = 2}}two{{else unless a = 3}}not three{{else}}three{{/if}}"))
317
- .toBe("{{#if (eq a 1)}}one{{else if (eq a 2)}}two{{else unless (eq a 3)}}not three{{else}}three{{/if}}");
318
- });
319
-
320
- test("#each and #with (#447)", () => {
321
- expect(rewriteInfixExpressions("{{#each (range 1 (count + 1))}}x{{/each}}{{#with party[leader]}}y{{/with}}"))
322
- .toBe("{{#each (range 1 (add count 1))}}x{{/each}}{{#with (lookup party leader)}}y{{/with}}");
323
- // Without operators, or with block parameters, they're left as they are
324
- expect(rewriteInfixExpressions("{{#each bag}}x{{/each}}{{#each bag as |item i|}}y{{/each}}"))
325
- .toBe("{{#each bag}}x{{/each}}{{#each bag as |item i|}}y{{/each}}");
326
- });
327
-
328
- test("whitespace control", () => {
329
- expect(rewriteInfixExpressions("{{~#if a > 1~}}x{{~else if a < 0 ~}}y{{/if}}"))
330
- .toBe("{{~#if (gt a 1)~}}x{{~else if (lt a 0)~}}y{{/if}}");
331
- });
332
-
333
- test("!x with no space after the helper name", () => {
334
- expect(rewriteInfixExpressions("{{#if !x}}y{{/if}}")).toBe("{{#if (not x)}}y{{/if}}");
335
- });
336
-
337
- test("}} inside a string", () => {
338
- expect(rewriteInfixExpressions("{{#if x = \"}}\"}}y{{/if}}")).toBe("{{#if (eq x \"}}\")}}y{{/if}}");
339
- });
340
-
341
- test("leaves prefix syntax and other text untouched", () => {
342
- const text = "Score: {{score}}. {{#if (gt score 100)}}High{{/if}} {{#each items}}{{this}}{{/each}} a > b";
343
- expect(rewriteInfixExpressions(text)).toBe(text);
344
- });
345
-
346
- test("leaves escaped mustaches untouched", () => {
347
- expect(rewriteInfixExpressions("\\{{#if a > 1}}")).toBe("\\{{#if a > 1}}");
348
- expect(rewriteInfixExpressions("\\\\{{#if a > 1}}x{{/if}}")).toBe("\\\\{{#if (gt a 1)}}x{{/if}}");
349
- });
350
-
351
- test("doesn't match helpers that only start with if", () => {
352
- expect(rewriteInfixExpressions("{{#iffy a > 1}}")).toBe("{{#iffy a > 1}}");
353
- });
354
-
355
- test("multiline", () => {
356
- expect(rewriteInfixExpressions("{{#if a > 1\n and b}}\nx\n{{/if}}")).toBe("{{#if (and (gt a 1) b)}}\nx\n{{/if}}");
357
- });
358
-
359
- test("throws for malformed infix", () => {
360
- expect(() => rewriteInfixExpressions("{{#if score >}}x{{/if}}")).toThrow("Invalid condition \"score >\"");
361
- });
362
-
363
- test("{{set}}, {{inc}} and {{dec}}", () => {
364
- expect(rewriteInfixExpressions("{{set \"gold\" gold + price * 2}}{{inc \"gold\" price * 2}}{{dec \"hp\" dmg - armour}}"))
365
- .toBe("{{set \"gold\" (add gold (mul price 2))}}{{inc \"gold\" (mul price 2)}}{{dec \"hp\" (sub dmg armour)}}");
366
- });
367
-
368
- test("{{set}} with prefix syntax is untouched", () => {
369
- const text = "{{set \"score\" 100}}{{inc \"x\"}}{{set \"items\" (array \"a\")}}{{settings}}{{inc}}";
370
- expect(rewriteInfixExpressions(text)).toBe(text);
371
- });
372
-
373
- test("{{= expression}}", () => {
374
- expect(rewriteInfixExpressions("Cost: {{= price * 2}}, {{~= a + b ~}} and {{= gold}}."))
375
- .toBe("Cost: {{mul price 2}}, {{~add a b~}} and {{gold}}.");
376
- });
377
-
378
- test("escaped {{= }} is untouched", () => {
379
- expect(rewriteInfixExpressions("\\{{= a + b}}")).toBe("\\{{= a + b}}");
380
- });
381
-
382
- test("throws for malformed {{set}} and {{= }}", () => {
383
- expect(() => rewriteInfixExpressions("{{set \"x\" a +}}")).toThrow("Invalid expression \"\"x\" a +\"");
384
- expect(() => rewriteInfixExpressions("{{= a +}}")).toThrow("Invalid expression \"a +\"");
385
- });
386
- });
387
-
388
- describe("rewriteValueExpression", () => {
389
- test.each([
390
- ["gold", "gold"],
391
- ["10", "10"],
392
- ['"sword"', '"sword"'],
393
- ["true", "true"],
394
- ["gold + price * 2", "(add gold (mul price 2))"],
395
- ["gold > 100", "(gt gold 100)"],
396
- ["not has_key", "(not has_key)"],
397
- ['(seen "intro")', '(seen "intro")'],
398
- ])("%s -> %s", (input, expected) => {
399
- expect(rewriteValueExpression(input)).toBe(expected);
400
- });
401
-
402
- test.each(["", "gold +", "gold price", "(gold"])("rejects %j", input => {
403
- expect(() => rewriteValueExpression(input)).toThrow(/Invalid expression/);
404
- });
405
- });
406
-
407
- describe("splitSetters", () => {
408
- test.each([
409
- ["a = 1, b += 2", ["a = 1", "b += 2"]],
410
- [' message = "Hi, there", done', ['message = "Hi, there"', "done"]],
411
- ["name = 'Bob, the Brave', x", ["name = 'Bob, the Brave'", "x"]],
412
- ['a = "say \\"hi, there\\"", b', ['a = "say \\"hi, there\\""', "b"]],
413
- ["a = (add b 1), c", ["a = (add b 1)", "c"]],
414
- ["a = 1,, b = 2,", ["a = 1", "b = 2"]],
415
- ])("%j", (input, expected) => {
416
- expect(splitSetters(input)).toEqual(expected);
417
- });
418
- });
419
-
420
- describe("objects and lists (#442)", () => {
421
- test.each([
422
- ["{ hp: 10, name: \"Ann\" }", "(__object \"hp\" 10 \"name\" \"Ann\")"],
423
- ["{ \"max hp\": 12, 'b': 1, }", "(__object \"max hp\" 12 'b' 1)"],
424
- ["{}", "(__object )"],
425
- ["{ hp: hp + 1, inner: { a: [1] } }", "(__object \"hp\" (add hp 1) \"inner\" (__object \"a\" (array 1)))"],
426
- ["[1, 2, 3]", "(array 1 2 3)"],
427
- ["[]", "(array )"],
428
- ["[\"lamp\"]", "(array \"lamp\")"],
429
- ["[a, b]", "(array a b)"],
430
- ["[{ name: \"lamp\" }, { name: \"rope\" }]", "(array (__object \"name\" \"lamp\") (__object \"name\" \"rope\"))"],
431
- ["inventory[0]", "(lookup inventory 0)"],
432
- ["inventory[i].name", "(lookup (lookup inventory i) \"name\")"],
433
- ["grid[x][y + 1]", "(lookup (lookup grid x) (add y 1))"],
434
- ["hero[\"max hp\"]", "(lookup hero \"max hp\")"],
435
- ["inventory[i].stats.hp * 2", "(mul (lookup (lookup (lookup inventory i) \"stats\") \"hp\") 2)"],
436
- ["hero.hp", "hero.hp"],
437
- ["inventory.[0].name", "inventory.[0].name"],
438
- ["[my attribute]", "[my attribute]"],
439
- ["[a]", "[a]"],
440
- ])("value: %s", (input, expected) => {
441
- expect(rewriteValueExpression(input)).toBe(expected);
442
- });
443
-
444
- test("conditions", () => {
445
- expect(rewriteInfixCondition("inventory[i].hp > 5")).toBe("(gt (lookup (lookup inventory i) \"hp\") 5)");
446
- expect(rewriteInfixCondition("hero.hp > 5")).toBe("(gt hero.hp 5)");
447
- expect(rewriteInfixCondition("hero[key]")).toBe("(lookup hero key)");
448
- });
449
-
450
- test("an object in a tag", () => {
451
- expect(rewriteInfixExpressions("{{set \"hero\" { hp: 10, stats: { str: 2 } }}}!"))
452
- .toBe("{{set \"hero\" (__object \"hp\" 10 \"stats\" (__object \"str\" 2))}}!");
453
- expect(rewriteInfixExpressions("{{= inventory[i].name}}")).toBe("{{lookup (lookup inventory i) \"name\"}}");
454
- });
455
-
456
- test.each([
457
- ["{ hp 10 }", "expected ':'"],
458
- ["{ hp: 10", "expected '}'"],
459
- ["[1, 2", "expected ']'"],
460
- ["{ (a): 3 }", "expected a name in the object"],
461
- ["inventory[0", "expected ']'"],
462
- ])("errors: %s", (input, message) => {
463
- expect(() => rewriteValueExpression(input)).toThrow(message);
464
- });
465
-
466
- test("setters are split outside objects and lists", () => {
467
- expect(splitSetters("hero = { hp: 10, name: \"Ann\" }, list = [1, 2], gold = 0"))
468
- .toEqual(["hero = { hp: 10, name: \"Ann\" }", "list = [1, 2]", "gold = 0"]);
469
- });
470
- });
471
-
472
- describe("contains and ? : (#446)", () => {
473
- test.each([
474
- ["bag contains \"key\"", "(__contains bag \"key\")"],
475
- ["name contains \"o\" and hp > 0", "(and (__contains name \"o\") (gt hp 0))"],
476
- ["not bag contains \"key\"", "(not (__contains bag \"key\"))"],
477
- ["[1, 2] contains x + 1", "(__contains (array 1 2) (add x 1))"],
478
- ["hp > 0 ? \"alive\" : \"dead\"", "(cond (gt hp 0) \"alive\" \"dead\")"],
479
- ["a ? 1 : b ? 2 : 3", "(cond a 1 (cond b 2 3))"],
480
- ["a ? b ? 1 : 2 : 3", "(cond a (cond b 1 2) 3)"],
481
- ["x or y ? 1 : 2", "(cond (or x y) 1 2)"],
482
- ["gold + (rich ? 10 : 1)", "(add gold (cond rich 10 1))"],
483
- ["{ status: hp > 0 ? \"ok\" : \"hurt\", hp: hp }", "(__object \"status\" (cond (gt hp 0) \"ok\" \"hurt\") \"hp\" hp)"],
484
- ["[a ? 1 : 2, 3]", "(array (cond a 1 2) 3)"],
485
- ["list[a ? 0 : 1]", "(lookup list (cond a 0 1))"],
486
- ["(round (gold / 3))", "(round (div gold 3))"],
487
- ])("value: %s", (input, expected) => {
488
- expect(rewriteValueExpression(input)).toBe(expected);
489
- });
490
-
491
- test("conditions", () => {
492
- expect(rewriteInfixCondition("bag contains \"key\"")).toBe("(__contains bag \"key\")");
493
- expect(rewriteInfixCondition("(a ? b : c)")).toBe("(cond a b c)");
494
- });
495
-
496
- test("the contains helper is still a helper", () => {
497
- expect(rewriteInfixCondition("(contains \"bag\" \"key\")")).toBeNull();
498
- expect(rewriteInfixCondition("(contains \"bag\" \"key\") and x")).toBe("(and (contains \"bag\" \"key\") x)");
499
- expect(rewriteInfixCondition("contains")).toBeNull();
500
- });
501
-
502
- test("in tags", () => {
503
- expect(rewriteInfixExpressions("{{= hp > 0 ? \"alive\" : \"dead\"}}")).toBe("{{cond (gt hp 0) \"alive\" \"dead\"}}");
504
- expect(rewriteInfixExpressions("{{set \"x\" a ? 1 : 2}}")).toBe("{{set \"x\" (cond a 1 2)}}");
505
- expect(rewriteInfixExpressions("{{#if name contains \"x\"}}")).toBe("{{#if (__contains name \"x\")}}");
506
- });
507
-
508
- test.each([
509
- ["a ?", "expected a value after '?'"],
510
- ["a ? 1", "expected ':'"],
511
- ["a ? 1 :", "expected a value after ':'"],
512
- ["a contains", "expected a value after 'contains'"],
513
- ])("errors: %s", (input, message) => {
514
- expect(() => rewriteValueExpression(input)).toThrow(message);
515
- });
516
- });