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