/// <summary> /// Generates code that implements standard features of a model, including /// equality checks, hash code generation, cloning methods, and a friendly ToString(). /// </summary> /// <param name="modelType">The type of model.</param> /// <returns> /// Generated code that implements the standard features for the specified model type. /// </returns> public static string GenerateCodeForModelImplementation( this ModelType modelType) { new { modelType }.AsArg().Must().NotBeNull(); var modelImplementationItems = new List <string>(); if (modelType.RequiresEquality) { modelImplementationItems.Add(string.Empty); modelImplementationItems.Add(modelType.GenerateEqualityMethods()); } if (modelType.RequiresComparability) { modelImplementationItems.Add(string.Empty); modelImplementationItems.Add(modelType.GenerateComparableMethods()); } if (modelType.RequiresHashing) { var hashingMethods = modelType.GenerateHashingMethods(); if (!string.IsNullOrWhiteSpace(hashingMethods)) { modelImplementationItems.Add(string.Empty); modelImplementationItems.Add(hashingMethods); } } if (modelType.RequiresDeepCloning) { modelImplementationItems.Add(string.Empty); modelImplementationItems.Add(modelType.GenerateCloningMethods()); } if (modelType.RequiresStringRepresentation) { var stringRepresentationMethods = modelType.GenerateStringRepresentationMethods(); if (!string.IsNullOrWhiteSpace(stringRepresentationMethods)) { modelImplementationItems.Add(string.Empty); modelImplementationItems.Add(stringRepresentationMethods); } } var modelImplementationCode = modelImplementationItems.Where(_ => _ != null).ToNewLineDelimited(); var codeTemplate = typeof(ModelImplementationGeneration).GetCodeTemplate(CodeTemplateKind.Model, KeyMethodKinds.Both); var result = codeTemplate .Replace(Tokens.UsingStatementsToken, modelType.GetUsingStatementsForModelObject()) .Replace(Tokens.CodeGenAssemblyNameToken, GenerationShared.GetCodeGenAssemblyName()) .Replace(Tokens.CodeGenAssemblyVersionToken, GenerationShared.GetCodeGenAssemblyVersion()) .Replace(Tokens.ModelTypeNamespaceToken, modelType.TypeNamespace) .Replace(Tokens.ModelTypeNameInCodeToken, modelType.TypeNameInCodeString) .Replace(Tokens.RequiredInterfacesToken, modelType.RequiredInterfaces.Select(_ => _.ToStringReadable()).ToDelimitedString(", ")) .Replace(Tokens.ModelImplementationToken, modelImplementationCode); return(result); }
public static string GenerateCodeForTests( this ModelType modelType, GenerateFor kind) { var testImplementationItems = new List <string>(); if (modelType.RequiresStringRepresentation) { var stringFieldsCode = modelType.GenerateStringRepresentationTestFields().ReplaceCodeAnalysisSuppressionTokensInTestCode(); if (!string.IsNullOrWhiteSpace(stringFieldsCode)) { testImplementationItems.Add(string.Empty); testImplementationItems.Add(stringFieldsCode); } } if (modelType.RequiresModel) { var constructorFieldsCode = modelType.GenerateConstructorTestFields().ReplaceCodeAnalysisSuppressionTokensInTestCode(); if (constructorFieldsCode != null) { testImplementationItems.Add(string.Empty); testImplementationItems.Add(constructorFieldsCode); } } if (modelType.RequiresDeepCloning) { var deepCloneWithFieldsCode = modelType.GenerateDeepCloneWithTestFields().ReplaceCodeAnalysisSuppressionTokensInTestCode(); if (!string.IsNullOrWhiteSpace(deepCloneWithFieldsCode)) { testImplementationItems.Add(string.Empty); testImplementationItems.Add(deepCloneWithFieldsCode); } } if (modelType.RequiresEquality || modelType.RequiresHashing) { var equalityFieldsCode = modelType.GenerateEqualityTestFields().ReplaceCodeAnalysisSuppressionTokensInTestCode(); if (equalityFieldsCode != null) { testImplementationItems.Add(string.Empty); testImplementationItems.Add(equalityFieldsCode); } } if (modelType.RequiresComparability) { var comparableTestFields = modelType.GenerateComparableTestFields().ReplaceCodeAnalysisSuppressionTokensInTestCode(); testImplementationItems.Add(string.Empty); testImplementationItems.Add(comparableTestFields); } var structuralTestMethodsCode = modelType.GenerateStructuralTestMethods().ReplaceCodeAnalysisSuppressionTokensInTestCode(); testImplementationItems.Add(string.Empty); testImplementationItems.Add(structuralTestMethodsCode); if (modelType.RequiresStringRepresentation) { var stringTestMethodsCode = modelType.GenerateStringRepresentationTestMethods().ReplaceCodeAnalysisSuppressionTokensInTestCode(); if (!string.IsNullOrWhiteSpace(stringTestMethodsCode)) { testImplementationItems.Add(string.Empty); testImplementationItems.Add(stringTestMethodsCode); } } if (modelType.RequiresModel) { var constructorTestMethodsCode = modelType.GenerateConstructorTestMethods().ReplaceCodeAnalysisSuppressionTokensInTestCode(); if (constructorTestMethodsCode != null) { testImplementationItems.Add(string.Empty); testImplementationItems.Add(constructorTestMethodsCode); } } if (modelType.RequiresDeepCloning) { var cloningTestMethodsCode = modelType.GenerateCloningTestMethods().ReplaceCodeAnalysisSuppressionTokensInTestCode(); testImplementationItems.Add(string.Empty); testImplementationItems.Add(cloningTestMethodsCode); } if (kind.HasFlag(GenerateFor.ModelImplementationTestsPartialClassWithSerialization) && modelType.RequiresModel) { var serializationTestMethodsCode = modelType.GenerateSerializationTestMethods().ReplaceCodeAnalysisSuppressionTokensInTestCode(); testImplementationItems.Add(string.Empty); testImplementationItems.Add(serializationTestMethodsCode); } if (modelType.RequiresEquality) { var equalityTestMethodsCode = modelType.GenerateEqualityTestMethods().ReplaceCodeAnalysisSuppressionTokensInTestCode(); testImplementationItems.Add(string.Empty); testImplementationItems.Add(equalityTestMethodsCode); } if (modelType.RequiresHashing) { var hashingTestMethodsCode = modelType.GenerateHashingTestMethods().ReplaceCodeAnalysisSuppressionTokensInTestCode(); testImplementationItems.Add(string.Empty); testImplementationItems.Add(hashingTestMethodsCode); } if (modelType.RequiresComparability) { var comparabilityTestMethodsCode = modelType.GenerateComparabilityTestMethods().ReplaceCodeAnalysisSuppressionTokensInTestCode(); testImplementationItems.Add(string.Empty); testImplementationItems.Add(comparabilityTestMethodsCode); } var testImplementationCode = testImplementationItems.Where(_ => _ != null).ToNewLineDelimited(); var codeTemplate = typeof(ModelImplementationGeneration).GetCodeTemplate(CodeTemplateKind.Test, KeyMethodKinds.Both); var result = codeTemplate .Replace(Tokens.UsingStatementsToken, modelType.GetUsingStatementsForTestClass(kind)) .Replace(Tokens.CodeGenAssemblyNameToken, GenerationShared.GetCodeGenAssemblyName()) .Replace(Tokens.CodeGenAssemblyVersionToken, GenerationShared.GetCodeGenAssemblyVersion()) .Replace(Tokens.ModelTypeNamespaceToken, modelType.TypeNamespace) .Replace(Tokens.ModelTypeNameInCodeToken, modelType.TypeNameInCodeString) .Replace(Tokens.ModelTypeNameInTestClassNameToken, modelType.TypeNameInTestClassNameString) .Replace(Tokens.TestImplementationToken, testImplementationCode); return(result); }