Skip to content

Commit

Permalink
Merge pull request #146 from halcyon-tech/feature/file_tests
Browse files Browse the repository at this point in the history
File tests
  • Loading branch information
worksofliam authored Oct 26, 2022
2 parents b67a947 + 84d492d commit 2d170cd
Show file tree
Hide file tree
Showing 7 changed files with 3,341 additions and 2 deletions.
60 changes: 58 additions & 2 deletions tests/parserSetup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import Parser from '../server/src/language/parser';
import Declaration from '../server/src/language/models/declaration';
import { getPrettyType } from '../server/src/language/models/fixed';

import glob from "glob";
import { readFile } from 'fs/promises';
import path from 'path';

import { readFile } from 'fs/promises';

import tables from './tables';

export default function setupParser(): Parser {
const parser = new Parser();

Expand Down Expand Up @@ -36,7 +42,57 @@ export default function setupParser(): Parser {
return {
found: false
};
})
});

parser.setTableFetch(async (table: string, aliases = false) => {
let recordFormats: {[name: string]: Declaration} = {};
const upperName = table.toUpperCase();

const data = tables[upperName] ? tables[upperName] : [];

data.forEach((row: any) => {
const {
WHNAME: formatName,
WHFLDT: type,
WHFLDB: strLength,
WHFLDD: digits,
WHFLDP: decimals,
WHFTXT: text,
} = row;

const name = aliases ? row.WHALIS || row.WHFLDE : row.WHFLDE;

if (name.trim() === ``) return;
if (name.startsWith(`*`)) return;

let recordFormat;
if (recordFormats[formatName]) {
recordFormat = recordFormats[formatName];
} else {
recordFormat = new Declaration(`struct`);
recordFormat.name = formatName;
recordFormats[formatName] = recordFormat;
}

const currentSubfield = new Declaration(`subitem`);
currentSubfield.name = name;
const keywords = [];

if (row.WHVARL === `Y`) keywords.push(`VARYING`);

currentSubfield.keywords = [getPrettyType({
type,
len: digits === 0 ? strLength : digits,
decimals: decimals,
keywords: [],
})];
currentSubfield.description = text.trim();

recordFormat.subItems.push(currentSubfield);
});

return Object.values(recordFormats);
});

return parser;
}
147 changes: 147 additions & 0 deletions tests/suite/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@

const assert = require(`assert`);

const {default: parserSetup} = require(`../parserSetup`);

const uri = `source.rpgle`;

exports.simple_file = async () => {
const lines = [
`**free`,
``,
`dcl-f employee disk usage(*input);`,
``,
`dsply employee.workdept;`,
``,
`return;`
].join(`\n`);

const parser = parserSetup();
const cache = await parser.getDocs(uri, lines);

assert.strictEqual(cache.files.length, 1);
assert.strictEqual(cache.structs.length, 0);

const fileDef = cache.find(`employee`);
assert.strictEqual(fileDef.name, `employee`);
assert.strictEqual(fileDef.keyword[`DISK`], true);
assert.strictEqual(fileDef.keyword[`USAGE`], `*INPUT`);

// file record formats should be expanded into the subitems
assert.strictEqual(fileDef.subItems.length, 1);

const empRdcFmt = fileDef.subItems[0];

assert.strictEqual(empRdcFmt.name, `EMPLOYEE`);

// 14 fields inside of this record format
assert.strictEqual(empRdcFmt.subItems.length, 14);
};

exports.many_formats = async () => {
const lines = [
`**free`,
``,
`dcl-f emps workstn;`,
``,
`write SFLDTA;`,
``,
`return;`
].join(`\n`);

const parser = parserSetup();
const cache = await parser.getDocs(uri, lines);

assert.strictEqual(cache.files.length, 1);

const fileDef = cache.find(`emps`);
assert.strictEqual(fileDef.name, `emps`);
assert.strictEqual(fileDef.keyword[`WORKSTN`], true);

// file record formats should be expanded into the subitems
assert.strictEqual(fileDef.subItems.length, 2);

const sfldta = fileDef.subItems[0];
assert.strictEqual(sfldta.name, `SFLDTA`);
assert.strictEqual(sfldta.subItems.length, 5);

const sflctl = fileDef.subItems[1];
assert.strictEqual(sflctl.name, `SFLCTL`);
assert.strictEqual(sflctl.subItems.length, 1);
};

exports.ds_extname = async () => {
const lines = [
`**free`,
``,
`Dcl-Ds Employee ExtName('EMPLOYEE') Qualified;`,
`end-ds;`,
``,
`Dsply Employee.empno;`,
``,
`return;`
].join(`\n`);

const parser = parserSetup();
const cache = await parser.getDocs(uri, lines);

assert.strictEqual(cache.files.length, 0);
assert.strictEqual(cache.structs.length, 1);

const structDef = cache.find(`employee`);
assert.strictEqual(structDef.name, `Employee`);
assert.strictEqual(structDef.subItems.length, 14);
};

exports.ds_extname_no_alias = async () => {
const lines = [
`**free`,
``,
`Dcl-Ds dept ExtName('department') Qualified;`,
`end-ds;`,
``,
`Dsply dept.deptname;`,
``,
`return;`
].join(`\n`);

const parser = parserSetup();
const cache = await parser.getDocs(uri, lines);

assert.strictEqual(cache.files.length, 0);
assert.strictEqual(cache.structs.length, 1);

const dept = cache.find(`dept`);
assert.strictEqual(dept.subItems.length, 5);

assert.strictEqual(dept.subItems[0].name, `DEPTNO`);
assert.strictEqual(dept.subItems[1].name, `DEPTNAME`);
}

exports.ds_extname_alias = async () => {
const lines = [
`**free`,
``,
`Dcl-Ds dept ExtName('department') alias Qualified;`,
`end-ds;`,
``,
`Dsply dept.deptname;`,
``,
`return;`
].join(`\n`);

const parser = parserSetup();
const cache = await parser.getDocs(uri, lines);

assert.strictEqual(cache.files.length, 0);
assert.strictEqual(cache.structs.length, 1);

const dept = cache.find(`dept`);
assert.strictEqual(dept.subItems.length, 5);

assert.strictEqual(dept.subItems[0].name, `DEPTNO`);
assert.strictEqual(dept.subItems[1].name, `DEPT_NAME`);
}

// exports.file_prefix = async () => {
// }
1 change: 1 addition & 0 deletions tests/suite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ module.exports = {
...require(`./fixed`),
...require(`./keywords`),
...require(`./linter`),
...require(`./files`)
}
Loading

0 comments on commit 2d170cd

Please sign in to comment.