public void Main_tests_and_functions() { var csrunSource = new Sourcecode { Filename = "main.csrun", Text = @"m1 m2 #functions f1 f2 #test test 1 t1.1 t1.2 #test test 2 t2.1 t2.2 #test t3.1 t3.2 t3.3 #functions f3 f4".Split('\n') }; var result = Dissection.Dissect(csrunSource).ToArray(); Assert.AreEqual(Sourcecode.Sections.CSRunMain, result[0].Section); Assert.AreEqual(new[] { "m1", "m2" }, result[0].Text); Assert.AreEqual(1, result[0].OriginLineNumber); Assert.AreEqual(Sourcecode.Sections.CSRunFunctions, result[1].Section); Assert.AreEqual(new[] { "f1", "f2" }, result[1].Text); Assert.AreEqual(4, result[1].OriginLineNumber); Assert.AreEqual(Sourcecode.Sections.CSRunTest, result[2].Section); Assert.AreEqual("main.csrun", result[2].Filename); Assert.AreEqual("test 1", result[2].Label); Assert.AreEqual(new[] { "t1.1", "t1.2" }, result[2].Text); Assert.AreEqual(7, result[2].OriginLineNumber); Assert.AreEqual(Sourcecode.Sections.CSRunTest, result[3].Section); Assert.AreEqual("main.csrun", result[3].Filename); Assert.AreEqual("test 2", result[3].Label); Assert.AreEqual(new[] { "t2.1", "t2.2" }, result[3].Text); Assert.AreEqual(10, result[3].OriginLineNumber); Assert.AreEqual(Sourcecode.Sections.CSRunTest, result[4].Section); Assert.AreEqual("main.csrun", result[4].Filename); Assert.AreEqual("", result[4].Label); Assert.AreEqual(new[] { "t3.1", "t3.2", "t3.3" }, result[4].Text); Assert.AreEqual(13, result[4].OriginLineNumber); Assert.AreEqual(Sourcecode.Sections.CSRunFunctions, result[5].Section); Assert.AreEqual(new[] { "f3", "f4" }, result[5].Text); Assert.AreEqual(17, result[5].OriginLineNumber); Assert.AreEqual(6, result.Length); }
public void Compile_with_errors() { var csSource = new Sourcecode { Filename = "program_with_errors.cs", Text = @"using System public class Program { public static void Main(string[] args) { #region main var answer == 42 Console.WriteLine(answer); #endregion } } ".Split('\n') }; csrun.data.domain.CompilerError[] result = null; CSCompiler.Compile(csSource, null, errors => result = errors.ToArray()); foreach (var err in result) { Console.WriteLine($"{err.Filename}-{err.LineNumber},{err.ColumnNumber}: {err.Description}"); } Assert.AreEqual(2, result.Length); Assert.AreEqual(2, result[0].LineNumber); Assert.AreEqual(5, result[1].LineNumber); }
public void Main_and_functions() { var csrunSource = new Sourcecode { Filename = "main.csrun", Text = new[] { "main 1", "main 2", "#functions", "f 1", "f 2", "f 3", "#functions", "f 4" } }; var result = Dissection.Dissect(csrunSource).ToArray(); Assert.AreEqual(3, result.Length); Assert.AreEqual(Sourcecode.Sections.CSRunMain, result[0].Section); Assert.AreEqual(new[] { "main 1", "main 2" }, result[0].Text); Assert.AreEqual(1, result[0].OriginLineNumber); Assert.AreEqual(Sourcecode.Sections.CSRunFunctions, result[1].Section); Assert.AreEqual(new[] { "f 1", "f 2", "f 3" }, result[1].Text); Assert.AreEqual(4, result[1].OriginLineNumber); Assert.AreEqual(Sourcecode.Sections.CSRunFunctions, result[2].Section); Assert.AreEqual(new[] { "f 4" }, result[2].Text); Assert.AreEqual(8, result[2].OriginLineNumber); }
public void Collect_testmethodnames() { var csSource = new Sourcecode { Filename = "program_collecttestmethodnames.cs", Text = @"using System; using NUnit.Framework; public class Program { public static void Main(string[] args) { #region main var answer = 42; Console.WriteLine(answer); #endregion } [Test(Description='Test 1!')] public void Test1() {} public void Dump() {} [Test()] public void Test2() {} } ".Replace("'", "\"").Split('\n') }; (string name, string label)[] result = null;
public static void Compile(Sourcecode csSource, Action <Executable> onSuccess, Action <IEnumerable <CompilerError> > onFailure) { var cr = Compile(csSource); if (cr.Errors.HasErrors) { onFailure(MapErrors(cr.Errors)); return; } onSuccess(new Executable(cr.CompiledAssembly)); }
static string[] Render_section(string[] csTemplate, Sourcecode csrunSection) { var regionStartLineNumber = csTemplate.Select((line, index) => new { LineNumber = index + 1, Text = line }) .First(line => line.Text.IndexOf(Regionname()) >= 0) .LineNumber; var csBeforeSection = csTemplate.Take(regionStartLineNumber); var csAfterSection = csTemplate.Skip(regionStartLineNumber); var originLabels = new OriginLabels(csrunSection.Filename, csrunSection.OriginLineNumber); return(csBeforeSection.Concat(new[] { originLabels.StartLabel }) .Concat(Render_source()) .Concat(new[] { originLabels.EndLabel }) .Concat(csAfterSection) .ToArray()); string Regionname() { switch (csrunSection.Section) { case Sourcecode.Sections.CSRunMain: return("#region main"); case Sourcecode.Sections.CSRunFunctions: return("#region functions"); case Sourcecode.Sections.CSRunTest: return("#region test"); default: throw new InvalidOperationException($"Cannot render section type {csrunSection.Section}!"); } } string[] Render_source() { switch (csrunSection.Section) { case Sourcecode.Sections.CSRunTest: return(new[] { $"[NUnit.Framework.Test(Description=\"{csrunSection.Label}\")]public void {CreateTestMethodName()}()" + "{" } .Concat(csrunSection.Text) .Concat(new[] { "}" }) .ToArray()); default: return(csrunSection.Text); } string CreateTestMethodName() => $"Test{Guid.NewGuid().ToString().Replace("-", "")}"; } }
public void Main_without_functions() { var csrunSource = new Sourcecode { Filename = "main.csrun", Text = new[] { "main 1", "main 2", } }; var result = Dissection.Dissect(csrunSource).ToArray(); Assert.AreEqual(1, result.Length); Assert.AreEqual(Sourcecode.Sections.CSRunMain, result[0].Section); Assert.AreEqual(new[] { "main 1", "main 2" }, result[0].Text); Assert.AreEqual(1, result[0].OriginLineNumber); }
public void Functions_without_main() { var csrunSource = new Sourcecode { Filename = "main.csrun", Text = new[] { "#functions", "f 1", "f 2", "f 3" } }; var result = Dissection.Dissect(csrunSource).ToArray(); Assert.AreEqual(1, result.Length); Assert.AreEqual(Sourcecode.Sections.CSRunFunctions, result[0].Section); Assert.AreEqual(new[] { "f 1", "f 2", "f 3" }, result[0].Text); Assert.AreEqual(2, result[0].OriginLineNumber); }
static CompilerResults Compile(Sourcecode csSource) { var provider = new CSharpCodeProvider(); var exePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); var cp = new CompilerParameters(); cp.ReferencedAssemblies.Add("System.dll"); cp.ReferencedAssemblies.Add("System.Core.dll"); cp.ReferencedAssemblies.Add("System.Xml.dll"); cp.ReferencedAssemblies.Add("System.Web.Extensions.dll"); cp.ReferencedAssemblies.Add(Path.Combine(exePath, "System.ValueTuple.dll")); cp.ReferencedAssemblies.Add(Path.Combine(exePath, "nunit.framework.dll")); cp.GenerateExecutable = true; cp.OutputAssembly = Path.GetFileNameWithoutExtension(csSource.Filename) + ".exe"; cp.GenerateInMemory = false; cp.IncludeDebugInformation = true; File.WriteAllLines(csSource.Filename, csSource.Text); return(provider.CompileAssemblyFromFile(cp, csSource.Filename)); }
public void Compile_no_errors() { var csSource = new Sourcecode { Filename = "program_no_errors.cs", Text = @"using System; public class Program { public static void Main(string[] args) { #region main var answer = 42; Console.WriteLine(answer); #endregion } } ".Split('\n') }; Executable result = null; CSCompiler.Compile(csSource, exe => result = exe, errors => { foreach (var err in errors) { Console.WriteLine($"{err.Filename} - {err.LineNumber},{err.ColumnNumber}: {err.Description}"); } Assert.Fail("There should not be any errors!"); }); Assert.NotNull(result); var output = ConsoleOutput.Capture(() => result.Main()); Assert.AreEqual("42", output.Trim()); }
public static IEnumerable <Sourcecode> Dissect(Sourcecode csrunSource) { var sections = new List <Sourcecode>(); var currentSectionText = new List <string>(); var currentSection = OpenSection(Sourcecode.Sections.CSRunMain, 1); var lineNumber = 0; foreach (var line in csrunSource.Text) { lineNumber++; switch (Line_classification(line)) { case Sourcecode.Sections.CSRunFunctions: FlushSection(); currentSection = OpenSection(Sourcecode.Sections.CSRunFunctions, lineNumber + 1); break; case Sourcecode.Sections.CSRunTest: FlushSection(); currentSection = OpenSection(Sourcecode.Sections.CSRunTest, lineNumber + 1); currentSection.Label = line.Trim().Substring("#test".Length).Trim(); break; default: currentSectionText.Add(line); break; } } FlushSection(); return(sections); Sourcecode.Sections Line_classification(string text) { if (text.Trim().StartsWith("#functions")) { return(Sourcecode.Sections.CSRunFunctions); } if (text.Trim().StartsWith("#test")) { return(Sourcecode.Sections.CSRunTest); } return(Sourcecode.Sections.CSRunRaw); } Sourcecode OpenSection(Sourcecode.Sections sectionType, int originLineNumber) { return(new Sourcecode { Section = sectionType, Filename = csrunSource.Filename, OriginLineNumber = originLineNumber }); } void FlushSection() { if (!currentSectionText.Any()) { return; } currentSection.Text = currentSectionText.ToArray(); sections.Add(currentSection); currentSectionText.Clear(); currentSection = null; } }
public ResultEvaluation(Sourcecode csSource, IResultLog log) { _csSource = csSource; _log = log; }