about summary refs log tree commit diff stats
path: root/js/baba-yaga/tests/parser-with-header.test.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/baba-yaga/tests/parser-with-header.test.js')
-rw-r--r--js/baba-yaga/tests/parser-with-header.test.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/js/baba-yaga/tests/parser-with-header.test.js b/js/baba-yaga/tests/parser-with-header.test.js
new file mode 100644
index 0000000..f9de453
--- /dev/null
+++ b/js/baba-yaga/tests/parser-with-header.test.js
@@ -0,0 +1,36 @@
+import assert from 'assert';
+import { createLexer } from '../src/core/lexer.js';
+import { createParser } from '../src/core/parser.js';
+
+function parse(code) {
+  const lexer = createLexer(code);
+  const tokens = lexer.allTokens();
+  const parser = createParser(tokens);
+  return parser.parse();
+}
+
+describe('parser: with header', () => {
+  it('parses basic with header', () => {
+    const ast = parse('f : x -> with (a : x + 1; b : a * 2;) -> a + b;');
+    const fn = ast.body[0];
+    assert.strictEqual(fn.type, 'FunctionDeclaration');
+    assert.strictEqual(fn.body.type, 'WithHeader');
+    assert.strictEqual(fn.body.entries.length, 2);
+    assert.strictEqual(fn.body.entries[0].type, 'WithAssign');
+  });
+
+  it('parses typed locals in header', () => {
+    const ast = parse('g : (x: Int) -> Int -> with (a Int; a : x;) -> a;');
+    const fn = ast.body[0];
+    assert.strictEqual(fn.body.entries[0].type, 'WithTypeDecl');
+    assert.strictEqual(fn.body.entries[1].type, 'WithAssign');
+  });
+
+  it('parses with rec variant', () => {
+    const ast = parse('h : -> with rec (f : x -> x; g : y -> y;) -> 0;');
+    const fn = ast.body[0];
+    assert.strictEqual(fn.body.recursive, true);
+  });
+});
+
+