<commit_before>﻿using ExtendedXmlSerializer.Configuration;
using ExtendedXmlSerializer.ContentModel;
using ExtendedXmlSerializer.ContentModel.Content;
using ExtendedXmlSerializer.ContentModel.Format;
using ExtendedXmlSerializer.ExtensionModel.Xml;
using ExtendedXmlSerializer.Tests.Support;
using FluentAssertions;
using System.Reflection;
using System.Text;
using Xunit;

namespace ExtendedXmlSerializer.Tests.ReportedIssues
{
	public sealed class Issue253Tests
	{
		[Fact]
		void Verify()
		{
			var instance = new ClassWithEncodingProperty{ Encoding = Encoding.ASCII };

			var serializer = new ConfigurationContainer().Register<Encoding, EncodingSerializer>()
			                                             .Create()
			                                             .ForTesting();
			serializer.Cycle(instance).ShouldBeEquivalentTo(instance);
		}

		[Fact]
		void VerifyAlternativeRegister()
		{
			var instance = new ClassWithEncodingProperty{ Encoding = Encoding.ASCII };

			var serializer = new ConfigurationContainer().Type<Encoding>()
			                                             .Register(typeof(EncodingSerializer))
			                                             .Create()
			                                             .ForTesting();
			serializer.Cycle(instance).ShouldBeEquivalentTo(instance);
		}

		public class ClassWithEncodingProperty
		{
			public ClassWithEncodingProperty() => Encoding = Encoding.Default;

			public Encoding Encoding { get; set; }
		}

		sealed class EncodingSerializer : ISerializer<Encoding>
		{
			readonly ISerializer _serializer;

			public EncodingSerializer(IContents contents) : this(contents.Get(typeof(int).GetTypeInfo())) {}

			public EncodingSerializer(ISerializer serializer) => _serializer = serializer;

			public Encoding Get(IFormatReader parameter) => Encoding.GetEncoding((int)_serializer.Get(parameter));

			public void Write(IFormatWriter writer, Encoding instance)
			{
				_serializer.Write(writer, instance.CodePage);
			}
		}

	}
}
<commit_msg>Additional tests for #253.<commit_after>﻿using ExtendedXmlSerializer.Configuration;
using ExtendedXmlSerializer.ContentModel;
using ExtendedXmlSerializer.ContentModel.Content;
using ExtendedXmlSerializer.ContentModel.Format;
using ExtendedXmlSerializer.ExtensionModel.Xml;
using ExtendedXmlSerializer.Tests.Support;
using FluentAssertions;
using System.Reflection;
using System.Text;
using Xunit;

namespace ExtendedXmlSerializer.Tests.ReportedIssues
{
	public sealed class Issue253Tests
	{
		[Fact]
		void Verify()
		{
			var serializer = new ConfigurationContainer().Register<Encoding, EncodingSerializer>()
			                                             .Create()
			                                             .ForTesting();

			var instance = new ClassWithEncodingProperty{ Encoding = Encoding.ASCII };
			var classWithEncodingProperty = serializer.Cycle(instance);
			classWithEncodingProperty.ShouldBeEquivalentTo(instance);
		}

		[Fact]
		void VerifyAlternativeRegister()
		{
			var instance = new ClassWithEncodingProperty{ Encoding = Encoding.ASCII };

			var serializer = new ConfigurationContainer().Type<Encoding>()
			                                             .Register(typeof(EncodingSerializer))
			                                             .Create()
			                                             .ForTesting();
			serializer.Cycle(instance).ShouldBeEquivalentTo(instance);
		}

		[Fact]
		void VerifyAdvanced()
		{
			var instance = new Settings{ DbSettings = new DBVCSV {Encoding = Encoding.ASCII}};
			var serializer = new ConfigurationContainer().Type<Encoding>()
			                                             .Register(typeof(EncodingSerializer))
			                                             .Create()
			                                             .ForTesting();
			serializer.Cycle(instance).ShouldBeEquivalentTo(instance);
		}

		public class Settings
		{
			public DbSettings DbSettings { get; set; }
		}

		public class DbSettings
		{
			public Encoding Encoding { get; set; }
		}

		public class DBVCSV : DbSettings
		{

		}

		public class ClassWithEncodingProperty
		{
			public ClassWithEncodingProperty() => Encoding = Encoding.Default;

			public Encoding Encoding { get; set; }
		}

		sealed class EncodingSerializer : ISerializer<Encoding>
		{
			readonly ISerializer _serializer;

			public EncodingSerializer(IContents contents) : this(contents.Get(typeof(int).GetTypeInfo())) {}

			public EncodingSerializer(ISerializer serializer) => _serializer = serializer;

			public Encoding Get(IFormatReader parameter) => Encoding.GetEncoding((int)_serializer.Get(parameter));

			public void Write(IFormatWriter writer, Encoding instance)
			{
				_serializer.Write(writer, instance.CodePage);
			}
		}

	}
}

{'fraction_non_alphanumeric': 0.08401424680494447, 'fraction_numerical': 0.0018856065367693275, 'mean_word_length': 2.776898734177215, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5395660', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1149, 'n_words': 248}