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();
}
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");
}
}
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");
}
}
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");
}
}
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);
}
Aggregations