mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 21:14:28 -08:00
function declarations
This commit is contained in:
64
test/test.js
64
test/test.js
@@ -110,6 +110,49 @@ export const tests = [
|
||||
return assert_code_evals_to('!false', true)
|
||||
}),
|
||||
|
||||
test('function expr', () => {
|
||||
assert_code_evals_to(
|
||||
`
|
||||
const x = function(){}
|
||||
x.name
|
||||
`,
|
||||
'x'
|
||||
)
|
||||
assert_code_evals_to(
|
||||
`
|
||||
const x = function foo(){}
|
||||
x.name
|
||||
`,
|
||||
'foo'
|
||||
)
|
||||
assert_code_evals_to(
|
||||
`
|
||||
(function foo(x) {
|
||||
return x*2
|
||||
}).name
|
||||
`,
|
||||
'foo'
|
||||
)
|
||||
assert_code_evals_to(
|
||||
`
|
||||
(function foo(x) {
|
||||
return x*2
|
||||
})(1)
|
||||
`,
|
||||
2
|
||||
)
|
||||
}),
|
||||
|
||||
test('function declaration', () => {
|
||||
assert_code_evals_to(
|
||||
`
|
||||
function x() {return 1}
|
||||
x()
|
||||
`,
|
||||
1
|
||||
)
|
||||
}),
|
||||
|
||||
test('More complex expression', () => {
|
||||
assert_code_evals_to(
|
||||
`
|
||||
@@ -1115,6 +1158,26 @@ export const tests = [
|
||||
return assert_equal(parse(code).problems[0].message, 'undeclared identifier: x')
|
||||
}),
|
||||
|
||||
test('function hoisting', () => {
|
||||
assert_code_evals_to(`
|
||||
function x() {
|
||||
return 1
|
||||
}
|
||||
x()
|
||||
`,
|
||||
1
|
||||
)
|
||||
assert_code_evals_to(`
|
||||
const y = x()
|
||||
function x() {
|
||||
return 1
|
||||
}
|
||||
y
|
||||
`,
|
||||
1
|
||||
)
|
||||
}),
|
||||
|
||||
/*
|
||||
TODO use before assignment
|
||||
test('no use before assignment', () => {
|
||||
@@ -2176,7 +2239,6 @@ const y = x()`
|
||||
assert_equal(s2.value_explorer.result.error.message, 'boom')
|
||||
}),
|
||||
|
||||
|
||||
test('frame follows cursor toplevel', () => {
|
||||
const code = `
|
||||
const x = () => {
|
||||
|
||||
@@ -21,7 +21,7 @@ export const assert_code_evals_to = (codestring, expected) => {
|
||||
assert_equal(parse_result.ok, true)
|
||||
const tree = eval_tree(parse_result.node)
|
||||
const frame = eval_frame(tree)
|
||||
const result = frame.children[frame.children.length - 1].result
|
||||
const result = frame.children.at(-1).result
|
||||
assert_equal({ok: result.ok, value: result.value}, {ok: true, value: expected})
|
||||
return frame
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user