<commit_before>﻿using System;
using System.Diagnostics.CodeAnalysis;
using Xunit;

namespace EPS.Utility.Tests.Unit
{
    public class GenericEqualityComparerTest
    {
        class A
        {
            public int Integer { get; set; }
            public string String { get; set; }

            public static GenericEqualityComparer<A> IntegerOnlyComparer = new GenericEqualityComparer<A>((a1, a2) => a1.Integer == a2.Integer);
            public static GenericEqualityComparer<A> AllPropertiesComparer = new GenericEqualityComparer<A>((a1, a2) => a1.Integer == a2.Integer && a1.String == a2.String);
        }

        [Fact]
        [SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", Justification = "We're testing the constructor!")]
        public void Constructor_ThrowsOnNullFunc()
        {
            Assert.Throws<ArgumentNullException>(() => { var comparer = new GenericEqualityComparer<A>(null); });
        }

        [Fact]
        [SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", Justification = "We're testing the constructor!")]
        public void Constructor_ThrowsOnNullHashGenerator()
        {
            Assert.Throws<ArgumentNullException>(() => { var comparer = new GenericEqualityComparer<A>((a,b) => a.Integer == b.Integer, null); });
        }

        [Fact]
        public void Compare_TrueOnMatchingObjectsWithSpecifiedPropertiesOnly()
        {
            A a = new A() { Integer = 1, String = "foo" }, b = new A() { Integer = 1, String = "bar" };
            Assert.Equal(a, a, A.IntegerOnlyComparer);
        }

        [Fact]
        public void Compare_TrueOnMatchingObjectsWithMultipleProperties()
        {
            A a = new A() { Integer = 1, String = "string" }, b = new A() { Integer = 1, String = "string" };
            Assert.Equal(a, a, A.AllPropertiesComparer);
        }

        [Fact]
        public void Compare_FalseOnMismatchedObjects()
        {
            A a = new A() { Integer = 1 }, b = new A() { Integer = 2 };
            Assert.NotEqual(a, b, A.IntegerOnlyComparer);
        }
    }
}
<commit_msg>added a constructor and refactored tests to avoid Gendarme 'duplication'<commit_after>﻿using System;
using System.Diagnostics.CodeAnalysis;
using Xunit;

namespace EPS.Utility.Tests.Unit
{
    public class GenericEqualityComparerTest
    {
        class A
        {
            public A(int integer, string @string)
            {
                Integer = integer;
                String = @string;
            }

            public int Integer { get; set; }
            public string String { get; set; }

            public static GenericEqualityComparer<A> IntegerOnlyComparer = new GenericEqualityComparer<A>((a1, a2) => a1.Integer == a2.Integer);
            public static GenericEqualityComparer<A> AllPropertiesComparer = new GenericEqualityComparer<A>((a1, a2) => a1.Integer == a2.Integer && a1.String == a2.String);
        }

        [Fact]
        [SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", Justification = "We're testing the constructor!")]
        public void Constructor_ThrowsOnNullFunc()
        {
            Assert.Throws<ArgumentNullException>(() => { var comparer = new GenericEqualityComparer<A>(null); });
        }

        [Fact]
        [SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", Justification = "We're testing the constructor!")]
        public void Constructor_ThrowsOnNullHashGenerator()
        {
            Assert.Throws<ArgumentNullException>(() => { var comparer = new GenericEqualityComparer<A>((a,b) => a.Integer == b.Integer, null); });
        }

        [Fact]
        public void Compare_TrueOnMatchingObjectsWithSpecifiedPropertiesOnly()
        {
            A a = new A(1, "foo"),
                b = new A(1, "bar");
            Assert.Equal(a, b, A.IntegerOnlyComparer);
        }

        [Fact]
        public void Compare_TrueOnMatchingObjectsWithMultipleProperties()
        {
            A a = new A(1, "string"), 
                b = new A(a.Integer, a.String);
            Assert.Equal(a, b, A.AllPropertiesComparer);
        }

        [Fact]
        public void Compare_FalseOnMismatchedObjects()
        {
            A a = new A(1, string.Empty), 
                b = new A(2, string.Empty);
            Assert.NotEqual(a, b, A.IntegerOnlyComparer);
        }
    }
}

{'fraction_non_alphanumeric': 0.1120727918331114, 'fraction_numerical': 0.010430537061695517, 'mean_word_length': 2.18966737438075, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 29, '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': '4887582', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1247, 'n_words': 284}