@bitloops/bl-transpiler
Version:
The one and only Bitloops Language transpiler
91 lines • 4.16 kB
JavaScript
import { BitloopsTypesMapping } from '../../../src/helpers/mappings.js';
import { BitloopsParser } from '../../../src/parser/index.js';
import { IntermediateASTParser } from '../../../src/ast/core/index.js';
import { isParserErrors } from '../../../src/parser/core/guards/index.js';
import { isIntermediateASTValidationErrors } from '../../../src/ast/core/guards/index.js';
import { invalidStructDeclarationCases, validStructDeclarationCases, validMultipleStructsTestCases, } from './mocks/struct.js';
import { ParserSyntacticError } from '../../../src/parser/core/types.js';
const BOUNDED_CONTEXT = 'Hello World';
const MODULE = 'core';
describe('Struct declaration is valid', () => {
let resultTree;
const parser = new BitloopsParser();
const intermediateParser = new IntermediateASTParser();
validStructDeclarationCases.forEach((testStructDeclaration) => {
test(`${testStructDeclaration.description}`, () => {
const initialModelOutput = parser.parse({
core: [
{
boundedContext: BOUNDED_CONTEXT,
module: MODULE,
fileId: testStructDeclaration.fileId,
fileContents: testStructDeclaration.inputBLString,
},
],
});
if (!isParserErrors(initialModelOutput)) {
const result = intermediateParser.parse(initialModelOutput);
if (!isIntermediateASTValidationErrors(result)) {
resultTree = result.core[BOUNDED_CONTEXT][MODULE];
}
}
const structDeclarationNodes = resultTree.getRootChildrenNodesByType(BitloopsTypesMapping.TStruct);
const value = structDeclarationNodes[0].getValue();
expect(value).toMatchObject(testStructDeclaration.expected);
expect(structDeclarationNodes.length).toBe(1);
});
});
});
describe('Struct declaration is invalid', () => {
const parser = new BitloopsParser();
invalidStructDeclarationCases.forEach((testStruct) => {
test(`${testStruct.description}`, () => {
const initialModelOutput = parser.parse({
core: [
{
boundedContext: BOUNDED_CONTEXT,
module: MODULE,
fileId: testStruct.fileId,
fileContents: testStruct.inputBLString,
},
],
});
expect(Array.isArray(initialModelOutput)).toBeTruthy();
if (!isParserErrors(initialModelOutput)) {
throw new Error('Parser should return errors');
}
initialModelOutput.forEach((error) => {
expect(error).toBeInstanceOf(ParserSyntacticError);
});
});
});
});
describe('Struct declaration with multiple structs is valid', () => {
let resultTree;
const parser = new BitloopsParser();
const intermediateParser = new IntermediateASTParser();
validMultipleStructsTestCases.forEach((testDTO) => {
test(`${testDTO.description}`, () => {
const initialModelOutput = parser.parse({
core: [
{
boundedContext: BOUNDED_CONTEXT,
module: MODULE,
fileId: testDTO.fileId,
fileContents: testDTO.inputBLString,
},
],
});
if (!isParserErrors(initialModelOutput)) {
const result = intermediateParser.parse(initialModelOutput);
if (!isIntermediateASTValidationErrors(result)) {
resultTree = result.core[BOUNDED_CONTEXT][MODULE];
}
}
const structNodes = resultTree.getRootChildrenNodesByType(BitloopsTypesMapping.TStruct);
const values = structNodes.map((node) => node.getValue());
expect(values).toMatchObject(testDTO.expected);
});
});
});
//# sourceMappingURL=structDeclaration.steps.js.map