private void ValidateCodeGen(string language, Type contextType, string bizLogicFileBase, IEnumerable<string> references, bool oDataEndpoint, string rootNamespace, string namespaceName) { #if UPDATE_BASELINES bool updateBaselines = true; #else bool updateBaselines = false; #endif string projectDir = Path.Combine(TestHelper.GetProjectDir(), @"Baselines"); string extension = TestHelper.ExtensionFromLanguage(language); string bizLogicBaseName = bizLogicFileBase + extension; string buddyClassBaseName = bizLogicFileBase + ".metadata" + extension; string bizLogicFilePath = Path.Combine(projectDir, bizLogicBaseName); string buddyClassFilePath = Path.Combine(projectDir, buddyClassBaseName); string generatedBizLogicFileName = Path.GetTempFileName(); string className = bizLogicFileBase; string assemblyName = (contextType == null) ? "NoAssembly" : contextType.Assembly.GetName().Name; Type[] contextTypes = (contextType == null) ? new Type[0] : new Type[] { contextType }; using (BusinessLogicViewModel model = new BusinessLogicViewModel(projectDir, className, language, rootNamespace, assemblyName, contextTypes, /* IVsHelp object */ null)) { // Always get the default, but will have 2 if specified a type int expectedCount = contextType == null ? 1 : 2; Assert.AreEqual(expectedCount, model.ContextViewModels.Count, "Expected this many view models"); ContextViewModel expectedViewModel = contextType == null ? model.ContextViewModels[0] : model.ContextViewModels[1]; Assert.AreEqual(expectedViewModel, model.CurrentContextViewModel, "current not as expected"); model.CurrentContextViewModel.IsODataEndpointEnabled = oDataEndpoint; // Select entities first to allow buddy class code-gen foreach (EntityViewModel entity in model.CurrentContextViewModel.Entities) { entity.IsIncluded = true; entity.IsEditable = true; } // Don't generate buddy classes for empty model model.IsMetadataClassGenerationRequested = contextType != null; // Generate the business logic class GeneratedCode generatedCode = model.GenerateBusinessLogicClass(namespaceName); File.AppendAllText(generatedBizLogicFileName, generatedCode.SourceCode); TestHelper.AssertReferenceListContains(references, generatedCode.References, true); // Generate the buddy class // Note: we pass in an optional "Buddy" suffix to both the namespace and class names // because the compiler would reject an attempt to use 'partial class' on an already // compiled class. We put it in a separate namespace because we still need to import // the entity's real namespace, and they cannot be the same. // string generatedBuddyFileName = null; if (model.IsMetadataClassGenerationRequested) { generatedBuddyFileName = Path.GetTempFileName(); generatedCode = model.GenerateMetadataClasses("Buddy"); File.AppendAllText(generatedBuddyFileName, generatedCode.SourceCode); } #if !UPDATE_BASELINES // See if both files compile clean against the current project string[] files = (model.IsMetadataClassGenerationRequested ? new string[] { generatedBizLogicFileName, generatedBuddyFileName } : new string[] { generatedBizLogicFileName }); this.CompileGeneratedCode(TestHelper.GetProjectPath(), files, language); #endif // Compare files against known baselines. // Optionally allow update of baselines rather than comparison TestHelper.ValidateFilesEqual(generatedBizLogicFileName, bizLogicFilePath, updateBaselines); if (model.IsMetadataClassGenerationRequested) { TestHelper.ValidateFilesEqual(generatedBuddyFileName, buddyClassFilePath, updateBaselines); } // Clean up files. Won't get here unless test passes File.Delete(generatedBizLogicFileName); if (model.IsMetadataClassGenerationRequested) { File.Delete(generatedBuddyFileName); } } }
private void ValidateCodeGen(string language, Type contextType, string bizLogicFileBase, IEnumerable <string> references, bool oDataEndpoint, string rootNamespace, string namespaceName) { #if UPDATE_BASELINES bool updateBaselines = true; #else bool updateBaselines = false; #endif string projectDir = Path.Combine(TestHelper.GetProjectDir(), @"Baselines"); string extension = TestHelper.ExtensionFromLanguage(language); string bizLogicBaseName = bizLogicFileBase + extension; string buddyClassBaseName = bizLogicFileBase + ".metadata" + extension; string bizLogicFilePath = Path.Combine(projectDir, bizLogicBaseName); string buddyClassFilePath = Path.Combine(projectDir, buddyClassBaseName); string generatedBizLogicFileName = Path.GetTempFileName(); string className = bizLogicFileBase; string assemblyName = (contextType == null) ? "NoAssembly" : contextType.Assembly.GetName().Name; Type[] contextTypes = (contextType == null) ? Array.Empty <Type>() : new Type[] { contextType }; using (BusinessLogicViewModel model = new BusinessLogicViewModel(projectDir, className, language, rootNamespace, assemblyName, contextTypes, /* IVsHelp object */ null)) { // Always get the default, but will have 2 if specified a type int expectedCount = contextType == null ? 1 : 2; Assert.AreEqual(expectedCount, model.ContextViewModels.Count, "Expected this many view models"); ContextViewModel expectedViewModel = contextType == null ? model.ContextViewModels[0] : model.ContextViewModels[1]; Assert.AreEqual(expectedViewModel, model.CurrentContextViewModel, "current not as expected"); model.CurrentContextViewModel.IsODataEndpointEnabled = oDataEndpoint; // Select entities first to allow buddy class code-gen foreach (EntityViewModel entity in model.CurrentContextViewModel.Entities) { entity.IsIncluded = true; entity.IsEditable = true; } // Don't generate buddy classes for empty model model.IsMetadataClassGenerationRequested = contextType != null; // Generate the business logic class GeneratedCode generatedCode = model.GenerateBusinessLogicClass(namespaceName); File.AppendAllText(generatedBizLogicFileName, generatedCode.SourceCode); TestHelper.AssertReferenceListContains(references, generatedCode.References, true); // Generate the buddy class // Note: we pass in an optional "Buddy" suffix to both the namespace and class names // because the compiler would reject an attempt to use 'partial class' on an already // compiled class. We put it in a separate namespace because we still need to import // the entity's real namespace, and they cannot be the same. // string generatedBuddyFileName = null; if (model.IsMetadataClassGenerationRequested) { generatedBuddyFileName = Path.GetTempFileName(); generatedCode = model.GenerateMetadataClasses("Buddy"); File.AppendAllText(generatedBuddyFileName, generatedCode.SourceCode); } #if !UPDATE_BASELINES // See if both files compile clean against the current project string[] files = (model.IsMetadataClassGenerationRequested ? new string[] { generatedBizLogicFileName, generatedBuddyFileName } : new string[] { generatedBizLogicFileName }); this.CompileGeneratedCode(TestHelper.GetProjectPath(), files, language); #endif // Compare files against known baselines. // Optionally allow update of baselines rather than comparison TestHelper.ValidateFilesEqual(generatedBizLogicFileName, bizLogicFilePath, updateBaselines); if (model.IsMetadataClassGenerationRequested) { TestHelper.ValidateFilesEqual(generatedBuddyFileName, buddyClassFilePath, updateBaselines); } // Clean up files. Won't get here unless test passes File.Delete(generatedBizLogicFileName); if (model.IsMetadataClassGenerationRequested) { File.Delete(generatedBuddyFileName); } } }