From f0a317cc511a4a7b04a701c32881d1e3331f3711 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 16 May 2019 21:57:56 +0800 Subject: Change roles in UserInfo into isadmin. --- Timeline.Tests/Helpers/UserInfoComparers.cs | 49 +++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Timeline.Tests/Helpers/UserInfoComparers.cs (limited to 'Timeline.Tests/Helpers/UserInfoComparers.cs') diff --git a/Timeline.Tests/Helpers/UserInfoComparers.cs b/Timeline.Tests/Helpers/UserInfoComparers.cs new file mode 100644 index 00000000..d88b6622 --- /dev/null +++ b/Timeline.Tests/Helpers/UserInfoComparers.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Timeline.Entities; + +namespace Timeline.Tests.Helpers +{ + public static class UserInfoComparers + { + public static IEqualityComparer EqualityComparer { get; } = new EqualityComparerImpl(); + public static IComparer Comparer { get; } = Comparer.Create(Compare); + + + private class EqualityComparerImpl : IEqualityComparer + { + bool IEqualityComparer.Equals(UserInfo x, UserInfo y) + { + return Compare(x, y) == 0; + } + + int IEqualityComparer.GetHashCode(UserInfo obj) + { + return obj.Username.GetHashCode() ^ obj.IsAdmin.GetHashCode(); + } + } + + public static int Compare(UserInfo left, UserInfo right) + { + if (left == null) + { + if (right == null) + return 0; + return -1; + } + + if (right == null) + return 1; + + var uc = string.Compare(left.Username, right.Username); + if (uc != 0) + return uc; + + if (left.IsAdmin == right.IsAdmin) + return 0; + + return left.IsAdmin ? -1 : 1; + } + } +} -- cgit v1.2.3