public void ReadOnlyPropertiesWorkIfTheReferenceMatches()
        {
            var document = new OclDocument
            {
                new OclBlock("ReadOnlyPassengers")
                {
                    new OclAttribute("Name", "Bob")
                }
            };

            var expected = new Car();

            expected.ReadOnlyPassengers.Add(new Person
            {
                Name = "Bob"
            });

            var context = new OclConversionContext(new OclSerializerOptions() ?? new OclSerializerOptions());
            var result  = context.FromElement(typeof(Car), document, null);

            if (result == null)
            {
                throw new OclException("Document conversion resulted in null, which is not valid");
            }
            ((Car)result)
            .Should()
            .BeEquivalentTo(expected);
        }
        public object?FromElement(OclConversionContext context, Type type, IOclElement element, object?currentValue)
        {
            var collectionType = GetElementType(type);

            var collection = currentValue ?? CreateNewCollection(type, collectionType);

            var elements = element is OclDocument root
                ? root.ToArray()
                : new[] { element };

            foreach (var item in elements.Select(e => context.FromElement(collectionType, e, null)))
            {
                if (collection is IList list)
                {
                    list.Add(item);
                }
                else
                {
                    var addMethod = collection.GetType().GetMethod("Add", new[] { collectionType });
                    if (addMethod == null)
                    {
                        throw new Exception("Only collections that implement an Add method are supported");
                    }

                    addMethod.Invoke(collection, new[] { item });
                }
            }

            return(collection);
        }
        public void CollectionSingleItem()
        {
            var document = new OclDocument
            {
                new OclBlock("Passengers")
                {
                    new OclAttribute("Name", "Bob")
                }
            };

            var context = new OclConversionContext(new OclSerializerOptions() ?? new OclSerializerOptions());
            var result  = context.FromElement(typeof(Car), document, null);

            if (result == null)
            {
                throw new OclException("Document conversion resulted in null, which is not valid");
            }
            ((Car)result)
            .Should()
            .BeEquivalentTo(new Car
            {
                Passengers = new List <Person>
                {
                    new Person
                    {
                        Name = "Bob"
                    }
                }
            });
        }
        public void Empty()
        {
            var document = new OclDocument();
            var context  = new OclConversionContext(new OclSerializerOptions());
            var result   = context.FromElement(typeof(Car), document, null);

            if (result == null)
            {
                throw new OclException("Document conversion resulted in null, which is not valid");
            }
            ((Car)result)
            .Should()
            .BeEquivalentTo(new Car());
        }
        public void StringAttribute()
        {
            var document = new OclDocument
            {
                new OclAttribute("Name", "Mystery Machine")
            };

            var context = new OclConversionContext(new OclSerializerOptions() ?? new OclSerializerOptions());
            var result  = context.FromElement(typeof(Car), document, null);

            if (result == null)
            {
                throw new OclException("Document conversion resulted in null, which is not valid");
            }
            ((Car)result)
            .Should()
            .BeEquivalentTo(new Car
            {
                Name = "Mystery Machine"
            });
        }
示例#6
0
        protected virtual IReadOnlyList <IOclElement> SetProperties(
            OclConversionContext context,
            IEnumerable <IOclElement> elements,
            object target,
            IReadOnlyList <PropertyInfo> properties)
        {
            var notFound = new List <IOclElement>();

            foreach (var element in elements)
            {
                var name = element.Name?.Trim();
                if (string.IsNullOrWhiteSpace(name))
                {
                    throw new OclException("Encountered invalid child: " + element.GetType());
                }

                var propertyToSet = context.Namer.GetProperty(name, properties);
                if (propertyToSet == null)
                {
                    notFound.Add(element);
                }
                else
                {
                    var currentValue = propertyToSet.GetValue(target);
                    var valueToSet   = context.FromElement(propertyToSet.PropertyType, element, currentValue);

                    if (currentValue != valueToSet)
                    {
                        if (!propertyToSet.CanWrite)
                        {
                            throw new OclException($"The property '{propertyToSet.Name}' on '{target.GetType().Name}' does not have a setter");
                        }

                        propertyToSet.SetValue(target, CoerceValue(valueToSet, propertyToSet.PropertyType));
                    }
                }
            }

            return(notFound);
        }
        public void ExceptionIsThrownIfPropertyCantBeSet()
        {
            var document = new OclDocument
            {
                new OclAttribute("ReadOnly", 1)
            };

            Action action = () =>
            {
                var context = new OclConversionContext(new OclSerializerOptions() ?? new OclSerializerOptions());
                var result  = context.FromElement(typeof(Car), document, null);
                if (result == null)
                {
                    throw new OclException("Document conversion resulted in null, which is not valid");
                }
                var temp = (Car)result;
            };

            action.Should()
            .Throw <OclException>()
            .WithMessage("*The property 'ReadOnly' on 'Car' does not have a setter*");
        }
        public void ExceptionIsThrownIfPropertyDoesNotExist()
        {
            var document = new OclDocument
            {
                new OclAttribute("Wings", 1)
            };

            Action action = () =>
            {
                var context = new OclConversionContext(new OclSerializerOptions() ?? new OclSerializerOptions());
                var result  = context.FromElement(typeof(Car), document, null);
                if (result == null)
                {
                    throw new OclException("Document conversion resulted in null, which is not valid");
                }
                var temp = (Car)result;
            };

            action.Should()
            .Throw <OclException>()
            .WithMessage("*The property 'Wings' was not found on 'Car'*");
        }