UNPKG

fauton

Version:

A library to test any finite automaton with arbitrary alphabets

72 lines (71 loc) 3.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.test = void 0; const fs_1 = __importDefault(require("fs")); const countFileLines_1 = require("../../../utils/countFileLines"); const generateAggregateMessage_1 = require("../../../utils/generateAggregateMessage"); const GenerateString_1 = require("../../GenerateString"); const createFileWriteStreams_1 = require("./createFileWriteStreams"); const testAutomaton_1 = require("./testAutomaton"); async function test(logsPath, configs, preAutomatonTestCb, postAutomatonTestCb) { const finiteAutomatonTestInfos = configs.map(() => ({ falsePositives: 0, falseNegatives: 0, truePositives: 0, trueNegatives: 0, })); for (let index = 0; index < configs.length; index += 1) { const config = configs[index]; const finiteAutomatonTestInfo = finiteAutomatonTestInfos[index]; const { automaton, options } = config; const writeStreams = (0, createFileWriteStreams_1.createFileWriteStreams)(logsPath, automaton.automaton.label, { aggregate: options.outputFiles?.aggregate ?? true, case: options.outputFiles?.case ?? true, correct: options.outputFiles?.correct ?? true, incorrect: options.outputFiles?.incorrect ?? true, input: options.outputFiles?.input ?? true, accepted: options.outputFiles?.accepted ?? true, rejected: options.outputFiles?.rejected ?? true, }); if (options.type === 'file') { const readStream = fs_1.default.createReadStream(options.filePath, { encoding: 'utf-8' }); const fileLines = await (0, countFileLines_1.countFileLines)(options.filePath); if (preAutomatonTestCb) { preAutomatonTestCb(fileLines); } for await (const chunks of readStream) { const inputStrings = chunks.split('\n'); (0, testAutomaton_1.testAutomaton)(automaton, finiteAutomatonTestInfo, writeStreams.record, inputStrings, postAutomatonTestCb); } } else { let generatedStrings = []; if (options.type === 'generate') { if (options.random) { generatedStrings = GenerateString_1.GenerateString.generateRandomUnique(options.random.total, automaton.automaton.alphabets, options.random.minLength, options.random.maxLength); } else { generatedStrings = GenerateString_1.GenerateString.generateAllCombosWithinLength(automaton.automaton.alphabets, options.combo.maxLength, options.combo.startLength ?? 1); } } else { generatedStrings = options.inputs; } if (preAutomatonTestCb) { preAutomatonTestCb(generatedStrings.length); } (0, testAutomaton_1.testAutomaton)(automaton, finiteAutomatonTestInfo, writeStreams.record, generatedStrings, postAutomatonTestCb); } const { record: { aggregateWriteStream }, endStreams, } = writeStreams; const { withoutColors, withColors } = (0, generateAggregateMessage_1.generateAggregateMessage)(automaton.automaton.label, automaton.automaton.description, finiteAutomatonTestInfo); console.log(`\n${withColors}`); if (aggregateWriteStream) { aggregateWriteStream.write(withoutColors); } endStreams(); } } exports.test = test;