UNPKG

lua_to_cpls

Version:

A tool capable of compiling a subset of Lua to Please lang compiled files

58 lines (50 loc) 1.43 kB
// @ts-check /** * @description A file with the tests for the code generation * @author Daniel del Castillo de la Rosa <alu0101225548@ull.edu.es> * @since 24/05/2021 */ 'use strict'; const should = require('chai').should(); const {convertToCompiledPlease} = require('../src/parser.js'); const fs = require('fs'); const monkeyPatching = {}; const saveMonkeyPatching = () => { Object.keys(Object.prototype).forEach((key) => { monkeyPatching[key] = Object.prototype[key]; delete Object.prototype[key]; }); }; const restoreMonkeyPatching = () => { Object.keys(monkeyPatching).forEach((key) => { Object.prototype[key] = monkeyPatching[key]; }); }; describe('Code generation', () => { const runTest = (testName) => { saveMonkeyPatching(); const actual = JSON.stringify(convertToCompiledPlease( fs.readFileSync('test/lua/' + testName + '.lua', {encoding: 'utf8'}), ), null, 2).replace(/\r/g, ''); restoreMonkeyPatching(); const expected = fs.readFileSync('test/cpls/' + testName + '.cpls', {encoding: 'utf8'}); actual.should.eql(expected.replace(/\r/g, '')); }; const testList = [ 'declaration', 'assign', 'complex-operations', 'boolean-operations', 'while', 'branches', 'array', 'for', 'field-access', 'function', ]; testList.forEach((test) => { it(test, () => { runTest(test); }); }); });