<commit_before>﻿using Eventing.Core.Domain;
using Eventing.Core.Persistence;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Eventing.TestHelpers
{
    public class TestableEventSourcedService<T>
        : IGivenReady<T>, IWhenReady<T>, IThenReady, IAndThenReady where T : class
    {
        private readonly T service;
        private readonly TestableRepository repository = new TestableRepository();

        public TestableEventSourcedService(Func<IEventSourcedRepository, T> serviceFactory)
        {
            Ensure.NotNull(serviceFactory, nameof(serviceFactory));

            this.service = serviceFactory.Invoke(this.repository);
        }

        public IWhenReady<T> Given(string streamName, params object[] @events)
        {
            this.repository.Preload(streamName, @events);
            return this;
        }

        public IWhenReady<T> Given<TState>(string streamId, params object[] @events)
        {
            var streamName = StreamCategoryAttribute.GetFullStreamName<TState>(streamId);
            this.Given(streamId, @events);
            return this;
        }

        public IThenReady When(Action<T> handling)
        {
            handling.Invoke(this.service);
            return this;
        }

        public IAndThenReady Then(Action<ICollection<object>> assert)
        {
            assert.Invoke(this.repository.LastCommited);
            return this;
        }<commit_msg>Fixed small issue in TestableEventSourcedService, with the Given<T><commit_after>﻿using Eventing.Core.Domain;
using Eventing.Core.Persistence;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Eventing.TestHelpers
{
    public class TestableEventSourcedService<T>
        : IGivenReady<T>, IWhenReady<T>, IThenReady, IAndThenReady where T : class
    {
        private readonly T service;
        private readonly TestableRepository repository = new TestableRepository();

        public TestableEventSourcedService(Func<IEventSourcedRepository, T> serviceFactory)
        {
            Ensure.NotNull(serviceFactory, nameof(serviceFactory));

            this.service = serviceFactory.Invoke(this.repository);
        }

        public IWhenReady<T> Given(string streamName, params object[] @events)
        {
            this.repository.Preload(streamName, @events);
            return this;
        }

        public IWhenReady<T> Given<TState>(string streamId, params object[] @events)
        {
            var streamName = StreamCategoryAttribute.GetFullStreamName<TState>(streamId);
            this.Given(streamName, @events);
            return this;
        }

        public IThenReady When(Action<T> handling)
        {
            handling.Invoke(this.service);
            return this;
        }

        public IAndThenReady Then(Action<ICollection<object>> assert)
        {
            assert.Invoke(this.repository.LastCommited);
            return this;
        }
{'fraction_non_alphanumeric': 0.08697089947089948, 'fraction_numerical': 0.0, 'mean_word_length': 2.644578313253012, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, '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': '7554804', 'n_tokens_mistral': 797, 'n_tokens_neox': 784, 'n_words': 169}