globalFunctions => globals

This commit is contained in:
Chris Wanstrath 2025-10-29 12:04:14 -07:00
parent 3c06cac36c
commit 9e38fa7a44
5 changed files with 191 additions and 183 deletions

View File

@ -62,7 +62,7 @@
"hono": ["hono@4.9.8", "", {}, "sha512-JW8Bb4RFWD9iOKxg5PbUarBYGM99IcxFl2FPBo2gSJO11jjUDqlP1Bmfyqt8Z/dGhIQ63PMA9LdcLefXyIasyg=="],
"reefvm": ["reefvm@git+https://git.nose.space/defunkt/reefvm#e54207067734d2186cd788c3654b675b493c2585", { "peerDependencies": { "typescript": "^5" } }, "e54207067734d2186cd788c3654b675b493c2585"],
"reefvm": ["reefvm@git+https://git.nose.space/defunkt/reefvm#052f989e82430db638c649e91960bd8ce3cf6ceb", { "peerDependencies": { "typescript": "^5" } }, "052f989e82430db638c649e91960bd8ce3cf6ceb"],
"style-mod": ["style-mod@4.1.2", "", {}, "sha512-wnD1HyVqpJUI2+eKZ+eo1UwghftP6yuFheBqqe+bWCotBjC2K1YnteJILRMs3SM4V/0dLEW1SC27MWP5y+mwmw=="],

8
ha.sh Normal file
View File

@ -0,0 +1,8 @@
bob = [ name= Bob age= 44 ]
mike = [
name= Mike
age= 46
]
echo bob
echo (mike | at name)

View File

@ -21,7 +21,7 @@ export const colors = {
pink: '\x1b[38;2;255;105;180m'
}
export const globalFunctions = {
export const globals = {
// hello
echo: (...args: any[]) => {
console.log(...args.map(a => {

View File

@ -1,42 +1,42 @@
import { expect, describe, test } from 'bun:test'
import { globalFunctions } from '#prelude'
import { globals } from '#prelude'
describe('use', () => {
test(`imports all a file's functions`, async () => {
expect(`
math = load ./src/prelude/tests/math
math.double 4
`).toEvaluateTo(8, globalFunctions)
`).toEvaluateTo(8, globals)
expect(`
math = load ./src/prelude/tests/math
math.double (math.double 4)
`).toEvaluateTo(16, globalFunctions)
`).toEvaluateTo(16, globals)
expect(`
math = load ./src/prelude/tests/math
dbl = math.double
dbl (dbl 2)
`).toEvaluateTo(8, globalFunctions)
`).toEvaluateTo(8, globals)
expect(`
math = load ./src/prelude/tests/math
math.pi
`).toEvaluateTo(3.14, globalFunctions)
`).toEvaluateTo(3.14, globals)
expect(`
math = load ./src/prelude/tests/math
math | at 🥧
`).toEvaluateTo(3.14159265359, globalFunctions)
`).toEvaluateTo(3.14159265359, globals)
expect(`
math = load ./src/prelude/tests/math
math.🥧
`).toEvaluateTo(3.14159265359, globalFunctions)
`).toEvaluateTo(3.14159265359, globals)
expect(`
math = load ./src/prelude/tests/math
math.add1 5
`).toEvaluateTo(6, globalFunctions)
`).toEvaluateTo(6, globals)
})
})

View File

@ -1,239 +1,239 @@
import { expect, describe, test } from 'bun:test'
import { globalFunctions } from '#prelude'
import { globals } from '#prelude'
describe('string operations', () => {
test('to-upper converts to uppercase', async () => {
await expect(`str.to-upper 'hello'`).toEvaluateTo('HELLO', globalFunctions)
await expect(`str.to-upper 'Hello World!'`).toEvaluateTo('HELLO WORLD!', globalFunctions)
await expect(`str.to-upper 'hello'`).toEvaluateTo('HELLO', globals)
await expect(`str.to-upper 'Hello World!'`).toEvaluateTo('HELLO WORLD!', globals)
})
test('to-lower converts to lowercase', async () => {
await expect(`str.to-lower 'HELLO'`).toEvaluateTo('hello', globalFunctions)
await expect(`str.to-lower 'Hello World!'`).toEvaluateTo('hello world!', globalFunctions)
await expect(`str.to-lower 'HELLO'`).toEvaluateTo('hello', globals)
await expect(`str.to-lower 'Hello World!'`).toEvaluateTo('hello world!', globals)
})
test('trim removes whitespace', async () => {
await expect(`str.trim ' hello '`).toEvaluateTo('hello', globalFunctions)
await expect(`str.trim '\\n\\thello\\t\\n'`).toEvaluateTo('hello', globalFunctions)
await expect(`str.trim ' hello '`).toEvaluateTo('hello', globals)
await expect(`str.trim '\\n\\thello\\t\\n'`).toEvaluateTo('hello', globals)
})
test('split divides string by separator', async () => {
await expect(`str.split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c'], globalFunctions)
await expect(`str.split 'hello' ''`).toEvaluateTo(['h', 'e', 'l', 'l', 'o'], globalFunctions)
await expect(`str.split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c'], globals)
await expect(`str.split 'hello' ''`).toEvaluateTo(['h', 'e', 'l', 'l', 'o'], globals)
})
test('split with comma separator', async () => {
await expect(`str.split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c'], globalFunctions)
await expect(`str.split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c'], globals)
})
test('join combines array elements', async () => {
await expect(`str.join ['a' 'b' 'c'] '-'`).toEvaluateTo('a-b-c', globalFunctions)
await expect(`str.join ['hello' 'world'] ' '`).toEvaluateTo('hello world', globalFunctions)
await expect(`str.join ['a' 'b' 'c'] '-'`).toEvaluateTo('a-b-c', globals)
await expect(`str.join ['hello' 'world'] ' '`).toEvaluateTo('hello world', globals)
})
test('join with comma separator', async () => {
await expect(`str.join ['a' 'b' 'c'] ','`).toEvaluateTo('a,b,c', globalFunctions)
await expect(`str.join ['a' 'b' 'c'] ','`).toEvaluateTo('a,b,c', globals)
})
test('starts-with? checks string prefix', async () => {
await expect(`str.starts-with? 'hello' 'hel'`).toEvaluateTo(true, globalFunctions)
await expect(`str.starts-with? 'hello' 'bye'`).toEvaluateTo(false, globalFunctions)
await expect(`str.starts-with? 'hello' 'hel'`).toEvaluateTo(true, globals)
await expect(`str.starts-with? 'hello' 'bye'`).toEvaluateTo(false, globals)
})
test('ends-with? checks string suffix', async () => {
await expect(`str.ends-with? 'hello' 'lo'`).toEvaluateTo(true, globalFunctions)
await expect(`str.ends-with? 'hello' 'he'`).toEvaluateTo(false, globalFunctions)
await expect(`str.ends-with? 'hello' 'lo'`).toEvaluateTo(true, globals)
await expect(`str.ends-with? 'hello' 'he'`).toEvaluateTo(false, globals)
})
test('contains? checks for substring', async () => {
await expect(`str.contains? 'hello world' 'o w'`).toEvaluateTo(true, globalFunctions)
await expect(`str.contains? 'hello' 'bye'`).toEvaluateTo(false, globalFunctions)
await expect(`str.contains? 'hello world' 'o w'`).toEvaluateTo(true, globals)
await expect(`str.contains? 'hello' 'bye'`).toEvaluateTo(false, globals)
})
test('empty? checks if string is empty', async () => {
await expect(`str.empty? ''`).toEvaluateTo(true, globalFunctions)
await expect(`str.empty? 'hello'`).toEvaluateTo(false, globalFunctions)
await expect(`str.empty? ''`).toEvaluateTo(true, globals)
await expect(`str.empty? 'hello'`).toEvaluateTo(false, globals)
})
test('replace replaces first occurrence', async () => {
await expect(`str.replace 'hello hello' 'hello' 'hi'`).toEvaluateTo('hi hello', globalFunctions)
await expect(`str.replace 'hello hello' 'hello' 'hi'`).toEvaluateTo('hi hello', globals)
})
test('replace-all replaces all occurrences', async () => {
await expect(`str.replace-all 'hello hello' 'hello' 'hi'`).toEvaluateTo('hi hi', globalFunctions)
await expect(`str.replace-all 'hello hello' 'hello' 'hi'`).toEvaluateTo('hi hi', globals)
})
test('slice extracts substring', async () => {
await expect(`str.slice 'hello' 1 3`).toEvaluateTo('el', globalFunctions)
await expect(`str.slice 'hello' 2 null`).toEvaluateTo('llo', globalFunctions)
await expect(`str.slice 'hello' 1 3`).toEvaluateTo('el', globals)
await expect(`str.slice 'hello' 2 null`).toEvaluateTo('llo', globals)
})
test('repeat repeats string', async () => {
await expect(`str.repeat 'ha' 3`).toEvaluateTo('hahaha', globalFunctions)
await expect(`str.repeat 'ha' 3`).toEvaluateTo('hahaha', globals)
})
test('pad-start pads beginning', async () => {
await expect(`str.pad-start '5' 3 '0'`).toEvaluateTo('005', globalFunctions)
await expect(`str.pad-start '5' 3 '0'`).toEvaluateTo('005', globals)
})
test('pad-end pads end', async () => {
await expect(`str.pad-end '5' 3 '0'`).toEvaluateTo('500', globalFunctions)
await expect(`str.pad-end '5' 3 '0'`).toEvaluateTo('500', globals)
})
test('lines splits by newlines', async () => {
await expect(`str.lines 'a\\nb\\nc'`).toEvaluateTo(['a', 'b', 'c'], globalFunctions)
await expect(`str.lines 'a\\nb\\nc'`).toEvaluateTo(['a', 'b', 'c'], globals)
})
test('chars splits into characters', async () => {
await expect(`str.chars 'abc'`).toEvaluateTo(['a', 'b', 'c'], globalFunctions)
await expect(`str.chars 'abc'`).toEvaluateTo(['a', 'b', 'c'], globals)
})
test('index-of finds substring position', async () => {
await expect(`str.index-of 'hello world' 'world'`).toEvaluateTo(6, globalFunctions)
await expect(`str.index-of 'hello' 'bye'`).toEvaluateTo(-1, globalFunctions)
await expect(`str.index-of 'hello world' 'world'`).toEvaluateTo(6, globals)
await expect(`str.index-of 'hello' 'bye'`).toEvaluateTo(-1, globals)
})
test('last-index-of finds last occurrence', async () => {
await expect(`str.last-index-of 'hello hello' 'hello'`).toEvaluateTo(6, globalFunctions)
await expect(`str.last-index-of 'hello hello' 'hello'`).toEvaluateTo(6, globals)
})
})
describe('type predicates', () => {
test('string? checks for string type', async () => {
await expect(`string? 'hello'`).toEvaluateTo(true, globalFunctions)
await expect(`string? 42`).toEvaluateTo(false, globalFunctions)
await expect(`string? 'hello'`).toEvaluateTo(true, globals)
await expect(`string? 42`).toEvaluateTo(false, globals)
})
test('number? checks for number type', async () => {
await expect(`number? 42`).toEvaluateTo(true, globalFunctions)
await expect(`number? 'hello'`).toEvaluateTo(false, globalFunctions)
await expect(`number? 42`).toEvaluateTo(true, globals)
await expect(`number? 'hello'`).toEvaluateTo(false, globals)
})
test('boolean? checks for boolean type', async () => {
await expect(`boolean? true`).toEvaluateTo(true, globalFunctions)
await expect(`boolean? 42`).toEvaluateTo(false, globalFunctions)
await expect(`boolean? true`).toEvaluateTo(true, globals)
await expect(`boolean? 42`).toEvaluateTo(false, globals)
})
test('array? checks for array type', async () => {
await expect(`array? [1 2 3]`).toEvaluateTo(true, globalFunctions)
await expect(`array? 42`).toEvaluateTo(false, globalFunctions)
await expect(`array? [1 2 3]`).toEvaluateTo(true, globals)
await expect(`array? 42`).toEvaluateTo(false, globals)
})
test('dict? checks for dict type', async () => {
await expect(`dict? [a=1]`).toEvaluateTo(true, globalFunctions)
await expect(`dict? []`).toEvaluateTo(false, globalFunctions)
await expect(`dict? [a=1]`).toEvaluateTo(true, globals)
await expect(`dict? []`).toEvaluateTo(false, globals)
})
test('null? checks for null type', async () => {
await expect(`null? null`).toEvaluateTo(true, globalFunctions)
await expect(`null? 42`).toEvaluateTo(false, globalFunctions)
await expect(`null? null`).toEvaluateTo(true, globals)
await expect(`null? 42`).toEvaluateTo(false, globals)
})
test('some? checks for non-null', async () => {
await expect(`some? 42`).toEvaluateTo(true, globalFunctions)
await expect(`some? null`).toEvaluateTo(false, globalFunctions)
await expect(`some? 42`).toEvaluateTo(true, globals)
await expect(`some? null`).toEvaluateTo(false, globals)
})
})
describe('boolean logic', () => {
test('not negates value', async () => {
await expect(`not true`).toEvaluateTo(false, globalFunctions)
await expect(`not false`).toEvaluateTo(true, globalFunctions)
await expect(`not 42`).toEvaluateTo(false, globalFunctions)
await expect(`not null`).toEvaluateTo(true, globalFunctions)
await expect(`not true`).toEvaluateTo(false, globals)
await expect(`not false`).toEvaluateTo(true, globals)
await expect(`not 42`).toEvaluateTo(false, globals)
await expect(`not null`).toEvaluateTo(true, globals)
})
})
describe('utilities', () => {
test('inc increments by 1', async () => {
await expect(`inc 5`).toEvaluateTo(6, globalFunctions)
await expect(`inc -1`).toEvaluateTo(0, globalFunctions)
await expect(`inc 5`).toEvaluateTo(6, globals)
await expect(`inc -1`).toEvaluateTo(0, globals)
})
test('dec decrements by 1', async () => {
await expect(`dec 5`).toEvaluateTo(4, globalFunctions)
await expect(`dec 0`).toEvaluateTo(-1, globalFunctions)
await expect(`dec 5`).toEvaluateTo(4, globals)
await expect(`dec 0`).toEvaluateTo(-1, globals)
})
test('identity returns value as-is', async () => {
await expect(`identity 42`).toEvaluateTo(42, globalFunctions)
await expect(`identity 'hello'`).toEvaluateTo('hello', globalFunctions)
await expect(`identity 42`).toEvaluateTo(42, globals)
await expect(`identity 'hello'`).toEvaluateTo('hello', globals)
})
})
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)
await expect(`type 'hello'`).toEvaluateTo('string', globals)
await expect(`type 42`).toEvaluateTo('number', globals)
await expect(`type true`).toEvaluateTo('boolean', globals)
await expect(`type false`).toEvaluateTo('boolean', globals)
await expect(`type null`).toEvaluateTo('null', globals)
await expect(`type [1 2 3]`).toEvaluateTo('array', globals)
await expect(`type [a=1 b=2]`).toEvaluateTo('dict', globals)
})
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)
await expect(`length 'hello'`).toEvaluateTo(5, globals)
await expect(`length [1 2 3]`).toEvaluateTo(3, globals)
await expect(`length [a=1 b=2]`).toEvaluateTo(2, globals)
await expect(`length 42`).toEvaluateTo(0, globals)
await expect(`length true`).toEvaluateTo(0, globals)
await expect(`length null`).toEvaluateTo(0, globals)
})
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)
await expect(`type (inspect 'hello')`).toEvaluateTo('string', globals)
})
})
describe('collections', () => {
test('literal array creates array from arguments', async () => {
await expect(`[ 1 2 3 ]`).toEvaluateTo([1, 2, 3], globalFunctions)
await expect(`['a' 'b']`).toEvaluateTo(['a', 'b'], globalFunctions)
await expect(`[]`).toEvaluateTo([], globalFunctions)
await expect(`[ 1 2 3 ]`).toEvaluateTo([1, 2, 3], globals)
await expect(`['a' 'b']`).toEvaluateTo(['a', 'b'], globals)
await expect(`[]`).toEvaluateTo([], globals)
})
test('literal dict creates object from named arguments', async () => {
await expect(`[ a=1 b=2 ]`).toEvaluateTo({ a: 1, b: 2 }, globalFunctions)
await expect(`[=]`).toEvaluateTo({}, globalFunctions)
await expect(`[ a=1 b=2 ]`).toEvaluateTo({ a: 1, b: 2 }, globals)
await expect(`[=]`).toEvaluateTo({}, globals)
})
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)
await expect(`at [10 20 30] 0`).toEvaluateTo(10, globals)
await expect(`at [10 20 30] 2`).toEvaluateTo(30, globals)
})
test('at retrieves property from object', async () => {
await expect(`at [name='test'] 'name'`).toEvaluateTo('test', globalFunctions)
await expect(`at [name='test'] 'name'`).toEvaluateTo('test', globals)
})
test('slice extracts array subset', async () => {
await expect(`list.slice [1 2 3 4 5] 1 3`).toEvaluateTo([2, 3], globalFunctions)
await expect(`list.slice [1 2 3 4 5] 2 5`).toEvaluateTo([3, 4, 5], globalFunctions)
await expect(`list.slice [1 2 3 4 5] 1 3`).toEvaluateTo([2, 3], globals)
await expect(`list.slice [1 2 3 4 5] 2 5`).toEvaluateTo([3, 4, 5], globals)
})
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)
await expect(`range 0 5`).toEvaluateTo([0, 1, 2, 3, 4, 5], globals)
await expect(`range 3 6`).toEvaluateTo([3, 4, 5, 6], globals)
})
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)
await expect(`range 3 null`).toEvaluateTo([0, 1, 2, 3], globals)
await expect(`range 0 null`).toEvaluateTo([0], globals)
})
test('empty? checks if list, dict, string is empty', async () => {
await expect(`empty? []`).toEvaluateTo(true, globalFunctions)
await expect(`empty? [1]`).toEvaluateTo(false, globalFunctions)
await expect(`empty? []`).toEvaluateTo(true, globals)
await expect(`empty? [1]`).toEvaluateTo(false, globals)
await expect(`empty? [=]`).toEvaluateTo(true, globalFunctions)
await expect(`empty? [a=true]`).toEvaluateTo(false, globalFunctions)
await expect(`empty? [=]`).toEvaluateTo(true, globals)
await expect(`empty? [a=true]`).toEvaluateTo(false, globals)
await expect(`empty? ''`).toEvaluateTo(true, globalFunctions)
await expect(`empty? 'cat'`).toEvaluateTo(false, globalFunctions)
await expect(`empty? meow`).toEvaluateTo(false, globalFunctions)
await expect(`empty? ''`).toEvaluateTo(true, globals)
await expect(`empty? 'cat'`).toEvaluateTo(false, globals)
await expect(`empty? meow`).toEvaluateTo(false, globals)
})
test('list.filter keeps matching elements', async () => {
@ -242,7 +242,7 @@ describe('collections', () => {
x == 3 or x == 4 or x == 5
end
list.filter [1 2 3 4 5] is-positive
`).toEvaluateTo([3, 4, 5], globalFunctions)
`).toEvaluateTo([3, 4, 5], globals)
})
test('list.reduce accumulates values', async () => {
@ -251,7 +251,7 @@ describe('collections', () => {
acc + x
end
list.reduce [1 2 3 4] add 0
`).toEvaluateTo(10, globalFunctions)
`).toEvaluateTo(10, globals)
})
test('list.find returns first match', async () => {
@ -260,124 +260,124 @@ describe('collections', () => {
x == 4
end
list.find [1 2 4 5] is-four
`).toEvaluateTo(4, globalFunctions)
`).toEvaluateTo(4, globals)
})
test('list.find returns null if no match', async () => {
await expect(`
is-ten = do x: x == 10 end
list.find [1 2 3] is-ten
`).toEvaluateTo(null, globalFunctions)
`).toEvaluateTo(null, globals)
})
test('list.empty? checks if list is empty', async () => {
await expect(`list.empty? []`).toEvaluateTo(true, globalFunctions)
await expect(`list.empty? [1]`).toEvaluateTo(false, globalFunctions)
await expect(`list.empty? []`).toEvaluateTo(true, globals)
await expect(`list.empty? [1]`).toEvaluateTo(false, globals)
})
test('list.contains? checks for element', async () => {
await expect(`list.contains? [1 2 3] 2`).toEvaluateTo(true, globalFunctions)
await expect(`list.contains? [1 2 3] 5`).toEvaluateTo(false, globalFunctions)
await expect(`list.contains? [1 2 3] 2`).toEvaluateTo(true, globals)
await expect(`list.contains? [1 2 3] 5`).toEvaluateTo(false, globals)
})
test('list.reverse reverses array', async () => {
await expect(`list.reverse [1 2 3]`).toEvaluateTo([3, 2, 1], globalFunctions)
await expect(`list.reverse [1 2 3]`).toEvaluateTo([3, 2, 1], globals)
})
test('list.concat combines arrays', async () => {
await expect(`list.concat [1 2] [3 4]`).toEvaluateTo([1, 2, 3, 4], globalFunctions)
await expect(`list.concat [1 2] [3 4]`).toEvaluateTo([1, 2, 3, 4], globals)
})
test('list.flatten flattens nested arrays', async () => {
await expect(`list.flatten [[1 2] [3 4]] 1`).toEvaluateTo([1, 2, 3, 4], globalFunctions)
await expect(`list.flatten [[1 2] [3 4]] 1`).toEvaluateTo([1, 2, 3, 4], globals)
})
test('list.unique removes duplicates', async () => {
await expect(`list.unique [1 2 2 3 1]`).toEvaluateTo([1, 2, 3], globalFunctions)
await expect(`list.unique [1 2 2 3 1]`).toEvaluateTo([1, 2, 3], globals)
})
test('list.zip combines two arrays', async () => {
await expect(`list.zip [1 2] [3 4]`).toEvaluateTo([[1, 3], [2, 4]], globalFunctions)
await expect(`list.zip [1 2] [3 4]`).toEvaluateTo([[1, 3], [2, 4]], globals)
})
test('list.first returns first element', async () => {
await expect(`list.first [1 2 3]`).toEvaluateTo(1, globalFunctions)
await expect(`list.first []`).toEvaluateTo(null, globalFunctions)
await expect(`list.first [1 2 3]`).toEvaluateTo(1, globals)
await expect(`list.first []`).toEvaluateTo(null, globals)
})
test('list.last returns last element', async () => {
await expect(`list.last [1 2 3]`).toEvaluateTo(3, globalFunctions)
await expect(`list.last []`).toEvaluateTo(null, globalFunctions)
await expect(`list.last [1 2 3]`).toEvaluateTo(3, globals)
await expect(`list.last []`).toEvaluateTo(null, globals)
})
test('list.rest returns all but first', async () => {
await expect(`list.rest [1 2 3]`).toEvaluateTo([2, 3], globalFunctions)
await expect(`list.rest [1 2 3]`).toEvaluateTo([2, 3], globals)
})
test('list.take returns first n elements', async () => {
await expect(`list.take [1 2 3 4 5] 3`).toEvaluateTo([1, 2, 3], globalFunctions)
await expect(`list.take [1 2 3 4 5] 3`).toEvaluateTo([1, 2, 3], globals)
})
test('list.drop skips first n elements', async () => {
await expect(`list.drop [1 2 3 4 5] 2`).toEvaluateTo([3, 4, 5], globalFunctions)
await expect(`list.drop [1 2 3 4 5] 2`).toEvaluateTo([3, 4, 5], globals)
})
test('list.append adds to end', async () => {
await expect(`list.append [1 2] 3`).toEvaluateTo([1, 2, 3], globalFunctions)
await expect(`list.append [1 2] 3`).toEvaluateTo([1, 2, 3], globals)
})
test('list.prepend adds to start', async () => {
await expect(`list.prepend [2 3] 1`).toEvaluateTo([1, 2, 3], globalFunctions)
await expect(`list.prepend [2 3] 1`).toEvaluateTo([1, 2, 3], globals)
})
test('list.index-of finds element index', async () => {
await expect(`list.index-of [1 2 3] 2`).toEvaluateTo(1, globalFunctions)
await expect(`list.index-of [1 2 3] 5`).toEvaluateTo(-1, globalFunctions)
await expect(`list.index-of [1 2 3] 2`).toEvaluateTo(1, globals)
await expect(`list.index-of [1 2 3] 5`).toEvaluateTo(-1, globals)
})
test('list.any? checks if any element matches', async () => {
await expect(`
gt-three = do x: x > 3 end
list.any? [1 2 4 5] gt-three
`).toEvaluateTo(true, globalFunctions)
`).toEvaluateTo(true, globals)
await expect(`
gt-ten = do x: x > 10 end
list.any? [1 2 3] gt-ten
`).toEvaluateTo(false, globalFunctions)
`).toEvaluateTo(false, globals)
})
test('list.all? checks if all elements match', async () => {
await expect(`
positive = do x: x > 0 end
list.all? [1 2 3] positive
`).toEvaluateTo(true, globalFunctions)
`).toEvaluateTo(true, globals)
await expect(`
positive = do x: x > 0 end
list.all? [1 -2 3] positive
`).toEvaluateTo(false, globalFunctions)
`).toEvaluateTo(false, globals)
})
test('list.sum adds all numbers', async () => {
await expect(`list.sum [1 2 3 4]`).toEvaluateTo(10, globalFunctions)
await expect(`list.sum []`).toEvaluateTo(0, globalFunctions)
await expect(`list.sum [1 2 3 4]`).toEvaluateTo(10, globals)
await expect(`list.sum []`).toEvaluateTo(0, globals)
})
test('list.count counts matching elements', async () => {
await expect(`
gt-two = do x: x > 2 end
list.count [1 2 3 4 5] gt-two
`).toEvaluateTo(3, globalFunctions)
`).toEvaluateTo(3, globals)
})
test('list.partition splits array by predicate', async () => {
await expect(`
gt-two = do x: x > 2 end
list.partition [1 2 3 4 5] gt-two
`).toEvaluateTo([[3, 4, 5], [1, 2]], globalFunctions)
`).toEvaluateTo([[3, 4, 5], [1, 2]], globals)
})
test('list.compact removes null values', async () => {
await expect(`list.compact [1 null 2 null 3]`).toEvaluateTo([1, 2, 3], globalFunctions)
await expect(`list.compact [1 null 2 null 3]`).toEvaluateTo([1, 2, 3], globals)
})
test('list.group-by groups by key function', async () => {
@ -390,7 +390,7 @@ describe('collections', () => {
end
end
list.group-by ['a' 1 'b' 2] get-type
`).toEvaluateTo({ str: ['a', 'b'], num: [1, 2] }, globalFunctions)
`).toEvaluateTo({ str: ['a', 'b'], num: [1, 2] }, globals)
})
})
@ -399,14 +399,14 @@ describe('enumerables', () => {
await expect(`
double = do x: x * 2 end
list.map [1 2 3] double
`).toEvaluateTo([2, 4, 6], globalFunctions)
`).toEvaluateTo([2, 4, 6], globals)
})
test('map handles empty array', async () => {
await expect(`
double = do x: x * 2 end
list.map [] double
`).toEvaluateTo([], globalFunctions)
`).toEvaluateTo([], globals)
})
test('each iterates over array', async () => {
@ -415,14 +415,14 @@ describe('enumerables', () => {
await expect(`
double = do x: x * 2 end
each [1 2 3] double
`).toEvaluateTo([1, 2, 3], globalFunctions)
`).toEvaluateTo([1, 2, 3], globals)
})
test('each handles empty array', async () => {
await expect(`
fn = do x: x end
each [] fn
`).toEvaluateTo([], globalFunctions)
`).toEvaluateTo([], globals)
})
})
@ -432,9 +432,9 @@ describe('dict operations', () => {
const { Compiler } = await import('#compiler/compiler')
const { run, fromValue } = await import('reefvm')
const { setGlobals } = await import('#parser/tokenizer')
setGlobals(Object.keys(globalFunctions))
setGlobals(Object.keys(globals))
const c = new Compiler('dict.keys [a=1 b=2 c=3]')
const r = await run(c.bytecode, globalFunctions)
const r = await run(c.bytecode, globals)
return fromValue(r)
})()
// Check that all expected keys are present (order may vary)
@ -446,9 +446,9 @@ describe('dict operations', () => {
const { Compiler } = await import('#compiler/compiler')
const { run, fromValue } = await import('reefvm')
const { setGlobals } = await import('#parser/tokenizer')
setGlobals(Object.keys(globalFunctions))
setGlobals(Object.keys(globals))
const c = new Compiler('dict.values [a=1 b=2]')
const r = await run(c.bytecode, globalFunctions)
const r = await run(c.bytecode, globals)
return fromValue(r)
})()
// Check that all expected values are present (order may vary)
@ -456,119 +456,119 @@ describe('dict operations', () => {
})
test('dict.has? checks for key', async () => {
await expect(`dict.has? [a=1 b=2] 'a'`).toEvaluateTo(true, globalFunctions)
await expect(`dict.has? [a=1 b=2] 'c'`).toEvaluateTo(false, globalFunctions)
await expect(`dict.has? [a=1 b=2] 'a'`).toEvaluateTo(true, globals)
await expect(`dict.has? [a=1 b=2] 'c'`).toEvaluateTo(false, globals)
})
test('dict.get retrieves value with default', async () => {
await expect(`dict.get [a=1] 'a' 0`).toEvaluateTo(1, globalFunctions)
await expect(`dict.get [a=1] 'b' 99`).toEvaluateTo(99, globalFunctions)
await expect(`dict.get [a=1] 'a' 0`).toEvaluateTo(1, globals)
await expect(`dict.get [a=1] 'b' 99`).toEvaluateTo(99, globals)
})
test('dict.empty? checks if dict is empty', async () => {
await expect(`dict.empty? [=]`).toEvaluateTo(true, globalFunctions)
await expect(`dict.empty? [a=1]`).toEvaluateTo(false, globalFunctions)
await expect(`dict.empty? [=]`).toEvaluateTo(true, globals)
await expect(`dict.empty? [a=1]`).toEvaluateTo(false, globals)
})
test('dict.merge combines dicts', async () => {
await expect(`dict.merge [a=1] [b=2]`).toEvaluateTo({ a: 1, b: 2 }, globalFunctions)
await expect(`dict.merge [a=1] [b=2]`).toEvaluateTo({ a: 1, b: 2 }, globals)
})
test('dict.map transforms values', async () => {
await expect(`
double = do v k: v * 2 end
dict.map [a=1 b=2] double
`).toEvaluateTo({ a: 2, b: 4 }, globalFunctions)
`).toEvaluateTo({ a: 2, b: 4 }, globals)
})
test('dict.filter keeps matching entries', async () => {
await expect(`
gt-one = do v k: v > 1 end
dict.filter [a=1 b=2 c=3] gt-one
`).toEvaluateTo({ b: 2, c: 3 }, globalFunctions)
`).toEvaluateTo({ b: 2, c: 3 }, globals)
})
test('dict.from-entries creates dict from array', async () => {
await expect(`dict.from-entries [['a' 1] ['b' 2]]`).toEvaluateTo({ a: 1, b: 2 }, globalFunctions)
await expect(`dict.from-entries [['a' 1] ['b' 2]]`).toEvaluateTo({ a: 1, b: 2 }, globals)
})
})
describe('math operations', () => {
test('math.abs returns absolute value', async () => {
await expect(`math.abs -5`).toEvaluateTo(5, globalFunctions)
await expect(`math.abs 5`).toEvaluateTo(5, globalFunctions)
await expect(`math.abs -5`).toEvaluateTo(5, globals)
await expect(`math.abs 5`).toEvaluateTo(5, globals)
})
test('math.floor rounds down', async () => {
await expect(`math.floor 3.7`).toEvaluateTo(3, globalFunctions)
await expect(`math.floor 3.7`).toEvaluateTo(3, globals)
})
test('math.ceil rounds up', async () => {
await expect(`math.ceil 3.2`).toEvaluateTo(4, globalFunctions)
await expect(`math.ceil 3.2`).toEvaluateTo(4, globals)
})
test('math.round rounds to nearest', async () => {
await expect(`math.round 3.4`).toEvaluateTo(3, globalFunctions)
await expect(`math.round 3.6`).toEvaluateTo(4, globalFunctions)
await expect(`math.round 3.4`).toEvaluateTo(3, globals)
await expect(`math.round 3.6`).toEvaluateTo(4, globals)
})
test('math.min returns minimum', async () => {
await expect(`math.min 5 2 8 1`).toEvaluateTo(1, globalFunctions)
await expect(`math.min 5 2 8 1`).toEvaluateTo(1, globals)
})
test('math.max returns maximum', async () => {
await expect(`math.max 5 2 8 1`).toEvaluateTo(8, globalFunctions)
await expect(`math.max 5 2 8 1`).toEvaluateTo(8, globals)
})
test('math.pow computes power', async () => {
await expect(`math.pow 2 3`).toEvaluateTo(8, globalFunctions)
await expect(`math.pow 2 3`).toEvaluateTo(8, globals)
})
test('math.sqrt computes square root', async () => {
await expect(`math.sqrt 16`).toEvaluateTo(4, globalFunctions)
await expect(`math.sqrt 16`).toEvaluateTo(4, globals)
})
test('math.even? checks if even', async () => {
await expect(`math.even? 4`).toEvaluateTo(true, globalFunctions)
await expect(`math.even? 5`).toEvaluateTo(false, globalFunctions)
await expect(`math.even? 4`).toEvaluateTo(true, globals)
await expect(`math.even? 5`).toEvaluateTo(false, globals)
})
test('math.odd? checks if odd', async () => {
await expect(`math.odd? 5`).toEvaluateTo(true, globalFunctions)
await expect(`math.odd? 4`).toEvaluateTo(false, globalFunctions)
await expect(`math.odd? 5`).toEvaluateTo(true, globals)
await expect(`math.odd? 4`).toEvaluateTo(false, globals)
})
test('math.positive? checks if positive', async () => {
await expect(`math.positive? 5`).toEvaluateTo(true, globalFunctions)
await expect(`math.positive? -5`).toEvaluateTo(false, globalFunctions)
await expect(`math.positive? 0`).toEvaluateTo(false, globalFunctions)
await expect(`math.positive? 5`).toEvaluateTo(true, globals)
await expect(`math.positive? -5`).toEvaluateTo(false, globals)
await expect(`math.positive? 0`).toEvaluateTo(false, globals)
})
test('math.negative? checks if negative', async () => {
await expect(`math.negative? -5`).toEvaluateTo(true, globalFunctions)
await expect(`math.negative? 5`).toEvaluateTo(false, globalFunctions)
await expect(`math.negative? -5`).toEvaluateTo(true, globals)
await expect(`math.negative? 5`).toEvaluateTo(false, globals)
})
test('math.zero? checks if zero', async () => {
await expect(`math.zero? 0`).toEvaluateTo(true, globalFunctions)
await expect(`math.zero? 5`).toEvaluateTo(false, globalFunctions)
await expect(`math.zero? 0`).toEvaluateTo(true, globals)
await expect(`math.zero? 5`).toEvaluateTo(false, globals)
})
test('math.clamp restricts value to range', async () => {
await expect(`math.clamp 5 0 10`).toEvaluateTo(5, globalFunctions)
await expect(`math.clamp -5 0 10`).toEvaluateTo(0, globalFunctions)
await expect(`math.clamp 15 0 10`).toEvaluateTo(10, globalFunctions)
await expect(`math.clamp 5 0 10`).toEvaluateTo(5, globals)
await expect(`math.clamp -5 0 10`).toEvaluateTo(0, globals)
await expect(`math.clamp 15 0 10`).toEvaluateTo(10, globals)
})
test('math.sign returns sign of number', async () => {
await expect(`math.sign 5`).toEvaluateTo(1, globalFunctions)
await expect(`math.sign -5`).toEvaluateTo(-1, globalFunctions)
await expect(`math.sign 0`).toEvaluateTo(0, globalFunctions)
await expect(`math.sign 5`).toEvaluateTo(1, globals)
await expect(`math.sign -5`).toEvaluateTo(-1, globals)
await expect(`math.sign 0`).toEvaluateTo(0, globals)
})
test('math.trunc truncates decimal', async () => {
await expect(`math.trunc 3.7`).toEvaluateTo(3, globalFunctions)
await expect(`math.trunc -3.7`).toEvaluateTo(-3, globalFunctions)
await expect(`math.trunc 3.7`).toEvaluateTo(3, globals)
await expect(`math.trunc -3.7`).toEvaluateTo(-3, globals)
})
})