public void Map_type_should_return_a_null_element_for_null_dynamic_object_enumerable_element()
        {
            var dynamicObjects = new DynamicObject[]
            {
                new DynamicObject(new CustomClass()),
                null,
                new DynamicObject(new CustomClass()),
            };

            var result = new DynamicObjectMapper().Map(dynamicObjects, typeof(CustomClass)).Cast <object>();

            result.ShouldNotBeNull();
            result.Count().ShouldBe(3);
            result.ElementAt(0).ShouldNotBeNull();
            result.ElementAt(1).ShouldBeNull();
            result.ElementAt(2).ShouldNotBeNull();
        }
        public void Map_should_be_null_for_null_object_enumerable()
        {
            var objects = new object[]
            {
                new CustomClass(),
                null,
                new CustomClass(),
            };

            var result = new DynamicObjectMapper().MapCollection(objects);

            result.ShouldNotBeNull();
            result.Count().ShouldBe(3);
            result.ElementAt(0).ShouldBeOfType <DynamicObject>();
            result.ElementAt(1).ShouldBeNull();
            result.ElementAt(2).ShouldBeOfType <DynamicObject>();
        }
        public void Should_serialize_dynamic_object_collection(Type type, IEnumerable value)
        {
            SkipUnsupportedDataType(type, value);

            var dynamicObjects = new DynamicObjectMapper(new DynamicObjectMapperSettings {
                WrapNullAsDynamicObject = true
            }).MapCollection(value);

            var config = CreateModelFor(type);
            var copy   = dynamicObjects.Clone(config);

            var dynamicObjectsCount = dynamicObjects?.Count() ?? 0;
            var copyCount           = copy?.Count() ?? 0;

            copyCount.ShouldBe(dynamicObjectsCount);

            for (int i = 0; i < copyCount; i++)
            {
                var originalValue = dynamicObjects.ElementAt(i)?.Get();
                var copyValue     = copy.ElementAt(i)?.Get();
                copyValue.ShouldBe(originalValue);
            }

            var c = new DynamicObjectMapper().Map(copy);

            if (value is null)
            {
                c.ShouldBeNull();
            }
            else
            {
                var array     = value.Cast <object>().ToArray();
                var copyArray = c.Cast <object>().ToArray();

                for (int i = 0; i < copyArray.Length; i++)
                {
                    copyArray[i].ShouldBe(array[i]);
                }
            }
        }
Exemple #4
0
        public void Should_serialize_dynamic_object_collection(Type type, IEnumerable value, CultureInfo culture)
        {
            SkipUnsupportedDataType(type, value);

            using var cultureContext = culture.CreateContext();

            var dynamicObjects = new DynamicObjectMapper().MapCollection(value);
            var copy           = dynamicObjects.Serialize();

            var dynamicObjectsCount = dynamicObjects?.Count() ?? 0;
            var copyCount           = copy?.Count() ?? 0;

            copyCount.ShouldBe(dynamicObjectsCount);

            for (int i = 0; i < copyCount; i++)
            {
                var originalValue = dynamicObjects.ElementAt(i)?.Get();
                var copyValue     = copy.ElementAt(i)?.Get();
                copyValue.ShouldBe(originalValue);
            }

            var c = new DynamicObjectMapper().Map(copy);

            if (value is null)
            {
                c.ShouldBeNull();
            }
            else
            {
                var array     = value.Cast <object>().ToArray();
                var copyArray = c.Cast <object>().ToArray();

                for (int i = 0; i < copyArray.Length; i++)
                {
                    copyArray[i].ShouldBe(array[i]);
                }
            }
        }