globalFunctions => globals
This commit is contained in:
parent
3c06cac36c
commit
9e38fa7a44
2
bun.lock
2
bun.lock
|
|
@ -62,7 +62,7 @@
|
||||||
|
|
||||||
"hono": ["hono@4.9.8", "", {}, "sha512-JW8Bb4RFWD9iOKxg5PbUarBYGM99IcxFl2FPBo2gSJO11jjUDqlP1Bmfyqt8Z/dGhIQ63PMA9LdcLefXyIasyg=="],
|
"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=="],
|
"style-mod": ["style-mod@4.1.2", "", {}, "sha512-wnD1HyVqpJUI2+eKZ+eo1UwghftP6yuFheBqqe+bWCotBjC2K1YnteJILRMs3SM4V/0dLEW1SC27MWP5y+mwmw=="],
|
||||||
|
|
||||||
|
|
|
||||||
8
ha.sh
Normal file
8
ha.sh
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
bob = [ name= Bob age= 44 ]
|
||||||
|
mike = [
|
||||||
|
name= Mike
|
||||||
|
age= 46
|
||||||
|
]
|
||||||
|
|
||||||
|
echo bob
|
||||||
|
echo (mike | at name)
|
||||||
|
|
@ -21,7 +21,7 @@ export const colors = {
|
||||||
pink: '\x1b[38;2;255;105;180m'
|
pink: '\x1b[38;2;255;105;180m'
|
||||||
}
|
}
|
||||||
|
|
||||||
export const globalFunctions = {
|
export const globals = {
|
||||||
// hello
|
// hello
|
||||||
echo: (...args: any[]) => {
|
echo: (...args: any[]) => {
|
||||||
console.log(...args.map(a => {
|
console.log(...args.map(a => {
|
||||||
|
|
|
||||||
|
|
@ -1,42 +1,42 @@
|
||||||
import { expect, describe, test } from 'bun:test'
|
import { expect, describe, test } from 'bun:test'
|
||||||
import { globalFunctions } from '#prelude'
|
import { globals } from '#prelude'
|
||||||
|
|
||||||
describe('use', () => {
|
describe('use', () => {
|
||||||
test(`imports all a file's functions`, async () => {
|
test(`imports all a file's functions`, async () => {
|
||||||
expect(`
|
expect(`
|
||||||
math = load ./src/prelude/tests/math
|
math = load ./src/prelude/tests/math
|
||||||
math.double 4
|
math.double 4
|
||||||
`).toEvaluateTo(8, globalFunctions)
|
`).toEvaluateTo(8, globals)
|
||||||
|
|
||||||
expect(`
|
expect(`
|
||||||
math = load ./src/prelude/tests/math
|
math = load ./src/prelude/tests/math
|
||||||
math.double (math.double 4)
|
math.double (math.double 4)
|
||||||
`).toEvaluateTo(16, globalFunctions)
|
`).toEvaluateTo(16, globals)
|
||||||
|
|
||||||
expect(`
|
expect(`
|
||||||
math = load ./src/prelude/tests/math
|
math = load ./src/prelude/tests/math
|
||||||
dbl = math.double
|
dbl = math.double
|
||||||
dbl (dbl 2)
|
dbl (dbl 2)
|
||||||
`).toEvaluateTo(8, globalFunctions)
|
`).toEvaluateTo(8, globals)
|
||||||
|
|
||||||
expect(`
|
expect(`
|
||||||
math = load ./src/prelude/tests/math
|
math = load ./src/prelude/tests/math
|
||||||
math.pi
|
math.pi
|
||||||
`).toEvaluateTo(3.14, globalFunctions)
|
`).toEvaluateTo(3.14, globals)
|
||||||
|
|
||||||
expect(`
|
expect(`
|
||||||
math = load ./src/prelude/tests/math
|
math = load ./src/prelude/tests/math
|
||||||
math | at 🥧
|
math | at 🥧
|
||||||
`).toEvaluateTo(3.14159265359, globalFunctions)
|
`).toEvaluateTo(3.14159265359, globals)
|
||||||
|
|
||||||
expect(`
|
expect(`
|
||||||
math = load ./src/prelude/tests/math
|
math = load ./src/prelude/tests/math
|
||||||
math.🥧
|
math.🥧
|
||||||
`).toEvaluateTo(3.14159265359, globalFunctions)
|
`).toEvaluateTo(3.14159265359, globals)
|
||||||
|
|
||||||
expect(`
|
expect(`
|
||||||
math = load ./src/prelude/tests/math
|
math = load ./src/prelude/tests/math
|
||||||
math.add1 5
|
math.add1 5
|
||||||
`).toEvaluateTo(6, globalFunctions)
|
`).toEvaluateTo(6, globals)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -1,239 +1,239 @@
|
||||||
import { expect, describe, test } from 'bun:test'
|
import { expect, describe, test } from 'bun:test'
|
||||||
import { globalFunctions } from '#prelude'
|
import { globals } from '#prelude'
|
||||||
|
|
||||||
describe('string operations', () => {
|
describe('string operations', () => {
|
||||||
test('to-upper converts to uppercase', async () => {
|
test('to-upper converts to uppercase', async () => {
|
||||||
await expect(`str.to-upper 'hello'`).toEvaluateTo('HELLO', globalFunctions)
|
await expect(`str.to-upper 'hello'`).toEvaluateTo('HELLO', globals)
|
||||||
await expect(`str.to-upper 'Hello World!'`).toEvaluateTo('HELLO WORLD!', globalFunctions)
|
await expect(`str.to-upper 'Hello World!'`).toEvaluateTo('HELLO WORLD!', globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('to-lower converts to lowercase', async () => {
|
test('to-lower converts to lowercase', async () => {
|
||||||
await expect(`str.to-lower 'HELLO'`).toEvaluateTo('hello', globalFunctions)
|
await expect(`str.to-lower 'HELLO'`).toEvaluateTo('hello', globals)
|
||||||
await expect(`str.to-lower 'Hello World!'`).toEvaluateTo('hello world!', globalFunctions)
|
await expect(`str.to-lower 'Hello World!'`).toEvaluateTo('hello world!', globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('trim removes whitespace', async () => {
|
test('trim removes whitespace', async () => {
|
||||||
await expect(`str.trim ' hello '`).toEvaluateTo('hello', globalFunctions)
|
await expect(`str.trim ' hello '`).toEvaluateTo('hello', globals)
|
||||||
await expect(`str.trim '\\n\\thello\\t\\n'`).toEvaluateTo('hello', globalFunctions)
|
await expect(`str.trim '\\n\\thello\\t\\n'`).toEvaluateTo('hello', globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('split divides string by separator', async () => {
|
test('split divides string by 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)
|
||||||
await expect(`str.split 'hello' ''`).toEvaluateTo(['h', 'e', 'l', 'l', 'o'], globalFunctions)
|
await expect(`str.split 'hello' ''`).toEvaluateTo(['h', 'e', 'l', 'l', 'o'], globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('split with comma separator', async () => {
|
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 () => {
|
test('join combines array elements', 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)
|
||||||
await expect(`str.join ['hello' 'world'] ' '`).toEvaluateTo('hello world', globalFunctions)
|
await expect(`str.join ['hello' 'world'] ' '`).toEvaluateTo('hello world', globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('join with comma separator', async () => {
|
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 () => {
|
test('starts-with? checks string prefix', async () => {
|
||||||
await expect(`str.starts-with? 'hello' 'hel'`).toEvaluateTo(true, globalFunctions)
|
await expect(`str.starts-with? 'hello' 'hel'`).toEvaluateTo(true, globals)
|
||||||
await expect(`str.starts-with? 'hello' 'bye'`).toEvaluateTo(false, globalFunctions)
|
await expect(`str.starts-with? 'hello' 'bye'`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('ends-with? checks string suffix', async () => {
|
test('ends-with? checks string suffix', async () => {
|
||||||
await expect(`str.ends-with? 'hello' 'lo'`).toEvaluateTo(true, globalFunctions)
|
await expect(`str.ends-with? 'hello' 'lo'`).toEvaluateTo(true, globals)
|
||||||
await expect(`str.ends-with? 'hello' 'he'`).toEvaluateTo(false, globalFunctions)
|
await expect(`str.ends-with? 'hello' 'he'`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('contains? checks for substring', async () => {
|
test('contains? checks for substring', async () => {
|
||||||
await expect(`str.contains? 'hello world' 'o w'`).toEvaluateTo(true, globalFunctions)
|
await expect(`str.contains? 'hello world' 'o w'`).toEvaluateTo(true, globals)
|
||||||
await expect(`str.contains? 'hello' 'bye'`).toEvaluateTo(false, globalFunctions)
|
await expect(`str.contains? 'hello' 'bye'`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('empty? checks if string is empty', async () => {
|
test('empty? checks if string is empty', async () => {
|
||||||
await expect(`str.empty? ''`).toEvaluateTo(true, globalFunctions)
|
await expect(`str.empty? ''`).toEvaluateTo(true, globals)
|
||||||
await expect(`str.empty? 'hello'`).toEvaluateTo(false, globalFunctions)
|
await expect(`str.empty? 'hello'`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('replace replaces first occurrence', async () => {
|
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 () => {
|
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 () => {
|
test('slice extracts substring', async () => {
|
||||||
await expect(`str.slice 'hello' 1 3`).toEvaluateTo('el', globalFunctions)
|
await expect(`str.slice 'hello' 1 3`).toEvaluateTo('el', globals)
|
||||||
await expect(`str.slice 'hello' 2 null`).toEvaluateTo('llo', globalFunctions)
|
await expect(`str.slice 'hello' 2 null`).toEvaluateTo('llo', globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('repeat repeats string', async () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
test('index-of finds substring position', async () => {
|
||||||
await expect(`str.index-of 'hello world' 'world'`).toEvaluateTo(6, globalFunctions)
|
await expect(`str.index-of 'hello world' 'world'`).toEvaluateTo(6, globals)
|
||||||
await expect(`str.index-of 'hello' 'bye'`).toEvaluateTo(-1, globalFunctions)
|
await expect(`str.index-of 'hello' 'bye'`).toEvaluateTo(-1, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('last-index-of finds last occurrence', async () => {
|
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', () => {
|
describe('type predicates', () => {
|
||||||
test('string? checks for string type', async () => {
|
test('string? checks for string type', async () => {
|
||||||
await expect(`string? 'hello'`).toEvaluateTo(true, globalFunctions)
|
await expect(`string? 'hello'`).toEvaluateTo(true, globals)
|
||||||
await expect(`string? 42`).toEvaluateTo(false, globalFunctions)
|
await expect(`string? 42`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('number? checks for number type', async () => {
|
test('number? checks for number type', async () => {
|
||||||
await expect(`number? 42`).toEvaluateTo(true, globalFunctions)
|
await expect(`number? 42`).toEvaluateTo(true, globals)
|
||||||
await expect(`number? 'hello'`).toEvaluateTo(false, globalFunctions)
|
await expect(`number? 'hello'`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('boolean? checks for boolean type', async () => {
|
test('boolean? checks for boolean type', async () => {
|
||||||
await expect(`boolean? true`).toEvaluateTo(true, globalFunctions)
|
await expect(`boolean? true`).toEvaluateTo(true, globals)
|
||||||
await expect(`boolean? 42`).toEvaluateTo(false, globalFunctions)
|
await expect(`boolean? 42`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('array? checks for array type', async () => {
|
test('array? checks for array type', async () => {
|
||||||
await expect(`array? [1 2 3]`).toEvaluateTo(true, globalFunctions)
|
await expect(`array? [1 2 3]`).toEvaluateTo(true, globals)
|
||||||
await expect(`array? 42`).toEvaluateTo(false, globalFunctions)
|
await expect(`array? 42`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('dict? checks for dict type', async () => {
|
test('dict? checks for dict type', async () => {
|
||||||
await expect(`dict? [a=1]`).toEvaluateTo(true, globalFunctions)
|
await expect(`dict? [a=1]`).toEvaluateTo(true, globals)
|
||||||
await expect(`dict? []`).toEvaluateTo(false, globalFunctions)
|
await expect(`dict? []`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('null? checks for null type', async () => {
|
test('null? checks for null type', async () => {
|
||||||
await expect(`null? null`).toEvaluateTo(true, globalFunctions)
|
await expect(`null? null`).toEvaluateTo(true, globals)
|
||||||
await expect(`null? 42`).toEvaluateTo(false, globalFunctions)
|
await expect(`null? 42`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('some? checks for non-null', async () => {
|
test('some? checks for non-null', async () => {
|
||||||
await expect(`some? 42`).toEvaluateTo(true, globalFunctions)
|
await expect(`some? 42`).toEvaluateTo(true, globals)
|
||||||
await expect(`some? null`).toEvaluateTo(false, globalFunctions)
|
await expect(`some? null`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('boolean logic', () => {
|
describe('boolean logic', () => {
|
||||||
test('not negates value', async () => {
|
test('not negates value', async () => {
|
||||||
await expect(`not true`).toEvaluateTo(false, globalFunctions)
|
await expect(`not true`).toEvaluateTo(false, globals)
|
||||||
await expect(`not false`).toEvaluateTo(true, globalFunctions)
|
await expect(`not false`).toEvaluateTo(true, globals)
|
||||||
await expect(`not 42`).toEvaluateTo(false, globalFunctions)
|
await expect(`not 42`).toEvaluateTo(false, globals)
|
||||||
await expect(`not null`).toEvaluateTo(true, globalFunctions)
|
await expect(`not null`).toEvaluateTo(true, globals)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('utilities', () => {
|
describe('utilities', () => {
|
||||||
test('inc increments by 1', async () => {
|
test('inc increments by 1', async () => {
|
||||||
await expect(`inc 5`).toEvaluateTo(6, globalFunctions)
|
await expect(`inc 5`).toEvaluateTo(6, globals)
|
||||||
await expect(`inc -1`).toEvaluateTo(0, globalFunctions)
|
await expect(`inc -1`).toEvaluateTo(0, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('dec decrements by 1', async () => {
|
test('dec decrements by 1', async () => {
|
||||||
await expect(`dec 5`).toEvaluateTo(4, globalFunctions)
|
await expect(`dec 5`).toEvaluateTo(4, globals)
|
||||||
await expect(`dec 0`).toEvaluateTo(-1, globalFunctions)
|
await expect(`dec 0`).toEvaluateTo(-1, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('identity returns value as-is', async () => {
|
test('identity returns value as-is', async () => {
|
||||||
await expect(`identity 42`).toEvaluateTo(42, globalFunctions)
|
await expect(`identity 42`).toEvaluateTo(42, globals)
|
||||||
await expect(`identity 'hello'`).toEvaluateTo('hello', globalFunctions)
|
await expect(`identity 'hello'`).toEvaluateTo('hello', globals)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('introspection', () => {
|
describe('introspection', () => {
|
||||||
test('type returns proper types', async () => {
|
test('type returns proper types', async () => {
|
||||||
await expect(`type 'hello'`).toEvaluateTo('string', globalFunctions)
|
await expect(`type 'hello'`).toEvaluateTo('string', globals)
|
||||||
await expect(`type 42`).toEvaluateTo('number', globalFunctions)
|
await expect(`type 42`).toEvaluateTo('number', globals)
|
||||||
await expect(`type true`).toEvaluateTo('boolean', globalFunctions)
|
await expect(`type true`).toEvaluateTo('boolean', globals)
|
||||||
await expect(`type false`).toEvaluateTo('boolean', globalFunctions)
|
await expect(`type false`).toEvaluateTo('boolean', globals)
|
||||||
await expect(`type null`).toEvaluateTo('null', globalFunctions)
|
await expect(`type null`).toEvaluateTo('null', globals)
|
||||||
await expect(`type [1 2 3]`).toEvaluateTo('array', globalFunctions)
|
await expect(`type [1 2 3]`).toEvaluateTo('array', globals)
|
||||||
await expect(`type [a=1 b=2]`).toEvaluateTo('dict', globalFunctions)
|
await expect(`type [a=1 b=2]`).toEvaluateTo('dict', globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('length', async () => {
|
test('length', async () => {
|
||||||
await expect(`length 'hello'`).toEvaluateTo(5, globalFunctions)
|
await expect(`length 'hello'`).toEvaluateTo(5, globals)
|
||||||
await expect(`length [1 2 3]`).toEvaluateTo(3, globalFunctions)
|
await expect(`length [1 2 3]`).toEvaluateTo(3, globals)
|
||||||
await expect(`length [a=1 b=2]`).toEvaluateTo(2, globalFunctions)
|
await expect(`length [a=1 b=2]`).toEvaluateTo(2, globals)
|
||||||
await expect(`length 42`).toEvaluateTo(0, globalFunctions)
|
await expect(`length 42`).toEvaluateTo(0, globals)
|
||||||
await expect(`length true`).toEvaluateTo(0, globalFunctions)
|
await expect(`length true`).toEvaluateTo(0, globals)
|
||||||
await expect(`length null`).toEvaluateTo(0, globalFunctions)
|
await expect(`length null`).toEvaluateTo(0, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('inspect formats values', async () => {
|
test('inspect formats values', async () => {
|
||||||
// Just test that inspect returns something for now
|
// Just test that inspect returns something for now
|
||||||
// (we'd need more complex assertion to check the actual format)
|
// (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', () => {
|
describe('collections', () => {
|
||||||
test('literal array creates array from arguments', async () => {
|
test('literal array creates array from arguments', async () => {
|
||||||
await expect(`[ 1 2 3 ]`).toEvaluateTo([1, 2, 3], globalFunctions)
|
await expect(`[ 1 2 3 ]`).toEvaluateTo([1, 2, 3], globals)
|
||||||
await expect(`['a' 'b']`).toEvaluateTo(['a', 'b'], globalFunctions)
|
await expect(`['a' 'b']`).toEvaluateTo(['a', 'b'], globals)
|
||||||
await expect(`[]`).toEvaluateTo([], globalFunctions)
|
await expect(`[]`).toEvaluateTo([], globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('literal dict creates object from named arguments', async () => {
|
test('literal dict creates object from named arguments', async () => {
|
||||||
await expect(`[ a=1 b=2 ]`).toEvaluateTo({ a: 1, b: 2 }, globalFunctions)
|
await expect(`[ a=1 b=2 ]`).toEvaluateTo({ a: 1, b: 2 }, globals)
|
||||||
await expect(`[=]`).toEvaluateTo({}, globalFunctions)
|
await expect(`[=]`).toEvaluateTo({}, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('at retrieves element at index', async () => {
|
test('at retrieves element at index', async () => {
|
||||||
await expect(`at [10 20 30] 0`).toEvaluateTo(10, globalFunctions)
|
await expect(`at [10 20 30] 0`).toEvaluateTo(10, globals)
|
||||||
await expect(`at [10 20 30] 2`).toEvaluateTo(30, globalFunctions)
|
await expect(`at [10 20 30] 2`).toEvaluateTo(30, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('at retrieves property from object', async () => {
|
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 () => {
|
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] 1 3`).toEvaluateTo([2, 3], globals)
|
||||||
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] 2 5`).toEvaluateTo([3, 4, 5], globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('range creates number sequence', async () => {
|
test('range creates number sequence', async () => {
|
||||||
await expect(`range 0 5`).toEvaluateTo([0, 1, 2, 3, 4, 5], globalFunctions)
|
await expect(`range 0 5`).toEvaluateTo([0, 1, 2, 3, 4, 5], globals)
|
||||||
await expect(`range 3 6`).toEvaluateTo([3, 4, 5, 6], globalFunctions)
|
await expect(`range 3 6`).toEvaluateTo([3, 4, 5, 6], globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('range with single argument starts from 0', async () => {
|
test('range with single argument starts from 0', async () => {
|
||||||
await expect(`range 3 null`).toEvaluateTo([0, 1, 2, 3], globalFunctions)
|
await expect(`range 3 null`).toEvaluateTo([0, 1, 2, 3], globals)
|
||||||
await expect(`range 0 null`).toEvaluateTo([0], globalFunctions)
|
await expect(`range 0 null`).toEvaluateTo([0], globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('empty? checks if list, dict, string is empty', async () => {
|
test('empty? checks if list, dict, string is empty', async () => {
|
||||||
await expect(`empty? []`).toEvaluateTo(true, globalFunctions)
|
await expect(`empty? []`).toEvaluateTo(true, globals)
|
||||||
await expect(`empty? [1]`).toEvaluateTo(false, globalFunctions)
|
await expect(`empty? [1]`).toEvaluateTo(false, globals)
|
||||||
|
|
||||||
await expect(`empty? [=]`).toEvaluateTo(true, globalFunctions)
|
await expect(`empty? [=]`).toEvaluateTo(true, globals)
|
||||||
await expect(`empty? [a=true]`).toEvaluateTo(false, globalFunctions)
|
await expect(`empty? [a=true]`).toEvaluateTo(false, globals)
|
||||||
|
|
||||||
await expect(`empty? ''`).toEvaluateTo(true, globalFunctions)
|
await expect(`empty? ''`).toEvaluateTo(true, globals)
|
||||||
await expect(`empty? 'cat'`).toEvaluateTo(false, globalFunctions)
|
await expect(`empty? 'cat'`).toEvaluateTo(false, globals)
|
||||||
await expect(`empty? meow`).toEvaluateTo(false, globalFunctions)
|
await expect(`empty? meow`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('list.filter keeps matching elements', async () => {
|
test('list.filter keeps matching elements', async () => {
|
||||||
|
|
@ -242,7 +242,7 @@ describe('collections', () => {
|
||||||
x == 3 or x == 4 or x == 5
|
x == 3 or x == 4 or x == 5
|
||||||
end
|
end
|
||||||
list.filter [1 2 3 4 5] is-positive
|
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 () => {
|
test('list.reduce accumulates values', async () => {
|
||||||
|
|
@ -251,7 +251,7 @@ describe('collections', () => {
|
||||||
acc + x
|
acc + x
|
||||||
end
|
end
|
||||||
list.reduce [1 2 3 4] add 0
|
list.reduce [1 2 3 4] add 0
|
||||||
`).toEvaluateTo(10, globalFunctions)
|
`).toEvaluateTo(10, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('list.find returns first match', async () => {
|
test('list.find returns first match', async () => {
|
||||||
|
|
@ -260,124 +260,124 @@ describe('collections', () => {
|
||||||
x == 4
|
x == 4
|
||||||
end
|
end
|
||||||
list.find [1 2 4 5] is-four
|
list.find [1 2 4 5] is-four
|
||||||
`).toEvaluateTo(4, globalFunctions)
|
`).toEvaluateTo(4, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('list.find returns null if no match', async () => {
|
test('list.find returns null if no match', async () => {
|
||||||
await expect(`
|
await expect(`
|
||||||
is-ten = do x: x == 10 end
|
is-ten = do x: x == 10 end
|
||||||
list.find [1 2 3] is-ten
|
list.find [1 2 3] is-ten
|
||||||
`).toEvaluateTo(null, globalFunctions)
|
`).toEvaluateTo(null, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('list.empty? checks if list is empty', async () => {
|
test('list.empty? checks if list is empty', async () => {
|
||||||
await expect(`list.empty? []`).toEvaluateTo(true, globalFunctions)
|
await expect(`list.empty? []`).toEvaluateTo(true, globals)
|
||||||
await expect(`list.empty? [1]`).toEvaluateTo(false, globalFunctions)
|
await expect(`list.empty? [1]`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('list.contains? checks for element', async () => {
|
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] 2`).toEvaluateTo(true, globals)
|
||||||
await expect(`list.contains? [1 2 3] 5`).toEvaluateTo(false, globalFunctions)
|
await expect(`list.contains? [1 2 3] 5`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('list.reverse reverses array', async () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
test('list.first returns first element', async () => {
|
||||||
await expect(`list.first [1 2 3]`).toEvaluateTo(1, globalFunctions)
|
await expect(`list.first [1 2 3]`).toEvaluateTo(1, globals)
|
||||||
await expect(`list.first []`).toEvaluateTo(null, globalFunctions)
|
await expect(`list.first []`).toEvaluateTo(null, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('list.last returns last element', async () => {
|
test('list.last returns last element', async () => {
|
||||||
await expect(`list.last [1 2 3]`).toEvaluateTo(3, globalFunctions)
|
await expect(`list.last [1 2 3]`).toEvaluateTo(3, globals)
|
||||||
await expect(`list.last []`).toEvaluateTo(null, globalFunctions)
|
await expect(`list.last []`).toEvaluateTo(null, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('list.rest returns all but first', async () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
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] 2`).toEvaluateTo(1, globals)
|
||||||
await expect(`list.index-of [1 2 3] 5`).toEvaluateTo(-1, globalFunctions)
|
await expect(`list.index-of [1 2 3] 5`).toEvaluateTo(-1, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('list.any? checks if any element matches', async () => {
|
test('list.any? checks if any element matches', async () => {
|
||||||
await expect(`
|
await expect(`
|
||||||
gt-three = do x: x > 3 end
|
gt-three = do x: x > 3 end
|
||||||
list.any? [1 2 4 5] gt-three
|
list.any? [1 2 4 5] gt-three
|
||||||
`).toEvaluateTo(true, globalFunctions)
|
`).toEvaluateTo(true, globals)
|
||||||
await expect(`
|
await expect(`
|
||||||
gt-ten = do x: x > 10 end
|
gt-ten = do x: x > 10 end
|
||||||
list.any? [1 2 3] gt-ten
|
list.any? [1 2 3] gt-ten
|
||||||
`).toEvaluateTo(false, globalFunctions)
|
`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('list.all? checks if all elements match', async () => {
|
test('list.all? checks if all elements match', async () => {
|
||||||
await expect(`
|
await expect(`
|
||||||
positive = do x: x > 0 end
|
positive = do x: x > 0 end
|
||||||
list.all? [1 2 3] positive
|
list.all? [1 2 3] positive
|
||||||
`).toEvaluateTo(true, globalFunctions)
|
`).toEvaluateTo(true, globals)
|
||||||
await expect(`
|
await expect(`
|
||||||
positive = do x: x > 0 end
|
positive = do x: x > 0 end
|
||||||
list.all? [1 -2 3] positive
|
list.all? [1 -2 3] positive
|
||||||
`).toEvaluateTo(false, globalFunctions)
|
`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('list.sum adds all numbers', async () => {
|
test('list.sum adds all numbers', async () => {
|
||||||
await expect(`list.sum [1 2 3 4]`).toEvaluateTo(10, globalFunctions)
|
await expect(`list.sum [1 2 3 4]`).toEvaluateTo(10, globals)
|
||||||
await expect(`list.sum []`).toEvaluateTo(0, globalFunctions)
|
await expect(`list.sum []`).toEvaluateTo(0, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('list.count counts matching elements', async () => {
|
test('list.count counts matching elements', async () => {
|
||||||
await expect(`
|
await expect(`
|
||||||
gt-two = do x: x > 2 end
|
gt-two = do x: x > 2 end
|
||||||
list.count [1 2 3 4 5] gt-two
|
list.count [1 2 3 4 5] gt-two
|
||||||
`).toEvaluateTo(3, globalFunctions)
|
`).toEvaluateTo(3, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('list.partition splits array by predicate', async () => {
|
test('list.partition splits array by predicate', async () => {
|
||||||
await expect(`
|
await expect(`
|
||||||
gt-two = do x: x > 2 end
|
gt-two = do x: x > 2 end
|
||||||
list.partition [1 2 3 4 5] gt-two
|
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 () => {
|
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 () => {
|
test('list.group-by groups by key function', async () => {
|
||||||
|
|
@ -390,7 +390,7 @@ describe('collections', () => {
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
list.group-by ['a' 1 'b' 2] get-type
|
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(`
|
await expect(`
|
||||||
double = do x: x * 2 end
|
double = do x: x * 2 end
|
||||||
list.map [1 2 3] double
|
list.map [1 2 3] double
|
||||||
`).toEvaluateTo([2, 4, 6], globalFunctions)
|
`).toEvaluateTo([2, 4, 6], globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('map handles empty array', async () => {
|
test('map handles empty array', async () => {
|
||||||
await expect(`
|
await expect(`
|
||||||
double = do x: x * 2 end
|
double = do x: x * 2 end
|
||||||
list.map [] double
|
list.map [] double
|
||||||
`).toEvaluateTo([], globalFunctions)
|
`).toEvaluateTo([], globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('each iterates over array', async () => {
|
test('each iterates over array', async () => {
|
||||||
|
|
@ -415,14 +415,14 @@ describe('enumerables', () => {
|
||||||
await expect(`
|
await expect(`
|
||||||
double = do x: x * 2 end
|
double = do x: x * 2 end
|
||||||
each [1 2 3] double
|
each [1 2 3] double
|
||||||
`).toEvaluateTo([1, 2, 3], globalFunctions)
|
`).toEvaluateTo([1, 2, 3], globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('each handles empty array', async () => {
|
test('each handles empty array', async () => {
|
||||||
await expect(`
|
await expect(`
|
||||||
fn = do x: x end
|
fn = do x: x end
|
||||||
each [] fn
|
each [] fn
|
||||||
`).toEvaluateTo([], globalFunctions)
|
`).toEvaluateTo([], globals)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -432,9 +432,9 @@ describe('dict operations', () => {
|
||||||
const { Compiler } = await import('#compiler/compiler')
|
const { Compiler } = await import('#compiler/compiler')
|
||||||
const { run, fromValue } = await import('reefvm')
|
const { run, fromValue } = await import('reefvm')
|
||||||
const { setGlobals } = await import('#parser/tokenizer')
|
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 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)
|
return fromValue(r)
|
||||||
})()
|
})()
|
||||||
// Check that all expected keys are present (order may vary)
|
// Check that all expected keys are present (order may vary)
|
||||||
|
|
@ -446,9 +446,9 @@ describe('dict operations', () => {
|
||||||
const { Compiler } = await import('#compiler/compiler')
|
const { Compiler } = await import('#compiler/compiler')
|
||||||
const { run, fromValue } = await import('reefvm')
|
const { run, fromValue } = await import('reefvm')
|
||||||
const { setGlobals } = await import('#parser/tokenizer')
|
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 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)
|
return fromValue(r)
|
||||||
})()
|
})()
|
||||||
// Check that all expected values are present (order may vary)
|
// Check that all expected values are present (order may vary)
|
||||||
|
|
@ -456,119 +456,119 @@ describe('dict operations', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
test('dict.has? checks for key', async () => {
|
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] 'a'`).toEvaluateTo(true, globals)
|
||||||
await expect(`dict.has? [a=1 b=2] 'c'`).toEvaluateTo(false, globalFunctions)
|
await expect(`dict.has? [a=1 b=2] 'c'`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('dict.get retrieves value with default', async () => {
|
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] 'a' 0`).toEvaluateTo(1, globals)
|
||||||
await expect(`dict.get [a=1] 'b' 99`).toEvaluateTo(99, globalFunctions)
|
await expect(`dict.get [a=1] 'b' 99`).toEvaluateTo(99, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('dict.empty? checks if dict is empty', async () => {
|
test('dict.empty? checks if dict is empty', async () => {
|
||||||
await expect(`dict.empty? [=]`).toEvaluateTo(true, globalFunctions)
|
await expect(`dict.empty? [=]`).toEvaluateTo(true, globals)
|
||||||
await expect(`dict.empty? [a=1]`).toEvaluateTo(false, globalFunctions)
|
await expect(`dict.empty? [a=1]`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('dict.merge combines dicts', async () => {
|
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 () => {
|
test('dict.map transforms values', async () => {
|
||||||
await expect(`
|
await expect(`
|
||||||
double = do v k: v * 2 end
|
double = do v k: v * 2 end
|
||||||
dict.map [a=1 b=2] double
|
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 () => {
|
test('dict.filter keeps matching entries', async () => {
|
||||||
await expect(`
|
await expect(`
|
||||||
gt-one = do v k: v > 1 end
|
gt-one = do v k: v > 1 end
|
||||||
dict.filter [a=1 b=2 c=3] gt-one
|
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 () => {
|
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', () => {
|
describe('math operations', () => {
|
||||||
test('math.abs returns absolute value', async () => {
|
test('math.abs returns absolute value', async () => {
|
||||||
await expect(`math.abs -5`).toEvaluateTo(5, globalFunctions)
|
await expect(`math.abs -5`).toEvaluateTo(5, globals)
|
||||||
await expect(`math.abs 5`).toEvaluateTo(5, globalFunctions)
|
await expect(`math.abs 5`).toEvaluateTo(5, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('math.floor rounds down', async () => {
|
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 () => {
|
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 () => {
|
test('math.round rounds to nearest', async () => {
|
||||||
await expect(`math.round 3.4`).toEvaluateTo(3, globalFunctions)
|
await expect(`math.round 3.4`).toEvaluateTo(3, globals)
|
||||||
await expect(`math.round 3.6`).toEvaluateTo(4, globalFunctions)
|
await expect(`math.round 3.6`).toEvaluateTo(4, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('math.min returns minimum', async () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
test('math.even? checks if even', async () => {
|
||||||
await expect(`math.even? 4`).toEvaluateTo(true, globalFunctions)
|
await expect(`math.even? 4`).toEvaluateTo(true, globals)
|
||||||
await expect(`math.even? 5`).toEvaluateTo(false, globalFunctions)
|
await expect(`math.even? 5`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('math.odd? checks if odd', async () => {
|
test('math.odd? checks if odd', async () => {
|
||||||
await expect(`math.odd? 5`).toEvaluateTo(true, globalFunctions)
|
await expect(`math.odd? 5`).toEvaluateTo(true, globals)
|
||||||
await expect(`math.odd? 4`).toEvaluateTo(false, globalFunctions)
|
await expect(`math.odd? 4`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('math.positive? checks if positive', async () => {
|
test('math.positive? checks if positive', async () => {
|
||||||
await expect(`math.positive? 5`).toEvaluateTo(true, globalFunctions)
|
await expect(`math.positive? 5`).toEvaluateTo(true, globals)
|
||||||
await expect(`math.positive? -5`).toEvaluateTo(false, globalFunctions)
|
await expect(`math.positive? -5`).toEvaluateTo(false, globals)
|
||||||
await expect(`math.positive? 0`).toEvaluateTo(false, globalFunctions)
|
await expect(`math.positive? 0`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('math.negative? checks if negative', async () => {
|
test('math.negative? checks if negative', async () => {
|
||||||
await expect(`math.negative? -5`).toEvaluateTo(true, globalFunctions)
|
await expect(`math.negative? -5`).toEvaluateTo(true, globals)
|
||||||
await expect(`math.negative? 5`).toEvaluateTo(false, globalFunctions)
|
await expect(`math.negative? 5`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('math.zero? checks if zero', async () => {
|
test('math.zero? checks if zero', async () => {
|
||||||
await expect(`math.zero? 0`).toEvaluateTo(true, globalFunctions)
|
await expect(`math.zero? 0`).toEvaluateTo(true, globals)
|
||||||
await expect(`math.zero? 5`).toEvaluateTo(false, globalFunctions)
|
await expect(`math.zero? 5`).toEvaluateTo(false, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('math.clamp restricts value to range', async () => {
|
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(5, globals)
|
||||||
await expect(`math.clamp -5 0 10`).toEvaluateTo(0, globalFunctions)
|
await expect(`math.clamp -5 0 10`).toEvaluateTo(0, globals)
|
||||||
await expect(`math.clamp 15 0 10`).toEvaluateTo(10, globalFunctions)
|
await expect(`math.clamp 15 0 10`).toEvaluateTo(10, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('math.sign returns sign of number', async () => {
|
test('math.sign returns sign of number', async () => {
|
||||||
await expect(`math.sign 5`).toEvaluateTo(1, globalFunctions)
|
await expect(`math.sign 5`).toEvaluateTo(1, globals)
|
||||||
await expect(`math.sign -5`).toEvaluateTo(-1, globalFunctions)
|
await expect(`math.sign -5`).toEvaluateTo(-1, globals)
|
||||||
await expect(`math.sign 0`).toEvaluateTo(0, globalFunctions)
|
await expect(`math.sign 0`).toEvaluateTo(0, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('math.trunc truncates decimal', async () => {
|
test('math.trunc truncates decimal', async () => {
|
||||||
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, globalFunctions)
|
await expect(`math.trunc -3.7`).toEvaluateTo(-3, globals)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user