[Core] Fix resource GetHashCode with null Source

This commit is contained in:
Eric Maupin 2018-02-14 21:56:34 -05:00
Родитель 12d8d7f59b
Коммит 4d1df8b681
4 изменённых файлов: 89 добавлений и 9 удалений

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

@ -247,10 +247,14 @@ namespace Xamarin.PropertyEditing.Tests
var resource = new Resource ("name");
var target = new object ();
var resourcesMock = new Mock<IResourceProvider> ();
resourcesMock.Setup (rp => rp.GetResourcesAsync (new object(), mockProperty.Object, It.IsAny<CancellationToken> ())).ReturnsAsync (new[] { resource });
resourcesMock.Setup (rp => rp.GetResourcesAsync (target, mockProperty.Object, It.IsAny<CancellationToken> ())).ReturnsAsync (new[] { resource });
var vm = GetViewModel (mockProperty.Object, new[] { new Mock<IObjectEditor> ().Object });
var editorMock = new Mock<IObjectEditor> ();
editorMock.SetupGet (oe => oe.Target).Returns (target);
var vm = GetViewModel (mockProperty.Object, new[] { editorMock.Object });
Assume.That (vm.SetValueResourceCommand.CanExecute (resource), Is.False);
bool changed = false;
@ -269,10 +273,13 @@ namespace Xamarin.PropertyEditing.Tests
var resource = new Resource ("name");
var target = new object();
var resourcesMock = new Mock<IResourceProvider> ();
resourcesMock.Setup (rp => rp.GetResourcesAsync (new object(), mockProperty.Object, It.IsAny<CancellationToken> ())).ReturnsAsync (new[] { resource });
resourcesMock.Setup (rp => rp.GetResourcesAsync (target, mockProperty.Object, It.IsAny<CancellationToken> ())).ReturnsAsync (new[] { resource });
var vm = GetViewModel (mockProperty.Object, new[] { new Mock<IObjectEditor> ().Object });
var editorMock = new Mock<IObjectEditor> ();
editorMock.SetupGet (oe => oe.Target).Returns (target);
var vm = GetViewModel (mockProperty.Object, new[] { editorMock.Object });
vm.ResourceProvider = resourcesMock.Object;
Assume.That (vm.SetValueResourceCommand, Is.Not.Null);
@ -287,7 +294,10 @@ namespace Xamarin.PropertyEditing.Tests
var resourcesMock = new Mock<IResourceProvider>();
var vm = GetViewModel (mockProperty.Object, new[] { new Mock<IObjectEditor>().Object });
var editorMock = new Mock<IObjectEditor> ();
editorMock.SetupGet (oe => oe.Target).Returns (new object());
var vm = GetViewModel (mockProperty.Object, new[] { editorMock.Object });
vm.ResourceProvider = resourcesMock.Object;
Assert.That (vm.RequestResourceCommand.CanExecute (null), Is.True);
@ -378,10 +388,11 @@ namespace Xamarin.PropertyEditing.Tests
var resource = new Resource ("name");
var value = GetNonDefaultRandomTestValue ();
var resourcesMock = new Mock<IResourceProvider> ();
resourcesMock.Setup (rp => rp.GetResourcesAsync (new object(), mockProperty.Object, It.IsAny<CancellationToken> ())).ReturnsAsync (new[] { resource });
var editor = new MockObjectEditor (mockProperty.Object);
var resourcesMock = new Mock<IResourceProvider> ();
resourcesMock.Setup (rp => rp.GetResourcesAsync (editor.Target, mockProperty.Object, It.IsAny<CancellationToken> ())).ReturnsAsync (new[] { resource });
editor.ValueEvaluator = (info, o) => {
if (o == resource)
return value;

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

@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
namespace Xamarin.PropertyEditing.Tests
{
[TestFixture]
internal class ResourceTests
{
[Test]
public void NameEquality ()
{
const string name = "name";
const string name2 = "other";
var r = new Resource (name);
var r2 = new Resource (name);
var r3 = new Resource (name2);
Assert.That (r, Is.EqualTo (r2));
Assert.That (r, Is.Not.EqualTo (r3));
}
[Test]
public void SourceEquality ()
{
const string name = "name";
const string sourceName = "source";
var source = new ResourceSource (sourceName, isLocal: true);
var source2 = new ResourceSource (sourceName, isLocal: false);
Assume.That (source, Is.Not.EqualTo (source2));
var r = new Resource (source, name);
var r2 = new Resource (source, name);
var r3 = new Resource (name);
var r4 = new Resource (source2, name);
var r5 = new Resource (source, sourceName);
Assert.That (r, Is.EqualTo (r2));
Assert.That (r, Is.Not.EqualTo (r3));
Assert.That (r, Is.Not.EqualTo (r4));
Assert.That (r, Is.Not.EqualTo (r5));
}
[Test]
public void GetHashCodeSource ()
{
const string name = "name";
const string sourceName = "source";
var source = new ResourceSource (sourceName, isLocal: true);
var source2 = new ResourceSource (sourceName, isLocal: false);
Assume.That (source.GetHashCode(), Is.Not.EqualTo (source2.GetHashCode()));
var r = new Resource (source, name);
var r2 = new Resource (source, name);
var r3 = new Resource (name);
var r4 = new Resource (source2, name);
var r5 = new Resource (source, sourceName);
Assert.That (r.GetHashCode(), Is.EqualTo (r2.GetHashCode()));
Assert.That (r.GetHashCode(), Is.Not.EqualTo (r3.GetHashCode()));
Assert.That (r.GetHashCode(), Is.Not.EqualTo (r4.GetHashCode()));
Assert.That (r.GetHashCode(), Is.Not.EqualTo (r5.GetHashCode()));
}
}
}

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

@ -72,6 +72,7 @@
<Compile Include="NumericTests.cs" />
<Compile Include="NumericViewModelTests.cs" />
<Compile Include="ResourceSelectorViewModelTests.cs" />
<Compile Include="ResourceTests.cs" />
<Compile Include="SimpleCollectionViewTests.cs" />
<Compile Include="CommonColorTests.cs" />
<Compile Include="Helpers.cs" />

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

@ -78,7 +78,7 @@ namespace Xamarin.PropertyEditing
public override int GetHashCode ()
{
unchecked {
int hashCode = Source.GetHashCode();
int hashCode = Source?.GetHashCode() ?? 0;
hashCode = (hashCode * 397) ^ Name.GetHashCode();
return hashCode;
}