<commit_before>﻿using System;
using System.Collections;
using System.Linq;
using System.Reflection;
using System.Text;
using NUnit.Framework;
using dokimi.core;

namespace dokimi.nunit
{
    [TestFixture]
    public abstract class NUnitSpecificationTest : SpecificationTest
    {
        public Func<Specification, Specification> TransformForEnvironment = x => x;


        protected IEnumerable SpecificationFinder()
        {
            var specMethods =
                GetType().GetMethods().Where(x => typeof(Specification).IsAssignableFrom(x.ReturnType));

            foreach (var specMethod in specMethods)
            {
                var testCase = new TestCaseData(specMethod);
                testCase.SetName(getSpecName(specMethod.DeclaringType, specMethod));

                yield return testCase;
            }
        }

        [Test, TestCaseSource("SpecificationFinder")]
        public void Specification(MethodInfo testMethod)
        {
            var inconclusive = false;
            var testResult = new SpecInfo();

            var skip = testMethod.GetCustomAttributes().OfType<SkipAttributeContract>()
                    .FirstOrDefault();

            if (skip != null)
            {
                testResult.ReportSkipped(skip.Reason);
                inconclusive = true;
            }
            else
            {
                testResult.ReportSpecExecutionHasTriggered();
            }

            try
            {
                testResult.Name = getSpecName(testMethod.DeclaringType, testMethod);

                var spec = testMethod.Invoke(this, new object[0]) as Specification;
                var formatter = MessageFormatterRegistry.GetFormatter(spec.SpecificationCategory);

                testResult.UseFormatter(formatter);

                if (skip != null)
                    spec.EnrichDescription(testResult, formatter);
                else
                    testResult = spec.Run(testResult, formatter);
            }
            catch (Exception e)
            {
                testResult.Exception = e;
            }

            var sb = new StringBuilder();
            sb.AppendLine(getDescription(testResult));

            var description = sb.ToString();

            Console.WriteLine(description);

            if(inconclusive)
                Assert.Inconclusive(testResult.Skipped.Reason);
            else
                if(!testResult.Passed)
                    Assert.Fail(description);
                else
                    Assert.Pass(description);
        }

        private string getDescription(SpecInfo spec)
        {<commit_msg>removed double printing for nunit specs on failure<commit_after>﻿using System;
using System.Collections;
using System.Linq;
using System.Reflection;
using System.Text;
using NUnit.Framework;
using dokimi.core;

namespace dokimi.nunit
{
    [TestFixture]
    public abstract class NUnitSpecificationTest : SpecificationTest
    {
        public Func<Specification, Specification> TransformForEnvironment = x => x;


        protected IEnumerable SpecificationFinder()
        {
            var specMethods =
                GetType().GetMethods().Where(x => typeof(Specification).IsAssignableFrom(x.ReturnType));

            foreach (var specMethod in specMethods)
            {
                var testCase = new TestCaseData(specMethod);
                testCase.SetName(getSpecName(specMethod.DeclaringType, specMethod));

                yield return testCase;
            }
        }

        [Test, TestCaseSource("SpecificationFinder")]
        public void Specification(MethodInfo testMethod)
        {
            var inconclusive = false;
            var testResult = new SpecInfo();

            var skip = testMethod.GetCustomAttributes().OfType<SkipAttributeContract>()
                    .FirstOrDefault();

            if (skip != null)
            {
                testResult.ReportSkipped(skip.Reason);
                inconclusive = true;
            }
            else
            {
                testResult.ReportSpecExecutionHasTriggered();
            }

            try
            {
                testResult.Name = getSpecName(testMethod.DeclaringType, testMethod);

                var spec = testMethod.Invoke(this, new object[0]) as Specification;
                var formatter = MessageFormatterRegistry.GetFormatter(spec.SpecificationCategory);

                testResult.UseFormatter(formatter);

                if (skip != null)
                    spec.EnrichDescription(testResult, formatter);
                else
                    testResult = spec.Run(testResult, formatter);
            }
            catch (Exception e)
            {
                testResult.Exception = e;
            }

            var sb = new StringBuilder();
            sb.AppendLine(getDescription(testResult));

            var description = sb.ToString();
            
            if(inconclusive)
                Assert.Inconclusive(testResult.Skipped.Reason);
            else if (!testResult.Passed)
                Assert.Fail(description);
            else
            {
                Console.WriteLine(description);
                Assert.Pass(description);
            }
        }

        private string getDescription(SpecInfo spec)
        {
{'fraction_non_alphanumeric': 0.07917059377945335, 'fraction_numerical': 0.00037700282752120643, 'mean_word_length': 1.6879432624113475, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '3864520', 'n_tokens_mistral': 1277, 'n_tokens_neox': 1217, 'n_words': 254}