예제 #1
0
        internal static TValue InContainer <TValue, TData>(
            TValue item,
            string paremeterName,
            Func <ContainerClass <TValue>, TData> toData,
            Func <TData, ContainerClass <TValue> > fromData,
            Action <TData, TData> isDataEqual)
        {
            try
            {
                var container1     = new ContainerClass <TValue>(item);
                var containerData1 = toData(container1);

                // doing it twice to catch errors when deserializing
                var container2 = fromData(containerData1);
                FieldAssert.Equal(container1, container2);

                var containerXml2 = toData(container2);
                isDataEqual(containerData1, containerXml2);

                return(container2.Other);
            }
            catch (Exception e)
            {
                var message = $"  Roundtrip of {paremeterName} in {nameof(ContainerClass<TValue>)} Failed.\r\n" +
                              "  This means there is an error in serialization.\r\n" +
                              $"  If you are implementing {nameof(IXmlSerializable)} check that you handle ReadEndElement properly as it is a common source of bugs.";
                throw new AssertException(message, e);
            }
        }
        /// <summary>
        /// 1. Places <paramref name="item"/> in a ContainerClass{T} container1
        /// 2. Serializes container1
        /// 3. Deserializes the containerXml to container2 and does FieldAssert.Equal(container1, container2);
        /// 4. Serializes container2
        /// 5. Checks XmlAssert.Equal(containerXml1, containerXml2, XmlAssertOptions.Verbatim);
        /// </summary>
        /// <typeparam name="T">The type of <paramref name="item"/></typeparam>
        /// <param name="item">The instance to roundtrip</param>
        /// <returns>The serialized and deserialized instance (container2.Other)</returns>
        public static T Roundtrip <T>(T item)
        {
            Roundtripper.Simple(item, nameof(item), ToXml, FromXml <T>);
            var roundtripped = Roundtripper.InContainer(
                item,
                nameof(item),
                ToXml,
                FromXml <ContainerClass <T> >,
                (e, a) => XmlAssert.Equal(e, a, XmlAssertOptions.Verbatim));

            FieldAssert.Equal(item, roundtripped);

            return(roundtripped);
        }
예제 #3
0
        internal static TValue Simple <TValue, TData>(
            TValue item,
            string paremeterName,
            Func <TValue, TData> toData,
            Func <TData, TValue> fromData)
        {
            var data         = toData(item);
            var roundtripped = fromData(data);

            try
            {
                FieldAssert.Equal(item, roundtripped);
            }
            catch (Exception e)
            {
                throw AssertException.CreateFromException(
                          $"Simple roundtrip failed. Source is not equal to roundtripped.",
                          e);
            }

            return(roundtripped);
        }