run-bundle.test.js (3514B)
1 import { readFileSync } from 'node:fs'; 2 import { strictEqual, ok, throws } from 'node:assert'; 3 import { describe, it } from 'node:test'; 4 import { 5 findLib, 6 init, 7 free, 8 loadBundleDefault, 9 loadBundle, 10 reduce, 11 app, 12 ofString, 13 ofNumber, 14 toBool, 15 toString, 16 decode, 17 decodeType, 18 } from '../src/lib.js'; 19 20 const fixtureDir = '../../test/fixtures'; 21 const libPath = findLib(); 22 23 describe('run bundle — booleans', () => { 24 it('true.arboricx evaluates to true', () => { 25 const ctx = init(libPath); 26 try { 27 const bundle = readFileSync(`${fixtureDir}/true.arboricx`); 28 const root = loadBundleDefault(ctx, bundle); 29 const result = reduce(ctx, root); 30 strictEqual(toBool(ctx, result), true); 31 strictEqual(decodeType(ctx, result), 'bool'); 32 strictEqual(decode(ctx, result), 'true'); 33 } finally { 34 free(ctx); 35 } 36 }); 37 38 it('false.arboricx evaluates to false', () => { 39 const ctx = init(libPath); 40 try { 41 const bundle = readFileSync(`${fixtureDir}/false.arboricx`); 42 const root = loadBundleDefault(ctx, bundle); 43 const result = reduce(ctx, root); 44 strictEqual(toBool(ctx, result), false); 45 strictEqual(decodeType(ctx, result), 'bool'); 46 strictEqual(decode(ctx, result), 'false'); 47 } finally { 48 free(ctx); 49 } 50 }); 51 }); 52 53 describe('run bundle — id', () => { 54 it('id applied to string returns the string', () => { 55 const ctx = init(libPath); 56 try { 57 const bundle = readFileSync(`${fixtureDir}/id.arboricx`); 58 const idRoot = loadBundleDefault(ctx, bundle); 59 const arg = ofString(ctx, 'hello'); 60 const applied = app(ctx, idRoot, arg); 61 const result = reduce(ctx, applied); 62 strictEqual(toString(ctx, result), 'hello'); 63 strictEqual(decodeType(ctx, result), 'string'); 64 } finally { 65 free(ctx); 66 } 67 }); 68 }); 69 70 describe('run bundle — append', () => { 71 it('append "hello " "world" = "hello world"', () => { 72 const ctx = init(libPath); 73 try { 74 const bundle = readFileSync(`${fixtureDir}/append.arboricx`); 75 let term = loadBundleDefault(ctx, bundle); 76 term = app(ctx, term, ofString(ctx, 'hello ')); 77 term = app(ctx, term, ofString(ctx, 'world')); 78 const result = reduce(ctx, term); 79 strictEqual(toString(ctx, result), 'hello world'); 80 } finally { 81 free(ctx); 82 } 83 }); 84 }); 85 86 describe('run bundle — notQ', () => { 87 it('notQ loads and reduces without error', () => { 88 const ctx = init(libPath); 89 try { 90 const bundle = readFileSync(`${fixtureDir}/notQ.arboricx`); 91 const root = loadBundleDefault(ctx, bundle); 92 const result = reduce(ctx, root); 93 ok(result > 0); 94 } finally { 95 free(ctx); 96 } 97 }); 98 }); 99 100 describe('run bundle — named export', () => { 101 it('loadBundle selects named export', () => { 102 const ctx = init(libPath); 103 try { 104 const bundle = readFileSync(`${fixtureDir}/id.arboricx`); 105 const root = loadBundle(ctx, bundle, 'id'); 106 ok(root > 0); 107 // id is a function; apply it before reducing 108 const applied = app(ctx, root, ofString(ctx, 'test')); 109 const result = reduce(ctx, applied); 110 strictEqual(toString(ctx, result), 'test'); 111 } finally { 112 free(ctx); 113 } 114 }); 115 116 it('missing export throws', () => { 117 const ctx = init(libPath); 118 try { 119 const bundle = readFileSync(`${fixtureDir}/id.arboricx`); 120 throws(() => loadBundle(ctx, bundle, 'nonexistent'), /failed/); 121 } finally { 122 free(ctx); 123 } 124 }); 125 });