protected internal override void OnContextCreated(Context cx) { cx.SetLanguageVersion(languageVersion); cx.SetOptimizationLevel(optimizationLevel); if (errorReporter != null) { cx.SetErrorReporter(errorReporter); } cx.SetGeneratingDebug(generatingDebug); base.OnContextCreated(cx); }
public object Run(Context cx) { status.Running(jsFile); testState.errors = new ShellTest.ErrorReporterWrapper(cx.GetErrorReporter()); cx.SetErrorReporter(testState.errors); global.Init(cx); try { ShellTest.RunFileIfExists(cx, global, new FilePath(jsFile.GetParentFile().GetParentFile().GetParentFile(), "shell.js")); ShellTest.RunFileIfExists(cx, global, new FilePath(jsFile.GetParentFile().GetParentFile(), "shell.js")); ShellTest.RunFileIfExists(cx, global, new FilePath(jsFile.GetParentFile(), "shell.js")); ShellTest.RunFileIfExists(cx, global, jsFile); status.HadErrors(jsFile, Sharpen.Collections.ToArray(testState.errors.errors, new ShellTest.Status.JsError[0])); } catch (ThreadDeath) { } catch (Exception t) { status.Threw(t); } return null; }
public virtual int RunDoctest(Context cx, Scriptable scope, string session, string sourceName, int lineNumber) { doctestCanonicalizations = new Dictionary<string, string>(); string[] lines = session.Split("[\n\r]+"); string prompt0 = this.prompts[0].Trim(); string prompt1 = this.prompts[1].Trim(); int testCount = 0; int i = 0; while (i < lines.Length && !lines[i].Trim().StartsWith(prompt0)) { i++; } // skip lines that don't look like shell sessions while (i < lines.Length) { string inputString = Sharpen.Runtime.Substring(lines[i].Trim(), prompt0.Length); inputString += "\n"; i++; while (i < lines.Length && lines[i].Trim().StartsWith(prompt1)) { inputString += Sharpen.Runtime.Substring(lines[i].Trim(), prompt1.Length); inputString += "\n"; i++; } string expectedString = string.Empty; while (i < lines.Length && !lines[i].Trim().StartsWith(prompt0)) { expectedString += lines[i] + "\n"; i++; } TextWriter savedOut = this.GetOut(); TextWriter savedErr = this.GetErr(); MemoryStream @out = new MemoryStream(); MemoryStream err = new MemoryStream(); this.SetOut(new TextWriter(@out)); this.SetErr(new TextWriter(err)); string resultString = string.Empty; ErrorReporter savedErrorReporter = cx.GetErrorReporter(); cx.SetErrorReporter(new ToolErrorReporter(false, this.GetErr())); try { testCount++; object result = cx.EvaluateString(scope, inputString, "doctest input", 1, null); if (result != Context.GetUndefinedValue() && !(result is Function && inputString.Trim().StartsWith("function"))) { resultString = Context.ToString(result); } } catch (RhinoException e) { ToolErrorReporter.ReportException(cx.GetErrorReporter(), e); } finally { this.SetOut(savedOut); this.SetErr(savedErr); cx.SetErrorReporter(savedErrorReporter); resultString += err.ToString() + @out.ToString(); } if (!DoctestOutputMatches(expectedString, resultString)) { string message = "doctest failure running:\n" + inputString + "expected: " + expectedString + "actual: " + resultString + "\n"; if (sourceName != null) { throw Context.ReportRuntimeError(message, sourceName, lineNumber + i - 1, null, 0); } else { throw Context.ReportRuntimeError(message); } } } return testCount; }