public static void Assert(this JavascriptTest test) { NU.Assert.True(test.Passed, test.Error); }
public void RunJavascriptTests(JavascriptTest test) { test.Assert(); }
public static IEnumerable <JavascriptTest> RunAllTests(string testsDirectory = null) { var phantomJs = new FileInfo((testsDirectory ?? DefaultDirectory) + PhantomJs); if (!phantomJs.Exists) { throw new InvalidOperationException("Could not find the phantomjs.exe file at: " + phantomJs.FullName); } var mochaPhantomJs = new FileInfo((testsDirectory ?? DefaultDirectory) + MochaPhantomJs); if (!mochaPhantomJs.Exists) { throw new InvalidOperationException("Could not find the mocha-phantomjs.coffee file at: " + mochaPhantomJs.FullName); } var testHtml = new FileInfo((testsDirectory ?? DefaultDirectory) + DefaultTestFile); if (!mochaPhantomJs.Exists) { throw new InvalidOperationException("Could not find the tests.html file at: " + testHtml.FullName); } var testData = ExecuteTestsAndReadLines(phantomJs, mochaPhantomJs, testHtml); var enumerator = testData.GetEnumerator(); var count = 0; JavascriptTest current = null; List <JavascriptTest> errorred = new List <JavascriptTest>(); List <JavascriptTest> results = new List <JavascriptTest>(); // First section describes our tests while (enumerator.MoveNext()) { string currentLine = enumerator.Current; if (currentLine.StartsWith(Start)) { continue; } if (currentLine.StartsWith(Pass) || currentLine.StartsWith(Skip) || currentLine.StartsWith(Failed) || currentLine.StartsWith(TestsComplete)) { if (current != null) { results.Add(current); } current = new JavascriptTest() { Number = count }; count++; if (currentLine.StartsWith(Pass)) { currentLine = currentLine.Replace(Pass, string.Empty); if (currentLine.Contains(Time)) { var split = currentLine.Split('*'); currentLine = split[0]; double time; if (double.TryParse(split[2].Replace("ms", string.Empty), out time)) { current.Duration = time; } } current.Passed = true; current.Name = currentLine; continue; } if (currentLine.StartsWith(Skip)) { current.Skipped = true; current.Name = currentLine.Replace(Skip, string.Empty); continue; } if (currentLine.StartsWith(Failed)) { currentLine = currentLine.Replace(Failed, string.Empty); current.Name = currentLine.Substring(currentLine.IndexOf(')') + 1); current.Error = string.Empty; errorred.Add(current); continue; } if (currentLine.StartsWith(TestsComplete)) { break; } } // If the test has passed we expect a time at some point... if (current.Passed) { if (currentLine.Contains(Time)) { var split = currentLine.Split('*'); currentLine = split[0]; double time; if (double.TryParse(split[2].Replace("ms", string.Empty), out time)) { current.Duration = time; } } } // At this point it is just more of the name... current.Name = current.Name + "\r\n" + currentLine; } // Do we have to continue grabbing stuff to work out the errors? if (errorred.Any()) { current = null; string previousLine = null; // we want to skip the summary if (enumerator.MoveNext()) { previousLine = enumerator.Current; } while (enumerator.MoveNext()) { var currentLine = enumerator.Current; if (currentLine.StartsWith(Failed)) { int errorId = int.Parse(previousLine.Substring(0, previousLine.IndexOf(')'))) - 1; current = errorred[errorId]; currentLine = currentLine.Replace(Failed, string.Empty).Replace(Time, string.Empty); current.Error = currentLine; } else { if (current != null) { current.Error = current.Error + "\r\n" + currentLine; } } previousLine = currentLine; } } return(results); }
public static IEnumerable<JavascriptTest> RunAllTests(string testsDirectory = null) { var phantomJs = new FileInfo((testsDirectory ?? DefaultDirectory) + PhantomJs); if (!phantomJs.Exists) { throw new InvalidOperationException("Could not find the phantomjs.exe file at: " + phantomJs.FullName); } var mochaPhantomJs = new FileInfo((testsDirectory ?? DefaultDirectory) + MochaPhantomJs); if (!mochaPhantomJs.Exists) { throw new InvalidOperationException("Could not find the mocha-phantomjs.coffee file at: " + mochaPhantomJs.FullName); } var testHtml = new FileInfo((testsDirectory ?? DefaultDirectory) + DefaultTestFile); if (!mochaPhantomJs.Exists) { throw new InvalidOperationException("Could not find the tests.html file at: " + testHtml.FullName); } var testData = ExecuteTestsAndReadLines(phantomJs, mochaPhantomJs, testHtml); var enumerator = testData.GetEnumerator(); var count = 0; JavascriptTest current = null; List<JavascriptTest> errorred = new List<JavascriptTest>(); List<JavascriptTest> results = new List<JavascriptTest>(); // First section describes our tests while (enumerator.MoveNext()) { string currentLine = enumerator.Current; if (currentLine.StartsWith(Start)) { continue; } if (currentLine.StartsWith(Pass) || currentLine.StartsWith(Skip) || currentLine.StartsWith(Failed) || currentLine.StartsWith(TestsComplete)) { if (current != null) { results.Add(current); } current = new JavascriptTest() { Number = count }; count++; if (currentLine.StartsWith(Pass)) { currentLine = currentLine.Replace(Pass, string.Empty); if (currentLine.Contains(Time)) { var split = currentLine.Split('*'); currentLine = split[0]; double time; if (double.TryParse(split[2].Replace("ms", string.Empty), out time)) { current.Duration = time; } } current.Passed = true; current.Name = currentLine; continue; } if (currentLine.StartsWith(Skip)) { current.Skipped = true; current.Name = currentLine.Replace(Skip, string.Empty); continue; } if (currentLine.StartsWith(Failed)) { currentLine = currentLine.Replace(Failed, string.Empty); current.Name = currentLine.Substring(currentLine.IndexOf(')') + 1); current.Error = string.Empty; errorred.Add(current); continue; } if (currentLine.StartsWith(TestsComplete)) { break; } } // If the test has passed we expect a time at some point... if (current.Passed) { if (currentLine.Contains(Time)) { var split = currentLine.Split('*'); currentLine = split[0]; double time; if (double.TryParse(split[2].Replace("ms", string.Empty), out time)) { current.Duration = time; } } } // At this point it is just more of the name... current.Name = current.Name + "\r\n" + currentLine; } // Do we have to continue grabbing stuff to work out the errors? if (errorred.Any()) { current = null; string previousLine = null; // we want to skip the summary if(enumerator.MoveNext()) { previousLine = enumerator.Current; } while (enumerator.MoveNext()) { var currentLine = enumerator.Current; if (currentLine.StartsWith(Failed)) { int errorId = int.Parse(previousLine.Substring(0, previousLine.IndexOf(')'))) - 1; current = errorred[errorId]; currentLine = currentLine.Replace(Failed, string.Empty).Replace(Time, string.Empty); current.Error = currentLine; } else { if (current != null) { current.Error = current.Error + "\r\n" + currentLine; } } previousLine = currentLine; } } return results; }