use of com.google.api.codegen.config.TypeModel 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);
}
use of com.google.api.codegen.config.TypeModel in project toolkit by googleapis.
the class InitCodeTransformer method generateRequestAssertViews.
/**
* Generates assert views for the test of the tested method and its fields.
*/
List<ClientTestAssertView> generateRequestAssertViews(MethodContext methodContext, InitCodeContext initContext) {
InitCodeNode rootNode = InitCodeNode.createTree(initContext.cloneWithEmptySymbolTable());
List<ClientTestAssertView> assertViews = new ArrayList<>();
SurfaceNamer namer = methodContext.getNamer();
// Add request fields checking
for (InitCodeNode fieldItemTree : rootNode.getChildren().values()) {
FieldConfig fieldConfig = fieldItemTree.getFieldConfig();
String getterMethod = namer.getFieldGetFunctionName(methodContext.getFeatureConfig(), fieldConfig);
String expectedValueIdentifier = getVariableName(methodContext, fieldItemTree);
String expectedTransformFunction = null;
String actualTransformFunction = null;
if (methodContext.getFeatureConfig().useResourceNameFormatOption(fieldConfig)) {
if (fieldConfig.requiresParamTransformationFromAny()) {
expectedTransformFunction = namer.getToStringMethod();
actualTransformFunction = namer.getToStringMethod();
} else if (fieldConfig.requiresParamTransformation()) {
if (methodContext.getFeatureConfig().useResourceNameConverters(fieldConfig)) {
expectedTransformFunction = namer.getToStringMethod();
} else {
expectedTransformFunction = namer.getResourceOneofCreateMethod(methodContext.getTypeTable(), fieldConfig);
}
} else if (methodContext.getFeatureConfig().useResourceNameConverters(fieldConfig)) {
if (fieldConfig.getField().isRepeated()) {
actualTransformFunction = namer.getResourceTypeParseListMethodName(methodContext.getTypeTable(), fieldConfig);
} else if (fieldConfig.getResourceNameConfig().getResourceNameType() == ResourceNameType.ONEOF) {
actualTransformFunction = namer.getResourceTypeParentParseMethod(methodContext, fieldConfig);
} else {
actualTransformFunction = namer.getResourceTypeParseMethodName(methodContext.getTypeTable(), fieldConfig);
}
}
}
boolean isMap = fieldConfig.getField().isMap();
boolean isArray = fieldConfig.getField().isRepeated() && !isMap;
boolean isFloatingPointNumber = fieldConfig.getField().getType().isFloatType() || fieldConfig.getField().getType().isDoubleType();
TypeModel fieldType = fieldItemTree.getType();
String messageTypeName = null;
if (fieldType.isMessage()) {
messageTypeName = methodContext.getTypeTable().getFullNameForMessageType(fieldType);
}
assertViews.add(createAssertView(expectedValueIdentifier, expectedTransformFunction, actualTransformFunction, isMap, isArray, isFloatingPointNumber, getterMethod, messageTypeName));
}
return assertViews;
}
Aggregations