ReefVM/tests/examples.test.ts
2025-10-05 22:24:46 -07:00

32 lines
996 B
TypeScript

import { test, expect } from "bun:test"
import { readdirSync } from "fs"
import { join } from "path"
import { toBytecode } from "#bytecode"
import { VM } from "#vm"
// Get all .reef files from examples directory
const examplesDir = join(import.meta.dir, "..", "examples")
const exampleFiles = readdirSync(examplesDir)
.filter(file => file.endsWith(".reef"))
.sort()
// Create a test for each example file
for (const file of exampleFiles) {
test(`examples/${file} runs without errors`, async () => {
const filePath = join(examplesDir, file)
const content = await Bun.file(filePath).text()
// Parse and run the bytecode
const bytecode = toBytecode(content)
const vm = new VM(bytecode)
// Should not throw
const result = await vm.run()
// Result should be a valid Value
expect(result).toBeDefined()
expect(result).toHaveProperty("type")
expect(["null", "boolean", "number", "string", "array", "dict", "function"]).toContain(result.type)
})
}