Skip to content

Commit 69f8c43

Browse files
committed
add tests for record types
1 parent 139bcf0 commit 69f8c43

File tree

6 files changed

+22
-7
lines changed

6 files changed

+22
-7
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,17 @@ public partial class UserImport
8989
public List<DateTimeOffset>? History { get; set; }
9090
}
9191
```
92+
93+
Works for `record` types too
94+
95+
```c#
96+
[Equatable]
97+
public partial record StatusRecord(
98+
int Id,
99+
[property: StringEquality(StringComparison.OrdinalIgnoreCase)] string Name,
100+
string? Description,
101+
int DisplayOrder,
102+
bool IsActive,
103+
[property: SequenceEquality] List<string> Versions
104+
);
105+
```

src/Equatable.SourceGenerator/EquatableGenerator.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Equatable.SourceGenerator;
1212
[Generator]
1313
public class EquatableGenerator : IIncrementalGenerator
1414
{
15+
private static SymbolDisplayFormat FullyQualifiedNullableFormat = SymbolDisplayFormat.FullyQualifiedFormat.WithMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier);
16+
1517
public void Initialize(IncrementalGeneratorInitializationContext context)
1618
{
1719
var provider = context.SyntaxProvider.ForAttributeWithMetadataName(
@@ -152,8 +154,7 @@ private static IEnumerable<IPropertySymbol> GetProperties(INamedTypeSymbol targe
152154

153155
private static EquatableProperty CreateProperty(List<Diagnostic> diagnostics, IPropertySymbol propertySymbol)
154156
{
155-
var format = SymbolDisplayFormat.FullyQualifiedFormat.WithMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier);
156-
var propertyType = propertySymbol.Type.ToDisplayString(format);
157+
var propertyType = propertySymbol.Type.ToDisplayString(FullyQualifiedNullableFormat);
157158
var propertyName = propertySymbol.Name;
158159

159160
// look for custom equality

test/Equatable.Entities/StatusRecord.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Equatable.Entities;
77
[Equatable]
88
public sealed partial record StatusRecord(
99
int Id,
10-
string Name,
10+
[property: StringEquality(StringComparison.OrdinalIgnoreCase)] string Name,
1111
string? Description,
1212
int DisplayOrder,
1313
bool IsActive,

test/Equatable.Entities/StatusRecordStruct.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Equatable.Entities;
77
[Equatable]
88
public partial record struct StatusRecordStruct(
99
int Id,
10-
string Name,
10+
[property: StringEquality(StringComparison.OrdinalIgnoreCase)] string Name,
1111
string? Description,
1212
int DisplayOrder,
1313
bool IsActive,

test/Equatable.Generator.Tests/EquatableGeneratorTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ namespace Equatable.Entities;
228228
[Equatable]
229229
public partial record StatusRecordList(
230230
int Id,
231-
string Name,
231+
[property: StringEquality(StringComparison.OrdinalIgnoreCase)] string Name,
232232
string? Description,
233233
int DisplayOrder,
234234
bool IsActive,

test/Equatable.Generator.Tests/Snapshots/EquatableGeneratorTest.GenerateRecordSequence.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Equatable.Entities
1010
{
1111
return !(other is null)
1212
&& global::System.Collections.Generic.EqualityComparer<global::System.Int32>.Default.Equals(Id, other.Id)
13-
&& global::System.Collections.Generic.EqualityComparer<global::System.String>.Default.Equals(Name, other.Name)
13+
&& global::System.StringComparer.OrdinalIgnoreCase.Equals(Name, other.Name)
1414
&& global::System.Collections.Generic.EqualityComparer<global::System.String?>.Default.Equals(Description, other.Description)
1515
&& global::System.Collections.Generic.EqualityComparer<global::System.Int32>.Default.Equals(DisplayOrder, other.DisplayOrder)
1616
&& global::System.Collections.Generic.EqualityComparer<global::System.Boolean>.Default.Equals(IsActive, other.IsActive)
@@ -33,7 +33,7 @@ namespace Equatable.Entities
3333
public override int GetHashCode(){
3434
int hashCode = 1051616673;
3535
hashCode = (hashCode * -1521134295) + global::System.Collections.Generic.EqualityComparer<global::System.Int32>.Default.GetHashCode(Id!);
36-
hashCode = (hashCode * -1521134295) + global::System.Collections.Generic.EqualityComparer<global::System.String>.Default.GetHashCode(Name!);
36+
hashCode = (hashCode * -1521134295) + global::System.StringComparer.OrdinalIgnoreCase.GetHashCode(Name!);
3737
hashCode = (hashCode * -1521134295) + global::System.Collections.Generic.EqualityComparer<global::System.String?>.Default.GetHashCode(Description!);
3838
hashCode = (hashCode * -1521134295) + global::System.Collections.Generic.EqualityComparer<global::System.Int32>.Default.GetHashCode(DisplayOrder!);
3939
hashCode = (hashCode * -1521134295) + global::System.Collections.Generic.EqualityComparer<global::System.Boolean>.Default.GetHashCode(IsActive!);

0 commit comments

Comments
 (0)