expexp
Version:
The express model io and express model and data representation.
130 lines (118 loc) • 4.79 kB
JavaScript
import ist from 'ist'
import fs from 'fs'
import {traverse, path2str, ALPHANUM_ORDER} from '../dist/index.cjs'
import {loadCases, genCaseInfo} from './helper.js'
function traverseAndLog(inputJson, lines, keyOrder) {
const afterFct = function(json, path, ii) {
lines.push(`A ${path2str(ii, path, json)}`)
}
const beforeFct = function(json, path, ii) {
lines.push(`B ${path2str(ii, path, json)}`)
}
traverse(inputJson, afterFct, beforeFct, keyOrder)
}
function apply(name, inputJson, shouldEventOutput, keyOrder) {
const isLines = []
traverseAndLog(inputJson, isLines, keyOrder)
const shdLines = shouldEventOutput.split(/\r?\n/)
function fLine(str) {
str = str.trim()
if (0 < str.length) {
return str
} else {
return 'Line is empty.'
}
}
// ist(`Has ${isLines.length} lines.`,`Has ${shdLines.length} lines.`)
if ( isLines.length == shdLines.length
|| (
isLines.length == shdLines.length - 1
&& shdLines[isLines.length].trim() == ''
)
) {
// We cannot use shdLines.length to stop iteration, because of
// the one empty should line exemption.
for (let i=0; i<isLines.length; i++) { // Not i<shdLines.length .
const lineNumber = i + 1
const paddedLineNumber = String(lineNumber).padStart(3, ' ')
const shdLine = `${paddedLineNumber} ${fLine(shdLines[i])}`
const isLine = `${paddedLineNumber} ${fLine(isLines[i])}`
ist(isLine, shdLine)
}
} else if (isLines.length < shdLines.length) {
// Exceptionally accept one additional empty line in the
// should file. Common consequence from normal handling.
const lineNumber = isLines.length+1
const paddedLineNumber = String(lineNumber).padStart(3, ' ')
const shdLine = `${paddedLineNumber} ${fLine(shdLines[lineNumber-1])}`
ist(`${paddedLineNumber} Line does not exist.`, shdLine)
} else if (shdLines.length < isLines.length) {
const lineNumber = shdLines.length+1
const paddedLineNumber = String(lineNumber).padStart(3, ' ')
const isLine = `${paddedLineNumber} ${fLine(isLines[lineNumber-1])}`
ist(isLine, `${paddedLineNumber} Line does not exist.`)
}
}
function applyCase(cInfo) {
let jsonStr = fs.readFileSync(cInfo.json, 'utf8')
jsonStr = `{"t":"scm","id":"T_S","refs":null,"csts":null,"scts":{"t":"a_scm_scts","s":[${jsonStr}]}}`
const json = JSON.parse(jsonStr)
const shdLog = fs.readFileSync(cInfo.tlog, 'utf8')
// Load once and test twice.
it(`The ${cInfo.name} is traversed correctly. (Fields in pure alphanumerical order)`, () => {
apply(cInfo.name, json, shdLog, ALPHANUM_ORDER())
})
// The pre-sorted order results must be identical for the same
// should-log-files can be used. But internally the more
// complicated sorting algo is triggered. - Just for coverage.
// That is why there must be the two very first keys: 'abs' and 'arg' (not 'arg0').
it(`The ${cInfo.name} is traversed correctly. (Fields in pre-sorted order)`, () => {
apply(cInfo.name, json, shdLog, ['abs', 'arg'])
})
}
function testCase(dirName, caseName) {
const ci = genCaseInfo(dirName, caseName)
// it(`The ${ci.name} is traversed correctly.`, () => {
applyCase(ci)
// })
}
function testCases(dirName) {
const cases = loadCases(dirName, true)
for (let i = 0; i<cases.length; i++) {
const ci = cases[i]
// if (ci.name != 'NttPerson') continue // uncomment for focus testing
// it(`The ${ci.name} is traversed correctly.`, () => {
applyCase(ci)
// })
}
}
describe("Tree traverse", () => {
context("The before and after events are triggered the right way.", () => {
// testCase('uUsers', 'TpTotallyMixedSomething')
// testCase('uUsers', 'TpThingSet', true)
// testCase('uGeometry', 'TpOneOrTwoReal', true)
// testCase('units', 'TpLabel', true)
// testCase('units', 'TpIntegerGreaterThanZero', true)
// testCase('uGeometry', 'TpHhMmSsMx', true) // WHEREs with SELF, AND and OR and comarators, ABS, SIZEOF and index qualifier
// testCase('uGeometry', 'Tp3GreaterThanZero', true)
// testCase('units', 'TpCompass', true)
// testCase('units', 'StcCourseConstraints', true)
// testCase('units', 'PrcSimpleParams', true)
// testCase('units', 'NttWithSimpleAttrs', true)
// testCase('units', 'NttWithGenAggrTypeAttrs', true)
// testCase('uUsers', 'NttSoftware', true)
// testCase('uUsers', 'NttPerson', true)
// testCase('uUsers', 'NttOrganisation', true)
// testCase('uUsers', 'NttBase', true)
// testCase('units', 'FctWoParams', true)
// testCase('units', 'FctManySameParams', true)
// testCase('units', 'FctListToArray', true)
// testCase('units', 'FctBoundingPoints', true)
// Enable one or many of the tests above, if focus is needed.
// testCase('units', 'file_name_wo_ext_goes_here', true)
testCases('units')
testCases('uUsers')
testCases('uGeometry')
testCases('uCourses')
})
})