forked from defunkt/ReefVM
83 lines
2.7 KiB
TypeScript
83 lines
2.7 KiB
TypeScript
import { test, expect } from "bun:test"
|
|
import { isValue, toValue } from "#reef"
|
|
|
|
test("isValue - recognizes valid Value objects", () => {
|
|
expect(isValue({ type: 'number', value: 42 })).toBe(true)
|
|
expect(isValue({ type: 'number', value: 0 })).toBe(true)
|
|
expect(isValue({ type: 'string', value: 'hello' })).toBe(true)
|
|
expect(isValue({ type: 'string', value: '' })).toBe(true)
|
|
expect(isValue({ type: 'boolean', value: true })).toBe(true)
|
|
expect(isValue({ type: 'boolean', value: false })).toBe(true)
|
|
expect(isValue({ type: 'null', value: null })).toBe(true)
|
|
expect(isValue({ type: 'array', value: [] })).toBe(true)
|
|
expect(isValue({ type: 'array', value: [toValue(1), toValue(2)] })).toBe(true)
|
|
expect(isValue({ type: 'dict', value: new Map() })).toBe(true)
|
|
expect(isValue({ type: 'regex', value: /test/ })).toBe(true)
|
|
expect(isValue({
|
|
type: 'function',
|
|
value: '<function>',
|
|
params: [],
|
|
defaults: {},
|
|
body: 0,
|
|
variadic: false,
|
|
named: false,
|
|
parentScope: null as any
|
|
})).toBe(true)
|
|
expect(isValue({
|
|
type: 'native',
|
|
value: '<function>',
|
|
fn: (() => { }) as any
|
|
})).toBe(true)
|
|
})
|
|
|
|
test("isValue - rejects primitives", () => {
|
|
expect(isValue(42)).toBe(false)
|
|
expect(isValue(0)).toBe(false)
|
|
expect(isValue('hello')).toBe(false)
|
|
expect(isValue('')).toBe(false)
|
|
expect(isValue(true)).toBe(false)
|
|
expect(isValue(false)).toBe(false)
|
|
expect(isValue(null)).toBe(false)
|
|
expect(isValue(undefined)).toBe(false)
|
|
})
|
|
|
|
test("isValue - rejects plain objects", () => {
|
|
expect(isValue({})).toBe(false)
|
|
expect(isValue({ foo: 'bar' })).toBe(false)
|
|
expect(isValue({ type: 'number' })).toBe(false)
|
|
expect(isValue({ value: 42 })).toBe(false)
|
|
})
|
|
|
|
test("isValue - rejects arrays and functions", () => {
|
|
expect(isValue([])).toBe(false)
|
|
expect(isValue([1, 2, 3])).toBe(false)
|
|
expect(isValue(() => { })).toBe(false)
|
|
expect(isValue(function () { })).toBe(false)
|
|
})
|
|
|
|
test("isValue - rejects other object types", () => {
|
|
expect(isValue(new Date())).toBe(false)
|
|
expect(isValue(/regex/)).toBe(false)
|
|
expect(isValue(new Map())).toBe(false)
|
|
expect(isValue(new Set())).toBe(false)
|
|
})
|
|
|
|
test("isValue - used by toValue to detect already-converted values", () => {
|
|
const value = toValue(42)
|
|
expect(isValue(value)).toBe(true)
|
|
|
|
const result = toValue(value)
|
|
expect(result).toBe(value)
|
|
})
|
|
|
|
test("isValue - edge cases with type and value properties", () => {
|
|
// extra properties
|
|
expect(isValue({ type: 'number', value: 42, extra: 'data' })).toBe(true)
|
|
|
|
expect(isValue({ type: null, value: 42 })).toBe(false)
|
|
expect(isValue({ type: 'number', value: undefined })).toBe(true)
|
|
|
|
expect(isValue({ type: 'number', val: 42 })).toBe(false)
|
|
expect(isValue({ typ: 'number', value: 42 })).toBe(false)
|
|
})
|