Skip to content

Commit 1185b05

Browse files
giorgosntyVaragos
andauthored
Fix for of loop (#493)
* fixed semantic test paths Co-authored-by: Markos Girgis <[email protected]> * added statetent test * published package and fixed swc options --------- Co-authored-by: Markos Girgis <[email protected]>
1 parent 7cfbf65 commit 1185b05

File tree

93 files changed

+448
-194
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+448
-194
lines changed

transpiler/__tests__/ast/core/builders/statement/statementDirector.ts

+23
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,29 @@ export class StatementDirector {
100100
});
101101
}
102102

103+
buildConstDeclarationWithValueObjectIfError({
104+
name,
105+
valueObjectIdentifier,
106+
valueObjectFields,
107+
}: {
108+
name: string;
109+
valueObjectIdentifier: string;
110+
valueObjectFields: { identifier: string; expression: TExpression }[];
111+
}): TConstDeclaration {
112+
return new ConstDeclarationBuilderDirector().buildConstDeclarationWithValueObjectEvaluationIfError(
113+
{
114+
name,
115+
valueObjectIdentifier,
116+
fields: valueObjectFields.map((field) => {
117+
return new EvaluationFieldBuilderDirector().buildEvaluationField(
118+
field.identifier,
119+
field.expression,
120+
);
121+
}),
122+
},
123+
);
124+
}
125+
103126
buildConstDeclarationWithEntity({
104127
name,
105128
entityIdentifier,

transpiler/__tests__/ast/core/builders/statement/variableDeclarationDirector.ts

+27
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
import { ArgumentListBuilderDirector } from '../argumentListBuilderDirector.js';
2929
import { EvaluationBuilderDirector } from '../evaluationDirector.js';
3030
import { ExpressionBuilderDirector } from '../expressionDirector.js';
31+
import { IfErrorExpressionBuilderDirector } from '../ifErrorExpressionBuilderDirector.js';
3132
import { ConstDeclarationBuilder } from './constDeclarationBuilder.js';
3233
import { VariableDeclarationBuilder } from './variableDeclarationBuilder.js';
3334

@@ -250,6 +251,32 @@ export class ConstDeclarationBuilderDirector {
250251
return constDeclaration;
251252
}
252253

254+
buildConstDeclarationWithValueObjectEvaluationIfError({
255+
name,
256+
valueObjectIdentifier,
257+
fields,
258+
}: {
259+
name: string;
260+
valueObjectIdentifier: string;
261+
fields: TEvaluationField[];
262+
}): TConstDeclaration {
263+
const constDeclaration = this.constDeclarationBuilder
264+
.withIdentifier(name)
265+
.withExpression(
266+
new ExpressionBuilderDirector().buildIfErrorExpression(
267+
new IfErrorExpressionBuilderDirector().buildEmptyIfError(
268+
new ExpressionBuilderDirector().buildEvaluation(
269+
new EvaluationBuilderDirector().buildValueObjectEvaluation(valueObjectIdentifier, {
270+
fields,
271+
}),
272+
),
273+
),
274+
),
275+
)
276+
.build();
277+
return constDeclaration;
278+
}
279+
253280
buildConstDeclarationWithEntityEvaluation({
254281
name,
255282
entityIdentifier,

transpiler/__tests__/ast/core/forOfStatement.steps.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,8 @@ describe('For Of Statement is valid', () => {
5454
}
5555
}
5656

57-
const propsNodes = resultTree.getRootChildrenNodesByType(
58-
BitloopsTypesMapping.TForOfStatement,
59-
);
60-
const value = propsNodes[0].getValue();
57+
const forOfNode = resultTree.getRootChildrenNodesByType(BitloopsTypesMapping.TForOfStatement);
58+
const value = forOfNode[0].getValue();
6159

6260
expect(value).toMatchObject(forOfStatement.expectedForOfStatement);
6361
});

transpiler/__tests__/ast/core/mocks/statements/for-of/forOfStatement.ts

+45
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,49 @@ export const validForOfStatementTestCases: ForOfStatementTestCase[] = [
4747
])
4848
.build(),
4949
},
50+
{
51+
description: 'Simple for of statement',
52+
fileId: 'testFile.bl',
53+
inputBLString: FileUtil.readFileString(
54+
'transpiler/__tests__/ast/core/mocks/statements/for-of/forOfStatementMemberDot.bl',
55+
),
56+
expectedForOfStatement: new ForOfStatementBuilder()
57+
.withIdentifier('match')
58+
.withExpression(
59+
new ExpressionBuilderDirector().buildMemberExpression(
60+
new ExpressionBuilderDirector().buildIdentifierExpression('reconciliationReportResult'),
61+
'matches',
62+
),
63+
)
64+
.withStatementList([
65+
new StatementDirector().buildConstDeclarationWithValueObjectIfError({
66+
name: 'matchVO',
67+
valueObjectIdentifier: 'MatchVO',
68+
valueObjectFields: [
69+
{
70+
identifier: 'ledgerTransactionIds',
71+
expression: new ExpressionBuilderDirector().buildMemberExpression(
72+
new ExpressionBuilderDirector().buildIdentifierExpression('match'),
73+
'ledgerTransactionIds',
74+
),
75+
},
76+
{
77+
identifier: 'externalTransactionIds',
78+
expression: new ExpressionBuilderDirector().buildMemberExpression(
79+
new ExpressionBuilderDirector().buildIdentifierExpression('match'),
80+
'externalTransactionIds',
81+
),
82+
},
83+
{
84+
identifier: 'createdBy',
85+
expression: new ExpressionBuilderDirector().buildMemberExpression(
86+
new ExpressionBuilderDirector().buildIdentifierExpression('match'),
87+
'createdBy',
88+
),
89+
},
90+
],
91+
}),
92+
])
93+
.build(),
94+
},
5095
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Props MatchProps {
2+
optional string matchId;
3+
string[] ledgerTransactionIds;
4+
string[] externalTransactionIds;
5+
string createdBy;
6+
}
7+
8+
9+
JestTestStatement {
10+
for(match of reconciliationReportResult.matches) {
11+
const matchVO = MatchVO.create({
12+
ledgerTransactionIds: match.ledgerTransactionIds,
13+
externalTransactionIds: match.externalTransactionIds,
14+
createdBy: match.createdBy
15+
}).ifError();
16+
};
17+
}

transpiler/__tests__/end-to-end/mocks/array-methods/index.ts

-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
import { FileUtil } from '../../../../src/utils/file.js';
22

33
export const ARRAY_FUNCTIONAL_METHODS_END_TO_END_TEST_CASES = [
4-
// {
5-
// description: 'Testing forEach method',
6-
// input: FileUtil.readFileString(
7-
// 'transpiler/__tests__/end-to-end/mocks/array-methods/for-each.bl',
8-
// ),
9-
// output: FileUtil.readFileString(
10-
// 'transpiler/__tests__/end-to-end/mocks/array-methods/for-each.mock.ts',
11-
// ),
12-
// },
134
{
145
description: 'Testing map method',
156
input: FileUtil.readFileString('transpiler/__tests__/end-to-end/mocks/array-methods/map.bl'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Struct Match {
2+
string[] ledgerTransactionIds;
3+
string[] externalTransactionIds;
4+
string createdBy;
5+
}
6+
7+
Struct ReconciliationReport {
8+
Match[] matches;
9+
}
10+
11+
Props MatchProps {
12+
string[] ledgerTransactionIds;
13+
string[] externalTransactionIds;
14+
string createdBy;
15+
}
16+
17+
ValueObject MatchVO {
18+
static create(props: MatchProps): (OK(MatchVO), Errors()) {}
19+
}
20+
21+
DomainService ReconciliationDomainService() {
22+
23+
public reconcile(reconciliationReportsResult: ReconciliationReport): (OK(void),Errors()) {
24+
25+
const matchesVO = [];
26+
for(match of reconciliationReportsResult.matches) {
27+
const matchVO = MatchVO.create({
28+
ledgerTransactionIds: match.ledgerTransactionIds,
29+
externalTransactionIds: match.externalTransactionIds,
30+
createdBy: match.createdBy
31+
}).ifError();
32+
matchesVO.push(matchVO);
33+
}
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Application, Either, fail, ok } from '@bitloops/bl-boilerplate-core';
2+
import { MatchVO } from '../match.value-object';
3+
import { ReconciliationReport } from '../../structs/reconciliation-report.struct';
4+
export class ReconciliationDomainService {
5+
constructor() {}
6+
public async reconcile(
7+
reconciliationReportsResult: ReconciliationReport,
8+
): Promise<Either<void, Application.Repo.Errors.Unexpected>> {
9+
const matchesVO = [];
10+
for (const match of reconciliationReportsResult.matches) {
11+
const matchVO = MatchVO.create({
12+
ledgerTransactionIds: match.ledgerTransactionIds,
13+
externalTransactionIds: match.externalTransactionIds,
14+
createdBy: match.createdBy,
15+
});
16+
if (matchVO.isFail()) {
17+
return fail(matchVO.value);
18+
}
19+
matchesVO.push(matchVO.value);
20+
}
21+
return ok();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ClassTypes } from '../../../../src/helpers/mappings.js';
2+
import { FileUtil } from '../../../../src/utils/file.js';
3+
4+
export const STATEMENTS_TEST_CASES = [
5+
{
6+
description: 'Testing For of statement what pushes value objects inside an array',
7+
input: FileUtil.readFileString(
8+
'transpiler/__tests__/end-to-end/mocks/statements/for-of-statement.bl',
9+
),
10+
classType: ClassTypes.DomainService,
11+
output: FileUtil.readFileString(
12+
'transpiler/__tests__/end-to-end/mocks/statements/for-of-statement.mock.ts',
13+
),
14+
},
15+
];

transpiler/__tests__/end-to-end/already-declared.steps.ts renamed to transpiler/__tests__/end-to-end/semantics/already-declared.steps.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import Transpiler from '../../src/Transpiler.js';
2-
import { IntermediateASTParser } from '../../src/ast/core/index.js';
3-
import { BitloopsParser } from '../../src/parser/index.js';
4-
import { TargetGenerator } from '../../src/target/index.js';
5-
import { SYMBOL_TABLE_ALREADY_DECLARED_TEST_CASES } from './mocks/symbol-table/already-declared/index.js';
1+
import Transpiler from '../../../src/Transpiler.js';
2+
import { IntermediateASTParser } from '../../../src/ast/core/index.js';
3+
import { BitloopsParser } from '../../../src/parser/index.js';
4+
import { TargetGenerator } from '../../../src/target/index.js';
5+
import { SYMBOL_TABLE_ALREADY_DECLARED_TEST_CASES } from './mocks/semantic-errors/already-declared/index.js';
66

77
describe('Already declared variables test cases', () => {
88
const boundedContext = 'Hello world';

transpiler/__tests__/end-to-end/const-reassingment.steps.ts renamed to transpiler/__tests__/end-to-end/semantics/const-reassingment.steps.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import Transpiler from '../../src/Transpiler.js';
2-
import { IntermediateASTParser } from '../../src/ast/core/index.js';
3-
import { BitloopsParser } from '../../src/parser/index.js';
4-
import { TargetGenerator } from '../../src/target/index.js';
5-
import { SYMBOL_TABLE_CONSTANT_REASSIGNMENT_TEST_CASES } from './mocks/symbol-table/constant-reassignment/index.js';
1+
import Transpiler from '../../../src/Transpiler.js';
2+
import { IntermediateASTParser } from '../../../src/ast/core/index.js';
3+
import { BitloopsParser } from '../../../src/parser/index.js';
4+
import { TargetGenerator } from '../../../src/target/index.js';
5+
import { SYMBOL_TABLE_CONSTANT_REASSIGNMENT_TEST_CASES } from './mocks/semantic-errors/constant-reassignment/index.js';
66

77
describe('constant reassingment identifiers test cases', () => {
88
const boundedContext = 'Hello world';

transpiler/__tests__/end-to-end/member-not-defined.steps.ts renamed to transpiler/__tests__/end-to-end/semantics/member-not-defined.steps.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import Transpiler from '../../src/Transpiler.js';
2-
import { IntermediateASTParser } from '../../src/ast/core/index.js';
3-
import { BitloopsParser } from '../../src/parser/index.js';
4-
import { TargetGenerator } from '../../src/target/index.js';
5-
import { SYMBOL_TABLE_MEMBER_NOT_DEFINED_TEST_CASES } from './mocks/symbol-table/not-defined-member/index.js';
1+
import Transpiler from '../../../src/Transpiler.js';
2+
import { IntermediateASTParser } from '../../../src/ast/core/index.js';
3+
import { BitloopsParser } from '../../../src/parser/index.js';
4+
import { TargetGenerator } from '../../../src/target/index.js';
5+
import { SYMBOL_TABLE_MEMBER_NOT_DEFINED_TEST_CASES } from './mocks/semantic-errors/not-defined-member/index.js';
66

77
describe('Not defined members test cases', () => {
88
const boundedContext = 'Hello world';

transpiler/__tests__/end-to-end/semantics/method-scopes.steps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* For further information you can contact legal(at)bitloops.com.
1919
*/
2020
import Transpiler from '../../../src/Transpiler.js';
21-
import { SEMANTIC_ERRORS_METHOD_SCOPES_END_TO_END_TEST_CASES } from './mocks/semantic-errors/index.js';
21+
import { SEMANTIC_ERRORS_METHOD_SCOPES_END_TO_END_TEST_CASES } from './mocks/access-violation/index.js';
2222
import { BitloopsParser } from '../../../src/parser/index.js';
2323
import { IntermediateASTParser } from '../../../src/ast/core/index.js';
2424
import { TargetGenerator } from '../../../src/target/index.js';

transpiler/__tests__/end-to-end/missing-identifiers.steps.ts renamed to transpiler/__tests__/end-to-end/semantics/missing-identifiers.steps.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import Transpiler from '../../src/Transpiler.js';
2-
import { IntermediateASTParser } from '../../src/ast/core/index.js';
3-
import { BitloopsParser } from '../../src/parser/index.js';
4-
import { TargetGenerator } from '../../src/target/index.js';
5-
import { SYMBOL_TABLE_MISSING_IDENTIFIERS_TEST_CASES } from './mocks/symbol-table/missing-identifiers/index.js';
1+
import Transpiler from '../../../src/Transpiler.js';
2+
import { IntermediateASTParser } from '../../../src/ast/core/index.js';
3+
import { BitloopsParser } from '../../../src/parser/index.js';
4+
import { TargetGenerator } from '../../../src/target/index.js';
5+
import { SYMBOL_TABLE_MISSING_IDENTIFIERS_TEST_CASES } from './mocks/semantic-errors/missing-identifiers/index.js';
66

77
describe('Missing identifiers test cases', () => {
88
const boundedContext = 'Hello world';

transpiler/__tests__/end-to-end/semantics/mocks/semantic-errors/index.ts renamed to transpiler/__tests__/end-to-end/semantics/mocks/access-violation/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import { FileUtil } from '../../../../../src/utils/file.js';
2121
export const SEMANTIC_ERRORS_METHOD_SCOPES_END_TO_END_TEST_CASES = [
2222
{
23-
description: 'Entity not found',
23+
description: 'Private method working correctly',
2424
input: FileUtil.readFileString(
25-
'transpiler/__tests__/end-to-end/semantics/mocks/semantic-errors/input.bl',
25+
'transpiler/__tests__/end-to-end/semantics/mocks/access-violation/input.bl',
2626
),
2727
},
2828
];

transpiler/__tests__/end-to-end/mocks/symbol-table/already-declared/index.ts renamed to transpiler/__tests__/end-to-end/semantics/mocks/semantic-errors/already-declared/index.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import { FileUtil } from '../../../../../src/utils/file.js';
2-
import { SymbolTableErrorTestCase } from '../symbol-table.js';
1+
import { FileUtil } from '../../../../../../src/utils/file.js';
2+
import { SymbolTableErrorTestCase } from '../../symbol-table/symbol-table.js';
33

44
export const SYMBOL_TABLE_ALREADY_DECLARED_TEST_CASES: SymbolTableErrorTestCase[] = [
55
{
66
description: 'Should return error that const declaration is already defined',
77
inputCore: FileUtil.readFileString(
8-
'transpiler/__tests__/end-to-end/mocks/symbol-table/already-declared/already-declared-const.bl',
8+
'transpiler/__tests__/end-to-end/semantics/mocks/semantic-errors/already-declared/already-declared-const.bl',
99
),
1010
inputSetup: FileUtil.readFileString(
11-
'transpiler/__tests__/end-to-end/mocks/semantic-errors/setup.bl',
11+
'transpiler/__tests__/end-to-end/semantics/mocks/semantic-errors/setup.bl',
1212
),
1313
errorMessages: ['Identifier accountId is already defined.'],
1414
},
1515
{
1616
description: 'Should return error that variable declaration is already defined',
1717
inputCore: FileUtil.readFileString(
18-
'transpiler/__tests__/end-to-end/mocks/symbol-table/already-declared/already-declared-variable.bl',
18+
'transpiler/__tests__/end-to-end/semantics/mocks/semantic-errors/already-declared/already-declared-variable.bl',
1919
),
2020
inputSetup: FileUtil.readFileString(
21-
'transpiler/__tests__/end-to-end/mocks/semantic-errors/setup.bl',
21+
'transpiler/__tests__/end-to-end/semantics/mocks/semantic-errors/setup.bl',
2222
),
2323
errorMessages: ['Identifier accountId is already defined.'],
2424
},

transpiler/__tests__/end-to-end/mocks/symbol-table/constant-reassignment/index.ts renamed to transpiler/__tests__/end-to-end/semantics/mocks/semantic-errors/constant-reassignment/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { FileUtil } from '../../../../../src/utils/file.js';
2-
import { SymbolTableErrorTestCase } from '../symbol-table.js';
1+
import { FileUtil } from '../../../../../../src/utils/file.js';
2+
import { SymbolTableErrorTestCase } from '../../symbol-table/symbol-table.js';
33

44
export const SYMBOL_TABLE_CONSTANT_REASSIGNMENT_TEST_CASES: SymbolTableErrorTestCase[] = [
55
{
66
description: 'Should return error that const declaration cannot be reassigned',
77
inputCore: FileUtil.readFileString(
8-
'transpiler/__tests__/end-to-end/mocks/symbol-table/constant-reassignment/const-reassignment.bl',
8+
'transpiler/__tests__/end-to-end/semantics/mocks/semantic-errors/constant-reassignment/const-reassignment.bl',
99
),
1010
inputSetup: FileUtil.readFileString(
11-
'transpiler/__tests__/end-to-end/mocks/semantic-errors/setup.bl',
11+
'transpiler/__tests__/end-to-end/semantics/mocks/semantic-errors/setup.bl',
1212
),
1313
errorMessages: ['Cannot reassign constant variable accountId.'],
1414
},

0 commit comments

Comments
 (0)