Download source/test/syntax-hint.test.js from FormaLau/microduck-reachy-simulator: direct link, hf CLI and curl.
- Browser
- Download file 2.38 kB
-
https://huggingface.co/spaces/FormaLau/microduck-reachy-simulator/resolve/main/source/test/syntax-hint.test.js
- Command line
-
hf download hf://spaces/FormaLau/microduck-reachy-simulator/source/test/syntax-hint.test.js
-
curl -L -o syntax-hint.test.js https://huggingface.co/spaces/FormaLau/microduck-reachy-simulator/resolve/main/source/test/syntax-hint.test.js
2.38 kB
| import { test } from "node:test"; | |
| import assert from "node:assert/strict"; | |
| import { syntaxHintAtNewline, syntaxHintWhileEditing } from "../src/training/syntaxHint.js"; | |
| const hint = (code) => syntaxHintAtNewline(code, code.length); | |
| test("missing block brace is hinted as soon as an outdented statement starts", () => { | |
| const prefix = 'int factorial(int n)\n{\n for(int i=1; i<n; i++)\n {\n result *= i;\n'; | |
| const check = (tail) => { const text = prefix + tail; return syntaxHintWhileEditing(text, text.length); }; | |
| assert.match(check(' r').message, /closing brace/); | |
| for (const tail of [' ', ' }', ' result++;', ' // comment', ' }\n return result;']) assert.equal(check(tail), null, tail); | |
| const multiline = 'void f()\n{\n if (n)\n {\n value =\n helper'; | |
| assert.equal(syntaxHintWhileEditing(multiline, multiline.length), null); | |
| }); | |
| test("quack hint catches completed statements without semicolons", () => { | |
| for (const line of ['printf("hello")', 'int n = 2', 'char *s = "hello"', 'return 0', 'n++']) { | |
| assert.ok(hint(`int main(void) {\n${line}`), line); | |
| } | |
| assert.ok(hint('void helper(void) {\nputs("Hi")')); | |
| }); | |
| test("function spelling and malformed parameters quack while typing", () => { | |
| const live = (line) => syntaxHintWhileEditing(`int main(void) {\n ${line}`, 10000); | |
| assert.match(live('scnaf(').message, /Did you mean 'scanf'/); | |
| assert.match(live('scanf("%d"&').message, /comma/); | |
| assert.match(syntaxHintWhileEditing('int 5factorial(', 10000).message, /function name/); | |
| assert.match(syntaxHintWhileEditing('int factorial(int 5n', 10000).message, /Parameter/); | |
| }); | |
| test("legitimate newlines, comments and incomplete expressions stay quiet", () => { | |
| for (const line of ['printf("hello");', 'if (n > 0)', 'for (int i=0; i<3; i++)', 'else', 'int n = 1 +', 'printf(', '// printf("hello")', '/* explanation', 'label:', '#include <stdio.h>', '']) { | |
| assert.equal(hint(`int main(void) {\n${line}`), null, line); | |
| } | |
| assert.equal(hint('int main(void)'), null); | |
| assert.equal(hint('int main(void) {\nint values[] = {\n1'), null); | |
| assert.equal(hint('int main(void) {\nfor (int n=0;\nn < 3'), null); | |
| assert.equal(hint('#define EXAMPLE \\\n 42'), null); | |
| const existing = 'int main(void) {\nprintf("Hi");'; | |
| assert.equal(syntaxHintAtNewline(existing, existing.indexOf('Hi')), null); | |
| }); | |