Search in sources :

Example 6 with Scanner

use of com.google.api.codegen.util.Scanner 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 7 with Scanner

use of com.google.api.codegen.util.Scanner in project toolkit by googleapis.

the class OutputTransformerTest method testAccessorNewVariableResourceNameFromScopeTable.

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

Example 8 with Scanner

use of com.google.api.codegen.util.Scanner 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)

Example 9 with Scanner

use of com.google.api.codegen.util.Scanner in project toolkit by googleapis.

the class OutputTransformerTest method testAccessorNewVariablePageStreamingResponse.

@Test
public void testAccessorNewVariablePageStreamingResponse() {
    Scanner scanner = new Scanner("$resp");
    when(config.getPageStreaming()).thenReturn(pageStreamingConfig);
    when(pageStreamingConfig.getResourcesFieldConfig()).thenReturn(resourceFieldConfig);
    when(featureConfig.useResourceNameFormatOption(resourceFieldConfig)).thenReturn(false);
    FieldModel fieldModel = mock(FieldModel.class);
    when(resourceFieldConfig.getField()).thenReturn(fieldModel);
    TypeModel typeModel = mock(TypeModel.class);
    when(fieldModel.getType()).thenReturn(typeModel);
    when(typeModel.makeOptional()).thenReturn(typeModel);
    when(typeTable.getNicknameFor(typeModel)).thenReturn("TypeName");
    OutputView.VariableView variableView = accessorNewVariable(scanner, context, sampleContext, parent, "newVar", false);
    assertThat(variableView.variable()).isEqualTo("sampleResponseVarName");
    assertThat(variableView.accessors()).isEmpty();
    assertThat(parent.getTypeName("newVar")).isEqualTo("TypeName");
    assertThat(parent.getTypeModel("newVar")).isEqualTo(typeModel);
}
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 10 with Scanner

use of com.google.api.codegen.util.Scanner in project toolkit by googleapis.

the class OutputTransformerTest method testAccessorNewVariableResponse.

@Test
public void testAccessorNewVariableResponse() {
    Scanner scanner = new Scanner("$resp");
    when(config.getPageStreaming()).thenReturn(null);
    TypeModel typeModel = mock(TypeModel.class);
    when(typeTable.getNicknameFor(typeModel)).thenReturn("TypeName");
    when(model.getOutputType()).thenReturn(typeModel);
    OutputView.VariableView variableView = accessorNewVariable(scanner, context, sampleContext, parent, "newVar", false);
    assertThat(variableView.variable()).isEqualTo("sampleResponseVarName");
    assertThat(variableView.accessors()).isEmpty();
    assertThat(parent.getTypeName("newVar")).isEqualTo("TypeName");
    assertThat(parent.getTypeModel("newVar")).isEqualTo(typeModel);
}
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)

Aggregations

Scanner (com.google.api.codegen.util.Scanner)20 OutputView (com.google.api.codegen.viewmodel.OutputView)14 TypeModel (com.google.api.codegen.config.TypeModel)11 Test (org.junit.Test)11 FieldModel (com.google.api.codegen.config.FieldModel)7 ImmutableList (com.google.common.collect.ImmutableList)3 Map (java.util.Map)3 OutputSpec (com.google.api.codegen.OutputSpec)2 SampleValueSet (com.google.api.codegen.SampleValueSet)2 LongRunningConfig (com.google.api.codegen.config.LongRunningConfig)2 MethodContext (com.google.api.codegen.config.MethodContext)2 OutputContext (com.google.api.codegen.config.OutputContext)2 SampleConfig (com.google.api.codegen.config.SampleConfig)2 SampleContext (com.google.api.codegen.config.SampleContext)2 SampleSpec (com.google.api.codegen.config.SampleSpec)2 InitCodeOutputType (com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)2 ResponseStatementProto (com.google.api.codegen.samplegen.v1p2.ResponseStatementProto)2 Name (com.google.api.codegen.util.Name)2 CallingForm (com.google.api.codegen.viewmodel.CallingForm)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2