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();
}
}
}
}
});
}
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);
}
}
}
}
});
}
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));
}
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);
}
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);
}
Aggregations