shrimp/src/compiler/tests/literals.test.ts
2025-10-28 21:18:24 -07:00

158 lines
3.3 KiB
TypeScript

import { describe } from 'bun:test'
import { expect, test } from 'bun:test'
describe('array literals', () => {
test('work with numbers', () => {
expect('[1 2 3]').toEvaluateTo([1, 2, 3])
})
test('work with strings', () => {
expect("['one' 'two' 'three']").toEvaluateTo(['one', 'two', 'three'])
})
test('work with identifiers', () => {
expect('[one two three]').toEvaluateTo(['one', 'two', 'three'])
})
test('can be nested', () => {
expect('[one [two [three]]]').toEvaluateTo(['one', ['two', ['three']]])
})
test('can span multiple lines', () => {
expect(`[
1
2
3
]`).toEvaluateTo([1, 2, 3])
})
test('can span multiple w/o calling functions', () => {
expect(`[
one
two
three
]`).toEvaluateTo(['one', 'two', 'three'])
})
test('empty arrays', () => {
expect('[]').toEvaluateTo([])
})
test('mixed types', () => {
expect("[1 'two' three true null]").toEvaluateTo([1, 'two', 'three', true, null])
})
test('semicolons as separators', () => {
expect('[1; 2; 3]').toEvaluateTo([1, 2, 3])
})
test('expressions in arrays', () => {
expect('[(1 + 2) (3 * 4)]').toEvaluateTo([3, 12])
})
test('mixed separators - spaces and newlines', () => {
expect(`[1 2
3 4]`).toEvaluateTo([1, 2, 3, 4])
})
test('mixed separators - spaces and semicolons', () => {
expect('[1 2; 3 4]').toEvaluateTo([1, 2, 3, 4])
})
test('empty lines within arrays', () => {
expect(`[1
2]`).toEvaluateTo([1, 2])
})
test('comments within arrays', () => {
expect(`[1 # first
2 # second
]`).toEvaluateTo([1, 2])
})
test('complex nested multiline', () => {
expect(`[
[1 2]
[3 4]
[5 6]
]`).toEvaluateTo([
[1, 2],
[3, 4],
[5, 6],
])
})
test('boolean and null literals', () => {
expect('[true false null]').toEvaluateTo([true, false, null])
})
test('regex literals', () => {
expect('[//[0-9]+//]').toEvaluateTo([/[0-9]+/])
})
test('trailing newlines', () => {
expect(`[
1
2
]`).toEvaluateTo([1, 2])
})
})
describe('dict literals', () => {
test('work with numbers', () => {
expect('[a=1 b=2 c=3]').toEvaluateTo({ a: 1, b: 2, c: 3 })
})
test('work with strings', () => {
expect("[a='one' b='two' c='three']").toEvaluateTo({ a: 'one', b: 'two', c: 'three' })
})
test('work with identifiers', () => {
expect('[a=one b=two c=three]').toEvaluateTo({ a: 'one', b: 'two', c: 'three' })
})
test('can be nested', () => {
expect('[a=one b=[two [c=three]]]').toEvaluateTo({ a: 'one', b: ['two', { c: 'three' }] })
})
test('can span multiple lines', () => {
expect(`[
a=1
b=2
c=3
]`).toEvaluateTo({ a: 1, b: 2, c: 3 })
})
test('empty dict', () => {
expect('[=]').toEvaluateTo({})
expect('[ = ]').toEvaluateTo({})
})
test('mixed types', () => {
expect("[a=1 b='two' c=three d=true e=null]").toEvaluateTo({
a: 1,
b: 'two',
c: 'three',
d: true,
e: null,
})
})
test('semicolons as separators', () => {
expect('[a=1; b=2; c=3]').toEvaluateTo({ a: 1, b: 2, c: 3 })
})
test('expressions in dicts', () => {
expect('[a=(1 + 2) b=(3 * 4)]').toEvaluateTo({ a: 3, b: 12 })
})
test('empty lines within dicts', () => {
expect(`[a=1
b=2
c=3]`).toEvaluateTo({ a: 1, b: 2, c: 3 })
})
})