Пример #1
0
        public void TestDirectPoolingMethods()
        {
            var pool = new InstancePoolTest();

            // Test: Ctor with argument
            {
                SerializerConfig config = new SerializerConfig();

                config.ConfigType <Person>()
                // Select ctor, not delegate
                .ConstructBy(() => new Person("name"));

                var clone = DoRoundTripTest(config);
                Assert.True(clone != null);
                Assert.True(clone.Name.StartsWith("riki"));
                Assert.True(clone.Name.EndsWith(Person.CtorSuffix));
            }

            // Test: Manual config
            {
                SerializerConfig config = new SerializerConfig();

                config.ConfigType <Person>()
                .ConstructBy(TypeConstruction.ByStaticMethod(() => StaticPoolTest.CreatePerson()));

                var clone = DoRoundTripTest(config);
                Assert.True(clone != null);
            }

            // Test: Normal ctor, but explicitly
            {
                SerializerConfig config = new SerializerConfig();

                config.ConfigType <Person>()
                // Select ctor, not delegate
                .ConstructBy(() => new Person());

                var clone = DoRoundTripTest(config);
                Assert.True(clone != null);
            }

            // Test: Construct from instance-pool
            {
                SerializerConfig config = new SerializerConfig();

                config.ConfigType <Person>()
                // Instance + method select
                .ConstructBy(pool, () => pool.CreatePerson());

                var clone = DoRoundTripTest(config);
                Assert.True(clone != null);
                Assert.True(pool.IsFromPool(clone));
            }

            // Test: Construct from static-pool
            {
                SerializerConfig config = new SerializerConfig();

                config.ConfigType <Person>()
                // method select
                .ConstructBy(() => StaticPoolTest.CreatePerson());

                var clone = DoRoundTripTest(config);
                Assert.True(clone != null);
                Assert.True(StaticPoolTest.IsFromPool(clone));
            }

            // Test: Construct from any delegate (in this example: a lambda expression)
            {
                SerializerConfig config = new SerializerConfig();

                Person referenceCapturedByLambda = null;

                config.ConfigType <Person>()
                // Use delegate
                .ConstructByDelegate(() =>
                {
                    var obj = new Person();
                    referenceCapturedByLambda = obj;
                    return(obj);
                });

                var clone = DoRoundTripTest(config);
                Assert.True(clone != null);
                Assert.True(ReferenceEquals(clone, referenceCapturedByLambda));
            }

            // Test: Construct from instance-pool, with parameter
            {
                SerializerConfig config = new SerializerConfig();

                config.ConfigType <Person>()
                // Use instance + method selection
                .ConstructBy(pool, () => pool.CreatePersonWithName("abc"));

                var clone = DoRoundTripTest(config);
                Assert.True(clone != null);
                Assert.True(clone.Name.StartsWith("riki"));
                Assert.True(pool.IsFromPool(clone));
            }

            // Test: Construct from static-pool, with parameter
            {
                SerializerConfig config = new SerializerConfig();

                config.ConfigType <Person>()
                // Use instance + method selection
                .ConstructBy(() => StaticPoolTest.CreatePersonWithName("abc"));

                var clone = DoRoundTripTest(config);
                Assert.True(clone != null);
                Assert.True(clone.Name.StartsWith("riki"));
                Assert.True(StaticPoolTest.IsFromPool(clone));
            }
        }