-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement reference comparer, improve tests
- Loading branch information
Showing
12 changed files
with
490 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Collections.Generic; | ||
|
||
using Equatable.Attributes; | ||
|
||
namespace Equatable.Entities; | ||
|
||
[Equatable] | ||
public partial class CustomLength | ||
{ | ||
public int Id { get; set; } | ||
|
||
public string Name { get; set; } = null!; | ||
|
||
[EqualityComparer(typeof(LengthComparerDefault))] | ||
public string? Key { get; set; } | ||
|
||
[EqualityComparer(typeof(LengthComparerInstance), nameof(LengthComparerInstance.Instance))] | ||
public string? Value { get; set; } | ||
|
||
} | ||
|
||
public static class LengthComparerDefault | ||
{ | ||
public static readonly LengthEqualityComparer Default = new(); | ||
} | ||
|
||
public static class LengthComparerInstance | ||
{ | ||
public static readonly LengthEqualityComparer Instance = new(); | ||
} | ||
|
||
public class LengthEqualityComparer : IEqualityComparer<string?> | ||
{ | ||
public bool Equals(string? x, string? y) => x?.Length == y?.Length; | ||
|
||
public int GetHashCode(string? obj) => obj?.Length.GetHashCode() ?? 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
using Equatable.Entities; | ||
|
||
namespace Equatable.Generator.Tests.Entities; | ||
|
||
public class AuditTest | ||
{ | ||
[Fact] | ||
public void EqualAuditTrue() | ||
{ | ||
var lockObject = new object(); | ||
|
||
var left = new Audit | ||
{ | ||
Id = 1, | ||
Date = new DateTime(2024, 9, 1), | ||
UserId = 1, | ||
TaskId = 2, | ||
Lock = lockObject | ||
}; | ||
|
||
var right = new Audit | ||
{ | ||
Id = 1, | ||
Date = new DateTime(2024, 9, 1), | ||
UserId = 1, | ||
TaskId = 2, | ||
Lock = lockObject | ||
}; | ||
|
||
var isEqual = left.Equals(right); | ||
isEqual.Should().BeTrue(); | ||
|
||
// check operator == | ||
isEqual = left == right; | ||
isEqual.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void NotEqualAudit() | ||
{ | ||
var left = new Audit | ||
{ | ||
Id = 1, | ||
Date = new DateTime(2024, 9, 1), | ||
UserId = 1, | ||
TaskId = 2, | ||
Lock = new object() | ||
}; | ||
|
||
var right = new Audit | ||
{ | ||
Id = 1, | ||
Date = new DateTime(2024, 9, 1), | ||
UserId = 1, | ||
TaskId = 2, | ||
Lock = new object() | ||
}; | ||
|
||
var isEqual = left.Equals(right); | ||
isEqual.Should().BeFalse(); | ||
|
||
// check operator != | ||
isEqual = left != right; | ||
isEqual.Should().BeTrue(); | ||
|
||
} | ||
|
||
[Fact] | ||
public void HashCodeAuditTrue() | ||
{ | ||
var lockObject = new object(); | ||
var left = new Audit | ||
{ | ||
Id = 1, | ||
Date = new DateTime(2024, 9, 1), | ||
UserId = 1, | ||
TaskId = 2, | ||
Lock = lockObject | ||
}; | ||
|
||
var right = new Audit | ||
{ | ||
Id = 1, | ||
Date = new DateTime(2024, 9, 1), | ||
UserId = 1, | ||
TaskId = 2, | ||
Lock = lockObject | ||
}; | ||
|
||
var leftCode = left.GetHashCode(); | ||
var rightCode = right.GetHashCode(); | ||
|
||
leftCode.Should().Be(rightCode); | ||
} | ||
|
||
[Fact] | ||
public void HashCodeAuditNotEqual() | ||
{ | ||
var left = new Audit | ||
{ | ||
Id = 1, | ||
Date = new DateTime(2024, 9, 1), | ||
UserId = 1, | ||
TaskId = 2, | ||
Lock = new object() | ||
}; | ||
|
||
var right = new Audit | ||
{ | ||
Id = 1, | ||
Date = new DateTime(2024, 9, 1), | ||
UserId = 1, | ||
TaskId = 2, | ||
Lock = new object() | ||
}; | ||
|
||
var leftCode = left.GetHashCode(); | ||
var rightCode = right.GetHashCode(); | ||
|
||
leftCode.Should().NotBe(rightCode); | ||
} | ||
} |
Oops, something went wrong.