<commit_before>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Xunit;
using Xunit.Extensions;
using Xunit.Sdk;

namespace Jwc.Experiment
{
    /// <summary>
    /// 이 attribute는 method위에 선언되어 해당 method가 test case라는 것을
    /// 지칭하게 되며, non-parameterized test 뿐 아니라 parameterized test에도
    /// 사용될 수 있다.
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "Parameterized test에 auto data를 제공하기 위해, Subclass에서 ITestFixture factory를 제공할 수 있음.")]
    [AttributeUsage(AttributeTargets.Method)]
    public class DefaultTheoremAttribute : FactAttribute
    {
        private readonly ITestFixtureFactory _fixtureFactory;

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultTheoremAttribute"/> class.
        /// </summary>
        public DefaultTheoremAttribute()
        {
            _fixtureFactory = new TypeFixtureFactory(typeof(NotSupportedFixture));
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultTheoremAttribute"/> class.
        /// </summary>
        /// <param name="fixtureType">Type of the fixture.</param>
        /// <exception cref="System.ArgumentNullException">fixtureType</exception>
        public DefaultTheoremAttribute(Type fixtureType)
        {
            if (fixtureType == null)
            {
                throw new ArgumentNullException("fixtureType");
            }

            _fixtureFactory = new TypeFixtureFactory(fixtureType);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultTheoremAttribute"/> class.
        /// </summary>
        /// <param name="fixtureFactory">The fixture factory.</param>
        protected DefaultTheoremAttribute(ITestFixtureFactory fixtureFactory)
        {
            if (fixtureFactory == null)
            {
                throw new ArgumentNullException("fixtureFactory");
            }

            _fixtureFactory = fixtureFactory;
        }

        /// <summary>
        /// Enumerates the test commands represented by this test method.
        /// Derived classes should override this method to return instances of
        /// <see cref="ITestCommand" />, one per execution of a test method.
        /// </summary>
        /// <param name="method">The test method</param>
        /// <returns>
        /// The test commands which will execute the test runs for the given method
        /// </returns>
        protected override IEnumerable<ITestCommand> EnumerateTestCommands(IMethodInfo method)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            var dataAttributes = ((IEnumerable<DataAttribute>)method
                .MethodInfo
                .GetCustomAttributes(typeof(DataAttribute), false)).ToArray();
            var testData = new TestDataCollection(method.MethodInfo, dataAttributes);

            if (!testData.Any())
            {
                yield return CreateSingleTestCommand(method);
                yield break;
            }

            foreach (var testCommand in testData.Select(tcd => CreateEachTestCommand(method, tcd)))
            {
                yield return testCommand;
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification="auto data를 만들 때 발생되는 unhandled exception을 처리하기 위해서 이 경고 무시함.")]
        private ITestCommand CreateSingleTestCommand(IMethodInfo method)
        {
            var autoArguments = new AutoArgumentCollection(
                _fixtureFactory, method.MethodInfo, method.MethodInfo.GetParameters());

            if (!autoArguments.HasAutoParemeters)
            {
                return base.EnumerateTestCommands(method).Single();
            }
           
            try
            {
                return new TheoryCommand(method, autoArguments.ToArray());
            }
            catch (Exception exception)
            {
                return new ExceptionCommand(method, exception);
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "auto data를 만들 때 발생되는 unhandled exception을 처리하기 위해서 이 경고 무시함.")]
        private ITestCommand CreateEachTestCommand(IMethodInfo method, object[] testCaseData)
        {
            var autoArguments = new AutoArgumentCollection(
                _fixtureFactory,
                method.MethodInfo,<commit_msg>refactor: introduce the CreateTestFixture method.<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Xunit;
using Xunit.Extensions;
using Xunit.Sdk;

namespace Jwc.Experiment
{
    /// <summary>
    /// 이 attribute는 method위에 선언되어 해당 method가 test case라는 것을
    /// 지칭하게 되며, non-parameterized test 뿐 아니라 parameterized test에도
    /// 사용될 수 있다.
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "Parameterized test에 auto data를 제공하기 위해, Subclass에서 ITestFixture factory를 제공할 수 있음.")]
    [AttributeUsage(AttributeTargets.Method)]
    public class DefaultTheoremAttribute : FactAttribute
    {
        private readonly ITestFixtureFactory _fixtureFactory;

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultTheoremAttribute"/> class.
        /// </summary>
        public DefaultTheoremAttribute()
        {
            _fixtureFactory = new TypeFixtureFactory(typeof(NotSupportedFixture));
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultTheoremAttribute"/> class.
        /// </summary>
        /// <param name="fixtureType">Type of the fixture.</param>
        /// <exception cref="System.ArgumentNullException">fixtureType</exception>
        public DefaultTheoremAttribute(Type fixtureType)
        {
            if (fixtureType == null)
            {
                throw new ArgumentNullException("fixtureType");
            }

            _fixtureFactory = new TypeFixtureFactory(fixtureType);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultTheoremAttribute"/> class.
        /// </summary>
        /// <param name="fixtureFactory">The fixture factory.</param>
        protected DefaultTheoremAttribute(ITestFixtureFactory fixtureFactory)
        {
            if (fixtureFactory == null)
            {
                throw new ArgumentNullException("fixtureFactory");
            }

            _fixtureFactory = fixtureFactory;
        }

        /// <summary>
        /// Enumerates the test commands represented by this test method.
        /// Derived classes should override this method to return instances of
        /// <see cref="ITestCommand" />, one per execution of a test method.
        /// </summary>
        /// <param name="method">The test method</param>
        /// <returns>
        /// The test commands which will execute the test runs for the given method
        /// </returns>
        protected override IEnumerable<ITestCommand> EnumerateTestCommands(IMethodInfo method)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            var dataAttributes = ((IEnumerable<DataAttribute>)method
                .MethodInfo
                .GetCustomAttributes(typeof(DataAttribute), false)).ToArray();
            var testData = new TestDataCollection(method.MethodInfo, dataAttributes);

            if (!testData.Any())
            {
                yield return CreateSingleTestCommand(method);
                yield break;
            }

            foreach (var testCommand in testData.Select(tcd => CreateEachTestCommand(method, tcd)))
            {
                yield return testCommand;
            }
        }

        /// <summary>
        /// Creates an instance of <see cref="ITestFixture"/>.
        /// </summary>
        /// <param name="testMethod">
        /// The test method
        /// </param>
        /// <returns>
        /// The created fixture.
        /// </returns>
        protected virtual ITestFixture CreateTestFixture(MethodInfo testMethod)
        {
            throw new NotImplementedException();
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification="auto data를 만들 때 발생되는 unhandled exception을 처리하기 위해서 이 경고 무시함.")]
        private ITestCommand CreateSingleTestCommand(IMethodInfo method)
        {
            var autoArguments = new AutoArgumentCollection(
                _fixtureFactory, method.MethodInfo, method.MethodInfo.GetParameters());

            if (!autoArguments.HasAutoParemeters)
            {
                return base.EnumerateTestCommands(method).Single();
            }
           
            try
            {
                return new TheoryCommand(method, autoArguments.ToArray());
            }
            catch (Exception exception)
            {
                return new ExceptionCommand(method, exception);
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "auto data를 만들 때 발생되는 unhandled exception을 처리하기 위해서 이 경고 무시함.")]
        private ITestCommand CreateEachTestCommand(IMethodInfo method, object[] testCaseData)
        {
            var autoArguments = new AutoArgumentCollection(
                _fixtureFactory,
                method.MethodInfo,
{'fraction_non_alphanumeric': 0.09389289706330657, 'fraction_numerical': 0.002438776547098872, 'mean_word_length': 2.116529449018366, 'pattern_counts': {'":': 0, '<': 62, '<?xml version=': 0, '>': 64, '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': '4811699', 'n_tokens_mistral': 2548, 'n_tokens_neox': 2560, 'n_words': 614}