Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated IComputed interface and tests #2

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 53 additions & 10 deletions src/SystemsR3.Tests/Plugins/Computeds/ComputedFromDataTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using R3;
using SystemsR3.Extensions;
using SystemsR3.Tests.Plugins.Computeds.Models;
using Xunit;

Expand All @@ -17,45 +18,87 @@ public void should_populate_on_creation()
}

[Fact]
public void should_refresh_value_when_changed_and_value_requested()
public void should_refresh_value_and_raise_event_when_data_changed_and_refreshed_implicitly()
{
var expectedData = 20;
var hasNotified = false;
var data = new DummyData{Data = 10};

var computedData = new TestComputedFromData(data);
computedData.Subscribe(x => hasNotified = true);

data.Data = expectedData;
computedData.ManuallyRefresh.OnNext(Unit.Default);

var actualData = computedData.Value;
Assert.Equal(expectedData, actualData);
Assert.True(hasNotified);
}

[Fact]
public void should_not_refresh_value_when_not_changed_and_value_requested()
public void should_refresh_value_and_raise_event_when_data_changed_and_refreshed_explicitly()
{
var expectedData = 20;
var data = new DummyData{Data = expectedData};
var hasNotified = false;
var data = new DummyData{Data = 10};

var computedData = new TestComputedFromData(data);
computedData.Subscribe(x => hasNotified = true);

data.Data = 10;
data.Data = expectedData;
computedData.RefreshData();

var actualData = computedData.Value;
Assert.Equal(expectedData, actualData);
Assert.True(hasNotified);
}

[Fact]
public void should_refresh_data_when_changed_with_subs()
public void should_not_refresh_value_when_datasource_changed_but_not_refreshed()
{
var expectedData = 10;
var data = new DummyData{Data = 20};
var expectedData = 20;
var hasNotified = false;
var data = new DummyData{Data = expectedData};

var computedData = new TestComputedFromData(data);
data.Data = expectedData;
computedData.Subscribe(x => hasNotified = true);
data.Data = 10;

var actualData = computedData.Value;
Assert.Equal(expectedData, actualData);
Assert.False(hasNotified);
}

[Fact]
public void should_not_refresh_value_or_notify_when_datasource_not_changed_even_when_refreshed_implicitly()
{
var expectedData = 20;
var hasNotified = false;
var data = new DummyData{Data = expectedData};

var computedData = new TestComputedFromData(data);
computedData.Subscribe(x => hasNotified = true);
computedData.ManuallyRefresh.OnNext(Unit.Default);

var actualData = computedData.Value;
Assert.Equal(expectedData, actualData);
Assert.False(hasNotified);
}

[Fact]
public void should_not_refresh_value_or_notify_when_datasource_not_changed_even_when_refreshed_explicitly()
{
var expectedData = 20;
var hasNotified = false;
var data = new DummyData{Data = expectedData};

Assert.Equal(expectedData, computedData.CachedData);
}
var computedData = new TestComputedFromData(data);
computedData.Subscribe(x => hasNotified = true);
computedData.RefreshData();

var actualData = computedData.Value;
Assert.Equal(expectedData, actualData);
Assert.False(hasNotified);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using R3;
using SystemsR3.Extensions;
using SystemsR3.Tests.Plugins.Computeds.Models;
using Xunit;

namespace SystemsR3.Tests.Plugins.Computeds
{
public class ComputedFromObservableTests
{
[Fact]
public void should_populate_on_creation()
{
var expectedData = 10;
var data = new ReactiveProperty<int>(expectedData);

var computedData = new TestComputedFromObservable(data);
Assert.Equal(expectedData, computedData.CachedData);
}

[Fact]
public void should_refresh_value_and_raise_event_when_data_changed_and_refreshed_implicitly()
{
var expectedData = 20;
var hasNotified = false;
var data = new ReactiveProperty<int>(10);

var computedData = new TestComputedFromObservable(data);
computedData.Subscribe(x => hasNotified = true);
data.Value = expectedData;

var actualData = computedData.Value;
Assert.Equal(expectedData, actualData);
Assert.True(hasNotified);
}

[Fact]
public void should_refresh_value_and_raise_event_when_data_changed_and_refreshed_explicitly()
{
var expectedData = 20;
var hasNotified = false;
var data = new ReactiveProperty<int>(10);

var computedData = new TestComputedFromObservable(data);
computedData.Subscribe(x => hasNotified = true);
computedData.RefreshData(expectedData);

var actualData = computedData.Value;
Assert.Equal(expectedData, actualData);
Assert.True(hasNotified);
}

[Fact]
public void should_not_refresh_value_or_notify_when_datasource_not_changed_even_when_refreshed_implicitly()
{
var expectedData = 20;
var hasNotified = false;
var data = new ReactiveProperty<int>(expectedData);

var computedData = new TestComputedFromObservable(data);
computedData.Subscribe(x => hasNotified = true);
data.OnNext(expectedData);

var actualData = computedData.Value;
Assert.Equal(expectedData, actualData);
Assert.False(hasNotified);
}

[Fact]
public void should_not_refresh_value_or_notify_when_datasource_not_changed_even_when_refreshed_explicitly()
{
var expectedData = 20;
var hasNotified = false;
var data = new ReactiveProperty<int>(expectedData);

var computedData = new TestComputedFromObservable(data);
computedData.Subscribe(x => hasNotified = true);
computedData.RefreshData(expectedData);

var actualData = computedData.Value;
Assert.Equal(expectedData, actualData);
Assert.False(hasNotified);
}
}
}
6 changes: 6 additions & 0 deletions src/SystemsR3.Tests/Plugins/Computeds/Models/DummyData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SystemsR3.Tests.Plugins.Computeds.Models;

public class DummyData
{
public int Data { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@

namespace SystemsR3.Tests.Plugins.Computeds.Models
{
public class DummyData
{
public int Data { get; set; }
}

public class TestComputedFromData : ComputedFromData<int, DummyData>
{
public Subject<Unit> ManuallyRefresh = new Subject<Unit>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using R3;
using SystemsR3.Computeds.Data;

namespace SystemsR3.Tests.Plugins.Computeds.Models;

public class TestComputedFromObservable : ComputedFromObservable<int, int>
{
public TestComputedFromObservable(ReactiveProperty<int> observable) : base(observable)
{}

public override int Transform(int dataSource)
{ return dataSource; }
}
2 changes: 1 addition & 1 deletion src/SystemsR3/Computeds/Collections/IComputedCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace SystemsR3.Computeds.Collections
/// Represents a computed collection of elements
/// </summary>
/// <typeparam name="T">The data to contain</typeparam>
public interface IComputedCollection<out T> : IComputed<IEnumerable<T>>, IEnumerable<T>
public interface IComputedCollection<T> : IComputed<IEnumerable<T>>, IEnumerable<T>
{
/// <summary>
/// Get an element by its index
Expand Down
1 change: 0 additions & 1 deletion src/SystemsR3/Computeds/Data/ComputedFromObservable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public IDisposable Subscribe(Observer<TOutput> observer)
public void MonitorChanges()
{ DataSource.Subscribe(RefreshData).AddTo(Subscriptions); }


public void RefreshData(TInput data)
{
lock (_lock)
Expand Down
4 changes: 3 additions & 1 deletion src/SystemsR3/Computeds/IComputed.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using R3;

namespace SystemsR3.Computeds
{
public interface IComputed<out T>
public interface IComputed<T>
{
T Value { get; }
IDisposable Subscribe(Observer<T> observer);
}
}
15 changes: 15 additions & 0 deletions src/SystemsR3/Extensions/IComputedExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using SystemsR3.Computeds;
using SystemsR3.Observers;

namespace SystemsR3.Extensions
{
public static class IComputedExtensions
{
public static IDisposable Subscribe<T>(this IComputed<T> computed, Action<T> onNext)
{ return computed.Subscribe(new BasicObserver<T>(onNext)); }

public static IDisposable Subscribe<T>(this IComputed<T> computed, Action<T> onNext, Action<Exception> onError)
{ return computed.Subscribe(new BasicObserver<T>(onNext, onError)); }
}
}
29 changes: 29 additions & 0 deletions src/SystemsR3/Observers/BasicObserver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using R3;

namespace SystemsR3.Observers
{
public class BasicObserver<T> : Observer<T>
{
public Action<T> OnNext { get; }

Check warning on line 8 in src/SystemsR3/Observers/BasicObserver.cs

View workflow job for this annotation

GitHub Actions / build-and-test

'BasicObserver<T>.OnNext' hides inherited member 'Observer<T>.OnNext(T)'. Use the new keyword if hiding was intended.
public Action<Exception> OnErrorResume { get; }

Check warning on line 9 in src/SystemsR3/Observers/BasicObserver.cs

View workflow job for this annotation

GitHub Actions / build-and-test

'BasicObserver<T>.OnErrorResume' hides inherited member 'Observer<T>.OnErrorResume(Exception)'. Use the new keyword if hiding was intended.
public Action<Result> OnCompleted { get; }

Check warning on line 10 in src/SystemsR3/Observers/BasicObserver.cs

View workflow job for this annotation

GitHub Actions / build-and-test

'BasicObserver<T>.OnCompleted' hides inherited member 'Observer<T>.OnCompleted(Result)'. Use the new keyword if hiding was intended.

public BasicObserver(Action<T> onNext, Action<Exception> onErrorResume = null, Action<Result> onCompleted = null)
{
OnNext = onNext;
OnErrorResume = onErrorResume ?? ObservableSystem.GetUnhandledExceptionHandler();
OnCompleted = onCompleted ?? AutoHandleResult;
}

private void AutoHandleResult(Result result)
{
if (result.IsFailure)
{ ObservableSystem.GetUnhandledExceptionHandler().Invoke(result.Exception); }
}

protected override void OnNextCore(T value) => OnNext(value);
protected override void OnErrorResumeCore(Exception error) => OnErrorResume(error);
protected override void OnCompletedCore(Result complete) => OnCompleted(complete);
}
}
Loading