Search in sources :

Example 1 with ClientTestAssertView

use of com.google.api.codegen.viewmodel.testing.ClientTestAssertView in project toolkit by googleapis.

the class InitCodeTransformer method generateRequestAssertViews.

/**
 * Generates assert views for the test of the tested method and its fields.
 */
List<ClientTestAssertView> generateRequestAssertViews(MethodContext methodContext, InitCodeContext initContext) {
    InitCodeNode rootNode = InitCodeNode.createTree(initContext.cloneWithEmptySymbolTable());
    List<ClientTestAssertView> assertViews = new ArrayList<>();
    SurfaceNamer namer = methodContext.getNamer();
    // Add request fields checking
    for (InitCodeNode fieldItemTree : rootNode.getChildren().values()) {
        FieldConfig fieldConfig = fieldItemTree.getFieldConfig();
        String getterMethod = namer.getFieldGetFunctionName(methodContext.getFeatureConfig(), fieldConfig);
        String expectedValueIdentifier = getVariableName(methodContext, fieldItemTree);
        String expectedTransformFunction = null;
        String actualTransformFunction = null;
        if (methodContext.getFeatureConfig().useResourceNameFormatOption(fieldConfig)) {
            if (fieldConfig.requiresParamTransformationFromAny()) {
                expectedTransformFunction = namer.getToStringMethod();
                actualTransformFunction = namer.getToStringMethod();
            } else if (fieldConfig.requiresParamTransformation()) {
                if (methodContext.getFeatureConfig().useResourceNameConverters(fieldConfig)) {
                    expectedTransformFunction = namer.getToStringMethod();
                } else {
                    expectedTransformFunction = namer.getResourceOneofCreateMethod(methodContext.getTypeTable(), fieldConfig);
                }
            } else if (methodContext.getFeatureConfig().useResourceNameConverters(fieldConfig)) {
                if (fieldConfig.getField().isRepeated()) {
                    actualTransformFunction = namer.getResourceTypeParseListMethodName(methodContext.getTypeTable(), fieldConfig);
                } else if (fieldConfig.getResourceNameConfig().getResourceNameType() == ResourceNameType.ONEOF) {
                    actualTransformFunction = namer.getResourceTypeParentParseMethod(methodContext, fieldConfig);
                } else {
                    actualTransformFunction = namer.getResourceTypeParseMethodName(methodContext.getTypeTable(), fieldConfig);
                }
            }
        }
        boolean isMap = fieldConfig.getField().isMap();
        boolean isArray = fieldConfig.getField().isRepeated() && !isMap;
        boolean isFloatingPointNumber = fieldConfig.getField().getType().isFloatType() || fieldConfig.getField().getType().isDoubleType();
        TypeModel fieldType = fieldItemTree.getType();
        String messageTypeName = null;
        if (fieldType.isMessage()) {
            messageTypeName = methodContext.getTypeTable().getFullNameForMessageType(fieldType);
        }
        assertViews.add(createAssertView(expectedValueIdentifier, expectedTransformFunction, actualTransformFunction, isMap, isArray, isFloatingPointNumber, getterMethod, messageTypeName));
    }
    return assertViews;
}
Also used : ClientTestAssertView(com.google.api.codegen.viewmodel.testing.ClientTestAssertView) InitCodeNode(com.google.api.codegen.metacode.InitCodeNode) FieldConfig(com.google.api.codegen.config.FieldConfig) ArrayList(java.util.ArrayList) TypeModel(com.google.api.codegen.config.TypeModel)

Aggregations

FieldConfig (com.google.api.codegen.config.FieldConfig)1 TypeModel (com.google.api.codegen.config.TypeModel)1 InitCodeNode (com.google.api.codegen.metacode.InitCodeNode)1 ClientTestAssertView (com.google.api.codegen.viewmodel.testing.ClientTestAssertView)1 ArrayList (java.util.ArrayList)1