protected string Process(string source, string[] typeNames = null, string entryPoint = null, IEnumerable<IAssemblyResource> resources = null, IErrorReporter errorReporter = null) { bool assertNoErrors = errorReporter == null; errorReporter = errorReporter ?? new MockErrorReporter(true); var sourceFile = new MockSourceFile("file.cs", source); var n = new Namer(); var references = new[] { Files.Mscorlib }; var compilation = PreparedCompilation.CreateCompilation("x", new[] { sourceFile }, references, null, resources); var md = new MetadataImporter(errorReporter, compilation.Compilation, new CompilerOptions()); var rtl = new RuntimeLibrary(md, errorReporter, compilation.Compilation, n); var l = new MockLinker(); md.Prepare(compilation.Compilation.GetAllTypeDefinitions()); var compiler = new Compiler(md, n, rtl, errorReporter); var compiledTypes = compiler.Compile(compilation).ToList(); var obj = new OOPEmulator(compilation.Compilation, md, rtl, n, l, errorReporter); IMethod ep; if (entryPoint != null) { var type = compiledTypes.Single(c => c.CSharpTypeDefinition.FullName == entryPoint.Substring(0, entryPoint.IndexOf('.'))); ep = type.CSharpTypeDefinition.Methods.Single(m => m.FullName == entryPoint); } else { ep = null; } var rewritten = obj.Process(compiledTypes.Where(t => typeNames == null || typeNames.Contains(t.CSharpTypeDefinition.FullName)), ep); if (assertNoErrors) Assert.That(((MockErrorReporter)errorReporter).AllMessages, Is.Empty, "Should not have errors"); return string.Join("", rewritten.Select(s => OutputFormatter.Format(s, allowIntermediates: true))); }
public static Tuple<string, MockErrorReporter> Compile(string source, bool expectErrors = false) { var sourceFile = new MockSourceFile("file.cs", source); var er = new MockErrorReporter(!expectErrors); var n = new Namer(); var references = new[] { Files.Mscorlib }; var compilation = PreparedCompilation.CreateCompilation(new[] { sourceFile }, references, null);; var md = new MetadataImporter(er, compilation.Compilation, new CompilerOptions()); var rtl = new CoreLib.Plugin.RuntimeLibrary(md, er, compilation.Compilation, n); md.Prepare(compilation.Compilation.GetAllTypeDefinitions()); var compiler = new Saltarelle.Compiler.Compiler.Compiler(md, n, rtl, er); var compiledTypes = compiler.Compile(compilation); if (expectErrors) { Assert.That(er.AllMessages, Is.Not.Empty, "Compile should have generated errors"); return Tuple.Create((string)null, er); } Assert.That(er.AllMessages, Is.Empty, "Compile should not generate errors"); var js = new OOPEmulator(compilation.Compilation, md, rtl, n, er).Process(compiledTypes, null); js = new Linker(md, n, compilation.Compilation).Process(js); string script = string.Join("", js.Select(s => OutputFormatter.Format(s, allowIntermediates: false))); return Tuple.Create(script, er); }
public static Tuple <string, MockErrorReporter> Compile(string source, bool expectErrors = false) { var sourceFile = new MockSourceFile("file.cs", source); var er = new MockErrorReporter(!expectErrors); var n = new Namer(); var references = new[] { Files.Mscorlib }; var compilation = PreparedCompilation.CreateCompilation(new[] { sourceFile }, references, null);; var md = new MetadataImporter(er, compilation.Compilation, new CompilerOptions()); var rtl = new CoreLib.Plugin.RuntimeLibrary(md, er, compilation.Compilation, n); md.Prepare(compilation.Compilation.GetAllTypeDefinitions()); var compiler = new Saltarelle.Compiler.Compiler.Compiler(md, n, rtl, er); var compiledTypes = compiler.Compile(compilation); if (expectErrors) { Assert.That(er.AllMessages, Is.Not.Empty, "Compile should have generated errors"); return(Tuple.Create((string)null, er)); } Assert.That(er.AllMessages, Is.Empty, "Compile should not generate errors"); var js = new OOPEmulator(compilation.Compilation, md, rtl, n, er).Process(compiledTypes, null); js = new Linker(md, n, compilation.Compilation).Process(js); string script = string.Join("", js.Select(s => OutputFormatter.Format(s, allowIntermediates: false))); return(Tuple.Create(script, er)); }
public void ByNamespaceComparerOrdersTypesCorrectly() { var orig = new[] { "A", "B", "C", "A.B", "A.BA", "A.C", "A.BAA.A", "B.A", "B.B", "B.C", "B.A.A", "B.A.B", "B.B.A" }; var rnd = new Random(42); var shuffled = orig.Select(n => new { n, r = rnd.Next() }).OrderBy(x => x.r).Select(x => x.n).ToList(); var actual = OOPEmulator.OrderByNamespace(shuffled, s => s).ToList(); Assert.That(actual, Is.EqualTo(orig)); }
protected string Process(string source, string[] typeNames = null, string entryPoint = null, IErrorReporter errorReporter = null) { bool assertNoErrors = errorReporter == null; errorReporter = errorReporter ?? new MockErrorReporter(true); var sourceFile = new MockSourceFile("file.cs", source); var n = new Namer(); var references = new[] { Files.Mscorlib }; var compilation = PreparedCompilation.CreateCompilation(new[] { sourceFile }, references, null);; var md = new MetadataImporter(errorReporter, compilation.Compilation, new CompilerOptions()); var rtl = new RuntimeLibrary(md, errorReporter, compilation.Compilation, n); md.Prepare(compilation.Compilation.GetAllTypeDefinitions()); var compiler = new Compiler(md, n, rtl, errorReporter); var compiledTypes = compiler.Compile(compilation).ToList(); var obj = new OOPEmulator(compilation.Compilation, md, rtl, n, errorReporter); IMethod ep; if (entryPoint != null) { var type = compiledTypes.Single(c => c.CSharpTypeDefinition.FullName == entryPoint.Substring(0, entryPoint.IndexOf('.'))); ep = type.CSharpTypeDefinition.Methods.Single(m => m.FullName == entryPoint); } else { ep = null; } var rewritten = obj.Process(compiledTypes.Where(t => typeNames == null || typeNames.Contains(t.CSharpTypeDefinition.FullName)), ep); if (assertNoErrors) { Assert.That(((MockErrorReporter)errorReporter).AllMessages, Is.Empty, "Should not have errors"); } return(string.Join("", rewritten.Select(s => OutputFormatter.Format(s, allowIntermediates: true)))); }