Search in sources :

Example 31 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo 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 32 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo 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 33 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.

the class StartServices method execute.

/**
 * Starts all the services defined in the package specified in the 'packageName' argument.
 */
@Override
public void execute(Context ctx) {
    String packageName = ctx.getStringArgument(0);
    for (ProgramFile programFile : TesterinaRegistry.getInstance().getProgramFiles()) {
        // Get the service package
        PackageInfo servicesPackage = programFile.getEntryPackage();
        if (servicesPackage == null || !servicesPackage.getPkgPath().equals(packageName)) {
            continue;
        }
        if (!programFile.isServiceEPAvailable()) {
            throw new BallerinaException(String.format("no services found in package: %s", packageName));
        }
        Debugger debugger = new Debugger(programFile);
        initDebugger(programFile, debugger);
        // Invoke package init function
        BLangFunctions.invokePackageInitFunction(servicesPackage.getInitFunctionInfo());
        BLangFunctions.invokeVMUtilFunction(servicesPackage.getStartFunctionInfo());
        ctx.setReturnValues(new BBoolean(true));
        return;
    }
    // package could not be found
    ctx.setReturnValues(new BBoolean(false));
}
Also used : Debugger(org.ballerinalang.util.debugger.Debugger) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) BBoolean(org.ballerinalang.model.values.BBoolean) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Example 34 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.

the class BTestUtils method createAndGetStruct.

public static BStruct createAndGetStruct(ProgramFile programFile, String packagePath, String structName) {
    PackageInfo structPackageInfo = programFile.getPackageInfo(packagePath);
    StructInfo structInfo = structPackageInfo.getStructInfo(structName);
    BStructType structType = structInfo.getType();
    return new BStruct(structType);
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 35 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.

the class Utils method createSpanStruct.

public static BStruct createSpanStruct(Context context, String spanId, String serviceName, String spanName) {
    PackageInfo observePackage = context.getProgramFile().getPackageInfo("ballerina.observe");
    StructInfo spanStructInfo = observePackage.getStructInfo("Span");
    return BLangVMStructs.createBStruct(spanStructInfo, spanId, serviceName, spanName);
}
Also used : StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Aggregations

PackageInfo (org.ballerinalang.util.codegen.PackageInfo)48 StructInfo (org.ballerinalang.util.codegen.StructInfo)35 BStruct (org.ballerinalang.model.values.BStruct)17 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)10 BValue (org.ballerinalang.model.values.BValue)6 BStructType (org.ballerinalang.model.types.BStructType)5 FunctionInfo (org.ballerinalang.util.codegen.FunctionInfo)5 Debugger (org.ballerinalang.util.debugger.Debugger)5 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)5 BInteger (org.ballerinalang.model.values.BInteger)2 BMap (org.ballerinalang.model.values.BMap)2 BString (org.ballerinalang.model.values.BString)2 SocketIOChannel (org.ballerinalang.nativeimpl.io.channels.SocketIOChannel)2 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)2 TesterinaFunction (org.ballerinalang.testerina.core.entity.TesterinaFunction)2 Instruction (org.ballerinalang.util.codegen.Instruction)2 PackageVarInfo (org.ballerinalang.util.codegen.PackageVarInfo)2 BLangRuntimeException (org.ballerinalang.util.exceptions.BLangRuntimeException)2 Test (org.testng.annotations.Test)2 PrintStream (java.io.PrintStream)1