UNPKG

microvium

Version:

A compact, embeddable scripting engine for microcontrollers for executing small scripts written in a subset of JavaScript.

130 lines 5.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.testsInFolder = void 0; const common_1 = require("./common"); const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const os_1 = __importDefault(require("os")); function testsInFolder(folder, defineTest) { let phase = 'definition-phase'; const allFilenames = {}; const testCases = new Array(); const testCaseFolders = fs_extra_1.default.readdirSync(folder); for (const testCaseFolder of testCaseFolders) { const testName = testCaseFolder; const testCaseInputDir = path_1.default.join(folder, testCaseFolder); const testCaseExpectedOutputDir = path_1.default.join(folder, testCaseFolder, './expected'); const testCaseActualOutputPath = path_1.default.join(folder, testCaseFolder, './actual'); const filenamesForTestCase = {}; allFilenames[testName] = filenamesForTestCase; const handlers = new Array(); defineTest({ input, inputFilename, onRun, output, actualOutputFilename, expectedOutputFilename }); testCases.push({ name: testName, run() { fs_extra_1.default.emptyDirSync(testCaseActualOutputPath); for (const handler of handlers) { handler(); } } }); function onRun(handler) { expectDefinitionPhase('onRun'); handlers.push(handler); } function inputFilename(filename) { expectDefinitionPhase('inputFilename'); return path_1.default.join(testCaseInputDir, filename); } function input(filename, encoding) { expectDefinitionPhase('input'); const fullFilename = inputFilename(filename); return { read() { expectRunPhase('Input.read'); return fs_extra_1.default.readFileSync(fullFilename, encoding); } }; } function actualOutputFilename(filename) { expectDefinitionPhase('actualOutputFilename'); return path_1.default.join(testCaseActualOutputPath, filename); } function expectedOutputFilename(filename) { expectDefinitionPhase('expectedOutputFilename'); return path_1.default.join(testCaseExpectedOutputDir, filename); } function output(filename, encoding) { expectDefinitionPhase('output'); let actual; let expected; const file = { actualFilename: actualOutputFilename(filename), expectedFilename: expectedOutputFilename(filename), get actual() { expectRunPhase('Output.actual'); if (actual === undefined) { throw new Error('Getting `Output.actual` before setting it'); } return actual; }, set actual(value) { expectRunPhase('Output.actual'); actual = value ?? ''; if (encoding === 'utf8' || encoding === 'utf-8') { // If you use the git setting "auto" for line feeds (which is the // default setting), then git will automatically be translating line // feeds to that of the operating system when you check out or // commit files. Matching this behavior here will avoid warnings // when you commit these files to git. value = value.replace(/\r?\n/g, os_1.default.EOL); } fs_extra_1.default.writeFileSync(file.actualFilename, value, { encoding }); }, get expected() { expectRunPhase('Output.expected'); if (expected === undefined) { expected = fs_extra_1.default.readFileSync(file.expectedFilename, encoding); } return expected; }, check() { expectRunPhase('Output.check'); (0, common_1.assertSameCode)(file.actual, file.expected); } }; filenamesForTestCase[filename] = { output: file.actualFilename, expected: file.expectedFilename, isBinary: encoding !== 'utf8' }; return file; } } return { run, filenames: allFilenames }; function run() { phase = 'run-phase'; if (!globalThis.test) { throw new Error('Can only run tests from within the mocha test runner'); } // Invoke the mocha test function for (const { name, run } of testCases) { globalThis.test(name, run); } } function expectDefinitionPhase(methodName) { if (phase !== 'definition-phase') { throw new Error(`Method "${methodName}" can only be used during the definition phase`); } } function expectRunPhase(methodName) { if (phase !== 'run-phase') { throw new Error(`Method "${methodName}" can only be used during the run phase`); } } } exports.testsInFolder = testsInFolder; //# sourceMappingURL=file-based-tests.js.map