コード例 #1
0
        public void Discovering_with_Level3()
        {
            using (TestHelper.Monitor.OpenInfo("Without ObjectALevel4 class."))
            {
                var types = ThisAssembly.GetTypes()
                            .Where(t => t.IsClass)
                            .Where(t => (t.Namespace == "CK.StObj.Engine.Tests.SimpleObjects" ||
                                         t.Namespace == "CK.StObj.Engine.Tests.SimpleObjects.WithLevel3") &&
                                   t.Name != "ObjectALevel4");

                StObjCollector collector = new StObjCollector(TestHelper.Monitor, new SimpleServiceContainer());
                collector.RegisterTypes(types.ToList());

                var result = collector.GetResult( );
                Assert.That(result.HasFatalError, Is.False);
            }

            using (TestHelper.Monitor.OpenInfo("ObjectALevel4 class (specializes ObjectALevel3 and use IAbstractionBOnLevel2)."))
            {
                var types = ThisAssembly.GetTypes()
                            .Where(t => t.IsClass)
                            .Where(t => t.Namespace == "CK.StObj.Engine.Tests.SimpleObjects" ||
                                   t.Namespace == "CK.StObj.Engine.Tests.SimpleObjects.WithLevel3");

                StObjCollector collector = new StObjCollector(TestHelper.Monitor, new SimpleServiceContainer());
                collector.RegisterTypes(types.ToList());

                var result = collector.GetResult();
                Assert.That(result.HasFatalError, Is.False);
            }
        }
コード例 #2
0
        static StObjCollectorResult DoGetSuccessfulResult(StObjCollector c)
        {
            c.RegisteringFatalOrErrorCount.Should().Be(0, "There must be no registration error (CKTypeCollector must be successful).");
            StObjCollectorResult?r = c.GetResult();

            r.HasFatalError.Should().Be(false, "There must be no error.");
            return(r);
        }
コード例 #3
0
 public void InjectSingleton_must_not_be_scoped_service()
 {
     {
         StObjCollector collector = new StObjCollector(TestHelper.Monitor, new SimpleServiceContainer());
         collector.RegisterType(typeof(InvalidRealObjectProperty));
         collector.GetResult().HasFatalError.Should().BeTrue();
     }
 }
コード例 #4
0
        public void a_RealObject_that_references_an_auto_service_from_an_InjectObject_is_an_error()
        {
            var            types     = new[] { typeof(AmbientInject) };
            StObjCollector collector = new StObjCollector(TestHelper.Monitor, new SimpleServiceContainer());

            collector.RegisterTypes(types.ToList());
            var result = collector.GetResult();

            Assert.That(result.HasFatalError, Is.True);
        }
コード例 #5
0
        StObjCollectorResult?IStObjEngineTestHelperCore.GetFailedResult(StObjCollector c)
        {
            if (c.RegisteringFatalOrErrorCount != 0)
            {
                TestHelper.Monitor.Error($"GetFailedResult: {c.RegisteringFatalOrErrorCount} fatal or error during StObjCollector registration. (Everything is fine since an error was expected.)");
                return(null);
            }
            var r = c.GetResult();

            r.HasFatalError.Should().Be(true, "GetFailedResult: StObjCollector.GetResult() must have failed with at least one fatal error.");
            return(r);
        }
コード例 #6
0
 public void OneObject()
 {
     {
         StObjCollector collector = new StObjCollector(TestHelper.Monitor, new SimpleServiceContainer());
         collector.RegisterType(typeof(SimpleContainer));
         var result = collector.GetResult().EngineMap !.StObjs;
         result.OrderedStObjs
         .Where(o => o.FinalImplementation.Implementation is not PocoDirectory)
         .First()
         .GetStObjProperty("OneIntValue").Should().Be(3712);
     }
 }
コード例 #7
0
            public void DoTest()
            {
                var extraServices = new SimpleServiceContainer();

                extraServices.Add <Func <string> >(() => "Hello World!");

                StObjCollector collector = new StObjCollector(TestHelper.Monitor, extraServices);

                collector.RegisterType(typeof(JustForTheAttribute));
                var r = collector.GetResult();

                Assert.That(r.HasFatalError, Is.False);
            }
コード例 #8
0
        public void StObj_must_have_only_one_specialization_chain()
        {
            var types = ThisAssembly.GetTypes()
                        .Where(t => t.IsClass)
                        .Where(t => t.Namespace == "CK.StObj.Engine.Tests.SimpleObjects");

            var collector = new StObjCollector(TestHelper.Monitor, new SimpleServiceContainer());

            collector.RegisterTypes(types.ToList());
            collector.RegisterType(typeof(ObjectALevel1Conflict));
            var result = collector.GetResult();

            result.HasFatalError.Should().BeTrue();
        }
コード例 #9
0
        static StObjCollectorResult BuildPocoSample(params Type[] extra)
        {
            var types = typeof(PocoTests).Assembly.GetTypes()
                        .Where(t => t.Namespace == "CK.StObj.Engine.Tests.Poco.Sample")
                        .Concat(extra);

            StObjCollector collector = new StObjCollector(TestHelper.Monitor, new SimpleServiceContainer());

            collector.RegisterTypes(types.ToList());

            var result = collector.GetResult();

            Assert.That(result.HasFatalError, Is.False);
            return(result);
        }
コード例 #10
0
        public void Cycle_in_package()
        {
            using (TestHelper.Monitor.OpenInfo("A specialization of ObjectBLevel3 wants to be in PackageForAB."))
            {
                // ↳ PackageForAB ∋ ObjectBLevel3_InPackageForAB ⇒ ObjectBLevel2 ⇒ ObjectBLevel1 ∈ PackageForABLevel1 ⇒ PackageForAB.
                var types = ThisAssembly.GetTypes()
                            .Where(t => t.IsClass)
                            .Where(t => t.Namespace == "CK.StObj.Engine.Tests.SimpleObjects" ||
                                   t.Namespace == "CK.StObj.Engine.Tests.SimpleObjects.WithLevel3" ||
                                   t.Name == "ObjectBLevel3_InPackageForAB");

                StObjCollector collector = new StObjCollector(TestHelper.Monitor, new SimpleServiceContainer());
                collector.RegisterTypes(types.ToList());

                var result = collector.GetResult(  );
                Assert.That(result.HasFatalError, Is.True);
            }
        }
コード例 #11
0
 public void OneObjectDirectProperty()
 {
     var container = new SimpleServiceContainer();
     {
         StObjCollector collector = new StObjCollector(TestHelper.Monitor, container);
         collector.RegisterType(typeof(SimpleObjectDirect));
         var map = collector.GetResult().EngineMap;
         Debug.Assert(map != null, "No initialization error.");
         map.StObjs.Obtain <SimpleObjectDirect>() !.OneIntValue.Should().Be(3712, "Direct properties can be set by Attribute.");
     }
     {
         StObjCollector collector = new StObjCollector(TestHelper.Monitor, container, configurator: new ConfiguratorOneIntValueSetTo42());
         collector.RegisterType(typeof(SimpleObjectDirect));
         var map = collector.GetResult().EngineMap;
         Debug.Assert(map != null, "No initialization error.");
         map.StObjs.Obtain <SimpleObjectDirect>() !.OneIntValue.Should().Be(42, "Direct properties can be set by any IStObjStructuralConfigurator participant (here the global one).");
     }
 }
コード例 #12
0
 public void OneObjectAmbientProperty()
 {
     var container = new SimpleServiceContainer();
     {
         StObjCollector collector = new StObjCollector(TestHelper.Monitor, container);
         collector.RegisterType(typeof(SimpleObjectAmbient));
         var map = collector.GetResult().EngineMap;
         Debug.Assert(map != null, "No initialization error.");
         map.StObjs.OrderedStObjs.Should().NotBeEmpty("We registered SimpleObjectAmbient.");
         map.StObjs.Obtain <SimpleObjectAmbient>() !.OneIntValue.Should().Be(3712, "Same as Direct properties (above) regarding direct setting. The difference between Ambient and non-ambient lies in value propagation.");
     }
     {
         StObjCollector collector = new StObjCollector(TestHelper.Monitor, container, configurator: new ConfiguratorOneIntValueSetTo42());
         collector.RegisterType(typeof(SimpleObjectAmbient));
         var map = collector.GetResult().EngineMap;
         Debug.Assert(map != null, "No initialization error.");
         map.StObjs.Obtain <SimpleObjectAmbient>() !.OneIntValue.Should().Be(42, "Same as Direct properties (above) regarding direct setting. The difference between Ambient and non-ambient lies in value propagation.");
     }
 }
コード例 #13
0
 public void AmbientOrDirectPropertyDeclaredInBaseClassCanBeSet()
 {
     var container = new SimpleServiceContainer();
     {
         StObjCollector collector = new StObjCollector(TestHelper.Monitor, container);
         collector.RegisterType(typeof(SpecializedObjectDirect));
         var map = collector.GetResult().EngineMap;
         Debug.Assert(map != null, "No initialization error.");
         map.StObjs.OrderedStObjs.Select(o => o.ClassType).Should().Contain(new[] { typeof(SpecializedObjectDirect), typeof(SimpleObjectDirect) });
         map.StObjs.Obtain <SpecializedObjectDirect>() !.OneIntValue.Should().Be(999, "Direct properties can be set by Attribute (or any IStObjStructuralConfigurator).");
     }
     {
         StObjCollector collector = new StObjCollector(TestHelper.Monitor, container);
         collector.RegisterType(typeof(SpecializedObjectAmbient));
         var map = collector.GetResult().EngineMap;
         Debug.Assert(map != null, "No initialization error.");
         map.StObjs.OrderedStObjs.Select(o => o.ClassType).Should().Contain(new[] { typeof(SpecializedObjectAmbient), typeof(SimpleObjectAmbient) });
         map.StObjs.Obtain <SpecializedObjectAmbient>() !.OneIntValue.Should().Be(999, "Ambient properties can be set by Attribute (or any IStObjStructuralConfigurator).");
     }
 }
コード例 #14
0
        public void poco_marked_with_CKTypeDefiner_are_not_registered()
        {
            StObjCollector collector = new StObjCollector(TestHelper.Monitor, new SimpleServiceContainer());

            collector.RegisterType(typeof(IThing));
            collector.RegisteringFatalOrErrorCount.Should().Be(0);
            var poco = collector.GetResult().CKTypeResult.PocoSupport;

            Debug.Assert(poco != null, "Since there has been no error.");
            poco.Roots.Should().HaveCount(1);

            var TF = poco.Roots[0].PocoFactoryClass;
            var F  = Activator.CreateInstance(TF);

            Debug.Assert(F != null);
            var FP = (IPocoFactory)F;
            var PT = FP.PocoClassType;

            PT.Should().NotBeNull();

            poco.AllInterfaces.Should().HaveCount(1);
            poco.Find(typeof(IThingBase)).Should().BeNull();
            poco.Find(typeof(IThing)).Should().NotBeNull();
        }
            public void DoTest()
            {
                var container = new SimpleServiceContainer();
                {
                    StObjCollector collector = new StObjCollector(TestHelper.Monitor, container, configurator: new ConfiguratorOneStringValueSetToPouf());
                    collector.RegisterType(typeof(InheritedBaseObject));
                    var result = collector.GetResult().EngineMap;
                    Debug.Assert(result != null, "No initialization error.");
                    Assert.That(result.StObjs.Obtain <InheritedBaseObject>() !.OneStringValue, Is.EqualTo("Pouf"), "Since InheritedSimpleObject is a BaseObjectAmbient, it has been configured.");
                }
                {
                    StObjCollector collector = new StObjCollector(TestHelper.Monitor, container, configurator: new ConfiguratorOneStringValueSetToPouf());
                    collector.RegisterType(typeof(InheritedBaseObjectWithSet));
                    var result = collector.GetResult().EngineMap;
                    Debug.Assert(result != null, "No initialization error.");
                    Assert.That(result.StObjs.Obtain <InheritedBaseObjectWithSet>() !.OneStringValue, Is.EqualTo("OnInheritedWithSet"), "More specialized InheritedSimpleObjectWithSet has been set.");
                }
                {
                    StObjCollector collector = new StObjCollector(TestHelper.Monitor, container, configurator:
                                                                  new StructuralConfiguratorHelper(o =>
                    {
                        if (o.ClassType.Name == "InheritedBaseObjectWithSet")
                        {
                            o.Container.Type = typeof(AnotherContainer);
                        }
                    }));
                    collector.RegisterType(typeof(AnotherContainer));
                    collector.RegisterType(typeof(InheritedBaseObjectWithSet));
                    var result = collector.GetResult().EngineMap;
                    Debug.Assert(result != null, "No initialization error.");
                    Assert.That(result.StObjs.ToHead(typeof(InheritedBaseObjectWithSet)) !.Container !.ClassType.Name, Is.EqualTo("AnotherContainer"), "Container has changed.");
                    Assert.That(result.StObjs.Obtain <InheritedBaseObjectWithSet>() !.OneStringValue, Is.EqualTo("OnInheritedWithSet"), "Property does not change since it is set on the class itself.");
                }
                {
                    StObjCollector collector = new StObjCollector(TestHelper.Monitor, container, configurator:
                                                                  new StructuralConfiguratorHelper(o =>
                    {
                        if (o.ClassType.Name == "InheritedBaseObjectWithoutSet")
                        {
                            o.Container.Type = typeof(AnotherContainer);
                        }
                    }));
                    collector.RegisterType(typeof(AnotherContainer));
                    collector.RegisterType(typeof(InheritedBaseObjectWithoutSet));
                    var result = collector.GetResult().EngineMap;
                    Debug.Assert(result != null, "No initialization error.");
                    Assert.That(result.StObjs.ToHead(typeof(InheritedBaseObjectWithSet)) !.Container, Is.Null, "Container of InheritedSimpleObjectWithSet has NOT changed (no container).");
                    Assert.That(result.StObjs.ToHead(typeof(InheritedBaseObjectWithoutSet)) !.Container !.ClassType.Name, Is.EqualTo("AnotherContainer"), "Container of InheritedSimpleObjectWithoutSet has changed.");

                    Assert.That(result.StObjs.Obtain <InheritedBaseObjectWithoutSet>() !.OneStringValue, Is.EqualTo("OnAnotherContainer"), "Here, the container's value takes precedence since Property is NOT set on the class itself but on its Generalization.");
                }
                {
                    StObjCollector collector = new StObjCollector(TestHelper.Monitor, container, configurator:
                                                                  new StructuralConfiguratorHelper(o =>
                    {
                        if (o.ClassType.Name == "InheritedBaseObjectWithSet")
                        {
                            o.Container.Type = typeof(AnotherContainer);
                        }
                    }));
                    collector.RegisterType(typeof(AnotherContainer));
                    collector.RegisterType(typeof(InheritedBaseObjectWithoutSet));
                    var result = collector.GetResult().EngineMap;
                    Debug.Assert(result != null, "No initialization error.");
                    Assert.That(result.StObjs.ToHead(typeof(InheritedBaseObjectWithSet)) !.Container !.ClassType.Name, Is.EqualTo("AnotherContainer"), "Container of InheritedSimpleObjectWithSet has changed.");
                    Assert.That(result.StObjs.ToHead(typeof(InheritedBaseObjectWithoutSet)) !.Container !.ClassType.Name, Is.EqualTo("AnotherContainer"), "Container of InheritedSimpleObjectWithoutSet is inherited.");
                    Assert.That(result.StObjs.ToHead(typeof(InheritedBaseObjectWithoutSet)) !.ConfiguredContainer, Is.Null, "Container is inherited, not directly configured for the object.");

                    Assert.That(result.StObjs.Obtain <InheritedBaseObjectWithoutSet>() !.OneStringValue, Is.EqualTo("OnInheritedWithSet"), "The inherited value is used since container is (also) inherited.");
                }
            }
            public void DoTest()
            {
                var container = new SimpleServiceContainer();
                {
                    StObjCollector collector = new StObjCollector(TestHelper.Monitor, container);
                    collector.RegisterType(typeof(InheritedBaseObject));
                    var result = collector.GetResult().EngineMap;
                    Debug.Assert(result != null, "No initialization error.");
                    Assert.That(result.StObjs.Obtain <InheritedBaseObject>() !.OneStringValue, Is.Null, "No configuration.");
                }
                {
                    StObjCollector collector = new StObjCollector(TestHelper.Monitor, container, configurator: new ConfiguratorOneStringValueSetToPouf());
                    collector.RegisterType(typeof(InheritedBaseObject));
                    var result = collector.GetResult().EngineMap;
                    Debug.Assert(result != null, "No initialization error.");
                    Assert.That(result.StObjs.Obtain <InheritedBaseObject>() !.OneStringValue, Is.EqualTo("Pouf"), "Since InheritedSimpleObject is a BaseObjectAmbient, it has been configured.");
                }
                {
                    StObjCollector collector = new StObjCollector(TestHelper.Monitor, container, configurator: new ConfiguratorOneStringValueSetToPouf());
                    collector.RegisterType(typeof(InheritedBaseObjectWithSet));
                    var result = collector.GetResult().EngineMap;
                    Debug.Assert(result != null, "No initialization error.");
                    Assert.That(result.StObjs.Obtain <InheritedBaseObjectWithSet>() !.OneStringValue, Is.EqualTo("OnInheritedWithSet"), "More specialized InheritedSimpleObjectWithSet has been set.");
                    Assert.That(result.StObjs.Obtain <BaseObjectAmbient>() !.OneStringValue, Is.EqualTo("OnInheritedWithSet"), "The property is the same for any StObj.");
                }
                {
                    StObjCollector collector = new StObjCollector(TestHelper.Monitor, container, configurator:
                                                                  new StructuralConfiguratorHelper(o =>
                    {
                        if (o.ClassType.Name == "InheritedBaseObjectWithoutSet")
                        {
                            o.Container.Type = typeof(AnotherContainer);
                        }
                    }));
                    collector.RegisterType(typeof(AnotherContainer));
                    collector.RegisterType(typeof(InheritedBaseObjectWithoutSet));
                    var result = collector.GetResult().EngineMap;
                    Debug.Assert(result != null, "No initialization error.");
                    Assert.That(result.StObjs.ToHead(typeof(InheritedBaseObjectWithSet)) !.Container, Is.Null, "Container of InheritedSimpleObjectWithSet has NOT changed (no container).");
                    Assert.That(result.StObjs.ToHead(typeof(InheritedBaseObjectWithoutSet)) !.Container !.ClassType.Name, Is.EqualTo("AnotherContainer"), "Container of InheritedSimpleObjectWithoutSet has changed.");

                    Assert.That(result.StObjs.Obtain <InheritedBaseObjectWithoutSet>() !.OneStringValue, Is.EqualTo("OnInheritedWithSet"), "Generalization's value takes precedence, Container's value is ignored.");
                }
                {
                    StObjCollector collector = new StObjCollector(TestHelper.Monitor, container, configurator:
                                                                  new StructuralConfiguratorHelper(o =>
                    {
                        if (o.ClassType.Name == "InheritedBaseObject")
                        {
                            o.Container.Type = typeof(AnotherContainer);
                        }
                    }));
                    collector.RegisterType(typeof(AnotherContainer));
                    collector.RegisterType(typeof(InheritedBaseObject));
                    var result = collector.GetResult().EngineMap;
                    Debug.Assert(result != null, "No initialization error.");
                    Assert.That(result.StObjs.ToHead(typeof(InheritedBaseObject)) !.Container !.ClassType.Name, Is.EqualTo("AnotherContainer"), "Container of InheritedBaseObject has changed.");
                    Assert.That(result.StObjs.ToHead(typeof(BaseObjectAmbient)) !.Container, Is.Null, "BaseObjectAmbient has no container...");
                    Assert.That(result.StObjs.Obtain <BaseObjectAmbient>() !.OneStringValue, Is.EqualTo("OnAnotherContainer"), "The value comes from the container of the specialized object since the value is not set anywhere else.");
                }
                // Same as before except that the value is set on the BaseObjectAmbient:
                {
                    StObjCollector collector = new StObjCollector(TestHelper.Monitor, container, configurator:
                                                                  new StructuralConfiguratorHelper(o =>
                    {
                        if (o.ClassType.Name == "InheritedBaseObject")
                        {
                            o.Container.Type = typeof(AnotherContainer);
                        }
                        if (o.ClassType == typeof(BaseObjectAmbient))
                        {
                            o.SetAmbientPropertyValue(TestHelper.Monitor, "OneStringValue", "OnBaseObject", "Configurator");
                        }
                    }));
                    collector.RegisterType(typeof(AnotherContainer));
                    collector.RegisterType(typeof(InheritedBaseObject));
                    var result = collector.GetResult().EngineMap;
                    Debug.Assert(result != null, "No initialization error.");
                    Assert.That(result.StObjs.ToHead(typeof(InheritedBaseObject)) !.Container !.ClassType.Name, Is.EqualTo("AnotherContainer"), "Container of InheritedBaseObject has changed.");
                    Assert.That(result.StObjs.ToHead(typeof(BaseObjectAmbient)) !.Container, Is.Null, "BaseObjectAmbient has no container...");
                    Assert.That(result.StObjs.Obtain <InheritedBaseObject>() !.OneStringValue, Is.EqualTo("OnBaseObject"), "The value comes from the Generalization.");
                }
                // Two containers: the one of the Generalization wins.
                {
                    StObjCollector collector = new StObjCollector(TestHelper.Monitor, container, configurator:
                                                                  new StructuralConfiguratorHelper(o =>
                    {
                        if (o.ClassType.Name == "InheritedBaseObject")
                        {
                            o.Container.Type = typeof(AnotherContainer);
                        }
                        if (o.ClassType.Name == "BaseObjectAmbient")
                        {
                            o.Container.Type = typeof(ContainerForBaseObject);
                        }
                    }));
                    collector.RegisterType(typeof(AnotherContainer));
                    collector.RegisterType(typeof(ContainerForContainerForBaseObject));
                    collector.RegisterType(typeof(ContainerForBaseObject));
                    collector.RegisterType(typeof(InheritedBaseObject));
                    var result = collector.GetResult().EngineMap;
                    Debug.Assert(result != null, "No initialization error.");

                    Assert.That(result.StObjs.Obtain <InheritedBaseObject>() !.OneStringValue, Is.EqualTo("On Container of ContainerForBaseObject"), "The value comes from the Generalization's Container.");
                }
            }