Skip to content

Commit 5d901d7

Browse files
feat: parser unit tests
1 parent f66f169 commit 5d901d7

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

src/utils/apex/parser/apexparser.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/* eslint-disable @typescript-eslint/no-unsafe-call */
33
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
44
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
5-
import * as fs from 'fs';
65
import {
76
ApexLexer,
87
CommonTokenStream,
@@ -18,7 +17,7 @@ import { CharStreams, Token } from 'antlr4ts';
1817
import { ParseTreeWalker } from 'antlr4ts/tree/ParseTreeWalker';
1918

2019
export class ApexASTParser {
21-
private apexFilePath: string;
20+
private apexFileContent: string;
2221
private implementsInterface: Map<string, Token> = new Map();
2322
// private callsMethods: Map<string, Token[]>;
2423
private interfaceName: string;
@@ -30,16 +29,15 @@ export class ApexASTParser {
3029
return this.implementsInterface;
3130
}
3231

33-
public constructor(apexFilePath: string, interfaceName: string, methodName: string) {
34-
this.apexFilePath = apexFilePath;
32+
public constructor(apexFileContent: string, interfaceName: string, methodName: string) {
33+
this.apexFileContent = apexFileContent;
3534
this.interfaceName = interfaceName;
3635
this.methodName = methodName;
3736
this.astListener = this.createASTListener();
3837
}
3938

4039
public parse(): CompilationUnitContext {
41-
const fileContent = fs.readFileSync(this.apexFilePath).toString();
42-
const lexer = new ApexLexer(new CaseInsensitiveInputStream(CharStreams.fromString(fileContent)));
40+
const lexer = new ApexLexer(new CaseInsensitiveInputStream(CharStreams.fromString(this.apexFileContent)));
4341
const tokens = new CommonTokenStream(lexer);
4442
const parser = new ApexParser(tokens);
4543
const context = parser.compilationUnit();
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
The service implements Callable to support calling
3+
FormulaParser functions from Core/Java.
4+
The allowed functions are listed in FormulaParser FUNCTIONS_FROM_JAVA.
5+
*/
6+
global with sharing class FormulaParserService implements Callable
7+
{
8+
/* global Object call(String action, Map<String, Object> args)
9+
{
10+
Map<String, Object> input = (Map<String, Object>) args.get('input');
11+
Map<String, Object> output = (Map<String, Object>) args.get('output');
12+
13+
String function = (String)input.get('function');
14+
if (function == null || String.isBlank(function))
15+
{
16+
return null;
17+
}
18+
19+
List<Object> arguments = (input.get('arguments') != null && input.get('arguments') InstanceOf List<Object>)
20+
? (List<Object>)input.get('arguments') : new List<Object>();
21+
22+
if (action == 'evaluateFunctionFromJava')
23+
{
24+
Object res = FormulaParser.evaluateFunctionFromJava(function, arguments);
25+
if (res != null)
26+
{
27+
output.put('result', res);
28+
}
29+
}
30+
return null;
31+
}
32+
33+
global void evaluateFunctionFromJava(String function, List<Object> arguments)
34+
{
35+
Map < String, Object > testMap = new Map < String, Object > { 'BirthDate' => '1982-11-06' };
36+
DRProcessResult result = DRGlobal.process(testMap, 'DRTurboTemp2');
37+
//List < Object > resultMap = (List < Object >)((Map < String, Object >)result.toJson()).get('Contact');
38+
//System.assertEquals(Date.newInstance(1982, 11, 6), ((Map < String, Object >)resultMap[0]).get('Birthdate'));
39+
}*/
40+
41+
global void justForTest(String kkdbk) {
42+
/* Specify Data Mapper extract or transform to call */
43+
String DRName = 'DataMapperNewName';
44+
/* Populate the input JSON */
45+
Map<String, Object> myTransformData = new Map<String, Object>{'MyKey'=>'MyValue'};
46+
/* Call the Data Mapper */
47+
omnistudio.DRProcessResult result1 = omnistudio.DRGlobal.process(myTransformData, DRName);
48+
}
49+
}

test/utils/apex/parser/apexparser.test.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@ import { ApexASTParser } from '../../../../src/utils/apex/parser/apexparser';
33

44
describe('ApexASTParser', () => {
55
it('should parse the Apex file and collect interface implementations, method calls, and class names', () => {
6-
const apexFilePath = 'test/utils/apex/parser/resource/FormulaParserService.cls';
6+
const apexFileContent = `global with sharing class FormulaParserService implements Callable{
7+
8+
global void justForTest(String kkdbk) {
9+
/* Specify Data Mapper extract or transform to call */
10+
String DRName = 'DataMapperNewName';
11+
/* Populate the input JSON */
12+
Map<String, Object> myTransformData = new Map<String, Object>{'MyKey'=>'MyValue'};
13+
/* Call the Data Mapper */
14+
omnistudio.DRProcessResult result1 = omnistudio.DRGlobal.process(myTransformData, DRName);
15+
}
16+
}`;
717
const interfaceName = 'Callable';
818
const methodName = 'yourMethod';
919

10-
const apexParser = new ApexASTParser(apexFilePath, interfaceName, methodName);
20+
const apexParser = new ApexASTParser(apexFileContent, interfaceName, methodName);
1121
apexParser.parse();
1222
const implementsInterface = apexParser.implemementsInterface;
1323
// const callsMethods = apexParser.getCallsMethods();
@@ -16,7 +26,7 @@ describe('ApexASTParser', () => {
1626
// Add your assertions here based on the expected results
1727
// implementsInterface.get(interfaceName);
1828
expect(implementsInterface.get(interfaceName).charPositionInLine).to.be.equal(58);
19-
expect(implementsInterface.get(interfaceName).line).to.be.equal(6);
29+
expect(implementsInterface.get(interfaceName).line).to.be.equal(1);
2030
// expect(callsMethods).to.not.be.empty;
2131
// expect(className).to.equal('YourClass');
2232
});

0 commit comments

Comments
 (0)