public void ClientCodeGenerationDispatcher_Finds_Derived_Custom() { ConsoleLogger logger = new ConsoleLogger(); ClientCodeGenerationOptions options = new ClientCodeGenerationOptions() { Language = "G#" }; ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null); // Create a new dispatcher and call an internal extensibility point to add ourselves // into the MEF composition container using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher()) { string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location }; IClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, MockGSharpCodeGeneratorDerived.GeneratorName); Assert.IsNotNull(generator, "the dispatcher did not find any code generator"); Assert.AreEqual(generator.GetType(), typeof(MockGSharpCodeGeneratorDerived), "dispatcher found " + generator.GetType() + " but should have found MockGSharpCodeGeneratorDerived"); string generatedCode = generator.GenerateCode(host, Enumerable.Empty <EntityDescription>(), options); Assert.AreEqual(MockGSharpCodeGeneratorDerived.DerivedGeneratedCode, generatedCode, "test code generator did not generate expected code."); } }
public void ClientCodeGenerationDispatcher_Custom_Warns_Full() { ConsoleLogger logger = new ConsoleLogger(); ClientCodeGenerationOptions options = new ClientCodeGenerationOptions() { Language = "C#" }; ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null); // Create a new dispatcher and call an internal extensibility point to add ourselves // into the MEF composition container using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher()) { string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location }; IClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, MockCodeGenerator.GeneratorName); Assert.IsNotNull(generator, "the dispatcher did not find any code generator"); Assert.AreEqual(generator.GetType(), typeof(MockCodeGenerator), "dispatcher found " + generator.GetType() + " but should have found MockCodeGenerator"); // Setting this option makes our custom code generator emit the packet below to test LogWarning MockCodeGenerator.LogWarningsFull = true; string generatedCode = generator.GenerateCode(host, Enumerable.Empty <EntityDescription>(), options); Assert.AreEqual(MockCodeGenerator.FakeGeneratedCode, generatedCode, "test code generator did not generate expected code."); TestHelper.AssertContainsWarningPackets(logger, MockCodeGenerator.WarningPacket); } }
public void ClientCodeGenerationDispatcher_Throws_Exception_Fully_Qualified_Name() { ConsoleLogger logger = new ConsoleLogger(); ClientCodeGenerationOptions options = new ClientCodeGenerationOptions() { Language = "C#", ClientProjectPath = "SampleProject.csproj" }; ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null); using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher()) { // Disable MEF for this test string[] compositionAssemblies = new string[0]; // And use FQN instead string codeGeneratorName = typeof(MockCodeGenerator).AssemblyQualifiedName; // Ask our mock to throw MockCodeGenerator.ThrowException = true; dispatcher.GenerateCode(host, options, Enumerable.Empty <Type>(), compositionAssemblies, codeGeneratorName); string error = string.Format(CultureInfo.CurrentCulture, Resource.CodeGenerator_Threw_Exception, codeGeneratorName, options.ClientProjectPath, MockCodeGenerator.Exception.Message); TestHelper.AssertContainsErrors(logger, error); } }
public void ClientCodeGenerationDispatcher_Throws_Exception_Logical_Name() { ConsoleLogger logger = new ConsoleLogger(); ClientCodeGenerationOptions options = new ClientCodeGenerationOptions() { Language = "C#", ClientProjectPath = "SampleProject.csproj" }; ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null); using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher()) { string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location }; string codeGeneratorName = MockCodeGenerator.GeneratorName; // Ask our mock to throw MockCodeGenerator.ThrowException = true; dispatcher.GenerateCode(host, options, Enumerable.Empty <Type>(), compositionAssemblies, codeGeneratorName); string error = string.Format(CultureInfo.CurrentCulture, Resource.CodeGenerator_Threw_Exception, codeGeneratorName, options.ClientProjectPath, MockCodeGenerator.Exception.Message); TestHelper.AssertContainsErrors(logger, error); } }
public void ClientCodeGenerationDispatcher_Error_Missing_Custom() { ConsoleLogger logger = new ConsoleLogger(); ClientCodeGenerationOptions options = new ClientCodeGenerationOptions() { Language = "C#", ClientProjectPath = "ClientProject", ServerProjectPath = "ServerProject" }; ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null); // Create a new dispatcher and call an internal extensibility point to add ourselves // into the MEF composition container using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher()) { string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location }; IClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, "NotAGenerator"); Assert.IsNull(generator, "the dispatcher should not find any code generator"); string error = string.Format(CultureInfo.CurrentCulture, Resource.Code_Generator_Not_Found, "NotAGenerator", options.Language, options.ServerProjectPath, options.ClientProjectPath, CodeDomClientCodeGenerator.GeneratorName); TestHelper.AssertContainsErrors(logger, error); } }
public void ClientCodeGenerationDispatcher_Null_Language_Throws() { ClientCodeGenerationOptions options = new ClientCodeGenerationOptions(); ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(null, null); ExceptionHelper.ExpectArgumentException(() => { new ClientCodeGenerationDispatcher().FindCodeGenerator(host, options, /*compositionAssemblies*/ null, /*codeGeneratorName*/ null); }, Resource.Null_Language_Property, "options"); }
public void ClientCodeGenerationDispatcher_Error_TypeLoadException() { var logger = new ConsoleLogger(); var options = new ClientCodeGenerationOptions { Language = "C#" }; var host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null); // Create a new dispatcher and call an internal extensibility point to add ourselves // into the MEF composition container using (var dispatcher = new ClientCodeGenerationDispatcher()) { // We want to include into the MEF container an assembly that will throw TypeLoadException // when MEF tries to analyze it. This is to test our own recovery, which should consist // of logging an error making a default container containing only Tools. var unitTestAssemblyLocation = Assembly.GetExecutingAssembly().Location; var typeLoadExceptionProjectLocation = Path.Combine(Path.GetDirectoryName(unitTestAssemblyLocation), "TypeLoadExceptionProject.dll"); Assert.IsTrue(File.Exists(typeLoadExceptionProjectLocation), "Expected TypeLoadExceptionProject.dll to coreside with this assembly in test folder"); // Do what MEF does to load the types so we can capture the exception Exception expectedException = null; try { Assembly assembly = Assembly.LoadFrom(typeLoadExceptionProjectLocation); assembly.GetTypes(); } catch (Exception ex) { expectedException = ex; } Assert.IsNotNull(expectedException, "We did not generate the type load exception we expected"); var compositionAssemblies = new[] { unitTestAssemblyLocation, typeLoadExceptionProjectLocation }; IClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, /*generatorName*/ null); Assert.IsNotNull(generator, "the dispatcher did not pick default generator"); Assert.IsTrue(generator is CodeDomClientCodeGenerator, "the dispatcher did not pick the default code generator"); string error = (string.Format(CultureInfo.CurrentCulture, Resource.Failed_To_Create_Composition_Container, expectedException.Message)); TestHelper.AssertContainsWarnings(logger, error); } }
private string GenerateCode() { var options = new ClientCodeGenerationOptions { Language = _isCSharp ? "C#" : "VisualBasic", ClientProjectPath = "MockProject.proj", ClientRootNamespace = "TestRootNS", UseFullTypeNames = _useFullTypeNames }; var host = TestHelper.CreateMockCodeGenerationHost(ConsoleLogger, MockSharedCodeService); var generator = (_isCSharp) ? (CodeDomClientCodeGenerator) new CSharpCodeDomClientCodeGenerator() : (CodeDomClientCodeGenerator) new VisualBasicCodeDomClientCodeGenerator(); _entityCatalog = new EntityCatalog(_entityTypes, ConsoleLogger); string generatedCode = generator.GenerateCode(host, _entityCatalog.EntityDescriptions, options); return(generatedCode); }
public void ClientCodeGenerationDispatcher_Custom_By_AssemblyQualifiedName_Ctor_Throws() { ConsoleLogger logger = new ConsoleLogger(); ClientCodeGenerationOptions options = new ClientCodeGenerationOptions() { Language = "C#" }; ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null); string codeGeneratorName = typeof(ThrowingCtorCodeGenerator).AssemblyQualifiedName; // Create a new dispatcher and call an internal extensibility point to add ourselves // into the MEF composition container using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher()) { string[] compositionAssemblies = new string[0]; IClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, codeGeneratorName); Assert.IsNull(generator, "the dispatcher should not find the code generator"); string error = string.Format(CultureInfo.CurrentCulture, Resource.Code_Generator_Instantiation_Error, codeGeneratorName, ThrowingCtorCodeGenerator.ErrorMessage); TestHelper.AssertContainsErrors(logger, error); } }
public void ClientCodeGenerationDispatcher_Error_Multiple_Generators_Same_Name() { ConsoleLogger logger = new ConsoleLogger(); ClientCodeGenerationOptions options = new ClientCodeGenerationOptions() { Language = "F#", ClientProjectPath = "ClientProject", ServerProjectPath = "ServerProject" }; ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null); // Create a new dispatcher and call an internal extensibility point to add ourselves // into the MEF composition container using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher()) { string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location }; string generatorName = MockFSharpCodeGenerator1.GeneratorName; IClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, generatorName); Assert.IsNull(generator, "the dispatcher should not pick a generator"); string errorParam = " " + typeof(MockFSharpCodeGenerator1).FullName + Environment.NewLine + " " + typeof(MockFSharpCodeGenerator2).FullName + Environment.NewLine; string error = string.Format(CultureInfo.CurrentCulture, Resource.Multiple_Named_Code_Generators, generatorName, options.Language, errorParam, options.ServerProjectPath, options.ClientProjectPath, typeof(MockFSharpCodeGenerator1).AssemblyQualifiedName); TestHelper.AssertContainsErrors(logger, error); } }
/// <summary> /// Validates code gen for a specific language by comparing it against a file containing the expected output. /// </summary> /// <param name="codeGenOptions">The options specifying the type of validation to perform</param> /// <returns>A command that updates the comparison file</returns> internal static string ValidateLanguageCodeGen(CodeGenValidationOptions codeGenOptions) { Assert.IsFalse(string.IsNullOrEmpty(codeGenOptions.Language)); var outDataDir = GetOutputTestDataDir(codeGenOptions.RelativeDeployDir); var extension = ExtensionFromLanguage(codeGenOptions.Language); var diffMessage = string.Empty; // Compose the abs path to where the test file got deployed by MSTest string referenceFileName = TestHelper.GetTestFileName(codeGenOptions.RelativeDeployDir, codeGenOptions.BaseReferenceFileName + extension); Assert.IsTrue(File.Exists(referenceFileName), "Cannot find reference file " + referenceFileName); string generatedCode = string.Empty; ClientCodeGenerationOptions options = new ClientCodeGenerationOptions() { Language = codeGenOptions.Language, ClientRootNamespace = codeGenOptions.RootNamespace, ClientProjectPath = "MockProject.proj", UseFullTypeNames = codeGenOptions.UseFullTypeNames }; MockCodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(codeGenOptions.Logger, codeGenOptions.SharedCodeService); ILogger logger = host as ILogger; EntityCatalog catalog = new EntityCatalog(codeGenOptions.EntityTypes, logger); IClientCodeGenerator generator; using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher()) { generator = dispatcher.FindCodeGenerator(host, options, /*compositionAssemblies*/ null, /*codeGeneratorName*/ null); } Assert.IsNotNull(generator, "Failed to find a code generator"); generatedCode = generator.GenerateCode(host, catalog.EntityDescriptions, options); ConsoleLogger consoleLogger = logger as ConsoleLogger; string errors = consoleLogger == null ? "" : consoleLogger.Errors; Assert.IsTrue(generatedCode.Length > 0, "No code was generated: " + errors); // Dump the generated code into a file for comparison bool isCSharp = options.Language.Equals("C#", StringComparison.InvariantCultureIgnoreCase); string generatedFileName = Path.Combine(outDataDir, Path.GetFileName(referenceFileName) + ".testgen"); File.WriteAllText(generatedFileName, generatedCode); // TODO: (ron M3) Solve inability to get right MSBuild after checkin // First see if we compile List <string> referenceAssemblies = CompilerHelper.GetClientAssemblies(codeGenOptions.RelativeDeployDir); List <string> files = new List <string>(); files.Add(generatedFileName); // Unconditionally force generation of Xml doc comments to catch errors string documentationFile = Path.GetTempFileName(); try { if (isCSharp) { files.AddRange(codeGenOptions.SharedFiles.Where(sharedFile => Path.GetExtension(sharedFile).Equals(".cs"))); CompilerHelper.CompileCSharpSource(files, referenceAssemblies, documentationFile); } else { files.AddRange(codeGenOptions.SharedFiles.Where(sharedFile => Path.GetExtension(sharedFile).Equals(".vb"))); CompilerHelper.CompileVisualBasicSource(files, referenceAssemblies, options.ClientRootNamespace, documentationFile); } } finally { File.Delete(documentationFile); } // Do the diff if (codeGenOptions.FailOnDiff) { TestHelper.ValidateFilesEqual(codeGenOptions.RelativeTestDir, codeGenOptions.RelativeDeployDir, generatedFileName, referenceFileName, codeGenOptions.Language); } else { TestHelper.FilesMatch(codeGenOptions.RelativeTestDir, codeGenOptions.RelativeDeployDir, generatedFileName, referenceFileName, codeGenOptions.Language, out diffMessage); } return(diffMessage); }