Search in sources :

Example 11 with Scanner

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

the class OutputTransformerTest method testAccessorNewVariablePageStreamingResourceNameResponse.

@Test
public void testAccessorNewVariablePageStreamingResourceNameResponse() {
    Scanner scanner = new Scanner("$resp");
    when(config.getPageStreaming()).thenReturn(pageStreamingConfig);
    when(pageStreamingConfig.getResourcesFieldConfig()).thenReturn(resourceFieldConfig);
    when(namer.getAndSaveElementResourceTypeName(typeTable, resourceFieldConfig)).thenReturn("ShelfBookName");
    when(featureConfig.useResourceNameFormatOption(resourceFieldConfig)).thenReturn(true);
    OutputView.VariableView variableView = accessorNewVariable(scanner, context, sampleContext, parent, "newVar", false);
    assertThat(variableView.variable()).isEqualTo("sampleResponseVarName");
    assertThat(variableView.accessors()).isEmpty();
    assertThat(parent.getTypeName("newVar")).isEqualTo("ShelfBookName");
    assertThat(parent.getTypeModel("newVar")).isNull();
}
Also used : Scanner(com.google.api.codegen.util.Scanner) OutputView(com.google.api.codegen.viewmodel.OutputView) Test(org.junit.Test)

Example 12 with Scanner

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

the class OutputTransformerTest method testAccessorNewVariableFailWithReservedKeyword.

@Test
public void testAccessorNewVariableFailWithReservedKeyword() {
    Scanner scanner = new Scanner("$resp");
    when(config.getPageStreaming()).thenReturn(pageStreamingConfig);
    when(pageStreamingConfig.getResourcesFieldConfig()).thenReturn(resourceFieldConfig);
    when(namer.getAndSaveElementResourceTypeName(typeTable, resourceFieldConfig)).thenReturn("ShelfBookName");
    when(featureConfig.useResourceNameFormatOption(resourceFieldConfig)).thenReturn(true);
    when(namer.getSampleUsedVarNames(context, form)).thenReturn(ImmutableSet.of("fooResponse"));
    try {
        OutputView.VariableView variableView = accessorNewVariable(scanner, context, sampleContext, parent, "fooResponse", false);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage()).contains("cannot define variable \"fooResponse\": it is already used by the sample template" + " for calling form");
    }
}
Also used : Scanner(com.google.api.codegen.util.Scanner) OutputView(com.google.api.codegen.viewmodel.OutputView) Test(org.junit.Test)

Example 13 with Scanner

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

the class OutputTransformerTest method testAccessorNewVariableMapKeyUnquoatedStringFail.

@Test
public void testAccessorNewVariableMapKeyUnquoatedStringFail() {
    Scanner scanner = new Scanner("old_var.property{unquoted_string}");
    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 stringTypeModel = ProtoTypeRef.create(TypeRef.fromPrimitiveName("string"));
    when(propertyTypeModel.getMapKeyType()).thenReturn(stringTypeModel);
    when(propertyTypeModel.getMapValueType()).thenReturn(stringTypeModel);
    try {
        accessorNewVariable(scanner, context, sampleContext, parent, "newVar", false);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage()).contains("expected string type for map key");
    }
}
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 14 with Scanner

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

the class OutputTransformerTest method testAccessorNewVariableScalarTypeForCollectionFail.

@Test
public void testAccessorNewVariableScalarTypeForCollectionFail() {
    TypeModel oldVarTypeModel = mock(TypeModel.class);
    assertThat(parent.put("old_var", oldVarTypeModel, "OldVarTypeName")).isTrue();
    Scanner scanner = new Scanner("old_var");
    when(oldVarTypeModel.isRepeated()).thenReturn(false);
    when(namer.localVarName(Name.from("old_var"))).thenReturn("oldVar");
    when(namer.getAndSaveTypeName(typeTable, oldVarTypeModel)).thenReturn("OldVarTypeName");
    try {
        OutputView.VariableView variableView = accessorNewVariable(scanner, context, sampleContext, parent, "newVar", true);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage()).contains("is not a repeated field");
    }
}
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 15 with Scanner

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

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