Search in sources :

Example 6 with TypeModel

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

the class OutputTransformerTest method testScopeTableGetFromParent.

@Test
public void testScopeTableGetFromParent() {
    TypeModel stringTypeModel = ProtoTypeRef.create(TypeRef.fromPrimitiveName("string"));
    assertThat(parent.put("str", stringTypeModel, "String")).isTrue();
    assertThat(parent.put("book", null, "ShelfBookName")).isTrue();
    assertThat(child.getTypeModel("str")).isEqualTo(stringTypeModel);
    assertThat(child.getTypeName("str")).isEqualTo("String");
    assertThat(child.getTypeModel("book")).isEqualTo(null);
    assertThat(child.getTypeName("book")).isEqualTo("ShelfBookName");
}
Also used : TypeModel(com.google.api.codegen.config.TypeModel) Test(org.junit.Test)

Example 7 with TypeModel

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

the class OutputTransformerTest method testAccessorNewVariableMapKeyInvalidBooleanFail.

@Test
public void testAccessorNewVariableMapKeyInvalidBooleanFail() {
    Scanner scanner = new Scanner("old_var.property{not_boolean}");
    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(propertyFieldModel.getType()).thenReturn(propertyTypeModel);
    when(propertyTypeModel.isRepeated()).thenReturn(true);
    when(propertyTypeModel.isMap()).thenReturn(true);
    when(namer.getFieldGetFunctionName(propertyFieldModel)).thenReturn("getProperty");
    when(namer.getFieldAccessorName(propertyFieldModel)).thenReturn(".getProperty()");
    TypeModel boolTypeModel = ProtoTypeRef.create(TypeRef.fromPrimitiveName("bool"));
    TypeModel stringTypeModel = ProtoTypeRef.create(TypeRef.fromPrimitiveName("string"));
    when(propertyTypeModel.getMapKeyType()).thenReturn(boolTypeModel);
    when(propertyTypeModel.getMapValueType()).thenReturn(stringTypeModel);
    try {
        accessorNewVariable(scanner, context, sampleContext, parent, "newVar", false);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage()).contains("Could not assign value 'not_boolean' to type bool");
    }
}
Also used : Scanner(com.google.api.codegen.util.Scanner) TypeModel(com.google.api.codegen.config.TypeModel) FieldModel(com.google.api.codegen.config.FieldModel) Test(org.junit.Test)

Example 8 with TypeModel

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

the class OutputTransformerTest method testAccessorNewVariableFromScopeTable.

@Test
public void testAccessorNewVariableFromScopeTable() {
    TypeModel oldVarTypeModel = mock(TypeModel.class);
    assertThat(parent.put("old_var", oldVarTypeModel, "OldVarTypeName")).isTrue();
    Scanner scanner = new Scanner("old_var");
    when(namer.localVarName(Name.from("old_var"))).thenReturn("oldVar");
    when(typeTable.getNicknameFor(oldVarTypeModel)).thenReturn("OldVarTypeName");
    OutputView.VariableView variableView = accessorNewVariable(scanner, context, sampleContext, parent, "newVar", false);
    assertThat(variableView.variable()).isEqualTo("oldVar");
    assertThat(variableView.accessors()).isEmpty();
    assertThat(parent.getTypeName("newVar")).isEqualTo("OldVarTypeName");
    assertThat(parent.getTypeModel("newVar")).isEqualTo(oldVarTypeModel);
}
Also used : Scanner(com.google.api.codegen.util.Scanner) OutputView(com.google.api.codegen.viewmodel.OutputView) TypeModel(com.google.api.codegen.config.TypeModel) Test(org.junit.Test)

Example 9 with TypeModel

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

the class OutputTransformerTest method testScopeTablePutFail.

@Test
public void testScopeTablePutFail() {
    TypeModel stringTypeModel = ProtoTypeRef.create(TypeRef.fromPrimitiveName("string"));
    assertThat(parent.put("str", stringTypeModel, "String")).isTrue();
    assertThat(parent.put("str", stringTypeModel, "String")).isFalse();
}
Also used : TypeModel(com.google.api.codegen.config.TypeModel) Test(org.junit.Test)

Example 10 with TypeModel

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

the class OutputTransformerTest method testAccessorNewVariableWithIndex.

@Test
public void testAccessorNewVariableWithIndex() {
    Scanner scanner = new Scanner("old_var.property[0]");
    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(propertyTypeModel.isRepeated()).thenReturn(true);
    TypeModel optionalPropertyTypeModel = mock(TypeModel.class);
    when(propertyTypeModel.makeOptional()).thenReturn(optionalPropertyTypeModel);
    when(typeTable.getNicknameFor(optionalPropertyTypeModel)).thenReturn("OptionalPropertyTypeName");
    when(namer.getFieldAccessorName(propertyFieldModel)).thenReturn(".getProperty()");
    when(namer.getIndexAccessorName(0)).thenReturn("[0]");
    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()", "[0]").inOrder();
    assertThat(parent.getTypeName("newVar")).isEqualTo("OptionalPropertyTypeName");
    assertThat(parent.getTypeModel("newVar")).isEqualTo(optionalPropertyTypeModel);
}
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)

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