示例#1
0
        protected override TestResult ExecuteTest(IronJS.Hosting.CSharp.Context ctx, string test)
        {
            var errors = new StringBuilder();

            Action<string> appendError = err =>
            {
                if (errors.Length > 0)
                {
                    errors.AppendLine();
                }

                errors.Append(err);
            };

            var score = string.Empty;

            Action<string, string> notifyResult = (name, result) => { };
            Action<string, string> notifyError = (name, error) => appendError(name + ": " + error);
            Action<string> notifyScore = s => score = s;
            ctx.SetGlobal("NotifyResult", IronJS.Native.Utils.CreateFunction(ctx.Environment, 2, notifyResult));
            ctx.SetGlobal("NotifyError", IronJS.Native.Utils.CreateFunction(ctx.Environment, 2, notifyError));
            ctx.SetGlobal("NotifyScore", IronJS.Native.Utils.CreateFunction(ctx.Environment, 1, notifyScore));

            try
            {
                ctx.ExecuteFile(test);
                ctx.Execute(@"BenchmarkSuite.RunSuites({ NotifyResult: NotifyResult,
                                                         NotifyError: NotifyError,
                                                         NotifyScore: NotifyScore });");
            }
            catch (Exception ex)
            {
                appendError("Exception: " + ex.GetBaseException().Message);
            }

            if (errors.Length > 0)
            {
                return new TestResult { Error = errors.ToString() };
            }
            else
            {
                return new TestResult { Score = score };
            }
        }