148 lines
5.5 KiB
TypeScript
148 lines
5.5 KiB
TypeScript
import { expect, describe, test } from 'bun:test'
|
|
import { globalFunctions } from '#prelude'
|
|
|
|
describe('string operations', () => {
|
|
test('to-upper converts to uppercase', async () => {
|
|
await expect(`to-upper 'hello'`).toEvaluateTo('HELLO', globalFunctions)
|
|
await expect(`to-upper 'Hello World!'`).toEvaluateTo('HELLO WORLD!', globalFunctions)
|
|
})
|
|
|
|
test('to-lower converts to lowercase', async () => {
|
|
await expect(`to-lower 'HELLO'`).toEvaluateTo('hello', globalFunctions)
|
|
await expect(`to-lower 'Hello World!'`).toEvaluateTo('hello world!', globalFunctions)
|
|
})
|
|
|
|
test('trim removes whitespace', async () => {
|
|
await expect(`trim ' hello '`).toEvaluateTo('hello', globalFunctions)
|
|
await expect(`trim '\\n\\thello\\t\\n'`).toEvaluateTo('hello', globalFunctions)
|
|
})
|
|
|
|
test('split divides string by separator', async () => {
|
|
await expect(`split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c'], globalFunctions)
|
|
await expect(`split 'hello' ''`).toEvaluateTo(['h', 'e', 'l', 'l', 'o'], globalFunctions)
|
|
})
|
|
|
|
test('split with comma separator', async () => {
|
|
await expect(`split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c'], globalFunctions)
|
|
})
|
|
|
|
test('join combines array elements', async () => {
|
|
await expect(`join ['a' 'b' 'c'] '-'`).toEvaluateTo('a-b-c', globalFunctions)
|
|
await expect(`join ['hello' 'world'] ' '`).toEvaluateTo('hello world', globalFunctions)
|
|
})
|
|
|
|
test('join with comma separator', async () => {
|
|
await expect(`join ['a' 'b' 'c'] ','`).toEvaluateTo('a,b,c', globalFunctions)
|
|
})
|
|
})
|
|
|
|
describe('introspection', () => {
|
|
test('type returns proper types', async () => {
|
|
await expect(`type 'hello'`).toEvaluateTo('string', globalFunctions)
|
|
await expect(`type 42`).toEvaluateTo('number', globalFunctions)
|
|
await expect(`type true`).toEvaluateTo('boolean', globalFunctions)
|
|
await expect(`type false`).toEvaluateTo('boolean', globalFunctions)
|
|
await expect(`type null`).toEvaluateTo('null', globalFunctions)
|
|
await expect(`type [1 2 3]`).toEvaluateTo('array', globalFunctions)
|
|
await expect(`type [a=1 b=2]`).toEvaluateTo('dict', globalFunctions)
|
|
})
|
|
|
|
test('length', async () => {
|
|
await expect(`length 'hello'`).toEvaluateTo(5, globalFunctions)
|
|
await expect(`length [1 2 3]`).toEvaluateTo(3, globalFunctions)
|
|
await expect(`length [a=1 b=2]`).toEvaluateTo(2, globalFunctions)
|
|
await expect(`length 42`).toEvaluateTo(0, globalFunctions)
|
|
await expect(`length true`).toEvaluateTo(0, globalFunctions)
|
|
await expect(`length null`).toEvaluateTo(0, globalFunctions)
|
|
})
|
|
|
|
test('inspect formats values', async () => {
|
|
// Just test that inspect returns something for now
|
|
// (we'd need more complex assertion to check the actual format)
|
|
await expect(`type (inspect 'hello')`).toEvaluateTo('string', globalFunctions)
|
|
})
|
|
})
|
|
|
|
describe('collections', () => {
|
|
test('list creates array from arguments', async () => {
|
|
await expect(`list 1 2 3`).toEvaluateTo([1, 2, 3], globalFunctions)
|
|
await expect(`list 'a' 'b'`).toEvaluateTo(['a', 'b'], globalFunctions)
|
|
await expect(`list`).toEvaluateTo([], globalFunctions)
|
|
})
|
|
|
|
test('dict creates object from named arguments', async () => {
|
|
await expect(`dict a=1 b=2`).toEvaluateTo({ a: 1, b: 2 }, globalFunctions)
|
|
await expect(`dict`).toEvaluateTo({}, globalFunctions)
|
|
})
|
|
|
|
test('at retrieves element at index', async () => {
|
|
await expect(`at [10 20 30] 0`).toEvaluateTo(10, globalFunctions)
|
|
await expect(`at [10 20 30] 2`).toEvaluateTo(30, globalFunctions)
|
|
})
|
|
|
|
test('at retrieves property from object', async () => {
|
|
await expect(`at [name='test'] 'name'`).toEvaluateTo('test', globalFunctions)
|
|
})
|
|
|
|
test('slice extracts array subset', async () => {
|
|
await expect(`slice [1 2 3 4 5] 1 3`).toEvaluateTo([2, 3], globalFunctions)
|
|
await expect(`slice [1 2 3 4 5] 2 5`).toEvaluateTo([3, 4, 5], globalFunctions)
|
|
})
|
|
|
|
test('range creates number sequence', async () => {
|
|
await expect(`range 0 5`).toEvaluateTo([0, 1, 2, 3, 4, 5], globalFunctions)
|
|
await expect(`range 3 6`).toEvaluateTo([3, 4, 5, 6], globalFunctions)
|
|
})
|
|
|
|
test('range with single argument starts from 0', async () => {
|
|
await expect(`range 3 null`).toEvaluateTo([0, 1, 2, 3], globalFunctions)
|
|
await expect(`range 0 null`).toEvaluateTo([0], globalFunctions)
|
|
})
|
|
})
|
|
|
|
describe('enumerables', () => {
|
|
test('map transforms array elements', async () => {
|
|
await expect(`
|
|
double = do x: x * 2 end
|
|
map [1 2 3] double
|
|
`).toEvaluateTo([2, 4, 6], globalFunctions)
|
|
})
|
|
|
|
test('map handles empty array', async () => {
|
|
await expect(`
|
|
double = do x: x * 2 end
|
|
map [] double
|
|
`).toEvaluateTo([], globalFunctions)
|
|
})
|
|
|
|
test('each iterates over array', async () => {
|
|
// Note: each doesn't return the results, it returns null
|
|
// We can test it runs by checking the return value
|
|
await expect(`
|
|
double = do x: x * 2 end
|
|
each [1 2 3] double
|
|
`).toEvaluateTo([1, 2, 3], globalFunctions)
|
|
})
|
|
|
|
test('each handles empty array', async () => {
|
|
await expect(`
|
|
fn = do x: x end
|
|
each [] fn
|
|
`).toEvaluateTo([], globalFunctions)
|
|
})
|
|
})
|
|
|
|
// describe('echo', () => {
|
|
// test('echo returns null value', async () => {
|
|
// await expect(`echo 'hello' 'world'`).toEvaluateTo(null, globalFunctions)
|
|
// })
|
|
|
|
// test('echo with array', async () => {
|
|
// await expect(`echo [1 2 3]`).toEvaluateTo(null, globalFunctions)
|
|
// })
|
|
|
|
// test('echo with multiple arguments', async () => {
|
|
// await expect(`echo 'test' 42 true`).toEvaluateTo(null, globalFunctions)
|
|
// })
|
|
// })
|