<commit_before>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SerializationTestHelper.cs" company="Catel development team">
//   Copyright (c) 2008 - 2015 Catel development team. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------


namespace Catel.Test.Runtime.Serialization
{
    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Linq;
    using Catel.Data;
    using Catel.Runtime.Serialization;
    using Catel.Runtime.Serialization.Binary;

    public static class SerializationTestHelper
    {
        /// <summary>
        /// Serializes and deserializes using the specified serializer.
        /// </summary>
        /// <typeparam name="TModel">The type of the T model.</typeparam>
        /// <param name="model">The model.</param>
        /// <param name="serializer">The serializer.</param>
        /// <returns>System.Object.</returns>
        public static TModel SerializeAndDeserialize<TModel>(TModel model, ISerializer serializer)
        {
            using (var memoryStream = new MemoryStream())
            {
                serializer.Serialize(model, memoryStream);

                memoryStream.Position = 0L;

                if (!(serializer is BinarySerializer))
                {
                    var streamReader = new StreamReader(memoryStream);
                    var streamAsText = streamReader.ReadToEnd();

                    Console.WriteLine(streamAsText);

                    memoryStream.Position = 0L;
                }

                return (TModel)serializer.Deserialize(typeof (TModel), memoryStream);
            }
        }

        public static string ToXmlString(this object model)
        {
            Argument.IsNotNull(() => model);

            using (var memoryStream = new MemoryStream())
            {
                var xmlSerializer = SerializationFactory.GetXmlSerializer();
                xmlSerializer.Serialize(model, memoryStream);

                memoryStream.Position = 0L;
                using (var xmlReader = XmlReader.Create(memoryStream))
                {
                    return XDocument.Load(xmlReader).ToString();
                }
            }
        }

        public static T FromXmlString<T>(this string xml) 
            where T : ModelBase<commit_msg>Only write serialized data to output when debugger is attached (during unit tests)<commit_after>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SerializationTestHelper.cs" company="Catel development team">
//   Copyright (c) 2008 - 2015 Catel development team. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------


namespace Catel.Test.Runtime.Serialization
{
    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Linq;
    using Catel.Data;
    using Catel.Runtime.Serialization;
    using Catel.Runtime.Serialization.Binary;
    using System.Diagnostics;

    public static class SerializationTestHelper
    {
        /// <summary>
        /// Serializes and deserializes using the specified serializer.
        /// </summary>
        /// <typeparam name="TModel">The type of the T model.</typeparam>
        /// <param name="model">The model.</param>
        /// <param name="serializer">The serializer.</param>
        /// <returns>System.Object.</returns>
        public static TModel SerializeAndDeserialize<TModel>(TModel model, ISerializer serializer)
        {
            using (var memoryStream = new MemoryStream())
            {
                serializer.Serialize(model, memoryStream);

                memoryStream.Position = 0L;

                if (Debugger.IsAttached)
                {
                    if (!(serializer is BinarySerializer))
                    {
                        var streamReader = new StreamReader(memoryStream);
                        var streamAsText = streamReader.ReadToEnd();

                        Console.WriteLine(streamAsText);

                        memoryStream.Position = 0L;
                    }
                }

                return (TModel)serializer.Deserialize(typeof(TModel), memoryStream);
            }
        }

        public static string ToXmlString(this object model)
        {
            Argument.IsNotNull(() => model);

            using (var memoryStream = new MemoryStream())
            {
                var xmlSerializer = SerializationFactory.GetXmlSerializer();
                xmlSerializer.Serialize(model, memoryStream);

                memoryStream.Position = 0L;
                using (var xmlReader = XmlReader.Create(memoryStream))
                {
                    return XDocument.Load(xmlReader).ToString();
                }
            }
        }

        public static T FromXmlString<T>(this string xml)
            where T : ModelBase
{'fraction_non_alphanumeric': 0.17721273291925466, 'fraction_numerical': 0.004270186335403727, 'mean_word_length': 1.4023310023310023, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '1699306', 'n_tokens_mistral': 1174, 'n_tokens_neox': 1103, 'n_words': 272}