shrimp/src/parser/tests/multiline.test.ts

91 lines
1.6 KiB
TypeScript

import { expect, describe, test } from 'bun:test'
import '../shrimp.grammar' // Importing this so changes cause it to retest!
describe('multiline', () => {
test('parses multiline strings', () => {
expect(`'first'\n'second'`).toMatchTree(`
String
StringFragment first
String
StringFragment second`)
})
test('parses multiline functions', () => {
expect(`
add = do a b:
result = a + b
result
end
add 3 4
`).toMatchTree(`
Assign
AssignableIdentifier add
Eq =
FunctionDef
Do do
Params
Identifier a
Identifier b
colon :
Assign
AssignableIdentifier result
Eq =
BinOp
Identifier a
Plus +
Identifier b
FunctionCallOrIdentifier
Identifier result
keyword end
FunctionCall
Identifier add
PositionalArg
Number 3
PositionalArg
Number 4`)
})
test('ignores leading and trailing whitespace in expected tree', () => {
expect(`
3
do x y:
x
end
`).toMatchTree(`
Number 3
FunctionDef
Do do
Params
Identifier x
Identifier y
colon :
FunctionCallOrIdentifier
Identifier x
keyword end
`)
})
test('multiline with empty lines', () => {
expect(`
do:
2
end
`).toMatchTree(`
FunctionDef
keyword do
Params
colon :
Number 2
keyword end
`)
})
})