public void Npgsql() { const string npgsqlPath = @"C:\projects\npgsql\src\Npgsql"; var rewriter = new Rewriter(new ConsoleLoggingAdapter(LogLevel.Debug)); var generatedCode = rewriter.RewriteAndMerge(Directory.EnumerateFiles(npgsqlPath, "*.cs", SearchOption.AllDirectories).ToArray()); Console.WriteLine(generatedCode); }
public void TestWithGenericConstraints() { var rewriter = new Rewriter(); var root = Path.GetDirectoryName(new Uri(this.GetType().Assembly.CodeBase).LocalPath); var paths = new List<string> { "IQuery.cs" }; var result = rewriter.RewriteAndMerge(paths.Select(c => Path.Combine(root, c)).ToArray()); Console.WriteLine(result); }
public void TestConditionalAccess() { var rewriter = new Rewriter(); var root = Path.GetDirectoryName(new Uri(this.GetType().Assembly.CodeBase).LocalPath); var paths = new List<string> { "ConditionalAccess.cs" }; var result = rewriter.RewriteAndMerge(paths.Select(c => Path.Combine(root, c)).ToArray(), new[] { typeof(EnumerableExtensions).Assembly.Location }).ToArray(); Console.WriteLine(result); }
public void TestUsingExtensionMethods() { var rewriter = new Rewriter(); var root = Path.GetDirectoryName(new Uri(this.GetType().Assembly.CodeBase).LocalPath); var paths = new List<string> { "ExtensionMethodTests.cs", "Foo.cs" }; var result = rewriter.RewriteAndMerge(paths.Select(c => Path.Combine(root, c)).ToArray()); Console.WriteLine(result); }
public void TestRewrite(params string[] inputFiles) { var rewriter = new Rewriter(); var root = Path.Combine(Path.GetDirectoryName(new Uri(GetType().Assembly.CodeBase).LocalPath), "RewriteTests"); var result = rewriter.RewriteAndMerge(inputFiles.Select(c => Path.Combine(root, c)).ToArray()); Assert.IsFalse(result.Contains(": N}\"")); Console.WriteLine(result); }
public void TestTestGenericSpecialisedImplementation() { var rewriter = new Rewriter(); var root = Path.GetDirectoryName(new Uri(this.GetType().Assembly.CodeBase).LocalPath); var paths = new List <string> { "TestGenericSpecialisedImplementation.cs" }; var result = rewriter.RewriteAndMerge(paths.Select(c => Path.Combine(root, c)).ToArray()); Console.WriteLine(result); }
public void TestConditionalAccess() { var rewriter = new Rewriter(); var root = Path.GetDirectoryName(new Uri(this.GetType().Assembly.CodeBase).LocalPath); var paths = new List <string> { "ConditionalAccess.cs" }; var result = rewriter.RewriteAndMerge(paths.Select(c => Path.Combine(root, c)).ToArray(), new[] { typeof(EnumerableExtensions).Assembly.Location }).ToArray(); Console.WriteLine(result); }
public void TestUsingExtensionMethods() { var rewriter = new Rewriter(); var root = Path.GetDirectoryName(new Uri(this.GetType().Assembly.CodeBase).LocalPath); var paths = new List <string> { "ExtensionMethodTests.cs", "Foo.cs" }; var result = rewriter.RewriteAndMerge(paths.Select(c => Path.Combine(root, c)).ToArray()); Console.WriteLine(result); }
public void TestWithInterfaces() { var rewriter = new Rewriter(); var root = Path.GetDirectoryName(new Uri(this.GetType().Assembly.CodeBase).LocalPath); var paths = new List <string> { "ICommand.cs" }; var result = rewriter.RewriteAndMerge(paths.Select(c => Path.Combine(root, c)).ToArray()); Console.WriteLine(result); }
public void TestTestExplicitAwaitRewrittenAsyncMethod() { var rewriter = new Rewriter(); var root = Path.GetDirectoryName(new Uri(this.GetType().Assembly.CodeBase).LocalPath); var paths = new List <string> { "TestExplicitAwaitRewrittenAsyncMethod.cs" }; var result = rewriter.RewriteAndMerge(paths.Select(c => Path.Combine(root, c)).ToArray()); Console.WriteLine(result); }
public void TestRewriteCompile(params string[] inputFiles) { var rewriter = new Rewriter(); var root = Path.Combine(Path.GetDirectoryName(new Uri(this.GetType().Assembly.CodeBase).LocalPath), "RewriteTests"); var files = new List <string>(inputFiles) { "RewriteAsyncAttribute.cs" }; var references = new MetadataReference[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location), MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location), MetadataReference.CreateFromFile(typeof(QueryableExtensions).Assembly.Location) }; var rewriteResult = rewriter.RewriteAndMerge(files.Select(c => Path.Combine(root, c)).ToArray(), references.Select(c => c.Display).ToArray()); Console.WriteLine(rewriteResult); var syntaxTrees = new List <SyntaxTree>(files.Select(c => CSharpSyntaxTree.ParseText(File.ReadAllText(Path.Combine(root, c)), new CSharpParseOptions(LanguageVersion.CSharp7)))) { CSharpSyntaxTree.ParseText(rewriteResult) }; var assemblyName = Path.GetRandomFileName(); var compilation = CSharpCompilation.Create( assemblyName, syntaxTrees: syntaxTrees, references: references, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); using (var ms = new MemoryStream()) { var compileResult = compilation.Emit(ms); if (!compileResult.Success) { var failures = compileResult.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error); foreach (var diagnostic in failures) { Console.Error.WriteLine(diagnostic); } Assert.Fail(); } } }
public void AllTests(string inPath, string expectedPath) { var rewriter = new Rewriter(new ConsoleLoggingAdapter(LogLevel.Debug)); var actual = rewriter.RewriteAndMerge(new[] { inPath, asyncHelpersPath }); actual = SyntaxFactory.SyntaxTree(SyntaxFactory.ParseCompilationUnit(actual).NormalizeWhitespace()).ToString(); var expected = SyntaxFactory.SyntaxTree(SyntaxFactory.ParseCompilationUnit(File.ReadAllText(expectedPath)).NormalizeWhitespace()).ToString(); if (actual != expected) { Console.WriteLine("Actual:"); Console.WriteLine(actual); Console.WriteLine("********"); Console.WriteLine("Expected:"); Console.WriteLine(expected); Console.WriteLine("********"); } Assert.That(actual, Is.EqualTo(expected)); }
public void TestRewriteCompile(params string[] inputFiles) { var rewriter = new Rewriter(); var root = Path.Combine(Path.GetDirectoryName(new Uri(GetType().Assembly.CodeBase).LocalPath), "RewriteTests"); var files = new List <string>(inputFiles) { "RewriteAsyncAttribute.cs" }; var references = new MetadataReference[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location), MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location), MetadataReference.CreateFromFile(typeof(QueryableExtensions).Assembly.Location) }; var rewriteResult = rewriter.RewriteAndMerge(files.Select(c => Path.Combine(root, c)).ToArray(), references.Select(c => c.Display).ToArray()); Console.WriteLine(rewriteResult); var syntaxTrees = new List <SyntaxTree>(files.Select(c => CSharpSyntaxTree.ParseText(File.ReadAllText(Path.Combine(root, c)), new CSharpParseOptions(LanguageVersion.CSharp7)))) { CSharpSyntaxTree.ParseText(rewriteResult) }; var assemblyName = Path.GetRandomFileName(); var compilation = CSharpCompilation.Create( assemblyName, syntaxTrees: syntaxTrees, references: references, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); using (var ms = new MemoryStream()) { var compileResult = compilation.Emit(ms); if (!compileResult.Success) { var failures = compileResult.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error); foreach (var diagnostic in failures) { Console.Error.WriteLine(diagnostic); } Assert.Fail(); } var assembly = Assembly.Load(ms.ToArray()); var asyncMethod = assembly.DefinedTypes .SelectMany(c => c.GetMethods()) .FirstOrDefault(c => c.Name == "MainAsync" && c.IsStatic && c.GetParameters().Length == 0 && c.ReturnType.IsAssignableFromIgnoreGenericParameters(typeof(Task <>))); if (asyncMethod != null) { var task = (Task <int>)asyncMethod.Invoke(null, null); var result = task.GetAwaiter().GetResult(); if (result != 0) { Assert.Fail(); } } else { var method = assembly.DefinedTypes .SelectMany(c => c.GetMethods()) .FirstOrDefault(c => c.Name == "Main" && c.IsStatic && c.GetParameters().Length == 0); if (method != null) { if (method.ReturnType == typeof(int)) { var result = (int)method.Invoke(null, null); if (result != 0) { Assert.Fail(); } } else { method.Invoke(null, null); } } } } }
public void TestTestExplicitAwaitRewrittenAsyncMethod() { var rewriter = new Rewriter(); var root = Path.GetDirectoryName(new Uri(this.GetType().Assembly.CodeBase).LocalPath); var paths = new List<string> { "TestExplicitAwaitRewrittenAsyncMethod.cs" }; var result = rewriter.RewriteAndMerge(paths.Select(c => Path.Combine(root, c)).ToArray()); Console.WriteLine(result); }
public void TestTestGenericSpecialisedImplementation() { var rewriter = new Rewriter(); var root = Path.GetDirectoryName(new Uri(this.GetType().Assembly.CodeBase).LocalPath); var paths = new List<string> { "TestGenericSpecialisedImplementation.cs" }; var result = rewriter.RewriteAndMerge(paths.Select(c => Path.Combine(root, c)).ToArray()); Console.WriteLine(result); }