use of org.ballerinalang.testerina.core.entity.TesterinaFunction in project ballerina by ballerina-lang.
the class TestAnnotationProcessor method injectMocks.
/**
* Process a given {@link TestSuite} and inject the user defined mock functions.
*
* @param suite a @{@link TestSuite}
*/
public static void injectMocks(TestSuite suite) {
ProgramFile programFile = suite.getProgramFile();
Map<String, TesterinaFunction> mockFunctions = suite.getMockFunctionsMap();
mockFunctions.forEach((k, v) -> {
String[] info = k.split(MOCK_ANNOTATION_DELIMITER);
if (info.length != 2) {
return;
}
for (PackageInfo packageInfo : programFile.getPackageInfoEntries()) {
for (Instruction ins : packageInfo.getInstructions()) {
if (ins instanceof Instruction.InstructionCALL) {
// replace the function pointer of the instruction with the mock function pointer
Instruction.InstructionCALL call = (Instruction.InstructionCALL) ins;
if (call.functionInfo.getPkgPath().equals(info[0]) && call.functionInfo.getName().equals(info[1])) {
suite.addMockedRealFunction(k, call.functionInfo);
call.functionInfo = v.getbFunction();
}
}
}
}
});
}
use of org.ballerinalang.testerina.core.entity.TesterinaFunction in project ballerina by ballerina-lang.
the class TestAnnotationProcessor method resetMocks.
/**
* Process a given {@link TestSuite} and reset the mock functions with their original pointers.
*
* @param suite a @{@link TestSuite}
*/
public static void resetMocks(TestSuite suite) {
ProgramFile programFile = suite.getProgramFile();
Map<String, TesterinaFunction> mockFunctions = suite.getMockFunctionsMap();
Map<String, FunctionInfo> mockedRealFunctionsMap = suite.getMockedRealFunctionsMap();
mockFunctions.forEach((k, v) -> {
String[] info = k.split(MOCK_ANNOTATION_DELIMITER);
if (info.length != 2) {
return;
}
for (PackageInfo packageInfo : programFile.getPackageInfoEntries()) {
for (Instruction ins : packageInfo.getInstructions()) {
if (ins instanceof Instruction.InstructionCALL) {
Instruction.InstructionCALL call = (Instruction.InstructionCALL) ins;
if (call.functionInfo.getPkgPath().equals(info[0]) && call.functionInfo.getName().equals(info[1])) {
call.functionInfo = mockedRealFunctionsMap.get(k);
}
}
}
}
});
}
use of org.ballerinalang.testerina.core.entity.TesterinaFunction in project ballerina by ballerina-lang.
the class TestAnnotationProcessor method packageProcessed.
/**
* TODO this is a temporary solution, till we get a proper API from Ballerina Core.
* This method will get executed at the completion of the processing of a ballerina package.
*
* @param programFile {@link ProgramFile} corresponds to the current ballerina package
*/
public void packageProcessed(ProgramFile programFile) {
packageInit = false;
// TODO the below line is required since this method is currently getting explicitly called from BTestRunner
suite = TesterinaRegistry.getInstance().getTestSuites().get(programFile.getEntryPkgName());
suite.setInitFunction(new TesterinaFunction(programFile, programFile.getEntryPackage().getInitFunctionInfo(), TesterinaFunction.Type.INIT));
// add all functions of the package as utility functions
Arrays.stream(programFile.getEntryPackage().getFunctionInfoEntries()).forEach(functionInfo -> {
suite.addTestUtilityFunction(new TesterinaFunction(programFile, functionInfo, TesterinaFunction.Type.UTIL));
});
int[] testExecutionOrder = checkCyclicDependencies(suite.getTests());
resolveFunctions(suite);
List<Test> sortedTests = orderTests(suite.getTests(), testExecutionOrder);
suite.setTests(sortedTests);
suite.setProgramFile(programFile);
}
use of org.ballerinalang.testerina.core.entity.TesterinaFunction in project ballerina by ballerina-lang.
the class TestAnnotationProcessor method resolveFunctions.
/**
* Resolve function names to {@link TesterinaFunction}s.
*
* @param suite {@link TestSuite} whose functions to be resolved.
*/
private static void resolveFunctions(TestSuite suite) {
List<TesterinaFunction> functions = suite.getTestUtilityFunctions();
List<String> functionNames = functions.stream().map(testerinaFunction -> testerinaFunction.getName()).collect(Collectors.toList());
for (Test test : suite.getTests()) {
if (test.getTestName() != null && functionNames.contains(test.getTestName())) {
test.setTestFunction(functions.stream().filter(e -> e.getName().equals(test.getTestName())).findFirst().get());
}
if (test.getBeforeTestFunction() != null && functionNames.contains(test.getBeforeTestFunction())) {
test.setBeforeTestFunctionObj(functions.stream().filter(e -> e.getName().equals(test.getBeforeTestFunction())).findFirst().get());
}
if (test.getAfterTestFunction() != null && functionNames.contains(test.getAfterTestFunction())) {
test.setAfterTestFunctionObj(functions.stream().filter(e -> e.getName().equals(test.getAfterTestFunction())).findFirst().get());
}
if (test.getDataProvider() != null && functionNames.contains(test.getDataProvider())) {
String dataProvider = test.getDataProvider();
test.setDataProviderFunction(functions.stream().filter(e -> e.getName().equals(test.getDataProvider())).findFirst().map(func -> {
// TODO these validations are not working properly with the latest refactoring
if (func.getbFunction().getRetParamTypes().length == 1) {
BType bType = func.getbFunction().getRetParamTypes()[0];
if (bType.getTag() == TypeTags.ARRAY_TAG) {
BArrayType bArrayType = (BArrayType) bType;
if (bArrayType.getElementType().getTag() != TypeTags.ARRAY_TAG) {
String message = String.format("Data provider function [%s] should return an array of" + " arrays.", dataProvider);
throw new BallerinaException(message);
}
} else {
String message = String.format("Data provider function [%s] should return an array of " + "arrays.", dataProvider);
throw new BallerinaException(message);
}
} else {
String message = String.format("Data provider function [%s] should have only one return type" + ".", dataProvider);
throw new BallerinaException(message);
}
return func;
}).get());
if (test.getDataProviderFunction() == null) {
String message = String.format("Data provider function [%s] cannot be found.", dataProvider);
throw new BallerinaException(message);
}
}
for (String dependsOnFn : test.getDependsOnTestFunctions()) {
// TODO handle missing func case
test.addDependsOnTestFunction(functions.stream().filter(e -> e.getName().equals(dependsOnFn)).findFirst().get());
}
}
// resolve mock functions
suite.getMockFunctionNamesMap().forEach((id, functionName) -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addMockFunctionObj(id, function);
});
suite.getBeforeSuiteFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addBeforeSuiteFunctionObj(function);
});
suite.getAfterSuiteFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addAfterSuiteFunctionObj(function);
});
suite.getBeforeEachFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addBeforeEachFunctionObj(function);
});
suite.getAfterEachFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addAfterEachFunctionObj(function);
});
}
Aggregations