we get globals for free now

This commit is contained in:
Chris Wanstrath 2025-11-25 16:43:07 -08:00
parent de0d43a1d6
commit c7d4db8528
3 changed files with 54 additions and 57 deletions

View File

@ -1,90 +1,89 @@
import { expect, describe, test } from 'bun:test' import { expect, describe, test } from 'bun:test'
import { globals } from '#prelude'
describe('var and var?', () => { describe('var and var?', () => {
test('var? checks if a variable exists', async () => { test('var? checks if a variable exists', async () => {
await expect(`var? 'nada'`).toEvaluateTo(false, globals) await expect(`var? 'nada'`).toEvaluateTo(false)
await expect(`var? 'info'`).toEvaluateTo(false, globals) await expect(`var? 'info'`).toEvaluateTo(false)
await expect(`abc = abc; var? 'abc'`).toEvaluateTo(true, globals) await expect(`abc = abc; var? 'abc'`).toEvaluateTo(true)
await expect(`var? 'var?'`).toEvaluateTo(true, globals) await expect(`var? 'var?'`).toEvaluateTo(true)
await expect(`var? 'dict'`).toEvaluateTo(true, globals) await expect(`var? 'dict'`).toEvaluateTo(true)
await expect(`var? dict`).toEvaluateTo(true, globals) await expect(`var? dict`).toEvaluateTo(true)
}) })
test('var returns a value or null', async () => { test('var returns a value or null', async () => {
await expect(`var 'nada'`).toEvaluateTo(null, globals) await expect(`var 'nada'`).toEvaluateTo(null)
await expect(`var nada`).toEvaluateTo(null, globals) await expect(`var nada`).toEvaluateTo(null)
await expect(`var 'info'`).toEvaluateTo(null, globals) await expect(`var 'info'`).toEvaluateTo(null)
await expect(`abc = my-string; var 'abc'`).toEvaluateTo('my-string', globals) await expect(`abc = my-string; var 'abc'`).toEvaluateTo('my-string')
await expect(`abc = my-string; var abc`).toEvaluateTo(null, globals) await expect(`abc = my-string; var abc`).toEvaluateTo(null)
}) })
}) })
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, globals) await expect(`string? 'hello'`).toEvaluateTo(true)
await expect(`string? 42`).toEvaluateTo(false, globals) await expect(`string? 42`).toEvaluateTo(false)
}) })
test('number? checks for number type', async () => { test('number? checks for number type', async () => {
await expect(`number? 42`).toEvaluateTo(true, globals) await expect(`number? 42`).toEvaluateTo(true)
await expect(`number? 'hello'`).toEvaluateTo(false, globals) await expect(`number? 'hello'`).toEvaluateTo(false)
}) })
test('boolean? checks for boolean type', async () => { test('boolean? checks for boolean type', async () => {
await expect(`boolean? true`).toEvaluateTo(true, globals) await expect(`boolean? true`).toEvaluateTo(true)
await expect(`boolean? 42`).toEvaluateTo(false, globals) await expect(`boolean? 42`).toEvaluateTo(false)
}) })
test('array? checks for array type', async () => { test('array? checks for array type', async () => {
await expect(`array? [1 2 3]`).toEvaluateTo(true, globals) await expect(`array? [1 2 3]`).toEvaluateTo(true)
await expect(`array? 42`).toEvaluateTo(false, globals) await expect(`array? 42`).toEvaluateTo(false)
}) })
test('dict? checks for dict type', async () => { test('dict? checks for dict type', async () => {
await expect(`dict? [a=1]`).toEvaluateTo(true, globals) await expect(`dict? [a=1]`).toEvaluateTo(true)
await expect(`dict? []`).toEvaluateTo(false, globals) await expect(`dict? []`).toEvaluateTo(false)
}) })
test('null? checks for null type', async () => { test('null? checks for null type', async () => {
await expect(`null? null`).toEvaluateTo(true, globals) await expect(`null? null`).toEvaluateTo(true)
await expect(`null? 42`).toEvaluateTo(false, globals) await expect(`null? 42`).toEvaluateTo(false)
}) })
test('some? checks for non-null', async () => { test('some? checks for non-null', async () => {
await expect(`some? 42`).toEvaluateTo(true, globals) await expect(`some? 42`).toEvaluateTo(true)
await expect(`some? null`).toEvaluateTo(false, globals) await expect(`some? null`).toEvaluateTo(false)
}) })
}) })
describe('introspection', () => { describe('introspection', () => {
test('type returns proper types', async () => { test('type returns proper types', async () => {
await expect(`type 'hello'`).toEvaluateTo('string', globals) await expect(`type 'hello'`).toEvaluateTo('string')
await expect(`type 42`).toEvaluateTo('number', globals) await expect(`type 42`).toEvaluateTo('number')
await expect(`type true`).toEvaluateTo('boolean', globals) await expect(`type true`).toEvaluateTo('boolean')
await expect(`type false`).toEvaluateTo('boolean', globals) await expect(`type false`).toEvaluateTo('boolean')
await expect(`type null`).toEvaluateTo('null', globals) await expect(`type null`).toEvaluateTo('null')
await expect(`type [1 2 3]`).toEvaluateTo('array', globals) await expect(`type [1 2 3]`).toEvaluateTo('array')
await expect(`type [a=1 b=2]`).toEvaluateTo('dict', globals) await expect(`type [a=1 b=2]`).toEvaluateTo('dict')
}) })
test('inspect formats values', async () => { test('inspect formats values', async () => {
await expect(`inspect 'hello'`).toEvaluateTo("\u001b[32m'hello\u001b[32m'\u001b[0m", globals) await expect(`inspect 'hello'`).toEvaluateTo("\u001b[32m'hello\u001b[32m'\u001b[0m")
}) })
test('describe describes values', async () => { test('describe describes values', async () => {
await expect(`describe 'hello'`).toEvaluateTo("#<string: \u001b[32m'hello\u001b[32m'\u001b[0m>", globals) await expect(`describe 'hello'`).toEvaluateTo("#<string: \u001b[32m'hello\u001b[32m'\u001b[0m>")
}) })
}) })
describe('environment', () => { describe('environment', () => {
test('args is an array', async () => { test('args is an array', async () => {
await expect(`array? $.args`).toEvaluateTo(true, globals) await expect(`array? $.args`).toEvaluateTo(true)
}) })
test('args can be accessed', async () => { test('args can be accessed', async () => {
await expect(`type $.args`).toEvaluateTo('array', globals) await expect(`type $.args`).toEvaluateTo('array')
}) })
test('argv includes more than just the args', async () => { test('argv includes more than just the args', async () => {
@ -106,35 +105,35 @@ describe('ref', () => {
describe('$ global dictionary', () => { describe('$ global dictionary', () => {
test('$.args is an array', async () => { test('$.args is an array', async () => {
await expect(`$.args | array?`).toEvaluateTo(true, globals) await expect(`$.args | array?`).toEvaluateTo(true)
}) })
test('$.args can be accessed', async () => { test('$.args can be accessed', async () => {
await expect(`$.args | type`).toEvaluateTo('array', globals) await expect(`$.args | type`).toEvaluateTo('array')
}) })
test('$.script.name is a string', async () => { test('$.script.name is a string', async () => {
await expect(`$.script.name | string?`).toEvaluateTo(true, globals) await expect(`$.script.name | string?`).toEvaluateTo(true)
}) })
test('$.script.path is a string', async () => { test('$.script.path is a string', async () => {
await expect(`$.script.path | string?`).toEvaluateTo(true, globals) await expect(`$.script.path | string?`).toEvaluateTo(true)
}) })
test('$.env is a dict', async () => { test('$.env is a dict', async () => {
await expect(`$.env | dict?`).toEvaluateTo(true, globals) await expect(`$.env | dict?`).toEvaluateTo(true)
}) })
test('$.pid is a number', async () => { test('$.pid is a number', async () => {
await expect(`$.pid | number?`).toEvaluateTo(true, globals) await expect(`$.pid | number?`).toEvaluateTo(true)
await expect(`$.pid > 0`).toEvaluateTo(true, globals) await expect(`$.pid > 0`).toEvaluateTo(true)
}) })
test('$.cwd is a string', async () => { test('$.cwd is a string', async () => {
await expect(`$.cwd | string?`).toEvaluateTo(true, globals) await expect(`$.cwd | string?`).toEvaluateTo(true)
}) })
test('$.cwd returns current working directory', async () => { test('$.cwd returns current working directory', async () => {
await expect(`$.cwd`).toEvaluateTo(process.cwd(), globals) await expect(`$.cwd`).toEvaluateTo(process.cwd())
}) })
}) })

View File

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

View File

@ -1,5 +1,4 @@
import { expect, describe, test } from 'bun:test' import { expect, describe, test } from 'bun:test'
import { globals } from '#prelude'
describe('string operations', () => { describe('string operations', () => {
test('to-upper converts to uppercase', async () => { test('to-upper converts to uppercase', async () => {