private static async Task <GenerationResult> GenerateAsync(bool withRegeneration, IUnitTestGeneratorOptions options, Solution solution, GenerationItem generationItem, SemanticModel semanticModel) { GenerationResult result; if (File.Exists(generationItem.TargetFileName)) { var documentIds = solution.GetDocumentIdsWithFilePath(generationItem.TargetFileName); if (documentIds.FirstOrDefault() is DocumentId targetDocumentId) { var targetDocument = solution.GetDocument(targetDocumentId); var targetSemanticModel = await targetDocument.GetSemanticModelAsync().ConfigureAwait(true); result = await CoreGenerator.Generate(semanticModel, generationItem.SourceSymbol, targetSemanticModel, withRegeneration, options, generationItem.NamespaceTransform).ConfigureAwait(true); } else { result = await CoreGenerator.Generate(semanticModel, generationItem.SourceSymbol, null, withRegeneration, options, generationItem.NamespaceTransform).ConfigureAwait(true); } } else { result = await CoreGenerator.Generate(semanticModel, generationItem.SourceSymbol, null, withRegeneration, options, generationItem.NamespaceTransform).ConfigureAwait(true); } return(result); }
public async Task WhenIRegenerateTests() { var options = new UnitTestGeneratorOptions(new GenerationOptions(_context.TargetFramework, _context.MockFramework), new VersionOptions()); var result = await CoreGenerator.Generate(_context.SemanticModel, _context.ClassModel.Constructors.First().Node, _context.TestModel, true, options, x => x + ".Tests"); var tree = CSharpSyntaxTree.ParseText(result.FileContent, new CSharpParseOptions(LanguageVersion.Latest)); _context.Result = tree.GetRoot().DescendantNodes().OfType <ClassDeclarationSyntax>().First(); _context.CurrentClass = _context.Result; }
public static async Task AssertTestGeneration(string resourceName, TestFrameworkTypes testFrameworkTypes, MockingFrameworkType mockingFrameworkType) { var classAsText = TestClasses.ResourceManager.GetString(resourceName, TestClasses.Culture); var tree = CSharpSyntaxTree.ParseText(classAsText, new CSharpParseOptions(LanguageVersion.Latest)); var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location); var references = new List <MetadataReference> { MetadataReference.CreateFromFile(typeof(object).Assembly.Location), MetadataReference.CreateFromFile(typeof(INotifyPropertyChanged).Assembly.Location), MetadataReference.CreateFromFile(typeof(Expression).Assembly.Location), MetadataReference.CreateFromFile(typeof(Brush).Assembly.Location), MetadataReference.CreateFromFile(typeof(Stream).Assembly.Location), MetadataReference.CreateFromFile(typeof(Form).Assembly.Location), MetadataReference.CreateFromFile(typeof(SqlConnection).Assembly.Location), MetadataReference.CreateFromFile(typeof(Window).Assembly.Location), MetadataReference.CreateFromFile(typeof(UIElement).Assembly.Location), MetadataReference.CreateFromFile(typeof(DependencyObject).Assembly.Location), MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.dll")), MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Core.dll")), MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Runtime.dll")), MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Threading.Tasks.dll")), }; references.AddRange(GetReferences(mockingFrameworkType)); references.AddRange(GetReferences(testFrameworkTypes)); var compilation = CSharpCompilation.Create( "MyTest", syntaxTrees: new[] { tree }, references: references); var semanticModel = compilation.GetSemanticModel(tree); IUnitTestGeneratorOptions options = Substitute.For <IUnitTestGeneratorOptions>(); options.GenerationOptions.FrameworkType.Returns(testFrameworkTypes); options.GenerationOptions.MockingFrameworkType.Returns(mockingFrameworkType); options.GenerationOptions.TestFileNaming.Returns("{0}Tests"); options.GenerationOptions.TestTypeNaming.Returns("{0}Tests"); options.GenerationOptions.TestProjectNaming.Returns("{0}.Tests"); var core = await CoreGenerator.Generate(semanticModel, null, null, false, options, x => "Tests").ConfigureAwait(true); Assert.IsNotNull(core); Console.WriteLine(core.FileContent); Assert.IsNotNull(core.FileContent); var generatedTree = CSharpSyntaxTree.ParseText(core.FileContent, new CSharpParseOptions(LanguageVersion.Latest)); var syntaxTrees = new List <SyntaxTree> { tree, generatedTree }; if (core.RequiredAssets.Any(x => x == TargetAsset.PropertyTester)) { var testerAsset = AssetFactory.Create(TargetAsset.PropertyTester); var propertyTester = testerAsset.Content("Tests", testFrameworkTypes); syntaxTrees.Add(CSharpSyntaxTree.ParseText(propertyTester, new CSharpParseOptions(LanguageVersion.Latest))); } var validateCompilation = CSharpCompilation.Create( "MyTest", syntaxTrees: syntaxTrees, references: references, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); var stream = new MemoryStream(); var result = validateCompilation.Emit(stream); var streamLength = stream.Length; stream.Dispose(); Assert.IsTrue(result.Success, string.Join(",", result.Diagnostics.Select(x => x.Location.GetLineSpan().StartLinePosition.Line + ": " + x.GetMessage()))); Assert.That(streamLength, Is.GreaterThan(0)); }