<commit_before>﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DesertOctopus.Exceptions;
using DesertOctopus.Serialization;
using DesertOctopus.Utilities;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SerializerTests.TestObjects;

namespace DesertOctopus.Tests
{
    [TestClass]
    public class SerializerTest : BaseDuplicationTest
    {
        public override T Duplicate<T>(T obj)
        {
            var bytes = Serializer.Serialize(obj);
            return Deserializer.Deserialize<T>(bytes);
        }



        private TestContext _testContextInstance;
        public TestContext TestContext
        {
            get
            {
                return _testContextInstance;
            }
            set
            {
                _testContextInstance = value;
            }
        }
        [TestMethod]
        [TestCategory("Unit")]
        public void MyTestMethod()
        {
            string str = "";
            foreach (var key in TestContext.Properties.Keys)
            {
                str += key.ToString() + " = ";
                str += TestContext.Properties[key]?.ToString();
                str += "\r\n";
            }
            Assert.Fail(str);
        }

        [TestMethod]
        [TestCategory("Unit")]
        [ExpectedException(typeof(TypeNotFoundException))]
        public void DeserializeUnknownType()
        {
            ClassWithDynamicProperty instance = new ClassWithDynamicProperty { Value = 123 };
            var bytes = Serializer.Serialize(instance);
            bytes[25] = 54;

            Deserializer.Deserialize<ClassWithDynamicProperty>(bytes);
        }

        [TestMethod]
        [TestCategory("Unit")]
        [ExpectedException(typeof(TypeWasModifiedSinceSerializationException))]
        public void DeserializeTypeThatWasModified()
        {
            ClassWithDynamicProperty instance = new ClassWithDynamicProperty { Value = 123 };
            var bytes = Serializer.Serialize(instance);

            var needle = SerializedTypeResolver.GetShortNameFromType(typeof(ClassWithDynamicProperty));
            var index = System.Text.Encoding.UTF8.GetString(bytes).IndexOf(needle);

            // this is a hackish way to change the hashcode of a serialized object
            // if the way/order (currently TypeName + Hash) that an object is serialized changes the line below will need to be modified to target a byte of the hashcode<commit_msg>Remove 'MyTestMethod'<commit_after>﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DesertOctopus.Exceptions;
using DesertOctopus.Serialization;
using DesertOctopus.Utilities;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SerializerTests.TestObjects;

namespace DesertOctopus.Tests
{
    [TestClass]
    public class SerializerTest : BaseDuplicationTest
    {
        public override T Duplicate<T>(T obj)
        {
            var bytes = Serializer.Serialize(obj);
            return Deserializer.Deserialize<T>(bytes);
        }

        [TestMethod]
        [TestCategory("Unit")]
        [ExpectedException(typeof(TypeNotFoundException))]
        public void DeserializeUnknownType()
        {
            ClassWithDynamicProperty instance = new ClassWithDynamicProperty { Value = 123 };
            var bytes = Serializer.Serialize(instance);
            bytes[25] = 54;

            Deserializer.Deserialize<ClassWithDynamicProperty>(bytes);
        }

        [TestMethod]
        [TestCategory("Unit")]
        [ExpectedException(typeof(TypeWasModifiedSinceSerializationException))]
        public void DeserializeTypeThatWasModified()
        {
            ClassWithDynamicProperty instance = new ClassWithDynamicProperty { Value = 123 };
            var bytes = Serializer.Serialize(instance);

            var needle = SerializedTypeResolver.GetShortNameFromType(typeof(ClassWithDynamicProperty));
            var index = System.Text.Encoding.UTF8.GetString(bytes).IndexOf(needle);

            // this is a hackish way to change the hashcode of a serialized object
            // if the way/order (currently TypeName + Hash) that an object is serialized changes the line below will need to be modified to target a byte of the hashcode
{'fraction_non_alphanumeric': 0.07961208840775823, 'fraction_numerical': 0.004961659900766802, 'mean_word_length': 2.559390048154093, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3510197', 'n_tokens_mistral': 1115, 'n_tokens_neox': 1076, 'n_words': 271}