Ejemplo n.º 1
0
        public void CanAccessFilteredParameters()
        {
            var param      = 1;
            var collection =
                new ParameterCollection(
                    new object[] { 10, 20, 30, 40, 50 },
                    StaticReflection.GetMethodInfo(() => TestMethod(null, null, null, null, null)).GetParameters(),
                    pi => param++ % 2 == 1);

            Assert.AreEqual(3, collection.Count);
            CollectionAssertExtensions.AreEqual(
                new[] { 10, 30, 50 },
                collection);
            CollectionAssertExtensions.AreEqual(
                new[] { 50, 30, 10 },
                new[] { 2, 1, 0 }.Select(i => collection[i]).ToArray());
            CollectionAssertExtensions.AreEqual(
                new[] { 50, 30, 10 },
                new[] { "param5", "param3", "param1" }.Select(i => collection[i]).ToArray());
            CollectionAssertExtensions.AreEqual(
                new[] { "param1", "param3", "param5" },
                Enumerable.Range(0, 3).Select(i => collection.ParameterName(i)).ToArray());
            CollectionAssertExtensions.AreEqual(
                new[] { "param1", "param3", "param5" },
                Enumerable.Range(0, 3).Select(i => collection.GetParameterInfo(i).Name).ToArray());
        }
        private static void AssertOrder(IStrategyChain chain,
                                        params FakeStrategy[] strategies)
        {
            List <IBuilderStrategy> strategiesInChain = new List <IBuilderStrategy>(chain);

            CollectionAssertExtensions.AreEqual(strategies, strategiesInChain);
        }
Ejemplo n.º 3
0
        public void ContainerReturnsEmptyArrayIfNoObjectsRegistered()
        {
            IUnityContainer container = new UnityContainer();
            List <object>   results   = new List <object>(container.ResolveAll <object>());

            Assert.IsNotNull(results);
            CollectionAssertExtensions.AreEqual(new object[0], results);
        }
        public void Then_ArrayPropertyValuesHaveExpectedNames()
        {
            var prop       = this.GetArrayPropertyElement("specificElements");
            var arrayValue = (ArrayElement)prop.Value;

            CollectionAssertExtensions.AreEqual(new[] { "main", "special" },
                                                arrayValue.Values.Cast <DependencyElement>().Select(e => e.Name).ToList());
        }
Ejemplo n.º 5
0
        public void Then_PropertyNamesAreProperlyDeserialized()
        {
            var registration = (from reg in section.Containers.Default.Registrations
                                where reg.TypeName == "ObjectWithTwoProperties" && reg.Name == "twoProperties"
                                select reg).First();

            CollectionAssertExtensions.AreEqual(new string[] { "Obj1", "Obj2" },
                                                registration.InjectionMembers.OfType <PropertyElement>().Select(pe => pe.Name).ToList());
        }
Ejemplo n.º 6
0
        public void ContainerAutomaticallyResolvesAllWhenInjectingArrays()
        {
            ILogger[]       expected  = new ILogger[] { new MockLogger(), new SpecialLogger() };
            IUnityContainer container = new UnityContainer()
                                        .RegisterInstance("one", expected[0])
                                        .RegisterInstance("two", expected[1]);

            TypeWithArrayConstructorParameter result = container.Resolve <TypeWithArrayConstructorParameter>();

            CollectionAssertExtensions.AreEqual(expected, result.Loggers);
        }
        public void Then_FirstRegistrationHasOneMethodInjection()
        {
            var registration = (from reg in section.Containers.Default.Registrations
                                where reg.TypeName == "ObjectWithInjectionMethod" && reg.Name == "singleMethod"
                                select reg).First();

            Assert.AreEqual(1, registration.InjectionMembers.Count);
            var methodRegistration = (MethodElement)registration.InjectionMembers[0];

            Assert.AreEqual("Initialize", methodRegistration.Name);
            CollectionAssertExtensions.AreEqual(new string[] { "connectionString", "logger" },
                                                methodRegistration.Parameters.Select(p => p.Name).ToList());
        }
Ejemplo n.º 8
0
        public void ResolveAllReturnsRegisteredObjects()
        {
            IUnityContainer container = new UnityContainer();
            object          o1        = new object();
            object          o2        = new object();

            container
            .RegisterInstance <object>("o1", o1)
            .RegisterInstance <object>("o2", o2);

            List <object> results = new List <object>(container.ResolveAll <object>());

            CollectionAssertExtensions.AreEqual(new object[] { o1, o2 }, results);
        }
Ejemplo n.º 9
0
        public void ResolveAllReturnsRegisteredObjectsForBaseClass()
        {
            IUnityContainer container = new UnityContainer();
            ILogger         o1        = new MockLogger();
            ILogger         o2        = new SpecialLogger();

            container
            .RegisterInstance <ILogger>("o1", o1)
            .RegisterInstance <ILogger>("o2", o2);

            List <ILogger> results = new List <ILogger>(container.ResolveAll <ILogger>());

            CollectionAssertExtensions.AreEqual(new ILogger[] { o1, o2 }, results);
        }
Ejemplo n.º 10
0
        public void FilteredCollectionReturnsRightParameterByName()
        {
            object dummy;
            object dummy2;
            var    inputsCollection =
                new ParameterCollection(new object[] { "one", "two", "three", "four" },
                                        StaticReflection.GetMethodInfo(() => MethodWithOuts(out dummy, null, out dummy2, null)).GetParameters(),
                                        pi => !pi.IsOut);

            Assert.AreEqual(2, inputsCollection.Count);
            CollectionAssertExtensions.AreEqual(new object[] { "two", "four" }, inputsCollection);
            Assert.AreEqual("two", inputsCollection["param2"]);
            Assert.AreEqual("four", inputsCollection["param4"]);
        }
Ejemplo n.º 11
0
        public void ShouldGetHandlersInOrderWithGetHandlersFor()
        {
            IMatchingRule[] rules     = { new MemberNameMatchingRule("ShouldGetHandlersInOrderWithGetHandlersFor") };
            IUnityContainer container = CreateConfiguredContainer();

            InjectionPolicy p = CreatePolicy(container, rules);

            MethodImplementationInfo member = GetMethodImplInfo <PolicyFixture>("ShouldGetHandlersInOrderWithGetHandlersFor");

            List <ICallHandler> expectedHandlers = new List <ICallHandler>(container.ResolveAll <ICallHandler>());
            List <ICallHandler> actualHandlers   = new List <ICallHandler>(p.GetHandlersFor(member, container));

            CollectionAssertExtensions.AreEqual(
                expectedHandlers,
                actualHandlers,
                new TypeComparer());
        }
Ejemplo n.º 12
0
        public void ShouldBeAbleToMatchPropertyGet()
        {
            IMatchingRule[] rules     = { new MemberNameMatchingRule("get_Balance") };
            IUnityContainer container = CreateConfiguredContainer();

            InjectionPolicy p = CreatePolicy(container, rules);

            PropertyInfo             balanceProperty = typeof(MockDal).GetProperty("Balance");
            MethodImplementationInfo getMethod       = new MethodImplementationInfo(null, balanceProperty.GetGetMethod());

            List <ICallHandler> expectedHandlers = new List <ICallHandler>(container.ResolveAll <ICallHandler>());
            List <ICallHandler> actualHandlers   = new List <ICallHandler>(p.GetHandlersFor(getMethod, container));

            CollectionAssertExtensions.AreEqual(
                expectedHandlers,
                actualHandlers,
                new TypeComparer());
        }
        public void Then_InstancesHaveExpectedContents()
        {
            var expected = new[]
            {
                // Name, InjectionParameterValue, Type, TypeConverter
                new[] { String.Empty, "AdventureWorks", String.Empty, String.Empty },
                new[] { String.Empty, "42", "System.Int32", String.Empty },
                new[] { "negated", "23", "int", "negator" },
                new[] { "forward", "23", "int", String.Empty }
            };

            for (int index = 0; index < expected.Length; ++index)
            {
                var instance = section.Containers.Default.Instances[index];
                CollectionAssertExtensions.AreEqual(expected[index],
                                                    new string[] { instance.Name, instance.Value, instance.TypeName, instance.TypeConverterTypeName },
                                                    string.Format("Element at index {0} does not match", index));
            }
        }
        public void CanFindClosedConstructorFromOpenConstructorInfo()
        {
            Type openType   = typeof(Pathological <,>);
            Type closedType = typeof(Pathological <User, Account>);

            ConstructorInfo openCtor = openType.GetTypeInfo().DeclaredConstructors.ElementAt(0);

            Assert.AreSame(openCtor.DeclaringType, openType);
            Type createdClosedType = openType.MakeGenericType(closedType.GenericTypeArguments);

            // Go through the parameter list of the open constructor and fill in the
            // type arguments for generic parameters based on the arguments used to
            // create the closed types.

            Type[] closedTypeParams = closedType.GenericTypeArguments;

            List <Type> closedCtorParamTypes = new List <Type>();

            List <int> parameterPositions = new List <int>();

            foreach (ParameterInfo openParam in openCtor.GetParameters())
            {
                closedCtorParamTypes.Add(ClosedTypeFromOpenParameter(openParam, closedTypeParams));

                Type[] genericParameters = openParam.ParameterType.GenericTypeArguments;
                foreach (Type gp in genericParameters)
                {
                    parameterPositions.Add(gp.GenericParameterPosition);
                }
            }

            CollectionAssertExtensions.AreEqual(new int[] { 1, 0 }, parameterPositions);

            ConstructorInfo targetCtor = closedType.GetMatchingConstructor(closedCtorParamTypes.ToArray());

            Assert.AreSame(closedType, createdClosedType);

            ConstructorInfo closedCtor =
                closedType.GetMatchingConstructor(Types(typeof(ICommand <Account>), typeof(ICommand <User>)));

            Assert.AreSame(closedCtor, targetCtor);
        }
 private void AssertRegistrationsAreSame(Func <RegisterElement, string> selector, params string[] expectedStrings)
 {
     CollectionAssertExtensions.AreEqual(expectedStrings, this.container.Registrations.Select(selector).ToList());
 }
Ejemplo n.º 16
0
 public void Then_EnumeratingContainersHappensInOrderOfConfigFile()
 {
     CollectionAssertExtensions.AreEqual(new[] { "one", "two" },
                                         section.Containers.Select(c => c.Name).ToList());
 }
 public void Then_ContainersArePresentInFileOrder()
 {
     CollectionAssertExtensions.AreEqual(new[] { String.Empty, "two" },
                                         section.Containers.Select(c => c.Name).ToList());
 }
Ejemplo n.º 18
0
 public void Then_AliasesAreAvailableInExpectedOrder()
 {
     CollectionAssertExtensions.AreEqual(
         new[] { "string", "int", "ILogger", "MockLogger", "SpecialLogger", "DependentConstructor", "TwoConstructorArgs", "MockDatabase" },
         section.TypeAliases.Select(a => a.Alias).ToList());
 }
 public void Then_EnumerationReturnsAliasesInOrderAsGivenInFile()
 {
     CollectionAssertExtensions.AreEqual(new[] { "int", "string" },
                                         section.TypeAliases.Select(alias => alias.Alias).ToList());
 }
        public static void PerformTest(IInterceptingProxy proxy)
        {
            var behavior = new FakeInterceptionBehavior();

            int argumentsCount = 0;

            object[] argumentsValuesByIndex = null;
            string[] argumentsNames         = null;
            object[] argumentsValuesByName  = null;
            int      inputsCount            = 0;

            object[] inputsValuesByIndex = null;
            string[] inputsNames         = null;
            object[] inputsValuesByName  = null;
            int      outputsCount        = 0;

            object[] outputsValuesByIndex = null;
            string[] outputsNames         = null;
            object[] outputsValuesByName  = null;
            object   originalReturnValue  = null;

            behavior.InvokeFunc = (IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) =>
            {
                argumentsCount         = input.Arguments.Count;
                argumentsValuesByIndex = Enumerable.Range(0, argumentsCount).Select(pi => input.Arguments[pi]).ToArray();
                argumentsNames         = Enumerable.Range(0, argumentsCount).Select(pi => input.Arguments.GetParameterInfo(pi).Name).ToArray();
                argumentsValuesByName  = argumentsNames.Select(an => input.Arguments[an]).ToArray();

                inputsCount         = input.Inputs.Count;
                inputsValuesByIndex = Enumerable.Range(0, inputsCount).Select(pi => input.Inputs[pi]).ToArray();
                inputsNames         = Enumerable.Range(0, inputsCount).Select(pi => input.Inputs.GetParameterInfo(pi).Name).ToArray();
                inputsValuesByName  = inputsNames.Select(an => input.Inputs[an]).ToArray();

                input.Inputs["param1"] = 11;
                input.Inputs[1]        = 13;
                input.Inputs["param4"] = 14;
                input.Inputs[3]        = 15;

                var result = getNext()(input, getNext);

                outputsCount         = result.Outputs.Count;
                outputsValuesByIndex = Enumerable.Range(0, outputsCount).Select(pi => result.Outputs[pi]).ToArray();
                outputsNames         = Enumerable.Range(0, outputsCount).Select(pi => result.Outputs.GetParameterInfo(pi).Name).ToArray();
                outputsValuesByName  = outputsNames.Select(an => result.Outputs[an]).ToArray();

                originalReturnValue = result.ReturnValue;

                result.Outputs[0]        = 82;
                result.Outputs["param4"] = 84;

                result.ReturnValue = 100;

                return(result);
            };

            proxy.AddInterceptionBehavior(behavior);

            int param2, param4;

            param4 = 4;
            var returnValue = ((ITypeWithAssertedParameterKinds)proxy).DoSomething(1, out param2, 3, ref param4, 5);

            Assert.AreEqual(100, returnValue);
            Assert.AreEqual(82, param2);
            Assert.AreEqual(84, param4);

            Assert.AreEqual(5, argumentsCount);
            CollectionAssertExtensions.AreEqual(new[] { 1, 0, 3, 4, 5 }, argumentsValuesByIndex);
            CollectionAssertExtensions.AreEqual(new[] { "param1", "param2", "param3", "param4", "param5" }, argumentsNames);
            CollectionAssertExtensions.AreEqual(new[] { 1, 0, 3, 4, 5 }, argumentsValuesByName);

            Assert.AreEqual(4, inputsCount);
            CollectionAssertExtensions.AreEqual(new[] { 1, 3, 4, 5 }, inputsValuesByIndex);
            CollectionAssertExtensions.AreEqual(new[] { "param1", "param3", "param4", "param5" }, inputsNames);
            CollectionAssertExtensions.AreEqual(new[] { 1, 3, 4, 5 }, inputsValuesByName);

            Assert.AreEqual(2, outputsCount);
            CollectionAssertExtensions.AreEqual(new[] { 25, 39 }, outputsValuesByIndex);
            CollectionAssertExtensions.AreEqual(new[] { "param2", "param4" }, outputsNames);
            CollectionAssertExtensions.AreEqual(new[] { 25, 39 }, outputsValuesByName);

            Assert.AreEqual(11 + 25 + 13 + 39 + 15, originalReturnValue);
        }