Search in sources :

Example 31 with TypeModel

use of com.google.api.codegen.config.TypeModel in project toolkit by googleapis.

the class OutputTransformerTest method testAccessorNewVariableWithAccessors.

@Test
public void testAccessorNewVariableWithAccessors() {
    Scanner scanner = new Scanner("old_var.property");
    when(namer.localVarName(Name.from("old_var"))).thenReturn("oldVar");
    TypeModel oldVarTypeModel = mock(TypeModel.class);
    assertThat(parent.put("old_var", oldVarTypeModel, "OldVarType")).isTrue();
    when(oldVarTypeModel.isMessage()).thenReturn(true);
    when(oldVarTypeModel.isRepeated()).thenReturn(false);
    when(oldVarTypeModel.isMap()).thenReturn(false);
    FieldModel propertyFieldModel = mock(FieldModel.class);
    when(oldVarTypeModel.getField("property")).thenReturn(propertyFieldModel);
    TypeModel propertyTypeModel = mock(TypeModel.class);
    when(namer.getFieldGetFunctionName(propertyFieldModel)).thenReturn("getProperty");
    when(typeTable.getNicknameFor(propertyTypeModel)).thenReturn("PropertyTypeName");
    when(namer.getFieldAccessorName(propertyFieldModel)).thenReturn(".getProperty()");
    when(propertyFieldModel.getType()).thenReturn(propertyTypeModel);
    OutputView.VariableView variableView = accessorNewVariable(scanner, context, sampleContext, parent, "newVar", false);
    assertThat(variableView.variable()).isEqualTo("oldVar");
    assertThat(variableView.accessors()).containsExactly(".getProperty()").inOrder();
    assertThat(parent.getTypeName("newVar")).isEqualTo("PropertyTypeName");
    assertThat(parent.getTypeModel("newVar")).isEqualTo(propertyTypeModel);
}
Also used : Scanner(com.google.api.codegen.util.Scanner) OutputView(com.google.api.codegen.viewmodel.OutputView) TypeModel(com.google.api.codegen.config.TypeModel) FieldModel(com.google.api.codegen.config.FieldModel) Test(org.junit.Test)

Example 32 with TypeModel

use of com.google.api.codegen.config.TypeModel 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

TypeModel (com.google.api.codegen.config.TypeModel)32 MethodModel (com.google.api.codegen.config.MethodModel)12 Test (org.junit.Test)12 Scanner (com.google.api.codegen.util.Scanner)10 StaticLangApiMethodView (com.google.api.codegen.viewmodel.StaticLangApiMethodView)10 Builder (com.google.api.codegen.viewmodel.StaticLangApiMethodView.Builder)10 OutputView (com.google.api.codegen.viewmodel.OutputView)8 FieldModel (com.google.api.codegen.config.FieldModel)7 ImmutableList (com.google.common.collect.ImmutableList)4 LongRunningConfig (com.google.api.codegen.config.LongRunningConfig)3 ArrayList (java.util.ArrayList)3 GapicMethodContext (com.google.api.codegen.config.GapicMethodContext)2 ImportTypeTable (com.google.api.codegen.transformer.ImportTypeTable)2 OutputTransformer (com.google.api.codegen.transformer.OutputTransformer)2 LongRunningOperationDetailView (com.google.api.codegen.viewmodel.LongRunningOperationDetailView)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 HashSet (java.util.HashSet)2 OutputSpec (com.google.api.codegen.OutputSpec)1 SampleValueSet (com.google.api.codegen.SampleValueSet)1 FieldConfig (com.google.api.codegen.config.FieldConfig)1