1
0
Форкнуть 0

authentication: Adopt new type comparer.

Adopt the `InstallationComparer` to perform all equality comparison operation defined by the `Installation` type.
This commit is contained in:
J Wyman ∞ 2018-03-15 11:44:17 -04:00
Родитель 6490bdec41
Коммит ba1b79fa57
3 изменённых файлов: 17 добавлений и 27 удалений

Просмотреть файл

@ -52,11 +52,11 @@ namespace Microsoft.Alm.Authentication.Git.Test
KnownDistribution kgd = (KnownDistribution)v;
var a = list.Where(x => x.Version == kgd);
Assert.True(a.All(x => x != a.First() || Installation.PathComparer.Equals(x.Cmd, a.First().Cmd)));
Assert.True(a.All(x => x != a.First() || Installation.PathComparer.Equals(x.Config, a.First().Config)));
Assert.True(a.All(x => x != a.First() || Installation.PathComparer.Equals(x.Git, a.First().Git)));
Assert.True(a.All(x => x != a.First() || Installation.PathComparer.Equals(x.Libexec, a.First().Libexec)));
Assert.True(a.All(x => x != a.First() || Installation.PathComparer.Equals(x.Sh, a.First().Sh)));
Assert.True(a.All(x => x != a.First() || InstallationComparer.PathComparer.Equals(x.Cmd, a.First().Cmd)));
Assert.True(a.All(x => x != a.First() || InstallationComparer.PathComparer.Equals(x.Config, a.First().Config)));
Assert.True(a.All(x => x != a.First() || InstallationComparer.PathComparer.Equals(x.Git, a.First().Git)));
Assert.True(a.All(x => x != a.First() || InstallationComparer.PathComparer.Equals(x.Libexec, a.First().Libexec)));
Assert.True(a.All(x => x != a.First() || InstallationComparer.PathComparer.Equals(x.Sh, a.First().Sh)));
}
}
}

Просмотреть файл

@ -25,15 +25,13 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace Microsoft.Alm.Authentication.Git
{
public class Installation : Base, IEquatable<Installation>
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly StringComparer PathComparer = StringComparer.InvariantCultureIgnoreCase;
public static readonly IEqualityComparer<Installation> Comparer = new InstallationComparer();
internal const string GitExeName = @"git.exe";
internal const string AllVersionCmdPath = @"cmd";
@ -259,21 +257,16 @@ namespace Microsoft.Alm.Authentication.Git
public override bool Equals(object obj)
{
if (obj is Installation)
return this == (Installation)obj;
return false;
return (obj is Installation other
&& Equals(other))
|| base.Equals(obj);
}
public bool Equals(Installation other)
{
return this == other;
}
=> Comparer.Equals(this, other);
public override int GetHashCode()
{
return StringComparer.OrdinalIgnoreCase.GetHashCode(_path);
}
=> Comparer.GetHashCode(this);
public override string ToString()
{
@ -287,15 +280,10 @@ namespace Microsoft.Alm.Authentication.Git
&& FileSystem.FileExists(_git);
}
public static bool operator ==(Installation install1, Installation install2)
{
return install1._distribution == install2._distribution
&& PathComparer.Equals(install1._path, install2._path);
}
public static bool operator ==(Installation lhs, Installation rhs)
=> Comparer.Equals(lhs, rhs);
public static bool operator !=(Installation install1, Installation install2)
{
return !(install1 == install2);
}
public static bool operator !=(Installation lhs, Installation rhs)
=> !Comparer.Equals(lhs, rhs);
}
}

Просмотреть файл

@ -33,6 +33,8 @@ namespace Microsoft.Alm.Authentication.Git
/// </summary>
public class InstallationComparer : IEqualityComparer<Installation>
{
public static readonly StringComparer PathComparer = StringComparer.OrdinalIgnoreCase;
/// <summary>
/// Determines whether the specified objects are equal.
/// <para/>