public void TestUnComment() { var rc = new RewriteCode(); rc.Options.PreserveLinesNumber = false; rc.Code = @" using System; namespace Test1 { class Program { static void Main(string[] args) { var dt=DateTime.Now; //dotnet-aop-uncomment System.Console.WriteLine(dt.ToString()); dt=dt; } } }"; var result = rc.RewriteCodeMethod(); var newCode = @" using System; namespace Test1 { class Program { static void Main(string[] args) { System.Console.WriteLine(" + "\"startProgram_Main_6\"" + @"); var dt = DateTime.Now; System.Console.WriteLine(dt.ToString()); dt=dt; System.Console.WriteLine(" + "\"endProgram_Main_6\"" + @"); } } }"; result = result.Replace(" ", "").Replace(Environment.NewLine, ""); newCode = newCode.Replace(" ", "").Replace(Environment.NewLine, ""); result.ShouldBe(newCode); }
public void TestMethodRewriterLastLine() { var rc = new RewriteCode( new AOPFormatter() { FormatterFirstLine = "Console.WriteLine(\"start {nameClass}_{nameMethod}_{lineStartNumber}\");", FormatterLastLine = "Console.WriteLine(\"end {nameClass}_{nameMethod}_{lineStartNumber}\");" } ); rc.Options.PreserveLinesNumber = false; rc.Code = @" using System; namespace Test1 { class Program { static void Main(string[] args) { var dt=DateTime.Now; } } }"; var result = rc.RewriteCodeMethod(); var newCode = @" using System; namespace Test1 { class Program { static void Main(string[] args) { Console.WriteLine(" + "\"start Program_Main_6\"" + @"); var dt = DateTime.Now; Console.WriteLine(" + "\"end Program_Main_6\"" + @"); } } }"; Assert.AreEqual(result.Replace(" ", "").Replace(Environment.NewLine, ""), newCode.Replace(" ", "").Replace(Environment.NewLine, "")); }
public void TestMethodRewriterAddVariable() { var rc = new RewriteCode( new AOPFormatter() { FormatterFirstLine = "string s=\"this is method {nameMethod} from class {nameClass} at line {lineStartNumber}\";", FormatterLastLine = null } ); rc.Options.PreserveLinesNumber = false; rc.Code = @" using System; namespace Test1 { class Program { static void Main(string[] args) { var dt=DateTime.Now; } } }"; var result = rc.RewriteCodeMethod(); var newCode = @" using System; namespace Test1 { class Program { static void Main(string[] args) { string s = ""this is method Main from class Program at line 6""; var dt = DateTime.Now; } } }"; Assert.AreEqual(result.Replace(" ", "").Replace(Environment.NewLine, ""), newCode.Replace(" ", "").Replace(Environment.NewLine, "")); }
public void TestOneRegion() { var rc = new RewriteCode(); rc.Options.PreserveLinesNumber = false; rc.Code = @" using System; namespace Test1 { class Program { static void Main(string[] args) { #region test var dt=DateTime.Now; #endregion } } }"; var result = rc.RewriteCodeMethod(); var newCode = @" using System; namespace Test1 { class Program { static void Main(string[] args) { System.Console.WriteLine(" + "\"startProgram_Main_6\"" + @"); //was a region var dt = DateTime.Now; System.Console.WriteLine(" + "\"endProgram_Main_6\"" + @"); } } }"; Assert.AreEqual(result.Replace(Environment.NewLine, "").Replace(" ", ""), newCode.Replace(Environment.NewLine, "").Replace(" ", "")); }
public void TestModifierMethod() { var rc = new RewriteCode(); rc.Options.PreserveLinesNumber = false; rc.Formatter.MethodsToLog = ModifierMethod.PublicKeyword; rc.Code = @" using System; namespace Test1 { class Program { static void Main(string[] args) { var dt=DateTime.Now; dt=dt; } } }"; var result = rc.RewriteCodeMethod(); var newCode = @" using System; namespace Test1 { class Program { static void Main(string[] args) { var dt=DateTime.Now; dt=dt; } } }";//same code, no aop result = result.Replace(" ", "").Replace(Environment.NewLine, ""); newCode = newCode.Replace(" ", "").Replace(Environment.NewLine, ""); result.ShouldBe(newCode); }
public void TestLineSimple() { var rc = new RewriteCode { Code = @" using System; namespace Test1 { class Program { static void Main(string[] args) { var dt=DateTime.Now; } } }" }; var result = rc.RewriteCodeMethod(); var newCode = @" using System; namespace Test1 { class Program { static void Main(string[] args) { System.Console.WriteLine(" + "\"startProgram_Main_6\"" + @"); #line 9 var dt = DateTime.Now; System.Console.WriteLine(" + "\"endProgram_Main_6\"" + @"); } } }"; Assert.AreEqual(result.Replace(" ", "").Replace(Environment.NewLine, ""), newCode.Replace(" ", "").Replace(Environment.NewLine, "")); }
public void TestException() { var rc = new RewriteCode( new AOPFormatter() { FormatterFirstLine = "System.Console.WriteLine(\"start {nameClass}_{nameMethod}_{lineStartNumber}\");", FormatterLastLine = "System.Console.WriteLine(\"end {nameClass}_{nameMethod}_{lineStartNumber}\");" } ) { Code = @"string s = null; // some comment at line 2 var upper = X.Test(s); // Null reference exception at line 3 // more code class X{ public static string Test(string s) { return s.ToUpper(); } }" }; try { var result = CSharpScript.EvaluateAsync <int>(rc.Code , ScriptOptions.Default.WithEmitDebugInformation(true)).Result; } catch (AggregateException e) { if (e.InnerException is NullReferenceException inner) { var startIndex = inner.StackTrace.IndexOf(":line ", StringComparison.Ordinal) + 6; var lineNumberStr = inner.StackTrace.Substring( startIndex, inner.StackTrace.IndexOf("\r", StringComparison.Ordinal) - startIndex); var lineNumber = Int32.Parse(lineNumberStr); Assert.AreEqual(7, lineNumber); } else { Assert.AreEqual(true, false, " should have exception"); } } try { var q = rc.RewriteCodeMethod(); var result = CSharpScript.EvaluateAsync <int>(q , ScriptOptions.Default.WithEmitDebugInformation(true)).Result; } catch (AggregateException e) { if (e.InnerException is NullReferenceException inner) { var startIndex = inner.StackTrace.IndexOf(":line ", StringComparison.Ordinal) + 6; var lineNumberStr = inner.StackTrace.Substring( startIndex, inner.StackTrace.IndexOf("\r", StringComparison.Ordinal) - startIndex); var lineNumber = Int32.Parse(lineNumberStr); Assert.AreEqual(7, lineNumber); } else { Assert.AreEqual(true, false, " should have exception"); } } }