public static string GenerateTestRunnerCode(this IType testSuite, TestRunnerGeneratorOptions options) { using (var writer = new StringWriter()) { testSuite.WriteRunnerCode(writer, options); return(writer.ToString()); } }
private static void EndMain(CodeTextWriter writer, TestRunnerGeneratorOptions options) { if (!String.IsNullOrEmpty(options.EndMarker)) { writer.WriteLine("Console.WriteLine(\"{0}\");", options.EndMarker); } EndMethod(writer); }
private static void WriteRunnerCode(this IMethod test, CodeTextWriter writer, TestRunnerGeneratorOptions options) { if (options == null) { options = new TestRunnerGeneratorOptions { Protect = true } } ; var testSuite = test.DeclaringType; BeginProgram(writer, testSuite); BeginMain(writer); RunTest(writer, options, test); EndMain(writer, options); EndProgram(writer); }
private static void WriteRunnerCode(this IType testSuite, CodeTextWriter writer, TestRunnerGeneratorOptions options) { if (options == null) { options = new TestRunnerGeneratorOptions { Protect = true } } ; BeginProgram(writer, testSuite); writer.WriteLine("static bool fail = false;"); var methods = new List <string>(); foreach (var test in testSuite.GetUnitTests()) { string name = "Run_" + test.Name; BeginMethod(writer, name); RunTest(writer, options, test); EndMethod(writer); methods.Add(name); } BeginMain(writer); foreach (var method in methods) { writer.WriteLine("Console.WriteLine(\"{0}\");", method); writer.WriteLine("{0}();", method); } EndMain(writer, options); EndProgram(writer); }
private static void WriteRunnerCode(this IType testSuite, TextWriter writer, TestRunnerGeneratorOptions options) { testSuite.WriteRunnerCode(new CodeTextWriter(writer), options); }
private static void WriteRunnerCode(this IMethod test, TextWriter writer, TestRunnerGeneratorOptions options) { test.WriteRunnerCode(new CodeTextWriter(writer), options); }
private static void RunTest(CodeTextWriter writer, TestRunnerGeneratorOptions options, IMethod test) { var type = test.DeclaringType; writer.WriteLine("bool fail = false;"); if (!test.IsStatic) { writer.WriteLine("{0} obj = new {0}();", type.Name); } CallSetUp(writer, test); writer.WriteLine(); string expectedException = test.GetExpectedException(); bool hasExpectedException = !String.IsNullOrEmpty(expectedException); bool hasTry = hasExpectedException || options.Protect; if (hasTry) { writer.WriteLine("try"); writer.WriteLine("{"); writer.Indent(); } CallMethod(writer, test); if (hasExpectedException) { writer.WriteLine("fail = true;"); writer.WriteLine("Console.WriteLine(\"No expected exception: {0}\");", expectedException); } if (hasTry) { writer.Unindent(); writer.WriteLine("}"); //end of try } if (hasExpectedException) { //catch for expected exception writer.WriteLine("catch ({0})", expectedException); writer.WriteLine("{"); writer.Indent(); writer.WriteLine("fail = false;"); writer.Unindent(); writer.WriteLine("}"); } if (hasTry) { writer.WriteLine("catch (Exception e)"); writer.WriteLine("{"); writer.Indent(); writer.WriteLine("fail = true;"); writer.WriteLine("Console.WriteLine(\"Unexpected exception: \" + e);"); writer.Unindent(); writer.WriteLine("}"); } writer.WriteLine(); string strFail = options.FailString; if (String.IsNullOrEmpty(strFail)) { strFail = "fail"; } string strSuccess = options.SuccessString; if (String.IsNullOrEmpty(strSuccess)) { strSuccess = "success"; } writer.WriteLine("Console.WriteLine(fail ? \"{0}\" : \"{1}\");", strFail, strSuccess); }