squiffy-runtime 7.0.0-beta.6 → 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.
- package/dist/assets.d.ts +1 -0
- package/dist/classes.test.d.ts +1 -0
- package/dist/confirmDialog.d.ts +8 -0
- package/dist/events.d.ts +11 -0
- package/dist/every.d.ts +12 -0
- package/dist/every.test.d.ts +1 -0
- package/dist/paths.d.ts +15 -0
- package/dist/paths.test.d.ts +1 -0
- package/dist/playerSettings.d.ts +17 -0
- package/dist/preview-host.d.ts +2 -0
- package/dist/preview-host.js +10 -0
- package/dist/preview-host.js.map +2 -2
- package/dist/preview-html.js +1 -1
- package/dist/preview-protocol.d.ts +6 -0
- package/dist/settings.test.d.ts +1 -0
- package/dist/settingsDialog.d.ts +17 -0
- package/dist/skill.test.d.ts +1 -0
- package/dist/sound.d.ts +35 -0
- package/dist/sound.test.d.ts +1 -0
- package/dist/squiffy.page.css +14 -3
- package/dist/squiffy.preview.html +680 -82
- package/dist/squiffy.runtime.css +531 -1
- package/dist/squiffy.runtime.d.ts +5 -1
- package/dist/squiffy.runtime.global.js +117 -96
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +5728 -4520
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/state.d.ts +4 -1
- package/dist/style.test.d.ts +1 -0
- package/dist/textProcessor.d.ts +16 -0
- package/dist/transitions.test.d.ts +1 -0
- package/dist/types.d.ts +54 -0
- package/dist/uiLinks.test.d.ts +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.test.d.ts +1 -0
- package/package.json +3 -3
- package/src/__snapshots__/squiffy.runtime.test.ts.snap +10 -10
- package/src/animation.test.ts +44 -0
- package/src/animation.ts +9 -5
- package/src/assets.test.ts +1 -3
- package/src/assets.ts +1 -1
- package/src/classes.test.ts +201 -0
- package/src/confirmDialog.ts +66 -0
- package/src/events.ts +18 -0
- package/src/every.test.ts +25 -0
- package/src/every.ts +94 -0
- package/src/panels.test.ts +24 -0
- package/src/paths.test.ts +66 -0
- package/src/paths.ts +155 -0
- package/src/playerSettings.ts +128 -0
- package/src/plugins/animate.ts +16 -3
- package/src/plugins/live.ts +10 -4
- package/src/plugins/plugins.test.ts +51 -6
- package/src/preview-host.test.ts +22 -0
- package/src/preview-host.ts +15 -0
- package/src/preview-protocol.ts +9 -1
- package/src/preview.test.ts +114 -3
- package/src/preview.ts +97 -3
- package/src/settings.test.ts +233 -0
- package/src/settingsDialog.ts +160 -0
- package/src/skill.test.ts +67 -0
- package/src/sound.test.ts +273 -0
- package/src/sound.ts +207 -0
- package/src/squiffy.page.css +14 -3
- package/src/squiffy.runtime.css +531 -1
- package/src/squiffy.runtime.test.ts +1130 -17
- package/src/squiffy.runtime.ts +600 -38
- package/src/state.ts +45 -10
- package/src/style.test.ts +89 -0
- package/src/textProcessor.ts +484 -35
- package/src/transitions.test.ts +302 -0
- package/src/types.ts +79 -0
- package/src/uiLinks.test.ts +156 -0
- package/src/updater.ts +26 -8
- package/src/utils.test.ts +60 -0
- package/src/utils.ts +44 -14
- package/dist/infixExpressions.d.ts +0 -6
- package/src/infixExpressions.test.ts +0 -379
- package/src/infixExpressions.ts +0 -488
- /package/dist/{infixExpressions.test.d.ts → animation.test.d.ts} +0 -0
package/src/utils.ts
CHANGED
|
@@ -1,17 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// {{#replace}}: fades the label's text out, replaces it, and fades it in. The section's links wait
|
|
2
|
+
// for it, so it always finishes: see transitionEnd.
|
|
3
|
+
export async function fadeReplace(element: HTMLElement, text: string): Promise<void> {
|
|
4
|
+
const fadedOut = transitionEnd(element);
|
|
5
|
+
element.classList.add("fade-out");
|
|
6
|
+
await fadedOut;
|
|
5
7
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
element.innerHTML = text;
|
|
9
|
+
const fadedIn = transitionEnd(element);
|
|
10
|
+
element.classList.remove("fade-out");
|
|
11
|
+
element.classList.add("fade-in");
|
|
12
|
+
await fadedIn;
|
|
13
|
+
element.classList.remove("fade-in");
|
|
14
|
+
}
|
|
10
15
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
// When the element's own transition ends. Not one inside it, whose transitionend bubbles up to it,
|
|
17
|
+
// such as the colour of a link that's just been clicked. And not more than a moment longer than
|
|
18
|
+
// the fades take (200ms), in case it doesn't run at all, e.g. if the element isn't shown, or its
|
|
19
|
+
// classes change back within a frame.
|
|
20
|
+
function transitionEnd(element: HTMLElement, timeout = 1000): Promise<void> {
|
|
21
|
+
return new Promise(resolve => {
|
|
22
|
+
const done = () => {
|
|
23
|
+
element.removeEventListener("transitionend", ended);
|
|
24
|
+
element.removeEventListener("transitioncancel", ended);
|
|
25
|
+
clearTimeout(timer);
|
|
26
|
+
resolve();
|
|
27
|
+
};
|
|
28
|
+
const ended = (event: Event) => {
|
|
29
|
+
if (event.target === element) done();
|
|
30
|
+
};
|
|
31
|
+
element.addEventListener("transitionend", ended);
|
|
32
|
+
element.addEventListener("transitioncancel", ended);
|
|
33
|
+
const timer = setTimeout(done, timeout);
|
|
16
34
|
});
|
|
17
|
-
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// A delay in milliseconds (#451): a number, or text with a number of seconds or milliseconds in
|
|
38
|
+
// it, as in CSS and Twine, e.g. "3s", "1.5s" or "500ms" ("500" is milliseconds too). Returns null
|
|
39
|
+
// if it isn't one, or is negative.
|
|
40
|
+
export function parseDelay(value: unknown): number | null {
|
|
41
|
+
if (typeof value == "number") return Number.isFinite(value) && value >= 0 ? value : null;
|
|
42
|
+
if (typeof value != "string") return null;
|
|
43
|
+
const match = /^\s*(\d+(?:\.\d+)?|\.\d+)\s*(ms|s)?\s*$/i.exec(value);
|
|
44
|
+
if (!match) return null;
|
|
45
|
+
const n = Number(match[1]);
|
|
46
|
+
return match[2]?.toLowerCase() == "s" ? n * 1000 : n;
|
|
47
|
+
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export declare function rewriteInfixCondition(condition: string): string | null;
|
|
2
|
-
export declare function rewriteInfixArguments(args: string): string | null;
|
|
3
|
-
export declare function rewriteDisplayExpression(expression: string): string;
|
|
4
|
-
export declare function rewriteValueExpression(expression: string): string;
|
|
5
|
-
export declare function splitSetters(setters: string): string[];
|
|
6
|
-
export declare function rewriteInfixExpressions(text: string): string;
|
|
@@ -1,379 +0,0 @@
|
|
|
1
|
-
import { expect, test, describe } from "vitest";
|
|
2
|
-
import { rewriteDisplayExpression, rewriteInfixArguments, 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("rewriteDisplayExpression", () => {
|
|
249
|
-
test.each([
|
|
250
|
-
["price * 2", "mul price 2"],
|
|
251
|
-
["gold + price * 2", "add gold (mul price 2)"],
|
|
252
|
-
["a > b", "gt a b"],
|
|
253
|
-
["gold", "gold"],
|
|
254
|
-
[" (gold) ", "gold"],
|
|
255
|
-
["(length \"items\")", "length \"items\""],
|
|
256
|
-
["-gold", "sub 0 gold"],
|
|
257
|
-
])("%s", (input, expected) => {
|
|
258
|
-
expect(rewriteDisplayExpression(input)).toBe(expected);
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
test.each([
|
|
262
|
-
["", "expected a value"],
|
|
263
|
-
["a +", "expected a value after '+'"],
|
|
264
|
-
["a b", "unexpected 'b'"],
|
|
265
|
-
["5", "expected an attribute or a calculation"],
|
|
266
|
-
["\"text\"", "expected an attribute or a calculation"],
|
|
267
|
-
["\"unterminated", "unterminated string"],
|
|
268
|
-
])("error: %s", (input, message) => {
|
|
269
|
-
expect(() => rewriteDisplayExpression(input)).toThrow(message);
|
|
270
|
-
});
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
describe("rewriteInfixExpressions", () => {
|
|
274
|
-
test("#if", () => {
|
|
275
|
-
expect(rewriteInfixExpressions("{{#if score > 100 and has_key}}yes{{/if}}"))
|
|
276
|
-
.toBe("{{#if (and (gt score 100) has_key)}}yes{{/if}}");
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
test("#unless", () => {
|
|
280
|
-
expect(rewriteInfixExpressions("{{#unless not (seen \"intro\") or count >= 3}}x{{/unless}}"))
|
|
281
|
-
.toBe("{{#unless (or (not (seen \"intro\")) (gte count 3))}}x{{/unless}}");
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
test("else if / else unless", () => {
|
|
285
|
-
expect(rewriteInfixExpressions("{{#if a = 1}}one{{else if a = 2}}two{{else unless a = 3}}not three{{else}}three{{/if}}"))
|
|
286
|
-
.toBe("{{#if (eq a 1)}}one{{else if (eq a 2)}}two{{else unless (eq a 3)}}not three{{else}}three{{/if}}");
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
test("whitespace control", () => {
|
|
290
|
-
expect(rewriteInfixExpressions("{{~#if a > 1~}}x{{~else if a < 0 ~}}y{{/if}}"))
|
|
291
|
-
.toBe("{{~#if (gt a 1)~}}x{{~else if (lt a 0)~}}y{{/if}}");
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
test("!x with no space after the helper name", () => {
|
|
295
|
-
expect(rewriteInfixExpressions("{{#if !x}}y{{/if}}")).toBe("{{#if (not x)}}y{{/if}}");
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
test("}} inside a string", () => {
|
|
299
|
-
expect(rewriteInfixExpressions("{{#if x = \"}}\"}}y{{/if}}")).toBe("{{#if (eq x \"}}\")}}y{{/if}}");
|
|
300
|
-
});
|
|
301
|
-
|
|
302
|
-
test("leaves prefix syntax and other text untouched", () => {
|
|
303
|
-
const text = "Score: {{score}}. {{#if (gt score 100)}}High{{/if}} {{#each items}}{{this}}{{/each}} a > b";
|
|
304
|
-
expect(rewriteInfixExpressions(text)).toBe(text);
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
test("leaves escaped mustaches untouched", () => {
|
|
308
|
-
expect(rewriteInfixExpressions("\\{{#if a > 1}}")).toBe("\\{{#if a > 1}}");
|
|
309
|
-
expect(rewriteInfixExpressions("\\\\{{#if a > 1}}x{{/if}}")).toBe("\\\\{{#if (gt a 1)}}x{{/if}}");
|
|
310
|
-
});
|
|
311
|
-
|
|
312
|
-
test("doesn't match helpers that only start with if", () => {
|
|
313
|
-
expect(rewriteInfixExpressions("{{#iffy a > 1}}")).toBe("{{#iffy a > 1}}");
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
test("multiline", () => {
|
|
317
|
-
expect(rewriteInfixExpressions("{{#if a > 1\n and b}}\nx\n{{/if}}")).toBe("{{#if (and (gt a 1) b)}}\nx\n{{/if}}");
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
test("throws for malformed infix", () => {
|
|
321
|
-
expect(() => rewriteInfixExpressions("{{#if score >}}x{{/if}}")).toThrow("Invalid condition \"score >\"");
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
test("{{set}}, {{inc}} and {{dec}}", () => {
|
|
325
|
-
expect(rewriteInfixExpressions("{{set \"gold\" gold + price * 2}}{{inc \"gold\" price * 2}}{{dec \"hp\" dmg - armour}}"))
|
|
326
|
-
.toBe("{{set \"gold\" (add gold (mul price 2))}}{{inc \"gold\" (mul price 2)}}{{dec \"hp\" (sub dmg armour)}}");
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
test("{{set}} with prefix syntax is untouched", () => {
|
|
330
|
-
const text = "{{set \"score\" 100}}{{inc \"x\"}}{{set \"items\" (array \"a\")}}{{settings}}{{inc}}";
|
|
331
|
-
expect(rewriteInfixExpressions(text)).toBe(text);
|
|
332
|
-
});
|
|
333
|
-
|
|
334
|
-
test("{{= expression}}", () => {
|
|
335
|
-
expect(rewriteInfixExpressions("Cost: {{= price * 2}}, {{~= a + b ~}} and {{= gold}}."))
|
|
336
|
-
.toBe("Cost: {{mul price 2}}, {{~add a b~}} and {{gold}}.");
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
test("escaped {{= }} is untouched", () => {
|
|
340
|
-
expect(rewriteInfixExpressions("\\{{= a + b}}")).toBe("\\{{= a + b}}");
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
test("throws for malformed {{set}} and {{= }}", () => {
|
|
344
|
-
expect(() => rewriteInfixExpressions("{{set \"x\" a +}}")).toThrow("Invalid expression \"\"x\" a +\"");
|
|
345
|
-
expect(() => rewriteInfixExpressions("{{= a +}}")).toThrow("Invalid expression \"a +\"");
|
|
346
|
-
});
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
describe("rewriteValueExpression", () => {
|
|
350
|
-
test.each([
|
|
351
|
-
["gold", "gold"],
|
|
352
|
-
["10", "10"],
|
|
353
|
-
['"sword"', '"sword"'],
|
|
354
|
-
["true", "true"],
|
|
355
|
-
["gold + price * 2", "(add gold (mul price 2))"],
|
|
356
|
-
["gold > 100", "(gt gold 100)"],
|
|
357
|
-
["not has_key", "(not has_key)"],
|
|
358
|
-
['(seen "intro")', '(seen "intro")'],
|
|
359
|
-
])("%s -> %s", (input, expected) => {
|
|
360
|
-
expect(rewriteValueExpression(input)).toBe(expected);
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
test.each(["", "gold +", "gold price", "(gold"])("rejects %j", input => {
|
|
364
|
-
expect(() => rewriteValueExpression(input)).toThrow(/Invalid expression/);
|
|
365
|
-
});
|
|
366
|
-
});
|
|
367
|
-
|
|
368
|
-
describe("splitSetters", () => {
|
|
369
|
-
test.each([
|
|
370
|
-
["a = 1, b += 2", ["a = 1", "b += 2"]],
|
|
371
|
-
[' message = "Hi, there", done', ['message = "Hi, there"', "done"]],
|
|
372
|
-
["name = 'Bob, the Brave', x", ["name = 'Bob, the Brave'", "x"]],
|
|
373
|
-
['a = "say \\"hi, there\\"", b', ['a = "say \\"hi, there\\""', "b"]],
|
|
374
|
-
["a = (add b 1), c", ["a = (add b 1)", "c"]],
|
|
375
|
-
["a = 1,, b = 2,", ["a = 1", "b = 2"]],
|
|
376
|
-
])("%j", (input, expected) => {
|
|
377
|
-
expect(splitSetters(input)).toEqual(expected);
|
|
378
|
-
});
|
|
379
|
-
});
|