expexp
Version:
The express model io and express model and data representation.
144 lines (123 loc) • 4.79 kB
JavaScript
import ist from 'ist'
import {ModelDeserializer} from '../dist/index.cjs'
import fs from 'fs'
import { loadCases, genCaseInfo, stringify } from './helper.js'
function apply(name, inputStr, shouldResJson, isParsingInfoToRemove=false) {
const psr = ModelDeserializer()
psr.withParsingInfo = isParsingInfoToRemove == false
let isResJson = psr.syntaxJsonFromString(inputStr)
// console.log('raw', name, isResJson)
isResJson = isResJson.scts.s[0] // TODO try to avoid this conversion
const isStr = stringify(isResJson)
const shouldStr = stringify(shouldResJson)
// if (shouldResJson.id == 'NttPerson') {
// console.log(getDifference(isStr, shouldStr))
// }
ist(isStr, shouldStr)
}
function applyCase(cInfo, addSchema=false) {
let xpl = fs.readFileSync(cInfo.xpl, 'utf8')
const xplPrefix = 'SCHEMA T_S;'
if (addSchema) {
xpl = xplPrefix + '\n' + xpl + '\nEND_SCHEMA;'
}
let jsonStr = fs.readFileSync(cInfo.json, 'utf8')
const json = JSON.parse(jsonStr)
// Change the output if parsing info is tested (or not)
let isParsingInfoToRemove = false
if (addSchema && 'p' in json) {
json.p.line++ // inc by prefix line
json.p.offset += xplPrefix.length
if ('spc' in json && 'p' in json.spc) {
json.spc.p.line++ // inc by prefix line
json.spc.p.offset += xplPrefix.length
}
if ('spc' in json && 'vals' in json.spc) {
for (let vl of json.spc.vals.s) {
vl.p.line++ // inc by prefix line
vl.p.offset += xplPrefix.length+1 // TODO what is that +1 ?
}
}
if ('spc' in json && 'refs' in json.spc) {
for (let vl of json.spc.refs.s) {
vl.p.line++ // inc by prefix line
vl.p.offset += xplPrefix.length+1 // TODO what is that +1 ?
}
}
} else {
isParsingInfoToRemove = true
}
apply(cInfo.name, xpl, json, isParsingInfoToRemove)
}
function testCase(dirName, caseName, addSchema=false) {
const ci = genCaseInfo(dirName, caseName)
it(`The ${ci.name} is transformed correctly.`, () => {
applyCase(ci, addSchema)
})
}
function testCases(dirName) {
const cases = loadCases(dirName)
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 transformed correctly.`, () => {
applyCase(ci, true)
})
}
// This for loop autimates explicit cases like the following one:
// const startsFrom0 = "SCHEMA TEST_SCHEMA; TYPE TpInt = INTEGER; END_TYPE; END_SCHEMA;"
// const resultsIn0 = [{
// t: 'type',
// id: 'TpInt',
// p: { line: 1, col: 6, offset: 6, lineBreaks: 0 },
// spec: { t: 'integer' }
// }]
// it("The TpInt is transformed correctly.", () =>
// apply('TpInt', startsFrom0, resultsIn0)
// )
}
describe("ModelDeserializer units", () => {
context("Remarks work.", () => {
testCase('remarks', 'comments')
})
context("The items and their parsing info are correct.", () => {
testCases('unitsWithParseInfo')
})
context("The items are parsed correctly. (Parsing info is ignored.)", () => {
// testCase('units', 'RuleWithRealConst', true) // precision spec, real literal
// testCase('units', 'RuleWithIfElseStmt', true) // IF, ELSE, interval expr
// testCase('units', 'RuleWithCompoundWithinCaseStmt', true)
// testCase('units', 'RuleWithAliasWithinCaseStmt', true) // ALIAS, qualifiers
// testCase('uUsers', 'TpThingSet', true)
// testCase('uGeometry', 'TpOneOrTwoReal', true)
// testCase('units', 'TpLabel', true)
// testCase('units', 'TpIntegerGreaterThanZero', true)
// testCase('uGeometry', 'TpHhMmSsMx', true)
// 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('uGeometry')
testCases('uUsers')
testCases('uCourses')
})
// More future todos:
// TODO functions / procedure / rules - locals
// TODO negatively test that the remarks example does not contain the
// types or entities mentioned in the remark only.
// TODO do WHERE tests of TpIntegerGreaterThanZero and TpHhMmSsMx
// TODO Test precision_spec for REAL as soon as this is implemented. (Add to NttWithSimpleAttrs.)
})