protected void VerifyDiagnostics <T>(params DiagnosticResult[] expected) { var namedTypeSymbol = diagnosticsCompilation.GetTypeByMetadataName(typeof(T).FullName); var factory = new RoslynImplementationFactory(diagnosticsCompilation); var(_, diagnostics) = factory.CreateImplementation(namedTypeSymbol); int lineOffset = namedTypeSymbol.DeclaringSyntaxReferences[0].GetSyntax().GetLocation().GetLineSpan().StartLinePosition.Line; DiagnosticVerifier.VerifyDiagnostics(diagnostics.Concat(factory.GetCompilationDiagnostics()), expected, lineOffset); }
protected T CreateImplementation <T>() { var typeToSearch = typeof(T).IsGenericType ? typeof(T).GetGenericTypeDefinition() : typeof(T); string metadataName = typeToSearch.FullName; var factory = new RoslynImplementationFactory(executionCompilation); var namedTypeSymbol = executionCompilation.GetTypeByMetadataName(metadataName); var(sourceText, diagnostics) = factory.CreateImplementation(namedTypeSymbol); var allDiagnostics = diagnostics.Concat(factory.GetCompilationDiagnostics()).ToList(); Assert.True(allDiagnostics.Count == 0, "Unexpected diagnostics:\r\n\r\n" + string.Join("\r\n", allDiagnostics.Select(x => x.ToString()))); Assert.NotNull(sourceText); // Just in case there are no diagnostics this.output.WriteLine(sourceText.ToString()); var syntaxTree = SyntaxFactory.ParseSyntaxTree(sourceText); var updatedCompilation = executionCompilation.AddSyntaxTrees(syntaxTree); using (var peStream = new MemoryStream()) { var emitResult = updatedCompilation.Emit(peStream); Assert.True(emitResult.Diagnostics.Length == 0, "Unexpected compiler diagnostics:\r\n\r\n" + string.Join("\r\n", emitResult.Diagnostics.Select(x => x.ToString()))); Assert.True(emitResult.Success, "Emit failed"); // Just in case it failed without diagnostics... var assembly = Assembly.Load(peStream.GetBuffer()); var implementationType = assembly.GetCustomAttributes <RestEaseInterfaceImplementationAttribute>() .FirstOrDefault(x => x.InterfaceType == typeToSearch)?.ImplementationType; Assert.NotNull(implementationType); if (typeof(T).IsGenericType) { implementationType = implementationType.MakeGenericType(typeof(T).GetGenericArguments()); } return((T)Activator.CreateInstance(implementationType, this.Requester.Object)); } }