<commit_before>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using NUnit.Framework;

namespace EventSorcerer.Tests.Unit
{
    // ReSharper disable InconsistentNaming
    [TestFixture]
    public class AggregateTests
    {
        [SetUp]
        public void SetUp()
        {
        }

        [TearDown]
        public void TearDown()
        {
        }

        [Test]
        public void InvokingBehaviour_GivenSimpleAggregateRoot_ShouldRecordEvents()
        {
            // Arrange
            var user = new User();
            user.Id = Guid.NewGuid().ToString();

            // Act
            user.ChangePassword("<PASSWORD>");
            IEnumerable<Event> actual = user.GetUncommittedEvents();
            IEnumerable<Event> expected = new Event[] { new UserChangedPassword(user.Id, "<PASSWORD>") };
            
            // Assert
            CollectionAssert.AreEqual(expected, actual);
        }
    }

    // ReSharper restore InconsistentNaming

    

    public class UserChangedPassword : Event
    {
        public string UserId { get; private set; }
        public string Newpassword { get; private set; }

        public UserChangedPassword(string userId, string newpassword)
        {
            if (userId == null) throw new ArgumentNullException("userId");
            if (newpassword == null) throw new ArgumentNullException("newpassword");

            UserId = userId;
            Newpassword = <PASSWORD>;
        }

        public bool Equals(UserChangedPassword other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.UserId, UserId) && Equals(other.Newpassword, Newpassword);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(UserChangedPassword)) return false;
            return Equals((UserChangedPassword)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((UserId != null ? UserId.GetHashCode() : 0) * 397) ^ (Newpassword != null ? Newpassword.GetHashCode() : 0);
            }
        }

        public static bool operator ==(UserChangedPassword left, UserChangedPassword right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(UserChangedPassword left, UserChangedPassword right)
        {
            return !Equals(left, right);
        }
    }

    public abstract class Event : Message
    {
    }

    public abstract class Message
    {
    }

    public class User : AggregateRoot
    {
        public void ChangePassword(string newpassword)
        {
            if (string.IsNullOrWhiteSpace(newpassword)) throw new InvalidOperationException("New password cannot be empty or whitespace.");
         
            Record(new UserChangedPassword(Id, newpassword));
        }
    }

    public abstract class AggregateRoot
    {
        private readonly IList<Event> _uncommittedEvents = new List<Event>(); 

        public string Id { get; set; }
        
        protected void Record(Event evt)
        {
            _uncommittedEvents.Add(evt);
        }

        public IEnumerable<Event> GetUncommittedEvents()
        {
            return _uncommittedEvents;
        }
    }
}<commit_msg>Unit test added for accepting uncommitted events on an aggregate<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using NUnit.Framework;

namespace EventSorcerer.Tests.Unit
{
    // ReSharper disable InconsistentNaming
    [TestFixture]
    public class AggregateTests
    {
        [SetUp]
        public void SetUp()
        {
        }

        [TearDown]
        public void TearDown()
        {
        }

        [Test]
        public void InvokingBehaviour_GivenSimpleAggregateRoot_ShouldRecordEvents()
        {
            // Arrange
            var user = new User();
            user.Id = Guid.NewGuid().ToString();

            // Act
            user.ChangePassword("<PASSWORD>");
            IEnumerable<Event> actual = user.GetUncommittedEvents();
            IEnumerable<Event> expected = new Event[] { new UserChangedPassword(user.Id, "<PASSWORD>") };
            
            // Assert
            CollectionAssert.AreEqual(expected, actual);
        }

        [Test]
        public void AcceptingEvents_GivenAggregateWithUncommittedEvents_ShouldClearUncommittedEvents()
        {
            // Arrange
            var user = new User();
            user.Id = Guid.NewGuid().ToString();
            user.ChangePassword("<PASSWORD>");

            // Act
            IEnumerable<Event> before = user.GetUncommittedEvents();
            IEnumerable<Event> expectedBefore = new Event[] { new UserChangedPassword(user.Id, "<PASSWORD>") };
            user.AcceptUncommittedEvents();
            IEnumerable<Event> after = user.GetUncommittedEvents();
            IEnumerable<Event> expectedAfter = new Event[0];

            // Assert
            CollectionAssert.AreEqual(expectedBefore, before);
            CollectionAssert.AreEqual(expectedAfter, after);
        }
    }

    // ReSharper restore InconsistentNaming

    

    public class UserChangedPassword : Event
    {
        public string UserId { get; private set; }
        public string Newpassword { get; private set; }

        public UserChangedPassword(string userId, string newpassword)
        {
            if (userId == null) throw new ArgumentNullException("userId");
            if (newpassword == null) throw new ArgumentNullException("newpassword");

            UserId = userId;
            Newpassword = newpassword;
        }

        public bool Equals(UserChangedPassword other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.UserId, UserId) && Equals(other.Newpassword, Newpassword);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(UserChangedPassword)) return false;
            return Equals((UserChangedPassword)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((UserId != null ? UserId.GetHashCode() : 0) * 397) ^ (Newpassword != null ? Newpassword.GetHashCode() : 0);
            }
        }

        public static bool operator ==(UserChangedPassword left, UserChangedPassword right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(UserChangedPassword left, UserChangedPassword right)
        {
            return !Equals(left, right);
        }
    }

    public abstract class Event : Message
    {
    }

    public abstract class Message
    {
    }

    public class User : AggregateRoot
    {
        public void ChangePassword(string newpassword)
        {
            if (string.IsNullOrWhiteSpace(newpassword)) throw new InvalidOperationException("New password cannot be empty or whitespace.");
         
            Record(new UserChangedPassword(Id, newpassword));
        }
    }

    public abstract class AggregateRoot
    {
        private IList<Event> _uncommittedEvents = new List<Event>(); 

        public string Id { get; set; }
        
        protected void Record(Event evt)
        {
            _uncommittedEvents.Add(evt);
        }

        public IEnumerable<Event> GetUncommittedEvents()
        {
            return _uncommittedEvents;
        }

        public void AcceptUncommittedEvents()
        {
            _uncommittedEvents = new List<Event>();
        }
    }
}
{'fraction_non_alphanumeric': 0.08804483188044832, 'fraction_numerical': 0.0013698630136986301, 'mean_word_length': 1.997760358342665, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '5551832', 'n_tokens_mistral': 2048, 'n_tokens_neox': 2002, 'n_words': 506}