Browse Source

Make comparing IndexPath with null do something useful.

pull/3469/head
Steven Kirk 6 years ago
parent
commit
27e11150b7
  1. 7
      src/Avalonia.Controls/IndexPath.cs
  2. 14
      tests/Avalonia.Controls.UnitTests/IndexPathTests.cs

7
src/Avalonia.Controls/IndexPath.cs

@ -58,6 +58,11 @@ namespace Avalonia.Controls
public int GetAt(int index)
{
if (index >= GetSize())
{
throw new IndexOutOfRangeException();
}
return _path?[index] ?? (_index - 1);
}
@ -169,5 +174,7 @@ namespace Avalonia.Controls
public static bool operator >=(IndexPath x, IndexPath y) { return x.CompareTo(y) >= 0; }
public static bool operator ==(IndexPath x, IndexPath y) { return x.CompareTo(y) == 0; }
public static bool operator !=(IndexPath x, IndexPath y) { return x.CompareTo(y) != 0; }
public static bool operator ==(IndexPath? x, IndexPath? y) { return (x ?? default).CompareTo(y ?? default) == 0; }
public static bool operator !=(IndexPath? x, IndexPath? y) { return (x ?? default).CompareTo(y ?? default) != 0; }
}
}

14
tests/Avalonia.Controls.UnitTests/IndexPathTests.cs

@ -77,5 +77,19 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(0, a.CompareTo(b));
Assert.Equal(a.GetHashCode(), b.GetHashCode());
}
[Fact]
public void Null_Equality()
{
var a = new IndexPath(null);
var b = new IndexPath(1);
// Implementing operator == on a struct automatically implements an operator which
// accepts null, so make sure this does something useful.
Assert.True(a == null);
Assert.False(a != null);
Assert.False(b == null);
Assert.True(b != null);
}
}
}

Loading…
Cancel
Save