Esempio n. 1
0
        public override void PrepareBasic()
        {
            var targets = new TargetContainer();

            RegisterBasic(targets);
            this.container = new Container(targets);
        }
Esempio n. 2
0
        public void Contravariant_ShouldDisableForOpenGeneric()
        {
            // Arrange
            var targets = new TargetContainer();

            // test shows that contravariance still works for Action<> but is disabled for all IContravariant<>
            targets.SetOption <Options.EnableContravariance>(false, typeof(IContravariant <>));

            // Act & Assert
            Assert.False(targets.GetOption(typeof(IContravariant <>), Options.EnableContravariance.Default).Value);
            var result1 = new TargetTypeSelector(typeof(IContravariant <BaseClassGrandchild>), targets).ToArray();
            var result2 = new TargetTypeSelector(typeof(Action <BaseClassGrandchild>), targets).ToArray();

            Assert.Equal(
                new[]
            {
                typeof(IContravariant <BaseClassGrandchild>),
                typeof(IContravariant <>)
            },
                result1
                );

            Assert.Equal(
                new[]
            {
                typeof(Action <BaseClassGrandchild>),
                typeof(Action <BaseClassChild>),
                typeof(Action <BaseClass>),
                typeof(Action <object>),
                typeof(Action <>)
            },
                result2);
        }
Esempio n. 3
0
        public void Covariant_Enumerable_ShouldContainOneMatchBecauseOptionDisablesEnumerableCovarianceForThatEnumerable()
        {
            // Arrange
            var targets = new TargetContainer();

            targets.SetOption <Options.EnableEnumerableCovariance, BaseClass>(false);
            var baseTarget             = Target.ForType <BaseClass>();
            var childTarget            = Target.ForType <BaseClassChild>();
            var grandChildTarget       = Target.ForType <BaseClassGrandchild>();
            var nestedbaseTarget       = Target.ForType <Covariant <BaseClass> >();
            var nestedchildTarget      = Target.ForType <Covariant <BaseClassChild> >();
            var nestedgrandChildTarget = Target.ForType <Covariant <BaseClassGrandchild> >();

            targets.Register(baseTarget);
            targets.Register(childTarget);
            targets.Register(grandChildTarget);
            targets.Register(nestedbaseTarget, typeof(ICovariant <BaseClass>));
            targets.Register(nestedchildTarget, typeof(ICovariant <BaseClassChild>));
            targets.Register(nestedgrandChildTarget, typeof(ICovariant <BaseClassGrandchild>));

            // Act
            var enumerableTarget       = Assert.IsType <EnumerableTarget>(targets.Fetch(typeof(IEnumerable <BaseClass>)));
            var nestedEnumerableTarget = Assert.IsType <EnumerableTarget>(targets.Fetch(typeof(IEnumerable <ICovariant <BaseClass> >)));

            // Assert
            Assert.Equal(new[] { baseTarget.Id }, enumerableTarget.Targets.Select(t => t.Id));
            Assert.Equal(new[] { nestedbaseTarget.Id, nestedchildTarget.Id, nestedgrandChildTarget.Id }, nestedEnumerableTarget.Targets.Select(t => t.Id));
        }
Esempio n. 4
0
        public void MustCopyParent()
        {
            var parent     = new TargetContainer();
            var overriding = new OverridingTargetContainer(parent);

            Assert.Same(parent, overriding.Parent);
        }
        public void ShouldRegisterAlias()
        {
            TargetContainer targets = new TargetContainer();

            targets.RegisterAlias <object, string>();
            Assert.NotNull(targets.Fetch(typeof(object)));
        }
Esempio n. 6
0
        public void Projection_ShouldAutoProject_SpecificImplementationType()
        {
            // Projecting a different implementation type from the type that we're projecting to

            // Arrange
            var targets = new TargetContainer();

            targets.RegisterType <From1>();
            targets.RegisterType <From2>();
            targets.RegisterProjection <From, ITo, To>();

            // Act
            var result = targets.Fetch(typeof(IEnumerable <ITo>));

            // Assert
            var enumTarget = Assert.IsType <EnumerableTarget>(result);

            Assert.Collection(enumTarget, new Action <ITarget>[] {
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.IsType <ConstructorTarget>(projTarget.OutputTarget);
                    Assert.Equal(typeof(To), projTarget.OutputTarget.DeclaredType);
                    Assert.Equal(typeof(From1), projTarget.InputTarget.DeclaredType);
                },
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.IsType <ConstructorTarget>(projTarget.OutputTarget);
                    Assert.Equal(typeof(To), projTarget.OutputTarget.DeclaredType);
                    Assert.Equal(typeof(From2), projTarget.InputTarget.DeclaredType);
                }
            });
        }
Esempio n. 7
0
        public void Projection_ShouldAutoProject()
        {
            // Arrange
            var targets = new TargetContainer();

            targets.RegisterType <From1>();
            targets.RegisterType <From2>();
            targets.RegisterProjection <From, To>();

            // Act
            var result = targets.Fetch(typeof(IEnumerable <To>));

            // Assert
            var enumTarget = Assert.IsType <EnumerableTarget>(result);

            Assert.Collection(enumTarget, new Action <ITarget>[] {
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.Equal(typeof(To), projTarget.DeclaredType);
                    Assert.IsType <ConstructorTarget>(projTarget.OutputTarget);
                    Assert.Equal(typeof(From1), projTarget.InputTarget.DeclaredType);
                },
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.Equal(typeof(To), projTarget.DeclaredType);
                    Assert.IsType <ConstructorTarget>(projTarget.OutputTarget);
                    Assert.Equal(typeof(From2), projTarget.InputTarget.DeclaredType);
                }
            });
        }
Esempio n. 8
0
        public void Projection_ShouldProject_FromRegistration()
        {
            // This time, projecting to an interface for which we have one registration
            // Demonstrates that the container will use a specific registration instead
            // of auto-binding the implementation type.

            // Arrange
            var targets = new TargetContainer();

            targets.RegisterType <From1>();
            targets.RegisterType <From2>();
            var expectedTarget = Target.ForType <To>();

            targets.Register(expectedTarget, typeof(ITo));
            targets.RegisterProjection <From, ITo>();

            // Act
            var result = targets.Fetch(typeof(IEnumerable <ITo>));

            // Assert
            var enumTarget = Assert.IsType <EnumerableTarget>(result);

            Assert.Collection(enumTarget, new Action <ITarget>[] {
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.Same(expectedTarget, projTarget.OutputTarget);
                    Assert.Equal(typeof(From1), projTarget.InputTarget.DeclaredType);
                },
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.Same(expectedTarget, projTarget.OutputTarget);
                    Assert.Equal(typeof(From2), projTarget.InputTarget.DeclaredType);
                }
            });
        }
Esempio n. 9
0
        public void ShouldNotRegisterIfTypesDontMatch()
        {
            ITarget          t = new ObjectTarget("hello world");
            ITargetContainer r = new TargetContainer();

            Assert.Throws <ArgumentException>(() => r.Register(t, serviceType: typeof(int)));
        }
        public void Covariant_ShouldIncludeAllDerivedRegistrations_MostRecentToLeast()
        {
            // Arrange
            var targets = new TargetContainer();

            targets.RegisterType <Covariant <BaseClass>, ICovariant <BaseClass> >();
            targets.RegisterType <Covariant <BaseClassChild>, ICovariant <BaseClassChild> >();
            targets.RegisterType <Covariant <BaseClassGrandchild>, ICovariant <BaseClassGrandchild> >();

            // Act
            var result = new TargetTypeSelector(typeof(ICovariant <BaseClass>), targets).ToArray();

            LogActual(result);

            // Assert
            // here, the order of the covariant types is determined by the order of registration.
            // the selector processes them in reverse chronological order.
            Assert.Equal(new[]
            {
                typeof(ICovariant <BaseClass>),
                typeof(ICovariant <BaseClassGrandchild>),
                typeof(ICovariant <BaseClassChild>),
                typeof(ICovariant <>)
            }, result);
        }
Esempio n. 11
0
        public void Projection_ShouldProject_FromTypeSelector()
        {
            // Arrange
            var targets = new TargetContainer();

            targets.RegisterType <From1>();
            targets.RegisterType <From2>();
            targets.RegisterProjection <From, ITo <From> >((r, t) => typeof(To <>).MakeGenericType(t.DeclaredType));

            // Act
            var result = targets.Fetch(typeof(IEnumerable <ITo <From> >));

            // Assert
            var enumTarget = Assert.IsType <EnumerableTarget>(result);

            Assert.Collection(enumTarget, new Action <ITarget>[] {
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.IsType <ConstructorTarget>(projTarget.OutputTarget);
                    Assert.Equal(typeof(To <From1>), projTarget.OutputTarget.DeclaredType);
                    Assert.Equal(typeof(From1), projTarget.InputTarget.DeclaredType);
                },
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.IsType <ConstructorTarget>(projTarget.OutputTarget);
                    Assert.Equal(typeof(To <From2>), projTarget.OutputTarget.DeclaredType);
                    Assert.Equal(typeof(From2), projTarget.InputTarget.DeclaredType);
                }
            });
        }
        public void ShouldOverrideDisallowingForOpenButNotForOneClosedGeneric()
        {
            var targets = new TargetContainer();

            targets.SetOption <AllowMultiple>(false);
            targets.SetOption <AllowMultiple>(true, typeof(Generic <>));
            targets.SetOption <AllowMultiple, Generic <string> >(false);
        }
        public void SettingOptionToFalseShouldDisallowMultipleTargets()
        {
            var targets = new TargetContainer();

            targets.SetOption <AllowMultiple>(false);

            Assert.Throws <InvalidOperationException>(() => RegisterTwoTargets(targets, typeof(int)));
        }
        public void ShouldUseOpenGenericServiceOptionForClosed()
        {
            var targets = new TargetContainer();

            targets.SetOption <TestOption>("open", typeof(IEnumerable <>));

            Assert.Equal("open", targets.GetOption <TestOption, IEnumerable <int> >());
        }
Esempio n. 15
0
    //Deletes the previous Target with the same name if it exists and then adds the Target to the History
    //This way the TargetText will be updated in case it changed and the most recently scanned Target is always at the end of the List
    public static void AddTarget(Target mTarget)
    {
        DeleteTarget(mTarget.GetName());
        TargetContainer tc = GetTargetContainer();

        tc.GetTargets().Add(mTarget);
        WriteToHistory(tc);
    }
        public void ShouldDisallowMultipleForOneTypeOnly()
        {
            var targets = new TargetContainer();

            targets.SetOption <AllowMultiple, int>(false);

            RegisterTwoTargets(targets, typeof(string));
            Assert.Throws <InvalidOperationException>(() => RegisterTwoTargets(targets, typeof(int)));
        }
        public void GetOptions_ShouldGetArrayOptionForAnyArrayType()
        {
            var targets = new TargetContainer();

            targets.SetOption <TestOption, Array>("array");
            Assert.Equal("array", targets.GetOption <TestOption, int[]>());
            Assert.Equal("array", targets.GetOption <TestOption, IServiceProvider[]>());
            Assert.Equal("array", targets.GetOption <TestOption, IEnumerable <string>[]>());
        }
        public void ShouldAllowForSpecificClosedGeneric()
        {
            var targets = new TargetContainer();

            targets.SetOption <AllowMultiple>(false, typeof(Generic <>));
            targets.SetOption <AllowMultiple, Generic <int> >(true);

            RegisterTwoTargets(targets, typeof(Generic <int>));
        }
Esempio n. 19
0
 public void SetTarget(int index)
 {
     if (targetEditorObject == null || targetEditorArray == null)
     {
         LoadTargetObject();
     }
     subTargetsContainers.Clear();
     if (index < 0)
     {
         index = 0;
     }
     target      = targetEditorArray[index];
     targetIndex = index;
     try
     {
         targetObject = (Target)Activator.CreateInstance(Type.GetType(target.name));
         Debug.Log("create target " + targetObject);
         var subTargetPrefabs = GetSubTargetPrefabs();
         if (subTargetPrefabs.Length > 1)
         {
             foreach (var _target in subTargetPrefabs)
             {
                 var    component   = _target.GetComponent <Item>();
                 Sprite extraObject = null;
                 if (component)
                 {
                     extraObject = component.sprRenderer.FirstOrDefault().sprite;
                 }
                 subTargetsContainers.Add(new SubTargetContainer(_target, 0, extraObject));
             }
         }
         else if (subTargetPrefabs.Length > 0 && subTargetPrefabs[0].GetComponent <IColorableComponent>())
         {
             foreach (var item in subTargetPrefabs[0].GetComponent <IColorableComponent>().GetSprites(levelNum))
             {
                 subTargetsContainers.Add(new SubTargetContainer(subTargetPrefabs[0], 0, item));
             }
         }
         else if (subTargetPrefabs.Length > 0)
         {
             foreach (var _target in subTargetPrefabs)
             {
                 var    component   = _target.GetComponent <Item>();
                 Sprite extraObject = null;
                 if (component)
                 {
                     extraObject = component.sprRenderer.FirstOrDefault().sprite;
                 }
                 subTargetsContainers.Add(new SubTargetContainer(_target, 0, extraObject));
             }
         }
     }
     catch (Exception)
     {
         Debug.LogError("Check the target name or create class " + target.name);
     }
 }
        public void ClosedGenericServiceOptionShouldOverrideOpenGeneric()
        {
            var targets = new TargetContainer();

            targets.SetOption <TestOption>("open", typeof(IEnumerable <>));
            targets.SetOption <TestOption, IEnumerable <int> >("int");

            Assert.Equal("int", targets.GetOption <TestOption, IEnumerable <int> >());
        }
        public void ShouldDisallowMultipleForAnOpenGenericAndAllClosedGenerics()
        {
            var targets = new TargetContainer();

            targets.SetOption <AllowMultiple>(false, typeof(Generic <>));

            RegisterTwoTargets(targets, typeof(int));
            Assert.Throws <InvalidOperationException>(() => RegisterTwoTargets(targets, typeof(Generic <>)));
            Assert.Throws <InvalidOperationException>(() => RegisterTwoTargets(targets, typeof(Generic <int>)));
        }
Esempio n. 22
0
        public void ShouldConfigureGlobalOption()
        {
            CombinedTargetContainerConfig config = new CombinedTargetContainerConfig();

            config.ConfigureOption((TestOption)"global");

            var targets = new TargetContainer(config);

            Assert.Equal("global", targets.GetOption <TestOption>());
        }
        public void ShouldUseOptionForBaseClass()
        {
            var targets = new TargetContainer();

            // the underlying options container that's used to store the option value
            // is contravariant, so should return an option defined at the base class level :)
            targets.SetOption <TestOption>("for baseclass", typeof(BaseClass));

            Assert.Equal("for baseclass", targets.GetOption <TestOption, BaseClassChild>());
        }
        public void GetOptions_GlobalOptionsShouldBeIncludedLast()
        {
            var targets = new TargetContainer();

            targets.SetOption <TestOption>("global");
            targets.SetOption <TestOption, int>("int");

            Assert.Equal(new[] { "int", "global" },
                         targets.GetOptions <TestOption, int>().Select(o => (string)o));
        }
        public void OptionShouldBeInheritedByOverridingTargetContainer()
        {
            var targets = new TargetContainer();

            targets.SetOption <TestOption>("base");

            var overriding = new OverridingTargetContainer(targets);

            Assert.Equal("base", overriding.GetOption <TestOption>());
        }
Esempio n. 26
0
        public void ShouldRegisterForImplicitType()
        {
            ITarget          t = new ObjectTarget("hello word");
            ITargetContainer rezolverBuilder = new TargetContainer();

            rezolverBuilder.Register(t);
            var t2 = rezolverBuilder.Fetch(typeof(string));

            Assert.Same(t, t2);
        }
        public void WillRejectBecauseIncompatibleType()
        {
            // <example3>
            var targets = new TargetContainer();

            // int is obviously not compatible with IMyService.
            Assert.Throws <ArgumentException>(
                () => targets.Register(Target.ForObject(50), typeof(IMyService)));
            // </example3>
        }
Esempio n. 28
0
        public void ShouldInheritParentRegistration()
        {
            var parent     = new TargetContainer();
            var overriding = new OverridingTargetContainer(parent);

            var parentTarget = new TestTarget(typeof(int), useFallBack: false, supportsType: true);

            parent.Register(parentTarget);

            Assert.Same(parentTarget, overriding.Fetch(typeof(int)));
        }
        public void GetOptions_ShouldGetAllServiceSpecificOptions()
        {
            var targets = new TargetContainer();

            targets.SetOption <TestOption, int>("first");
            targets.SetOption <TestOption, int>("second");
            targets.SetOption <TestOption, int>("third");

            Assert.Equal(new[] { "first", "second", "third" },
                         targets.GetOptions <TestOption, int>().Select(o => (string)o));
        }
Esempio n. 30
0
            public void Prepare()
            {
                var targets = new TargetContainer();

                targets.RegisterType <ScopedTransient, ITransient1>();
                targets.RegisterType <ScopedCombined1, ICombined1>();
                targets.RegisterType <ScopedCombined2, ICombined2>();
                targets.RegisterType <ScopedCombined3, ICombined3>();
                this.child      = new OverridingContainer(this.parent, targets);
                this.childScope = this.child.CreateScope();
            }
        public ActionResult Create(TargetContainer targetcontainer)
        {
            //ModelState.Clear();

            //targetcontainer.DesktopType = db.DesktopTypes.Find(targetcontainer.DesktopType_DesktopCode);
            //TryValidateModel(targetcontainer);

            if (ModelState.IsValid)
            {
                db.TargetContainers.Add(targetcontainer);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                ViewData.Add("Error", String.Join(";", ModelState.Values.SelectMany(value => value.Errors).Select(
                    error => !String.IsNullOrEmpty(error.ErrorMessage) ? error.ErrorMessage : error.Exception != null ? error.Exception.Message : "" )));
            }

            //ViewBag.DesktopType_DesktopCode = new SelectList(db.DesktopTypes, "DesktopCode", "Description", targetcontainer.DesktopType_DesktopCode);
            //ViewBag.ParentTargetContainer_Id = new SelectList(db.TargetContainers, "Id", "Description", targetcontainer.ParentTargetContainer_Id);
            return View(targetcontainer);
        }
 public ActionResult Edit(TargetContainer targetcontainer)
 {
     if (ModelState.IsValid)
     {
         db.Entry(targetcontainer).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     //ViewBag.DesktopType_DesktopCode = new SelectList(db.DesktopTypes, "DesktopCode", "Description", targetcontainer.DesktopType_DesktopCode);
     //ViewBag.ParentTargetContainer_Id = new SelectList(db.TargetContainers, "Id", "Description", targetcontainer.ParentTargetContainer_Id);
     return View(targetcontainer);
 }