<commit_before>﻿// <copyright file="ScenarioTestRunner.cs" company="xBehave.net contributors">
//  Copyright (c) xBehave.net contributors. All rights reserved.
// </copyright>

namespace Xbehave.Execution
{
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Threading;
    using System.Threading.Tasks;
    using Xunit.Abstractions;
    using Xunit.Sdk;

    public class ScenarioTestRunner
    {
        private readonly int scenarioNumber;
        private readonly IReadOnlyList<BeforeAfterTestAttribute> beforeAfterAttributes;

        public ScenarioTestRunner(
            int scenarioNumber,
            ITest test,
            IMessageBus messageBus,
            Type testClass,
            object[] constructorArguments,
            MethodInfo testMethod,
            object[] testMethodArguments,
            string skipReason,
            IReadOnlyList<BeforeAfterTestAttribute> beforeAfterAttributes,
            ExceptionAggregator aggregator,
            CancellationTokenSource cancellationTokenSource)
        {
            this.scenarioNumber = scenarioNumber;
            this.Test = test;
            this.MessageBus = messageBus;
            this.TestClass = testClass;
            this.ConstructorArguments = constructorArguments;
            this.TestMethod = testMethod;
            this.TestMethodArguments = testMethodArguments;
            this.SkipReason = skipReason;
            this.beforeAfterAttributes = beforeAfterAttributes;
            this.Aggregator = aggregator;
            this.CancellationTokenSource = cancellationTokenSource;
        }

        protected int ScenarioNumber
        {
            get { return this.scenarioNumber; }
        }

        protected ITest Test { get; set; }

        protected IMessageBus MessageBus { get; set; }

        protected Type TestClass { get; set; }

        protected object[] ConstructorArguments { get; set; }

        protected MethodInfo TestMethod { get; set; }

        protected object[] TestMethodArguments { get; set; }

        protected string SkipReason { get; set; }

        protected IReadOnlyList<BeforeAfterTestAttribute> BeforeAfterAttributes
        {
            get { return this.beforeAfterAttributes; }
        }

        protected ExceptionAggregator Aggregator { get; set; }

        protected CancellationTokenSource CancellationTokenSource { get; set; }

        public async Task<RunSummary> RunAsync()
        {
            var runSummary = new RunSummary();

            if (!string.IsNullOrEmpty(this.SkipReason))
            {
                runSummary.Total++;
                runSummary.Skipped++;
                this.MessageBus.Queue(this.Test, t => new TestSkipped(t, this.SkipReason), this.CancellationTokenSource);
            }
            else
            {
                var output = string.Empty;
                var aggregator = new ExceptionAggregator(this.Aggregator);
                if (!aggregator.HasExceptions)
                {
                    var tuple = await this.Aggregator.RunAsync(() => this.InvokeTestAsync(aggregator));
                    runSummary.Aggregate(tuple.Item1);
                    output = tuple.Item2;
                }

                var exception = aggregator.ToException();
                if (exception != null)
                {
                    runSummary.Total++;
                    runSummary.Failed++;
                    this.MessageBus.Queue(
                        this.Test,
                        t => new TestFailed(t, runSummary.Time, output, exception),
                        this.CancellationTokenSource);
                }
            }

            return runSummary;
        }

        protected virtual async Task<Tuple<RunSummary, string>> InvokeTestAsync(ExceptionAggregator aggregator)
        {
            // NOTE (adamralph): as in XunitTestRunner, we use a ScenarioOutputHelper here for output
            return Tuple.Create(await this.InvokeTestMethodAsync(), string.Empty);
        }

        protected virtual async Task<RunSummary> InvokeTestMethodAsync()
        {
            return await new ScenarioTestInvoker(
                    this.ScenarioNumber,
                    this.Test,
                    this.MessageBus,
                    this.TestClass,
                    this.ConstructorArguments,
                    this.TestMethod,
                    this.TestMethodArguments,
                    this.BeforeAfterAttributes,
                    this.Aggregator,
                    this.CancellationTokenSource)
                .RunAsync();
        }
    }
}<commit_msg>refactor: fix aggregator usage in ScenarioTestRunner<commit_after>﻿// <copyright file="ScenarioTestRunner.cs" company="xBehave.net contributors">
//  Copyright (c) xBehave.net contributors. All rights reserved.
// </copyright>

namespace Xbehave.Execution
{
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Threading;
    using System.Threading.Tasks;
    using Xunit.Abstractions;
    using Xunit.Sdk;

    public class ScenarioTestRunner
    {
        private readonly int scenarioNumber;
        private readonly IReadOnlyList<BeforeAfterTestAttribute> beforeAfterAttributes;

        public ScenarioTestRunner(
            int scenarioNumber,
            ITest test,
            IMessageBus messageBus,
            Type testClass,
            object[] constructorArguments,
            MethodInfo testMethod,
            object[] testMethodArguments,
            string skipReason,
            IReadOnlyList<BeforeAfterTestAttribute> beforeAfterAttributes,
            ExceptionAggregator aggregator,
            CancellationTokenSource cancellationTokenSource)
        {
            this.scenarioNumber = scenarioNumber;
            this.Test = test;
            this.MessageBus = messageBus;
            this.TestClass = testClass;
            this.ConstructorArguments = constructorArguments;
            this.TestMethod = testMethod;
            this.TestMethodArguments = testMethodArguments;
            this.SkipReason = skipReason;
            this.beforeAfterAttributes = beforeAfterAttributes;
            this.Aggregator = aggregator;
            this.CancellationTokenSource = cancellationTokenSource;
        }

        protected int ScenarioNumber
        {
            get { return this.scenarioNumber; }
        }

        protected ITest Test { get; set; }

        protected IMessageBus MessageBus { get; set; }

        protected Type TestClass { get; set; }

        protected object[] ConstructorArguments { get; set; }

        protected MethodInfo TestMethod { get; set; }

        protected object[] TestMethodArguments { get; set; }

        protected string SkipReason { get; set; }

        protected IReadOnlyList<BeforeAfterTestAttribute> BeforeAfterAttributes
        {
            get { return this.beforeAfterAttributes; }
        }

        protected ExceptionAggregator Aggregator { get; set; }

        protected CancellationTokenSource CancellationTokenSource { get; set; }

        public async Task<RunSummary> RunAsync()
        {
            var runSummary = new RunSummary();

            if (!string.IsNullOrEmpty(this.SkipReason))
            {
                runSummary.Total++;
                runSummary.Skipped++;
                this.MessageBus.Queue(this.Test, t => new TestSkipped(t, this.SkipReason), this.CancellationTokenSource);
            }
            else
            {
                var output = string.Empty;
                var aggregator = new ExceptionAggregator(this.Aggregator);
                if (!aggregator.HasExceptions)
                {
                    var tuple = await this.Aggregator.RunAsync(() => this.InvokeTestAsync(aggregator));
                    runSummary.Aggregate(tuple.Item1);
                    output = tuple.Item2;
                }

                var exception = aggregator.ToException();
                if (exception != null)
                {
                    runSummary.Total++;
                    runSummary.Failed++;
                    this.MessageBus.Queue(
                        this.Test,
                        t => new TestFailed(t, runSummary.Time, output, exception),
                        this.CancellationTokenSource);
                }
            }

            return runSummary;
        }

        protected virtual async Task<Tuple<RunSummary, string>> InvokeTestAsync(ExceptionAggregator aggregator)
        {
            // NOTE (adamralph): as in XunitTestRunner, we use a ScenarioOutputHelper here for output
            return Tuple.Create(await this.InvokeTestMethodAsync(aggregator), string.Empty);
        }

        protected virtual async Task<RunSummary> InvokeTestMethodAsync(ExceptionAggregator aggregator)
        {
            return await new ScenarioTestInvoker(
                    this.ScenarioNumber,
                    this.Test,
                    this.MessageBus,
                    this.TestClass,
                    this.ConstructorArguments,
                    this.TestMethod,
                    this.TestMethodArguments,
                    this.BeforeAfterAttributes,
                    aggregator,
                    this.CancellationTokenSource)
                .RunAsync();
        }
    }
}
{'fraction_non_alphanumeric': 0.07048269970098249, 'fraction_numerical': 0.00042716787697565144, 'mean_word_length': 1.75117508813161, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3397238', 'n_tokens_mistral': 2176, 'n_tokens_neox': 2129, 'n_words': 488}