<commit_before>﻿using Amadevus.RecordGenerator.TestsBase;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Amadevus.RecordGenerator.Test
{
    public class GeneratedCodeAttributeGeneratorTests : RecordTestsBase
    {
        [Fact]
        public void ReflectedClass_DoesNotHaveGeneratedCodeAttribute()
        {
            var recordType = typeof(Item);

            var result = HasCorrectCodeGeneratedAttribute(recordType.GetCustomAttributesData());

            Assert.False(result);
        }

        [Fact]
        public void ReflectedClassMethod_DoesHaveGeneratedCodeAttribute()
        {
            var recordType = typeof(Item);
            var method = recordType.GetMethod(nameof(Item.Update));

            var result = HasCorrectCodeGeneratedAttribute(method.GetCustomAttributesData());

            Assert.True(result);
        }

        [Fact]
        public void ReflectedClassNestedType_DoesNotHaveGeneratedCodeAttribute()
        {
            var recordType = typeof(Item);
            var nestedType = recordType.GetNestedType(nameof(Item.Builder));

            var result = HasCorrectCodeGeneratedAttribute(nestedType.GetCustomAttributesData());

            Assert.False(result);
        }

        [Fact]
        public void ReflectedClassNestedTypeProperty_DoesHaveGeneratedCodeAttribute()
        {
            var recordType = typeof(Item);
            var nestedTypeMethod = recordType.GetNestedType(nameof(Item.Builder))
                .GetProperty(nameof(Item.Builder.Name));

            var result = HasCorrectCodeGeneratedAttribute(nestedTypeMethod.GetCustomAttributesData());

            Assert.True(result);
        }

        [Fact]
        public void ReflectedClassConstructor_DoesHaveGeneratedCodeAttribute()
        {
            var recordType = typeof(Item);
            var constructor = recordType.GetConstructor(new[] { typeof(string), typeof(string) });

            var result = HasCorrectCodeGeneratedAttribute(constructor.GetCustomAttributesData());

            Assert.True(result);
        }

        [Fact]
        public void ReflectedClassOperators_DoesHaveGeneratedCodeAttribute()
        {
            var recordType = typeof(Item);
            var method = recordType.GetMethod("op_Equality", BindingFlags.Public | BindingFlags.Static);

            var result = HasCorrectCodeGeneratedAttribute(method.GetCustomAttributesData());

            Assert.True(result);
        }

        private bool HasCorrectCodeGeneratedAttribute(IList<CustomAttributeData> attributeData)
        {
            var codeGeneratedAttributeData = attributeData
                .Where(d => d.AttributeType.Equals(typeof(GeneratedCodeAttribute)));

            if (!codeGeneratedAttributeData.Any()) return false;

            var codeGeneratedAttribute = codeGeneratedAttributeData.Single();

            var toolName = codeGeneratedAttribute.ConstructorArguments.First().Value;
            var expectedToolName = "Amadevus.RecordGenerator";
            if (!toolName.Equals(expectedToolName)) return false;

            var toolVersion = codeGeneratedAttribute.ConstructorArguments.Skip(1).First().Value;
            var expectedToolVersion = GetType().Assembly.GetName().Version.ToString();
            if (!toolVersion.Equals(expectedToolVersion)) return false;

            return true;
        }
    }
}<commit_msg>tests: refactor GeneratedCodeAttribute tests (#92)<commit_after>﻿using System;
using System.CodeDom.Compiler;
using System.Linq.Expressions;
using System.Reflection;
using Amadevus.RecordGenerator.TestsBase;
using FluentAssertions;
using Xunit;

namespace Amadevus.RecordGenerator.Test
{
    public class GeneratedCodeAttributeGeneratorTests : RecordTestsBase
    {
        [Fact]
        public void ReflectedClass_DoesNotHaveGeneratedCodeAttribute()
        {
            var recordType = typeof(Item);

            recordType.Should()
                .NotBeDecoratedWith(GeneratedCodeAttributeWithCorrectToolNameAndVersion);
        }

        [Fact]
        public void ReflectedClassConstructor_DoesHaveGeneratedCodeAttribute()
        {
            var recordType = typeof(Item);
            var constructor = recordType.GetConstructor(new[] { typeof(string), typeof(string) });

            constructor.Should()
                .BeDecoratedWith(GeneratedCodeAttributeWithCorrectToolNameAndVersion);
        }

        [Fact]
        public void ReflectedClassMethod_DoesHaveGeneratedCodeAttribute()
        {
            var recordType = typeof(Item);
            var method = recordType.GetMethod(nameof(Item.Update));

            method.Should()
                .BeDecoratedWith(GeneratedCodeAttributeWithCorrectToolNameAndVersion);
        }

        [Fact]
        public void ReflectedClassOperator_DoesHaveGeneratedCodeAttribute()
        {
            var recordType = typeof(Item);
            var method = recordType.GetMethod("op_Equality", BindingFlags.Public | BindingFlags.Static);

            method.Should()
                .BeDecoratedWith(GeneratedCodeAttributeWithCorrectToolNameAndVersion);
        }

        [Fact]
        public void ReflectedClassNestedType_DoesNotHaveGeneratedCodeAttribute()
        {
            var recordType = typeof(Item);
            var nestedType = recordType.GetNestedType(nameof(Item.Builder));

            nestedType.Should()
                .NotBeDecoratedWith(GeneratedCodeAttributeWithCorrectToolNameAndVersion);
        }

        [Fact]
        public void ReflectedClassNestedTypeProperty_DoesHaveGeneratedCodeAttribute()
        {
            var recordType = typeof(Item);
            var nestedTypeMember = recordType.GetNestedType(nameof(Item.Builder))
                .GetProperty(nameof(Item.Builder.Name));

            nestedTypeMember.Should()
                .BeDecoratedWith(GeneratedCodeAttributeWithCorrectToolNameAndVersion);
        }

        private static Expression<Func<GeneratedCodeAttribute, bool>> GeneratedCodeAttributeWithCorrectToolNameAndVersion =>
            attribute => attribute.Tool == "Amadevus.RecordGenerator"
            && attribute.Version == ThisAssembly.AssemblyVersion;
    }
}
{'fraction_non_alphanumeric': 0.07970784375992379, 'fraction_numerical': 0.000476341695776437, 'mean_word_length': 2.7561121049493145, 'pattern_counts': {'":': 0, '<': 6, '<?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': '2499477', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1474, 'n_words': 256}