<commit_before>﻿using NUnit.Framework;

namespace Deferred.Test
{
  [TestFixture]
  public abstract class DeferredTest
  {
    public IDeferred SystemUnderTest { get; private set; }

    [TestFixtureSetUp]
    public void Setup()
    {
      SystemUnderTest = new Deferred();
      EstablishContext();
      Because();
    }

    protected virtual void EstablishContext()
    {
    }

    protected abstract void Because();

    public abstract class WhenUsingAPromise : DeferredTest
    {
      public IPromise PromiseUnderTest { get; private set; }

      protected override void EstablishContext()
      {
        base.EstablishContext();
        PromiseUnderTest = SystemUnderTest.Promise;
      }

      public abstract class WhenRegisteringADoneHandler : WhenUsingAPromise
      {
        public int DoneHandlerInvocationCount { get; private set; }

        protected override void EstablishContext()
        {
          base.EstablishContext();
          PromiseUnderTest.Done((sender, args) => { DoneHandlerInvocationCount++; });
        }

        public class WhenTheDeferredIsResolved : WhenRegisteringADoneHandler
        {
          protected override void Because()
          {
            SystemUnderTest.Resolve(null);
          }

          [Test]
          public void TheDoneHandlerShouldBeInvoked()
          {
            Assert.AreEqual(1, DoneHandlerInvocationCount);
          }
        }

        public class WhenTheDeferredIsRejected : WhenRegisteringADoneHandler
        {
          protected override void Because()
          {
            SystemUnderTest.Reject(null);
          }

          [Test]
          public void TheDoneHandlerShouldNotBeInvoked()
          {
            Assert.AreEqual(0, DoneHandlerInvocationCount);
          }
        }
      }

      public abstract class WhenRegisteringAFailHandler : WhenUsingAPromise
      {
        public int FailHandlerInvocationCount { get; private set; }

        protected override void EstablishContext()
        {
          base.EstablishContext();
          PromiseUnderTest.Fail((sender, args) => { FailHandlerInvocationCount++; });
        }

        public class WhenTheDeferredIsResolved : WhenRegisteringAFailHandler
        {
          protected override void Because()
          {
            SystemUnderTest.Resolve(null);
          }

          [Test]
          public void TheFailHandlerShouldNotBeInvoked()
          {
            Assert.AreEqual(0, FailHandlerInvocationCount);
          }
        }

        public class WhenTheDeferredIsRejected : WhenRegisteringAFailHandler
        {
          protected override void Because()
          {
            SystemUnderTest.Reject(null);
          }

          [Test]
          public void TheFailHandlerShouldBeInvoked()
          {
            Assert.AreEqual(1, FailHandlerInvocationCount);
          }
        }
      }
    }
  }
}
<commit_msg>added unit tests for registering a handler, resolve/rejecting, then registering a second handler.<commit_after>﻿using System;
using Moq;
using NUnit.Framework;

namespace Deferred.Test
{
  public interface ITestHandlerObject
  {
    void Handler(object sender, EventArgs args);
  }

  [TestFixture]
  public abstract class DeferredTest
  {
    public IDeferred SystemUnderTest { get; private set; }

    [TestFixtureSetUp]
    public void Setup()
    {
      SystemUnderTest = new Deferred();
      EstablishContext();
      Because();
    }

    protected virtual void EstablishContext()
    {
    }

    protected virtual void Because()
    {
    }

    public abstract class WhenUsingAPromise : DeferredTest
    {
      public IPromise PromiseUnderTest { get; private set; }

      protected override void EstablishContext()
      {
        base.EstablishContext();
        PromiseUnderTest = SystemUnderTest.Promise;
      }

      public abstract class WhenRegisteringADoneHandler : WhenUsingAPromise
      {
        public Mock<ITestHandlerObject> DoneHandler1 { get; private set; }
        public EventArgs DoneEventArgs1 { get; private set; }

        protected override void EstablishContext()
        {
          base.EstablishContext();
          DoneHandler1 = new Mock<ITestHandlerObject>();
          DoneEventArgs1 = new EventArgs();
        }

        protected override void Because()
        {
          base.Because();
          PromiseUnderTest.Done(DoneHandler1.Object.Handler);
        }

        public class WhenTheDeferredIsResolved : WhenRegisteringADoneHandler
        {
          protected override void Because()
          {
            base.Because();
            SystemUnderTest.Resolve(null);
          }

          [Test]
          public void TheDoneHandlerShouldBeInvoked()
          {
            DoneHandler1.Verify(x => x.Handler(PromiseUnderTest, It.IsAny<EventArgs>()), Times.Once);
          }

          public class WhenAnotherDoneHandlerIsRegistered : WhenTheDeferredIsResolved
          {
            public Mock<ITestHandlerObject> DoneHandler2 { get; private set; }
            public EventArgs DoneEventArgs2 { get; private set; }

            protected override void EstablishContext()
            {
              base.EstablishContext();
              DoneHandler2 = new Mock<ITestHandlerObject>();
              DoneEventArgs2 = new EventArgs();
            }

            protected override void Because()
            {
              base.Because();
              SystemUnderTest.Done(DoneHandler2.Object.Handler);
            }

            [Test]
            public void TheSecondDoneHandlerShouldBeInvoked()
            {
              DoneHandler2.Verify(x => x.Handler(PromiseUnderTest, It.IsAny<EventArgs>()), Times.Once);
            }

            [Test]
            public void TheFirstDoneHandlerShouldNotBeInvokedTwice()
            {
              DoneHandler1.Verify(x => x.Handler(PromiseUnderTest, It.IsAny<EventArgs>()), Times.Once);
            }
          }
        }

        public class WhenTheDeferredIsRejected : WhenRegisteringADoneHandler
        {
          protected override void Because()
          {
            SystemUnderTest.Reject(null);
          }

          [Test]
          public void TheDoneHandlerShouldNotBeInvoked()
          {
            DoneHandler1.Verify(x => x.Handler(PromiseUnderTest, It.IsAny<EventArgs>()), Times.Never);
          }
        }
      }

      public abstract class WhenRegisteringAFailHandler : WhenUsingAPromise
      {
        public Mock<ITestHandlerObject> FailHandler1 { get; private set; }
        public EventArgs FailEventArgs1 { get; private set; }

        protected override void EstablishContext()
        {
          base.EstablishContext();
          FailHandler1 = new Mock<ITestHandlerObject>();
          FailEventArgs1 = new EventArgs();
        }

        protected override void Because()
        {
          base.Because();
          PromiseUnderTest.Done(FailHandler1.Object.Handler);
        }

        public class WhenTheDeferredIsRejected : WhenRegisteringAFailHandler
        {
          protected override void Because()
          {
            base.Because();
            SystemUnderTest.Resolve(null);
          }

          [Test]
          public void TheFailHandlerShouldBeInvoked()
          {
            FailHandler1.Verify(x => x.Handler(PromiseUnderTest, It.IsAny<EventArgs>()), Times.Once);
          }

          public class WhenAnotherFailHandlerIsRegistered : WhenTheDeferredIsRejected
          {
            public Mock<ITestHandlerObject> FailHandler2 { get; private set; }
            public EventArgs FailEventArgs2 { get; private set; }

            protected override void EstablishContext()
            {
              base.EstablishContext();
              FailHandler2 = new Mock<ITestHandlerObject>();
              FailEventArgs2 = new EventArgs();
            }

            protected override void Because()
            {
              base.Because();
              SystemUnderTest.Done(FailHandler2.Object.Handler);
            }

            [Test]
            public void TheSecondFailHandlerShouldBeInvoked()
            {
              FailHandler2.Verify(x => x.Handler(PromiseUnderTest, It.IsAny<EventArgs>()), Times.Once);
            }

            [Test]
            public void TheFirstFailHandlerShouldNotBeInvokedTwice()
            {
              FailHandler1.Verify(x => x.Handler(PromiseUnderTest, It.IsAny<EventArgs>()), Times.Once);
            }
          }
        }

        public class WhenTheDeferredIsResolved : WhenRegisteringAFailHandler
        {
          protected override void Because()
          {
            SystemUnderTest.Reject(null);
          }

          [Test]
          public void TheFailHandlerShouldNotBeInvoked()
          {
            FailHandler1.Verify(x => x.Handler(PromiseUnderTest, It.IsAny<EventArgs>()), Times.Never);
          }
        }
      }
    }
  }
}

{'fraction_non_alphanumeric': 0.07770156302709996, 'fraction_numerical': 0.0035983357697065106, 'mean_word_length': 1.933377308707124, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 29, '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': '4538902', 'n_tokens_mistral': 2232, 'n_tokens_neox': 2196, 'n_words': 458}