<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using TestFirst.Net.Extensions.Moq;
using TestFirst.Net.Matcher;
using DivineInject;

namespace DivineInject.Test
{
    [TestFixture]
    public class InstantiatorTest : AbstractNUnitMoqScenarioTest
    {
        [Test]
        public void CreatesInstanceUsingNoArgConstructor()
        {
            Instantiator instantiator;
            object instance;

            Scenario()
                .Given(instantiator = new Instantiator(AMock<IDivineInjector>().Instance))

                .When(instance = instantiator.Create<TestClassWithNoArgConstructor>())

                .Then((TestClassWithNoArgConstructor) instance, Is(AnInstance.NotNull<TestClassWithNoArgConstructor>()))
            ;
        }

        [Test]
        public void CreatesInstanceResolvingConstructorParamsViaInjector()
        {
            Instantiator instantiator;
            TestClassWithConstructorWithDependency instance;
            ITestDependency dependency;
            IDivineInjector injector;

            Scenario()
                .Given(dependency = AMock<ITestDependency>().Instance)
                .Given(injector = AMock<IDivineInjector>()
                    .WhereMethod(i => i.Get(typeof(ITestDependency))).Returns(dependency)
                    .Instance)
                .Given(instantiator = new Instantiator(injector))

                .When(instance = instantiator.Create<TestClassWithConstructorWithDependency>())

                .Then(instance, Is(AnInstance.NotNull<TestClassWithConstructorWithDependency>()))
                .Then(instance.Dependency, Is(AnInstance.SameAs(dependency)))
            ;
        }

        [Test]
        public void CreatesInstanceUsingConstructorWithFewestNumberOfParameters()
        {
            Instantiator instantiator;
            TestClassWithTwoConstructorsWithDependencies instance;
            ITestDependency dependency;
            IDivineInjector injector;

            Scenario()
                .Given(dependency = AMock<ITestDependency>().Instance)
                .Given(injector = AMock<IDivineInjector>()
                    .WhereMethod(i => i.Get(typeof(ITestDependency))).Returns(dependency)
                    .Instance)
                .Given(instantiator = new Instantiator(injector))

                .When(instance = instantiator.Create<TestClassWithTwoConstructorsWithDependencies>())

                .Then(instance, Is(AnInstance.NotNull<TestClassWithTwoConstructorsWithDependencies>()))
                .Then(instance.Dependency, Is(AnInstance.SameAs(dependency)))
            ;
        }
    }

    public interface ITestDependency
    { }

    public interface ITestSecondDependency
    { }

    public class TestClassWithNoArgConstructor
    { }

    public class TestClassWithConstructorWithDependency
    {
        public TestClassWithConstructorWithDependency(ITestDependency dependency)
        {
            Dependency = dependency;
        }

        public ITestDependency Dependency { get; private set; }
    }

    public class TestClassWithTwoConstructorsWithDependencies
    {
        public TestClassWithTwoConstructorsWithDependencies(ITestDependency dependency, ITestSecondDependency secondDependency)
        {
            Dependency = dependency;
            SecondDependency = secondDependency;
        }

        public TestClassWithTwoConstructorsWithDependencies(ITestDependency dependency)
        {
            Dependency = dependency;
        }

        public ITestDependency Dependency { get; private set; }
        public ITestSecondDependency SecondDependency { get; private set; }
    }
}<commit_msg>Added (ignored) test that for omitting unusable constructors<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using TestFirst.Net.Extensions.Moq;
using TestFirst.Net.Matcher;
using DivineInject;

namespace DivineInject.Test
{
    [TestFixture]
    public class InstantiatorTest : AbstractNUnitMoqScenarioTest
    {
        [Test]
        public void CreatesInstanceUsingNoArgConstructor()
        {
            Instantiator instantiator;
            object instance;

            Scenario()
                .Given(instantiator = new Instantiator(AMock<IDivineInjector>().Instance))

                .When(instance = instantiator.Create<TestClassWithNoArgConstructor>())

                .Then((TestClassWithNoArgConstructor) instance, Is(AnInstance.NotNull<TestClassWithNoArgConstructor>()))
            ;
        }

        [Test]
        public void CreatesInstanceResolvingConstructorParamsViaInjector()
        {
            Instantiator instantiator;
            TestClassWithConstructorWithDependency instance;
            ITestDependency dependency;
            IDivineInjector injector;

            Scenario()
                .Given(dependency = AMock<ITestDependency>().Instance)
                .Given(injector = AMock<IDivineInjector>()
                    .WhereMethod(i => i.Get(typeof(ITestDependency))).Returns(dependency)
                    .Instance)
                .Given(instantiator = new Instantiator(injector))

                .When(instance = instantiator.Create<TestClassWithConstructorWithDependency>())

                .Then(instance, Is(AnInstance.NotNull<TestClassWithConstructorWithDependency>()))
                .Then(instance.Dependency, Is(AnInstance.SameAs(dependency)))
            ;
        }

        [Test]
        public void CreatesInstanceUsingConstructorWithFewestNumberOfParameters()
        {
            Instantiator instantiator;
            TestClassWithTwoConstructorsWithDependencies instance;
            ITestDependency dependency;
            IDivineInjector injector;

            Scenario()
                .Given(dependency = AMock<ITestDependency>().Instance)
                .Given(injector = AMock<IDivineInjector>()
                    .WhereMethod(i => i.Get(typeof(ITestDependency))).Returns(dependency)
                    .Instance)
                .Given(instantiator = new Instantiator(injector))

                .When(instance = instantiator.Create<TestClassWithTwoConstructorsWithDependencies>())

                .Then(instance, Is(AnInstance.NotNull<TestClassWithTwoConstructorsWithDependencies>()))
                .Then(instance.Dependency, Is(AnInstance.SameAs(dependency)))
            ;
        }

        [Test]
        [Ignore("wip")]
        public void CreatesInstanceUsingFirstConstructorThatCanBeInjected()
        {
            Instantiator instantiator;
            TestClassWithAnUninjectableConstructor instance;
            ITestDependency dependency;
            IDivineInjector injector;

            Scenario()
                .Given(dependency = AMock<ITestDependency>().Instance)
                .Given(injector = AMock<IDivineInjector>()
                    .WhereMethod(i => i.Get(typeof(ITestDependency))).Returns(dependency)
                    .Instance)
                .Given(instantiator = new Instantiator(injector))

                .When(instance = instantiator.Create<TestClassWithAnUninjectableConstructor>())

                .Then(instance, Is(AnInstance.NotNull<TestClassWithAnUninjectableConstructor>()))
                .Then(instance.Dependency, Is(AnInstance.SameAs(dependency)))
            ;
        }
    }

    public interface ITestDependency
    { }

    public interface ITestSecondDependency
    { }

    public class TestClassWithNoArgConstructor
    { }

    public class TestClassWithConstructorWithDependency
    {
        public TestClassWithConstructorWithDependency(ITestDependency dependency)
        {
            Dependency = dependency;
        }

        public ITestDependency Dependency { get; private set; }
    }

    public class TestClassWithTwoConstructorsWithDependencies
    {
        public TestClassWithTwoConstructorsWithDependencies(ITestDependency dependency, ITestSecondDependency secondDependency)
        {
            Dependency = dependency;
            SecondDependency = secondDependency;
        }

        public TestClassWithTwoConstructorsWithDependencies(ITestDependency dependency)
        {
            Dependency = dependency;
        }

        public ITestDependency Dependency { get; private set; }
        public ITestSecondDependency SecondDependency { get; private set; }
    }

    public class TestClassWithAnUninjectableConstructor
    {
        public TestClassWithAnUninjectableConstructor(string name)
        {
        }

        public TestClassWithAnUninjectableConstructor(ITestDependency dependency)
        {
            Dependency = dependency;
        }

        public ITestDependency Dependency { get; private set; }
    }
}
{'fraction_non_alphanumeric': 0.07478319630589031, 'fraction_numerical': 0.0, 'mean_word_length': 2.4180138568129332, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 34, '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': '1440862', 'n_tokens_mistral': 2240, 'n_tokens_neox': 2065, 'n_words': 358}