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

fix DistinctBy and Prepend #16

Merged
merged 3 commits into from
Jan 9, 2024
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
1 change: 1 addition & 0 deletions src/R3/Operators/AppendPrepend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ protected override IDisposable SubscribeCore(Observer<T> observer)
if (!append) // prepend
{
observer.OnNext(value);
return source.Subscribe(observer.Wrap());
}

return source.Subscribe(new _Append(observer, value));
Expand Down
2 changes: 1 addition & 1 deletion src/R3/Operators/Distinct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static Observable<T> Distinct<T>(this Observable<T> source, IEqualityComp

public static Observable<TSource> DistinctBy<TSource, TKey>(this Observable<TSource> source, Func<TSource, TKey> keySelector)
{
return DistinctBy(source, keySelector);
return DistinctBy(source, keySelector, EqualityComparer<TKey>.Default);
}

public static Observable<TSource> DistinctBy<TSource, TKey>(this Observable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
Expand Down
8 changes: 8 additions & 0 deletions tests/R3.Tests/OperatorTests/ConcatAppendPrependTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public void Prepend()

subject.OnCompleted();
list.AssertIsCompleted();
}

[Fact]
public void Prepend2()
{
using var list = Observable.Range(1, 3).Prepend(9999).ToLiveList();

list.AssertEqual([9999, 1, 2, 3]);
}

[Fact]
Expand Down
12 changes: 12 additions & 0 deletions tests/R3.Tests/OperatorTests/DefaultIfEmptyTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace R3.Tests.OperatorTests;

public class DefaultIfEmptyTest
{
[Fact]
public void DefaultIfEmpty()
{
using var list = Observable.Empty<int>().DefaultIfEmpty().ToLiveList();

list.AssertEqual([0]);
}
}
28 changes: 28 additions & 0 deletions tests/R3.Tests/OperatorTests/DistinctByTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace R3.Tests.OperatorTests;

public class DistinctByTest
{
[Fact]
public void DistinctBy()
{
var source = new Subject<(int, int)>();

using var list = source.DistinctBy(static x => x.Item1).ToLiveList();

source.OnNext((1, 10));
source.OnNext((2, 20));
source.OnNext((3, 30));
source.OnNext((1, 100));
source.OnNext((2, 200));
source.OnNext((3, 300));
source.OnNext((4, 400));

list.AssertEqual(
[
(1, 10),
(2, 20),
(3, 30),
(4, 400)
]);
}
}
22 changes: 22 additions & 0 deletions tests/R3.Tests/OperatorTests/DistinctTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace R3.Tests.OperatorTests;

public class DistinctTest
{
[Fact]
public void Distinct()
{
var source = new Subject<int>();

using var list = source.Distinct().ToLiveList();

source.OnNext(1);
source.OnNext(2);
source.OnNext(3);
source.OnNext(1);
source.OnNext(2);
source.OnNext(3);
source.OnNext(4);

list.AssertEqual([1, 2, 3, 4]);
}
}
28 changes: 28 additions & 0 deletions tests/R3.Tests/OperatorTests/DistinctUntilChangedByTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace R3.Tests.OperatorTests;

public class DistinctUntilChangedByTest
{
[Fact]
public void DistinctUntilChangedBy()
{
var source = new Subject<(int, int)>();

using var list = source.DistinctUntilChangedBy(static x => x.Item1).ToLiveList();

source.OnNext((1, 10));
source.OnNext((2, 20));
source.OnNext((3, 30));
source.OnNext((3, 300));
source.OnNext((2, 200));
source.OnNext((1, 100));

list.AssertEqual(
[
(1, 10),
(2, 20),
(3, 30),
(2, 200),
(1, 100)
]);
}
}
21 changes: 21 additions & 0 deletions tests/R3.Tests/OperatorTests/DistinctUntilChangedTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace R3.Tests.OperatorTests;

public class DistinctUntilChangedTest
{
[Fact]
public void DistinctUntilChanged()
{
var source = new Subject<int>();

using var list = source.DistinctUntilChanged().ToLiveList();

source.OnNext(1);
source.OnNext(2);
source.OnNext(3);
source.OnNext(3);
source.OnNext(2);
source.OnNext(1);

list.AssertEqual([1, 2, 3, 2, 1]);
}
}
16 changes: 16 additions & 0 deletions tests/R3.Tests/OperatorTests/PairwiseTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace R3.Tests.OperatorTests;

public class PairwiseTest
{
[Fact]
public void Pairwise()
{
using var list = Observable.Range(1, 3).Pairwise().ToLiveList();

list.AssertEqual(
[
(1, 2),
(2, 3)
]);
}
}
26 changes: 26 additions & 0 deletions tests/R3.Tests/OperatorTests/ScanTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace R3.Tests.OperatorTests;

public class ScanTest
{
[Fact]
public void Scan()
{
using var list = Observable
.Range(1, 3)
.Scan(static (first, second) => first + second)
.ToLiveList();

list.AssertEqual([ 1, 3, 6 ]);
}

[Fact]
public void ScanWithSeed()
{
using var list = Observable
.Range(1, 3)
.Scan(1, static (first, second) => first + second)
.ToLiveList();

list.AssertEqual([ 2, 4, 7]);
}
}
39 changes: 39 additions & 0 deletions tests/R3.Tests/OperatorTests/SwitchTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace R3.Tests.OperatorTests;

public class SwitchTest
{
[Fact]
public void Switch()
{
var sources = new Subject<Observable<int>>();

using var list = sources.Switch().ToLiveList();

var source1 = new Subject<int>();
var source2 = new Subject<int>();

sources.OnNext(source1);

list.AssertEmpty();

source1.OnNext(1);
source1.OnNext(2);

sources.OnNext(source2);

source2.OnNext(10);

source1.OnNext(3);

list.AssertEqual([1, 2, 10]);

source1.OnCompleted();
source2.OnCompleted();

list.AssertIsNotCompleted();

sources.OnCompleted();

list.AssertIsCompleted();
}
}