Search in sources :

Example 56 with BInteger

use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.

the class GlobalVarFunctionTest method testChangeGlobalVarWithinFunction.

@Test(description = "Test change global var within functions")
public void testChangeGlobalVarWithinFunction() {
    BValue[] args = { new BInteger(88) };
    BValue[] returns = BRunUtil.invoke(result, "changeGlobalVar", args);
    Assert.assertEquals(returns.length, 1);
    Assert.assertSame(returns[0].getClass(), BFloat.class);
    Assert.assertEquals(((BFloat) returns[0]).floatValue(), 165.0);
    CompileResult resultGlobalVar = BCompileUtil.compile("test-src/statements/variabledef/global-var-function.bal");
    BValue[] returnsChanged = BRunUtil.invoke(resultGlobalVar, "getGlobalFloatVar");
    Assert.assertEquals(returnsChanged.length, 1);
    Assert.assertSame(returnsChanged[0].getClass(), BFloat.class);
    Assert.assertEquals(((BFloat) returnsChanged[0]).floatValue(), 80.0);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) CompileResult(org.ballerinalang.launcher.util.CompileResult) Test(org.testng.annotations.Test)

Example 57 with BInteger

use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.

the class StructAccessWithIndexTest method testBasicStruct.

@Test(description = "Test Basic struct operations")
public void testBasicStruct() {
    BValue[] returns = BRunUtil.invoke(compileResult, "testCreateStruct");
    Assert.assertTrue(returns[0] instanceof BString);
    Assert.assertEquals(returns[0].stringValue(), "Jack");
    Assert.assertTrue(returns[1] instanceof BMap);
    BMap<String, ?> adrsMap = ((BMap) returns[1]);
    Assert.assertEquals(adrsMap.get("country"), new BString("USA"));
    Assert.assertEquals(adrsMap.get("state"), new BString("CA"));
    Assert.assertTrue(returns[2] instanceof BInteger);
    Assert.assertEquals(((BInteger) returns[2]).intValue(), 25);
}
Also used : BMap(org.ballerinalang.model.values.BMap) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 58 with BInteger

use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.

the class WhileStmtTest method testWhileStmtConditionTrue.

@Test(description = "Test while loop with a condition which evaluates to true")
public void testWhileStmtConditionTrue() {
    BValue[] args = { new BInteger(10), new BInteger(1) };
    BValue[] returns = BRunUtil.invoke(positiveCompileResult, "testWhileStmt", args);
    Assert.assertEquals(returns.length, 1);
    Assert.assertSame(returns[0].getClass(), BInteger.class);
    long actual = ((BInteger) returns[0]).intValue();
    long expected = 100;
    Assert.assertEquals(actual, expected);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) Test(org.testng.annotations.Test)

Example 59 with BInteger

use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.

the class WhileStmtTest method testWhileBlockScopes.

@Test(description = "Check the scope managing in while block")
public void testWhileBlockScopes() {
    BValue[] args = { new BInteger(1) };
    BValue[] returns = BRunUtil.invoke(positiveCompileResult, "testWhileScope", args);
    Assert.assertEquals(returns.length, 1);
    Assert.assertSame(returns[0].getClass(), BInteger.class, "Class type mismatched");
    BInteger actual = (BInteger) returns[0];
    Assert.assertEquals(actual.intValue(), 200, "mismatched output value");
    args = new BValue[] { new BInteger(2) };
    returns = BRunUtil.invoke(positiveCompileResult, "testWhileScope", args);
    Assert.assertEquals(returns.length, 1);
    Assert.assertSame(returns[0].getClass(), BInteger.class, "Class type mismatched");
    actual = (BInteger) returns[0];
    Assert.assertEquals(actual.intValue(), 400, "mismatched output value");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) Test(org.testng.annotations.Test)

Example 60 with BInteger

use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.

the class MessageUtils method generateProtoMessage.

/**
 * Returns protobuf message corresponding to the B7a message.
 *
 * @param responseValue B7a message.
 * @param outputType protobuf message type.
 * @return generated protobuf message.
 */
public static Message generateProtoMessage(BValue responseValue, Descriptors.Descriptor outputType) {
    Message.Builder responseBuilder = Message.newBuilder(outputType.getName());
    int stringIndex = 0;
    int intIndex = 0;
    int floatIndex = 0;
    int boolIndex = 0;
    int refIndex = 0;
    for (Descriptors.FieldDescriptor fieldDescriptor : outputType.getFields()) {
        String fieldName = fieldDescriptor.getName();
        switch(fieldDescriptor.getType().toProto().getNumber()) {
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_DOUBLE_VALUE:
                {
                    double value = 0F;
                    if (responseValue instanceof BStruct) {
                        value = ((BStruct) responseValue).getFloatField(floatIndex++);
                    } else {
                        if (responseValue instanceof BFloat) {
                            value = ((BFloat) responseValue).value();
                        }
                    }
                    responseBuilder.addField(fieldName, value);
                    break;
                }
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FLOAT_VALUE:
                {
                    float value = 0F;
                    if (responseValue instanceof BStruct) {
                        value = Float.parseFloat(String.valueOf(((BStruct) responseValue).getFloatField(floatIndex++)));
                    } else {
                        if (responseValue instanceof BFloat) {
                            value = Float.parseFloat(String.valueOf(((BFloat) responseValue).value()));
                        }
                    }
                    responseBuilder.addField(fieldName, value);
                    break;
                }
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT64_VALUE:
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_UINT64_VALUE:
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED64_VALUE:
                {
                    long value = 0;
                    if (responseValue instanceof BStruct) {
                        value = ((BStruct) responseValue).getIntField(intIndex++);
                    } else {
                        if (responseValue instanceof BInteger) {
                            value = ((BInteger) responseValue).value();
                        }
                    }
                    responseBuilder.addField(fieldName, value);
                    break;
                }
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32_VALUE:
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED32_VALUE:
                {
                    int value = 0;
                    if (responseValue instanceof BStruct) {
                        value = Integer.parseInt(String.valueOf(((BStruct) responseValue).getIntField(intIndex++)));
                    } else {
                        if (responseValue instanceof BInteger) {
                            value = Integer.parseInt(String.valueOf(((BInteger) responseValue).value()));
                        }
                    }
                    responseBuilder.addField(fieldName, value);
                    break;
                }
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_BOOL_VALUE:
                {
                    boolean value = false;
                    if (responseValue instanceof BStruct) {
                        value = ((BStruct) responseValue).getBooleanField(boolIndex++) > 0;
                    } else {
                        if (responseValue instanceof BBoolean) {
                            value = ((BBoolean) responseValue).value();
                        }
                    }
                    responseBuilder.addField(fieldName, value);
                    break;
                }
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_STRING_VALUE:
                {
                    String value = null;
                    if (responseValue instanceof BStruct) {
                        value = ((BStruct) responseValue).getStringField(stringIndex++);
                    } else {
                        if (responseValue instanceof BString) {
                            value = ((BString) responseValue).value();
                        }
                    }
                    responseBuilder.addField(fieldName, value);
                    break;
                }
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE_VALUE:
                {
                    if (responseValue instanceof BStruct) {
                        BValue bValue = ((BStruct) responseValue).getRefField(refIndex++);
                        responseBuilder.addField(fieldName, generateProtoMessage(bValue, fieldDescriptor.getMessageType()));
                    }
                    break;
                }
            default:
                {
                    throw new UnsupportedFieldTypeException("Error while decoding request message. Field " + "type is not supported : " + fieldDescriptor.getType());
                }
        }
    }
    return responseBuilder.build();
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BString(org.ballerinalang.model.values.BString) BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) BBoolean(org.ballerinalang.model.values.BBoolean) UnsupportedFieldTypeException(org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException) BString(org.ballerinalang.model.values.BString) BFloat(org.ballerinalang.model.values.BFloat) Descriptors(com.google.protobuf.Descriptors)

Aggregations

BInteger (org.ballerinalang.model.values.BInteger)364 BValue (org.ballerinalang.model.values.BValue)324 Test (org.testng.annotations.Test)305 BString (org.ballerinalang.model.values.BString)91 BFloat (org.ballerinalang.model.values.BFloat)55 BStruct (org.ballerinalang.model.values.BStruct)33 BBoolean (org.ballerinalang.model.values.BBoolean)24 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)18 CompileResult (org.ballerinalang.launcher.util.CompileResult)12 BBlob (org.ballerinalang.model.values.BBlob)11 BeforeTest (org.testng.annotations.BeforeTest)11 BIntArray (org.ballerinalang.model.values.BIntArray)9 BMap (org.ballerinalang.model.values.BMap)9 BRefType (org.ballerinalang.model.values.BRefType)8 BStringArray (org.ballerinalang.model.values.BStringArray)8 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)8 BType (org.ballerinalang.model.types.BType)6 BStructType (org.ballerinalang.model.types.BStructType)4 BJSON (org.ballerinalang.model.values.BJSON)4 UnsupportedFieldTypeException (org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException)4