<commit_before>﻿namespace Gu.Units.Tests
{
    using System;
    using System.IO;
    using System.Reflection;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;

    using Gu.Units.Tests.Sources;

    using NUnit.Framework;

    public class SerializationTests
    {
        [TestCaseSource(typeof(QuantityTypesProvider))]
        public void XmlSerializer(Type quantityType)
        {
            var ctor = quantityType.GetConstructor(
                        BindingFlags.NonPublic | BindingFlags.Instance,
                        null,
                        new[] { typeof(double) },
                        null
                    );
            var quantity = ctor.Invoke(new object[] { 1.2 });
            var serializer = new XmlSerializer(quantityType);
            var stringBuilder = new StringBuilder();
            string xml = "";
            using (var writer = new StringWriter(stringBuilder))
            {
                serializer.Serialize(writer, quantity);
                xml = stringBuilder.ToString();
                Console.Write(xml);
                var expected = string.Format(@"<?xml version=""1.0"" encoding=""utf-16""?>
<{0} Value=""1.2"" />", quantity.GetType().Name);
                Assert.AreEqual(expected, xml);
            }
            using (var reader = new StringReader(xml))
            {
                var deserialized = serializer.Deserialize(reader);
                Assert.AreEqual(deserialized, quantity);
            }
        }

        [TestCaseSource(typeof(QuantityTypesProvider))]
        public void DataContractSerializer(Type quantityType)
        {
            var ctor = quantityType.GetConstructor(
                        BindingFlags.NonPublic | BindingFlags.Instance,
                        null,
                        new[] { typeof(double) },
                        null
                    );
            var quantity = ctor.Invoke(new object[] { 1.2 });
            var serializer = new DataContractSerializer(quantityType);
            var stringBuilder = new StringBuilder();
            string xml = "";
            using (var writer = XmlWriter.Create(stringBuilder))
            {
                serializer.WriteObject(writer, quantity);
                writer.Flush();
                xml = stringBuilder.ToString();
                Console.Write(xml);
                var expected = string.Format(@"<?xml version=""1.0"" encoding=""utf-16""?><{0} Value=""1.2"" xmlns=""http://schemas.datacontract.org/2004/07/Gu.Units"" />", quantity.GetType().Name);
                Assert.AreEqual(expected, xml);
            }
            using (var reader = XmlReader.Create(new StringReader(xml)))
            {
                var deserialized = serializer.ReadObject(reader);
                Assert.AreEqual(deserialized, quantity);
            }
        }

        [TestCaseSource(typeof(QuantityTypesProvider))]
        public void BinaryFormatter(Type quantityType)
        {
            var ctor = quantityType.GetConstructor(
                        BindingFlags.NonPublic | BindingFlags.Instance,
                        null,
                        new[] { typeof(double) },
                        null
                    );
            var quantity = ctor.Invoke(new object[] { 1.2 });
            var binaryFormatter = new BinaryFormatter();
            var memoryStream = new MemoryStream();
            binaryFormatter.Serialize(memoryStream, quantity);
            memoryStream.Position = 0;
            var deserialized = binaryFormatter.Deserialize(memoryStream);
            Assert.AreEqual(deserialized, quantity);
        }
    }<commit_msg>Use Environment.NewLine in xml tests.<commit_after>﻿namespace Gu.Units.Tests
{
    using System;
    using System.IO;
    using System.Reflection;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;

    using Gu.Units.Tests.Sources;

    using NUnit.Framework;

    public class SerializationTests
    {
        [TestCaseSource(typeof(QuantityTypesProvider))]
        public void XmlSerializer(Type quantityType)
        {
            var ctor = quantityType.GetConstructor(
                        BindingFlags.NonPublic | BindingFlags.Instance,
                        null,
                        new[] { typeof(double) },
                        null
                    );
            var quantity = ctor.Invoke(new object[] { 1.2 });
            var serializer = new XmlSerializer(quantityType);
            var stringBuilder = new StringBuilder();
            var xml = "";
            using (var writer = new StringWriter(stringBuilder))
            {
                serializer.Serialize(writer, quantity);
                xml = stringBuilder.ToString();
                Console.Write(xml);
                var expected = $"<?xml version=\"1.0\" encoding=\"utf-16\"?>" + Environment.NewLine +
                               $"<{quantity.GetType().Name} Value=\"1.2\" />";
                Assert.AreEqual(expected, xml);
            }
            using (var reader = new StringReader(xml))
            {
                var deserialized = serializer.Deserialize(reader);
                Assert.AreEqual(deserialized, quantity);
            }
        }

        [TestCaseSource(typeof(QuantityTypesProvider))]
        public void DataContractSerializer(Type quantityType)
        {
            var ctor = quantityType.GetConstructor(
                        BindingFlags.NonPublic | BindingFlags.Instance,
                        null,
                        new[] { typeof(double) },
                        null
                    );
            var quantity = ctor.Invoke(new object[] { 1.2 });
            var serializer = new DataContractSerializer(quantityType);
            var stringBuilder = new StringBuilder();
            var xml = "";
            using (var writer = XmlWriter.Create(stringBuilder))
            {
                serializer.WriteObject(writer, quantity);
                writer.Flush();
                xml = stringBuilder.ToString();
                Console.Write(xml);
                var expected = $"<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
                               $"<{quantity.GetType().Name} Value=\"1.2\" xmlns=\"http://schemas.datacontract.org/2004/07/Gu.Units\" />";
                Assert.AreEqual(expected, xml);
            }
            using (var reader = XmlReader.Create(new StringReader(xml)))
            {
                var deserialized = serializer.ReadObject(reader);
                Assert.AreEqual(deserialized, quantity);
            }
        }

        [TestCaseSource(typeof(QuantityTypesProvider))]
        public void BinaryFormatter(Type quantityType)
        {
            var ctor = quantityType.GetConstructor(
                        BindingFlags.NonPublic | BindingFlags.Instance,
                        null,
                        new[] { typeof(double) },
                        null
                    );
            var quantity = ctor.Invoke(new object[] { 1.2 });
            var binaryFormatter = new BinaryFormatter();
            var memoryStream = new MemoryStream();
            binaryFormatter.Serialize(memoryStream, quantity);
            memoryStream.Position = 0;
            var deserialized = binaryFormatter.Deserialize(memoryStream);
            Assert.AreEqual(deserialized, quantity);
        }
    }
{'fraction_non_alphanumeric': 0.10140549060817024, 'fraction_numerical': 0.0068304216471824514, 'mean_word_length': 1.606641561109209, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 4, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 27}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2347078', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1742, 'n_words': 362}