Skip to content

Commit

Permalink
feat: Create comparer for strings (#55)
Browse files Browse the repository at this point in the history
* Add string equality comparer
* Separate analyzer and compilation unit definitions for enum
* Updated README
* Added more tests
* Separate analyzer and compilation unit definitions for enum for hashcodes as well
* More correct way to check enum value
  • Loading branch information
hayer authored Feb 2, 2024
1 parent a63fe59 commit 47e36ed
Show file tree
Hide file tree
Showing 21 changed files with 366 additions and 6 deletions.
13 changes: 13 additions & 0 deletions Generator.Equals.Runtime/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ public class SetEqualityAttribute : Attribute
{
}

[GeneratedCode("Generator.Equals", "1.0.0.0")]
[Conditional("GENERATOR_EQUALS")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class StringEqualityAttribute : Attribute
{
public StringComparison ComparisonType { get; }

public StringEqualityAttribute(StringComparison comparisonType)
{
ComparisonType = comparisonType;
}
}

[GeneratedCode("Generator.Equals", "1.0.0.0")]
[Conditional("GENERATOR_EQUALS")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
31 changes: 31 additions & 0 deletions Generator.Equals.Tests/Classes/StringEquality.Sample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;

namespace Generator.Equals.Tests.Classes
{
public partial class StringEquality
{
[Equatable]
public partial class SampleCaseInsensitive
{
public SampleCaseInsensitive(string name)
{
Name = name;
}

[StringEquality(StringComparison.CurrentCultureIgnoreCase)]
public string Name { get; }
}

[Equatable]
public partial class SampleCaseSensitive
{
public SampleCaseSensitive(string name)
{
Name = name;
}

[StringEquality(StringComparison.CurrentCulture)]
public string Name { get; }
}
}
}
55 changes: 55 additions & 0 deletions Generator.Equals.Tests/Classes/StringEquality.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace Generator.Equals.Tests.Classes
{
public partial class StringEquality
{
public class EqualsTestsNotCaseSensitive : EqualityTestCase
{
public override object Factory1() => new SampleCaseInsensitive("BAR");
public override object Factory2() => new SampleCaseInsensitive("bar");

public override bool EqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 == (SampleCaseInsensitive)value2;

public override bool NotEqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 != (SampleCaseInsensitive)value2;
}

public class NotEqualsTestsNotCaseSensitive : EqualityTestCase
{
public override object Factory1() => new SampleCaseInsensitive("BAR");
public override object Factory2() => new SampleCaseInsensitive("foo");
public override bool Expected => false;

public override bool EqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 == (SampleCaseInsensitive)value2;

public override bool NotEqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 != (SampleCaseInsensitive)value2;
}

public class EqualsTestsCaseSensitive : EqualityTestCase
{
public override object Factory1() => new SampleCaseSensitive("Foo");
public override object Factory2() => new SampleCaseSensitive("Foo");

public override bool EqualsOperator(object value1, object value2) =>
(SampleCaseSensitive)value1 == (SampleCaseSensitive)value2;

public override bool NotEqualsOperator(object value1, object value2) =>
(SampleCaseSensitive)value1 != (SampleCaseSensitive)value2;
}

public class NotEqualsTestsCaseSensitive : EqualityTestCase
{
public override object Factory1() => new SampleCaseSensitive("Foo");
public override object Factory2() => new SampleCaseSensitive("foo");
public override bool Expected => false;

public override bool EqualsOperator(object value1, object value2) =>
(SampleCaseSensitive)value1 == (SampleCaseSensitive)value2;

public override bool NotEqualsOperator(object value1, object value2) =>
(SampleCaseSensitive)value1 != (SampleCaseSensitive)value2;
}
}
}
17 changes: 17 additions & 0 deletions Generator.Equals.Tests/RecordStructs/StringEquality.Sample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace Generator.Equals.Tests.RecordStructs
{
public partial class StringEquality
{
[Equatable]
public partial record struct SampleCaseInsensitive(
[property: StringEquality(StringComparison.CurrentCultureIgnoreCase)]
string Name);

[Equatable]
public partial record struct SampleCaseSensitive(
[property: StringEquality(StringComparison.CurrentCulture)]
string Name);
}
}
30 changes: 30 additions & 0 deletions Generator.Equals.Tests/RecordStructs/StringEquality.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Generator.Equals.Tests.RecordStructs
{
public partial class StringEquality
{
public class EqualsTests : EqualityTestCase
{
public override object Factory1() => new SampleCaseInsensitive { Name = "BAR" };
public override object Factory2() => new SampleCaseInsensitive { Name = "bar" };

public override bool EqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 == (SampleCaseInsensitive)value2;

public override bool NotEqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 != (SampleCaseInsensitive)value2;
}

public class NotEqualsTest : EqualityTestCase
{
public override object Factory1() => new SampleCaseInsensitive { Name = "BAR" };
public override object Factory2() => new SampleCaseInsensitive { Name = "foo" };
public override bool Expected => false;

public override bool EqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 == (SampleCaseInsensitive)value2;

public override bool NotEqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 != (SampleCaseInsensitive)value2;
}
}
}
21 changes: 21 additions & 0 deletions Generator.Equals.Tests/Records/StringEquality.Sample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace Generator.Equals.Tests.Records
{
public partial class StringEquality
{
[Equatable]
public partial record SampleCaseInsensitive
{
[StringEquality(StringComparison.CurrentCultureIgnoreCase)]
public string Name { get; init; } = "";
}

[Equatable]
public partial record SampleCaseSensitive
{
[StringEquality(StringComparison.CurrentCulture)]
public string Name { get; init; } = "";
}
}
}
55 changes: 55 additions & 0 deletions Generator.Equals.Tests/Records/StringEquality.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace Generator.Equals.Tests.Records
{
public partial class StringEquality
{
public class EqualsTestsNotCaseSensitive : EqualityTestCase
{
public override object Factory1() => new SampleCaseInsensitive { Name = "BAR" };
public override object Factory2() => new SampleCaseInsensitive { Name = "bar" };

public override bool EqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 == (SampleCaseInsensitive)value2;

public override bool NotEqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 != (SampleCaseInsensitive)value2;
}

public class NotEqualsTestsNotCaseSensitive : EqualityTestCase
{
public override object Factory1() => new SampleCaseInsensitive { Name = "BAR" };
public override object Factory2() => new SampleCaseInsensitive { Name = "foo" };
public override bool Expected => false;

public override bool EqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 == (SampleCaseInsensitive)value2;

public override bool NotEqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 != (SampleCaseInsensitive)value2;
}

public class EqualsTestsCaseSensitive : EqualityTestCase
{
public override object Factory1() => new SampleCaseSensitive { Name = "Foo" };
public override object Factory2() => new SampleCaseSensitive { Name = "Foo" };

public override bool EqualsOperator(object value1, object value2) =>
(SampleCaseSensitive)value1 == (SampleCaseSensitive)value2;

public override bool NotEqualsOperator(object value1, object value2) =>
(SampleCaseSensitive)value1 != (SampleCaseSensitive)value2;
}

public class NotEqualsTestsCaseSensitive : EqualityTestCase
{
public override object Factory1() => new SampleCaseSensitive { Name = "Foo" };
public override object Factory2() => new SampleCaseSensitive { Name = "foo" };
public override bool Expected => false;

public override bool EqualsOperator(object value1, object value2) =>
(SampleCaseSensitive)value1 == (SampleCaseSensitive)value2;

public override bool NotEqualsOperator(object value1, object value2) =>
(SampleCaseSensitive)value1 != (SampleCaseSensitive)value2;
}
}
}
31 changes: 31 additions & 0 deletions Generator.Equals.Tests/Structs/StringEquality.Sample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;

namespace Generator.Equals.Tests.Structs
{
public partial class StringEquality
{
[Equatable]
public partial struct SampleCaseInsensitive
{
public SampleCaseInsensitive(string name)
{
Name = name;
}

[StringEquality(StringComparison.CurrentCultureIgnoreCase)]
public string Name { get; init; } = "";
}

[Equatable]
public partial struct SampleCaseSensitive
{
public SampleCaseSensitive(string name)
{
Name = name;
}

[StringEquality(StringComparison.CurrentCulture)]
public string Name { get; init; } = "";
}
}
}
30 changes: 30 additions & 0 deletions Generator.Equals.Tests/Structs/StringEquality.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Generator.Equals.Tests.Structs
{
public partial class StringEquality
{
public class EqualsTests : EqualityTestCase
{
public override object Factory1() => new SampleCaseInsensitive { Name = "BAR" };
public override object Factory2() => new SampleCaseInsensitive { Name = "bar" };

public override bool EqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 == (SampleCaseInsensitive)value2;

public override bool NotEqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 != (SampleCaseInsensitive)value2;
}

public class NotEqualsTest : EqualityTestCase
{
public override object Factory1() => new SampleCaseInsensitive { Name = "BAR" };
public override object Factory2() => new SampleCaseInsensitive { Name = "foo" };
public override bool Expected => false;

public override bool EqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 == (SampleCaseInsensitive)value2;

public override bool NotEqualsOperator(object value1, object value2) =>
(SampleCaseInsensitive)value1 != (SampleCaseInsensitive)value2;
}
}
}
15 changes: 11 additions & 4 deletions Generator.Equals/AttributesMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;

namespace Generator.Equals
{
Expand All @@ -11,17 +12,21 @@ public class AttributesMetadata
public INamedTypeSymbol UnorderedEquality { get; }
public INamedTypeSymbol ReferenceEquality { get; }
public INamedTypeSymbol SetEquality { get; }
public INamedTypeSymbol StringEquality { get; }
public INamedTypeSymbol CustomEquality { get; }
public ImmutableDictionary<long, string> StringComparisonLookup { get; }

public AttributesMetadata(
INamedTypeSymbol equatable,
INamedTypeSymbol defaultEquality,
INamedTypeSymbol orderedEquality,
INamedTypeSymbol ignoreEquality,
INamedTypeSymbol unorderedEquality,
INamedTypeSymbol referenceEquality,
INamedTypeSymbol unorderedEquality,
INamedTypeSymbol referenceEquality,
INamedTypeSymbol setEquality,
INamedTypeSymbol customEquality)
INamedTypeSymbol stringEquality,
INamedTypeSymbol customEquality,
ImmutableDictionary<long, string> stringComparisonLookup)
{
Equatable = equatable;
DefaultEquality = defaultEquality;
Expand All @@ -30,7 +35,9 @@ public AttributesMetadata(
UnorderedEquality = unorderedEquality;
ReferenceEquality = referenceEquality;
SetEquality = setEquality;
StringEquality = stringEquality;
CustomEquality = customEquality;
StringComparisonLookup = stringComparisonLookup;
}
}
}
Loading

0 comments on commit 47e36ed

Please sign in to comment.