コード例 #1
0
 private static XDocumentAndSource ParseDocument(string xml, string parameterName, XmlAssertOptions options)
 {
     try
     {
         return(new XDocumentAndSource(xml, XDocument.Parse(xml, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo), options));
     }
     catch (Exception e)
     {
         throw AssertException.CreateFromException($"{parameterName} is not valid xml.", e);
     }
 }
コード例 #2
0
 private static T FromXml <T>(string xml, string parameterName)
 {
     try
     {
         var serializer = new DataContractSerializer(typeof(T));
         using (var reader = new XmlTextReader(new StringReader(xml)))
         {
             return((T)serializer.ReadObject(reader));
         }
     }
     catch (Exception e)
     {
         throw AssertException.CreateFromException($"Could not deserialize {parameterName} to an instance of type {typeof(T)}", e);
     }
 }
コード例 #3
0
 /// <summary>Serializes <paramref name="o"/> and returns the bytes.</summary>
 internal static byte[] ToBytes(object o, string parameterName)
 {
     try
     {
         var formatter = new BinaryFormatter();
         using (var stream = new MemoryStream())
         {
             formatter.Serialize(stream, o);
             return(stream.ToArray());
         }
     }
     catch (Exception e)
     {
         throw AssertException.CreateFromException($"Writing {parameterName} to a stream failed.", e);
     }
 }
コード例 #4
0
 private static string ToXml <T>(T item, string parameterName)
 {
     try
     {
         var serializer = new XmlSerializer(typeof(T));
         using (var writer = new StringWriter())
         {
             serializer.Serialize(writer, item);
             return(writer.ToString());
         }
     }
     catch (Exception e)
     {
         throw AssertException.CreateFromException($"Could not serialize{parameterName}.", e);
     }
 }
コード例 #5
0
 private static T FromBytes <T>(byte[] bytes, string parameterName)
 {
     using (var stream = new MemoryStream(bytes))
     {
         try
         {
             var binaryFormatter = new BinaryFormatter();
             var value           = (T)binaryFormatter.Deserialize(stream);
             return(value);
         }
         catch (Exception e)
         {
             throw AssertException.CreateFromException($"Reading {parameterName} of type {typeof(T)} failed.", e);
         }
     }
 }
コード例 #6
0
        private static string ToXml <T>(T item, string parameterName)
        {
            try
            {
                var serializer    = new DataContractSerializer(typeof(T));
                var stringBuilder = new StringBuilder();
                using (var writer = XmlWriter.Create(stringBuilder, XmlWriterSettings))
                {
                    serializer.WriteObject(writer, item);
                }

                return(stringBuilder.ToString());
            }
            catch (Exception e)
            {
                throw AssertException.CreateFromException($"Could not serialize{parameterName}.", e);
            }
        }
コード例 #7
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);
        }