Exemplo n.º 1
0
        private static string GenerateDeepCloneWithCode(
            this ModelType modelType)
        {
            var deepCloneWithMethods = new List <string>();

            foreach (var property in modelType.PropertiesOfConcern)
            {
                if (modelType.IsAbstractBase && (!modelType.DeclaresProperty(property)))
                {
                    continue;
                }

                var propertiesCode = modelType.PropertiesOfConcern.Select(_ =>
                {
                    var referenceItemCloned = _.PropertyType.GenerateCloningLogicCodeForType("this." + _.Name);

                    var code = _.Name == property.Name
                        ? property.ToParameterName()
                        : referenceItemCloned;

                    code = modelType.CastIfConstructorParameterIsOfDifferentType(_) + code;

                    return(new MemberCode(_.Name, code));
                }).ToList();

                var deepCloneWithModelInstantiationCode = modelType.GenerateModelInstantiation(propertiesCode, parameterPaddingLength: 33);

                var effectiveHierarchyKind = (modelType.HierarchyKind == HierarchyKind.ConcreteInherited) && modelType.DeclaresProperty(property)
                    ? HierarchyKind.Standalone
                    : modelType.HierarchyKind;

                var deepCloneWithMethodTemplate = modelType.IsMissingCorrespondingConstructorParameter(property)
                    ? typeof(CloningGeneration).GetCodeTemplate(modelType.HierarchyKind, CodeTemplateKind.ModelSnippet, modelType.DeepCloneKeyMethodKinds, CodeSnippetKind.DeepCloneWithThrows)
                    : typeof(CloningGeneration).GetCodeTemplate(effectiveHierarchyKind, CodeTemplateKind.ModelSnippet, modelType.DeepCloneKeyMethodKinds, CodeSnippetKind.DeepCloneWith);

                var deepCloneWithMethod = deepCloneWithMethodTemplate
                                          .Replace(Tokens.ModelTypeNameInCodeToken, modelType.TypeNameInCodeString)
                                          .Replace(Tokens.ModelTypeNameInXmlDocToken, modelType.TypeNameInXmlDocString)
                                          .Replace(Tokens.ModelAncestorTypeNameToken, property.DeclaringType.ToStringReadable())
                                          .Replace(Tokens.PropertyNameToken, property.Name)
                                          .Replace(Tokens.ParameterNameToken, property.ToParameterName())
                                          .Replace(Tokens.ParameterNameInXmlDocToken, property.ToParameterName(forXmlDoc: true))
                                          .Replace(Tokens.PropertyTypeNameToken, property.PropertyType.ToStringReadable())
                                          .Replace(Tokens.DeepCloneWithModelInstantiationToken, deepCloneWithModelInstantiationCode);

                deepCloneWithMethods.Add(deepCloneWithMethod);
            }

            var result = deepCloneWithMethods.Any()
                ? Environment.NewLine + Environment.NewLine + string.Join(Environment.NewLine + Environment.NewLine, deepCloneWithMethods)
                : string.Empty;

            result = result
                     .Replace(Tokens.DeepCloneWithCodeAnalysisSuppressionsToken, typeof(CloningGeneration).GetCodeTemplate(CodeTemplateKind.ModelSnippet, KeyMethodKinds.Both, CodeSnippetKind.DeepCloneWithCodeAnalysisSuppressions));

            return(result);
        }
        private static string GetObjectsNotEqualToReferenceObject(
            this ModelType modelType)
        {
            var unequalSet = new List <string>();

            var objectNotEqualToReferenceObjectCodeSnippet = typeof(EqualityGeneration).GetCodeTemplate(modelType.ClassifiedHierarchyKind, CodeTemplateKind.TestSnippet, KeyMethodKinds.Generated, CodeSnippetKind.EquatableTestFieldsObjectNotEqualToReferenceObject);

            foreach (var property in modelType.PropertiesOfConcern)
            {
                if (modelType.IsAbstractBase)
                {
                    var cast = modelType.DeclaresProperty(property)
                        ? string.Empty
                        : Invariant($"({modelType.TypeNameInCodeString})");

                    var code = objectNotEqualToReferenceObjectCodeSnippet
                               .Replace(Tokens.PropertyNameToken, property.Name)
                               .Replace(Tokens.CastToken, cast);

                    unequalSet.Add(code);
                }
                else
                {
                    if (!modelType.IsMissingCorrespondingConstructorParameter(property))
                    {
                        var propertiesCode = modelType.PropertiesOfConcern.Select(_ =>
                        {
                            var propertyOfConcern = modelType.CaseInsensitivePropertyNameToPropertyOfConcernMap[_.Name];

                            var referenceProperty = modelType.CastIfConstructorParameterIsOfDifferentType(propertyOfConcern) + "ReferenceObjectForEquatableTestScenarios." + _.Name;

                            var dummyObject = modelType.CastIfConstructorParameterIsOfDifferentType(propertyOfConcern) + objectNotEqualToReferenceObjectCodeSnippet.Replace(Tokens.PropertyNameToken, property.Name);

                            var memberCode = _.Name != property.Name
                                ? referenceProperty
                                : dummyObject;

                            return(new MemberCode(_.Name, memberCode));
                        }).ToList();

                        var code = modelType.GenerateModelInstantiation(propertiesCode, parameterPaddingLength: 32);

                        unequalSet.Add(code);
                    }
                }
            }

            var result = unequalSet.Any()
                ? Environment.NewLine + "                        " + string.Join("," + Environment.NewLine + "                        ", unequalSet) + ","
                : string.Empty;

            return(result);
        }