Skip to content

Commit

Permalink
[Tests] Added tests to MVVM package
Browse files Browse the repository at this point in the history
  • Loading branch information
BAndysc committed Jan 22, 2021
1 parent 93aea77 commit 5e0e506
Show file tree
Hide file tree
Showing 9 changed files with 461 additions and 1 deletion.
90 changes: 90 additions & 0 deletions WDE.MVVM.Test/MVVM/NotifyPropertyChangedExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using NSubstitute;
using NUnit.Framework;

namespace WDE.MVVM.Test.MVVM
{
public class NotifyPropertyChangedExtensionsTest
{
[Test]
public void TestSimpleToObservable()
{
TestObjectNotifyPropertyChanged obj = new TestObjectNotifyPropertyChanged() {Number = 2};

var number = obj.ToObservable(o => o.Number);

var numberObserver = Substitute.For<IObserver<int>>();

using (number.Subscribe(numberObserver))
{
numberObserver.Received(1).OnNext(2);
obj.Number = 3;
numberObserver.Received(1).OnNext(3);
}

numberObserver.ReceivedWithAnyArgs(2).OnNext(default);
}

[Test]
public void TestToObservableCallsWhenNeeded()
{
TestObjectNotifyPropertyChanged obj = new TestObjectNotifyPropertyChanged() {Number = 2, StringValue = "abc"};

var number = obj.ToObservable(o => o.Number);

var numberObserver = Substitute.For<IObserver<int>>();

using (number.Subscribe(numberObserver))
{
numberObserver.Received(1).OnNext(2);
obj.StringValue = "edf";
}

numberObserver.ReceivedWithAnyArgs(1).OnNext(default);
}

[Test]
public void TestToObservableDoesNotAddListener()
{
TestObjectNotifyPropertyChanged obj = new TestObjectNotifyPropertyChanged() {Number = 2, StringValue = "abc"};
Assert.AreEqual(0, obj.EventHandlers);
obj.ToObservable(o => o.Number);
Assert.AreEqual(0, obj.EventHandlers);
}

[Test]
public void TestDisposeRemovesListener()
{
TestObjectNotifyPropertyChanged obj = new TestObjectNotifyPropertyChanged() {Number = 2, StringValue = "abc"};
Assert.AreEqual(0, obj.EventHandlers);
var number = obj.ToObservable(o => o.Number);
Assert.AreEqual(0, obj.EventHandlers);

using (number.Subscribe(Substitute.For<IObserver<int>>()))
{
Assert.AreEqual(1, obj.EventHandlers);
}

Assert.AreEqual(0, obj.EventHandlers);
}

[Test]
public void TestCannotObserveField()
{
TestObjectNotifyPropertyChanged obj = new();

Assert.Throws<ArgumentException>(() => obj.ToObservable(o => o.Field));
}

[Test]
public void TestCannotObserveAnyExpression()
{
TestObjectNotifyPropertyChanged obj = new();

Assert.Throws<ArgumentException>(() => obj.ToObservable(o => o.Number + 1));
}
}
}
93 changes: 93 additions & 0 deletions WDE.MVVM.Test/MVVM/ObservableBaseTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Diagnostics.CodeAnalysis;
using NUnit.Framework;
using WDE.MVVM.Observable;

namespace WDE.MVVM.Test.MVVM
{
public class ObservableBaseTest
{
[Test]
public void TestLinkObservable()
{
ReactiveProperty<int> property = new(4);
ObservableObjectLinkObservable o = new(property);

Assert.AreEqual(4, o.Number);

property.Value = 2;

Assert.AreEqual(2, o.Number);

o.Dispose();

Assert.AreEqual(0, property.SubscriptionsCount);
}

[Test]
public void TestLinkNotifyProperty()
{
TestObjectNotifyPropertyChanged source = new() {Number = 4};
ObservableObjectLinkNotifyPropertyChanged o = new(source);

Assert.AreEqual(4, o.Number);

source.Number = 2;

Assert.AreEqual(2, o.Number);

o.Dispose();

Assert.AreEqual(0, source.EventHandlers);
}


[Test]
public void TestWatch()
{
TestObjectNotifyPropertyChanged source = new() {Number = 4};
ObservableObjectWatchNotifyPropertyChanged o = new(source);

int calledPropertyChanged = 0;
o.PropertyChanged += (_, _) => calledPropertyChanged++;

source.Number = 2;
source.Number = 3;
source.Number = 4;

Assert.AreEqual(3, calledPropertyChanged);
}

[ExcludeFromCodeCoverage]
private class ObservableObjectLinkObservable : ObservableBase
{
public int Number { get; private set; }

public ObservableObjectLinkObservable(IObservable<int> observable)
{
Link(observable, () => Number);
}
}

[ExcludeFromCodeCoverage]
private class ObservableObjectLinkNotifyPropertyChanged : ObservableBase
{
public int Number { get; private set; }

public ObservableObjectLinkNotifyPropertyChanged(TestObjectNotifyPropertyChanged other)
{
Link(other, o => o.Number, () => Number);
}
}

[ExcludeFromCodeCoverage]
private class ObservableObjectWatchNotifyPropertyChanged : ObservableBase
{
public int Number { get; private set; }
public ObservableObjectWatchNotifyPropertyChanged(TestObjectNotifyPropertyChanged other)
{
Watch(other, o => o.Number, nameof(Number));
}
}
}
}
48 changes: 48 additions & 0 deletions WDE.MVVM.Test/MVVM/ObservableExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using NUnit.Framework;
using WDE.MVVM.Observable;

namespace WDE.MVVM.Test.MVVM
{
public class ObservableExtensionsTest
{
[Test]
public void TestSubscribeDelegate()
{
ReactiveProperty<int> property = new ReactiveProperty<int>(1);

int receivedValue = 0;

using (property.Subscribe(v => receivedValue = v))
{
Assert.AreEqual(1, receivedValue);
property.Value = 2;
Assert.AreEqual(2, receivedValue);
}

Assert.AreEqual(0, property.SubscriptionsCount);
}

[Test]
public void TestSubscribeINotify()
{
TestObjectNotifyPropertyChanged obj = new() {Number = 3};

int receivedValue = -1;

using (obj.Subscribe(o => o.Number, v => { receivedValue = v; }))
{
Assert.AreEqual(1, obj.EventHandlers);
Assert.AreEqual(3, receivedValue);

obj.Number = 2;

Assert.AreEqual(2, receivedValue);
}

Assert.AreEqual(0, obj.EventHandlers);
}
}
}
61 changes: 61 additions & 0 deletions WDE.MVVM.Test/MVVM/TestObjectNotifyPropertyChanged.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace WDE.MVVM.Test.MVVM
{
[ExcludeFromCodeCoverage]
internal sealed class TestObjectNotifyPropertyChanged : INotifyPropertyChanged
{
public int Field;
private int number;
private float floatNumber;
private string stringValue;

public int Number
{
get => number;
set
{
if (value == number)
return;
number = value;
OnPropertyChanged();
}
}

public float FloatNumber
{
get => floatNumber;
set
{
if (value.Equals(floatNumber))
return;
floatNumber = value;
OnPropertyChanged();
}
}

public string StringValue
{
get => stringValue;
set
{
if (value == stringValue)
return;
stringValue = value;
OnPropertyChanged();
}
}

public event PropertyChangedEventHandler? PropertyChanged;

private void OnPropertyChanged([CallerMemberName]
string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public int EventHandlers => PropertyChanged?.GetInvocationList().Length ?? 0;
}
}
50 changes: 50 additions & 0 deletions WDE.MVVM.Test/Observable/FunctionalExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using NSubstitute;
using NUnit.Framework;
using WDE.MVVM.Observable;

namespace WDE.MVVM.Test.Observable
{
public class FunctionalExtensionsTest
{
[Test]
public void TestSelect()
{
ReactiveProperty<int> property = new ReactiveProperty<int>(5);

var observer = Substitute.For<IObserver<int>>();
var doubleValue = property.Select(v => v * 2);

Assert.AreEqual(0, property.SubscriptionsCount);

using (doubleValue.Subscribe(observer))
{
Assert.AreEqual(1, property.SubscriptionsCount);
observer.Received(1).OnNext(10);
observer.ClearReceivedCalls();

property.Value = 4;
observer.Received(1).OnNext(8);
}
}

[Test]
public void TestNot()
{
ReactiveProperty<bool> property = new ReactiveProperty<bool>(false);

var observer = Substitute.For<IObserver<bool>>();
var not = property.Not();
Assert.AreEqual(0, property.SubscriptionsCount);

using (not.Subscribe(observer))
{
observer.Received(1).OnNext(true);
observer.ClearReceivedCalls();

property.Value = true;
observer.Received(1).OnNext(false);
}
}
}
}
Loading

0 comments on commit 5e0e506

Please sign in to comment.