Search in sources :

Example 1 with TesterinaFunction

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();
                    }
                }
            }
        }
    });
}
Also used : PackageInfo(org.ballerinalang.util.codegen.PackageInfo) Instruction(org.ballerinalang.util.codegen.Instruction) ProgramFile(org.ballerinalang.util.codegen.ProgramFile) TesterinaFunction(org.ballerinalang.testerina.core.entity.TesterinaFunction)

Example 2 with TesterinaFunction

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);
                    }
                }
            }
        }
    });
}
Also used : PackageInfo(org.ballerinalang.util.codegen.PackageInfo) FunctionInfo(org.ballerinalang.util.codegen.FunctionInfo) Instruction(org.ballerinalang.util.codegen.Instruction) ProgramFile(org.ballerinalang.util.codegen.ProgramFile) TesterinaFunction(org.ballerinalang.testerina.core.entity.TesterinaFunction)

Example 3 with TesterinaFunction

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);
}
Also used : Test(org.ballerinalang.testerina.core.entity.Test) TesterinaFunction(org.ballerinalang.testerina.core.entity.TesterinaFunction)

Example 4 with TesterinaFunction

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);
    });
}
Also used : Arrays(java.util.Arrays) PackageNode(org.ballerinalang.model.tree.PackageNode) BType(org.ballerinalang.model.types.BType) ProgramFile(org.ballerinalang.util.codegen.ProgramFile) TestSuite(org.ballerinalang.testerina.core.entity.TestSuite) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) DiagnosticLog(org.ballerinalang.util.diagnostic.DiagnosticLog) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) ArrayList(java.util.ArrayList) SupportedAnnotationPackages(org.ballerinalang.compiler.plugins.SupportedAnnotationPackages) AbstractCompilerPlugin(org.ballerinalang.compiler.plugins.AbstractCompilerPlugin) Vector(java.util.Vector) Map(java.util.Map) TesterinaFunction(org.ballerinalang.testerina.core.entity.TesterinaFunction) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) LinkedList(java.util.LinkedList) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) FunctionNode(org.ballerinalang.model.tree.FunctionNode) TypeTags(org.ballerinalang.model.types.TypeTags) Collectors(java.util.stream.Collectors) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) BArrayType(org.ballerinalang.model.types.BArrayType) List(java.util.List) Instruction(org.ballerinalang.util.codegen.Instruction) Test(org.ballerinalang.testerina.core.entity.Test) Queue(java.util.Queue) FunctionInfo(org.ballerinalang.util.codegen.FunctionInfo) BArrayType(org.ballerinalang.model.types.BArrayType) Test(org.ballerinalang.testerina.core.entity.Test) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) TesterinaFunction(org.ballerinalang.testerina.core.entity.TesterinaFunction)

Aggregations

TesterinaFunction (org.ballerinalang.testerina.core.entity.TesterinaFunction)4 Instruction (org.ballerinalang.util.codegen.Instruction)3 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)3 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)3 Test (org.ballerinalang.testerina.core.entity.Test)2 FunctionInfo (org.ballerinalang.util.codegen.FunctionInfo)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Queue (java.util.Queue)1 Vector (java.util.Vector)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Collectors (java.util.stream.Collectors)1 AbstractCompilerPlugin (org.ballerinalang.compiler.plugins.AbstractCompilerPlugin)1 SupportedAnnotationPackages (org.ballerinalang.compiler.plugins.SupportedAnnotationPackages)1 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)1 FunctionNode (org.ballerinalang.model.tree.FunctionNode)1 PackageNode (org.ballerinalang.model.tree.PackageNode)1