Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/samualtnorman/js-interpreter.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247import { promises as fsPromises } from "fs"import { evaluateString } from "."import { findFiles } from "@samual/lib/findFiles"import { is } from "@samual/lib/is"import { deeplyEquals } from "@samual/lib/deeplyEquals"
const { readFile } = fsPromises
for (const path of await findFiles(`tests`, [ `LICENSE` ])) { console.log(`\n${path}`)
let fails: number[] = [] let index = 0
// TODO this might need fixing // eslint-disable-next-line no-await-in-loop evaluateString(await readFile(path, { encoding: `utf-8` }), Object.assign(Object.create(globalThis), { test(name: string, callback: () => void) { console.log(`\t${name}`) fails = [] callback()
if (index) { if (fails.length) console.error(`\t\t${fails.join(`, `)}`)
index = 0 } else console.error(`\t\tno tests ran in "${name}"`) },
expect(a: any) { index++
return { toBe(b: any) { if (a != b) fails.push(index) },
toEval() { try { evaluateString(a) } catch { fails.push(index) } },
toEvalTo(b: any) { if (evaluateString(a) != b) fails.push(index) },
not: { toEval() { try { evaluateString(a) } catch { return }
fails.push(index) },
toBe(b: any) { if (a == b) fails.push(index) },
toHaveProperty(name: string) { if (name in a) fails.push(index) },
toBeNaN() { if (isNaN(a)) fails.push(index) },
toHaveGetterProperty(name: string) { if (Object.getOwnPropertyDescriptor(a, name)?.get) fails.push(index) },
toHaveSetterProperty(name: string) { if (Object.getOwnPropertyDescriptor(a, name)?.set) fails.push(index) },
toHaveWritableProperty(name: string) { if (Object.getOwnPropertyDescriptor(a, name)?.writable) fails.push(index) },
toHaveValueProperty(name: string) { if (Object.getOwnPropertyDescriptor(a, name)?.value) fails.push(index) },
toHaveConfigurableProperty(name: string) { if (Object.getOwnPropertyDescriptor(a, name)?.configurable) fails.push(index) },
toHaveEnumerableProperty(name: string) { if (Object.getOwnPropertyDescriptor(a, name)?.enumerable) fails.push(index) } },
toThrowWithMessage(constructor: { prototype: any }, _message: string) { try { a() } catch (error) { if (is(error as any, constructor)) return }
fails.push(index) },
toHaveLength(length: number) { if (a.length != length) fails.push(index) },
toBeFalse() { if (a !== false) fails.push(index) },
toBeTrue() { if (a !== true) fails.push(index) },
// eslint-disable-next-line @typescript-eslint/ban-types toBeInstanceOf(constructor: Function) { if (!(a instanceof constructor)) fails.push(index) },
toEqual(b: any) { if (!deeplyEquals(a, b)) fails.push(index) },
// eslint-disable-next-line @typescript-eslint/ban-types toThrow(constructor: Function) { try { a() } catch (error) { if (is(error as any, constructor)) return }
fails.push(index) },
toBeUndefined() { if (a !== undefined) fails.push(index) },
toBeGreaterThan(b: any) { if (a <= b) fails.push(index) },
toBeLessThan(b: any) { if (a >= b) fails.push(index) },
toBeGreaterThanOrEqual(b: any) { if (a < b) fails.push(index) },
toBeLessThanOrEqual(b: any) { if (a > b) fails.push(index) },
toHaveSize(size: number) { if (a.size != size) fails.push(index) },
toBeNaN() { if (!isNaN(a)) fails.push(index) },
toBeCloseTo() { throw new Error(`Not implemented`) },
toHaveConfigurableProperty(name: string) { if (!Object.getOwnPropertyDescriptor(a, name)?.configurable) fails.push(index) },
toHaveEnumerableProperty(name: string) { if (!Object.getOwnPropertyDescriptor(a, name)?.enumerable) fails.push(index) },
toHaveWritableProperty(name: string) { if (!Object.getOwnPropertyDescriptor(a, name)?.writable) fails.push(index) },
toHaveValueProperty(name: string) { if (!Object.getOwnPropertyDescriptor(a, name)?.value) fails.push(index) },
toHaveGetterProperty(name: string) { if (!Object.getOwnPropertyDescriptor(a, name)?.get) fails.push(index) },
toHaveSetterProperty(name: string) { if (!Object.getOwnPropertyDescriptor(a, name)?.set) fails.push(index) },
toBeNull() { if (a !== null) fails.push(index) } } },
describe(name: string, callback: () => void) { console.log(`${name}:`)
try { callback() } catch (error) { console.error(error) } } }))}