UNPKG

es-vm

Version:

A visual machine run in ES env like Node/Browser

86 lines (67 loc) 1.83 kB
const assert = require('assert'); const pack = require('./pack'); pack(({ESVM, Statement, register}) => describe('Statement::', function () { it('Constructor without error', function () { return new Statement(); }); it('Register a real statement without error', function () { class TestStatement extends Statement { *execute() { } } register(TestStatement, 'TEST'); }); it('Register(this) a no-execute statement with error.', function () { class TestStatement extends Statement {} assert.throws(() => { register(TestStatement, 'TEST'); }); }); it('Register(this, false) a no-execute statement without error.', function () { class TestStatement extends Statement {} register(TestStatement, 'TEST', false); }); it('Link syntax node by symbol without error', function () { class TestStatement extends Statement {} register(TestStatement, 'FOR::NODE', false); const syntax = { SYMBOL: 'FOR::NODE', POSITION: { type: '[native]' }, BODY: {} }; const node = new TestStatement(syntax); assert.deepEqual(node.position, { type: '[native]' }); }); it('doExecution ret & yield', function () { class TestStatement extends Statement { *execute() { yield 'exe'; return 'ret'; } } register(TestStatement, 'FOR::EXECUTION'); const syntax = { SYMBOL: 'FOR::EXECUTION', POSITION: { type: '[native]' }, BODY: {} }; const node = new TestStatement(syntax); const vm = new ESVM(); function* call() { yield 'start'; const ret = yield* node.doExecution({vm}); assert.equal(ret, 'ret'); yield 'end'; } const result = ['start', 'exe', 'end']; for(let tick of call()) { assert.equal(tick, result.shift()); } }); }));