Skip to content

Commit

Permalink
Merge pull request #13 from Cysharp/add-unit-tests
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
neuecc authored Jan 6, 2024
2 parents 680e5f3 + 33471be commit e707c64
Show file tree
Hide file tree
Showing 18 changed files with 458 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/R3/Operators/ContainsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal sealed class ContainsAsync<T>(T compareValue, IEqualityComparer<T> equa
{
protected override void OnNextCore(T value)
{
if (!equalityComparer.Equals(value, compareValue))
if (equalityComparer.Equals(value, compareValue))
{
TrySetResult(true);
}
Expand Down
4 changes: 0 additions & 4 deletions src/R3/Operators/MaxByMinByAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ protected override void OnNextCore(T value)
latestKey = key;
hasValue = true;
}

TrySetResult(value);
}

protected override void OnErrorResumeCore(Exception error)
Expand Down Expand Up @@ -103,8 +101,6 @@ protected override void OnNextCore(T value)
latestKey = key;
hasValue = true;
}

TrySetResult(value);
}

protected override void OnErrorResumeCore(Exception error)
Expand Down
2 changes: 1 addition & 1 deletion src/R3/Operators/SequenceEqualAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static partial class ObservableExtensions
{
public static Task<bool> SequenceEqualAsync<T>(this Observable<T> source, Observable<T> second, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
return SequenceEqualAsync(source, second, EqualityComparer<T>.Default, cancellationToken);
}

public static Task<bool> SequenceEqualAsync<T>(this Observable<T> source, Observable<T> second, IEqualityComparer<T> equalityComparer, CancellationToken cancellationToken = default)
Expand Down
4 changes: 2 additions & 2 deletions tests/R3.Tests/FactoryTests/ReturnFrameTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void UnitTest()
var frameProvider = new ManualFrameProvider();
var cts = new CancellationTokenSource();

var list = Observable.ReturnUnitFrame(frameProvider, cts.Token).ToLiveList();
var list = Observable.YieldFrame(frameProvider, cts.Token).ToLiveList();
list.AssertIsNotCompleted();

frameProvider.Advance();
Expand Down Expand Up @@ -82,7 +82,7 @@ public void NextFrameTest()
var frameProvider = new ManualFrameProvider(); // use custom fake
var cts = new CancellationTokenSource();

var list = Observable.ReturnUnitFrame(frameProvider, cts.Token).ToLiveList();
var list = Observable.YieldFrame(frameProvider, cts.Token).ToLiveList();
list.AssertIsNotCompleted();

// ReturnFrame run same frame.
Expand Down
20 changes: 20 additions & 0 deletions tests/R3.Tests/OperatorTests/AllTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace R3.Tests.OperatorTests;

public class AllTest
{
[Fact]
public async Task Positive()
{
var range = Observable.Range(1, 10);
var task = range.AllAsync(static x => x is > 0 and <= 10);
(await task).Should().BeTrue();
}

[Fact]
public async Task Negative()
{
var range = Observable.Range(1, 10);
var task = range.AllAsync(static x => x is > 0 and < 10);
(await task).Should().BeFalse();
}
}
20 changes: 20 additions & 0 deletions tests/R3.Tests/OperatorTests/AnyTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace R3.Tests.OperatorTests;

public class AnyTest
{
[Fact]
public async Task Positive()
{
var range = Observable.Range(1, 10);
var task = range.AnyAsync(static x => x is 5);
(await task).Should().BeTrue();
}

[Fact]
public async Task Negative()
{
var range = Observable.Range(1, 10);
var task = range.AnyAsync(static x => x is 11);
(await task).Should().BeFalse();
}
}
52 changes: 52 additions & 0 deletions tests/R3.Tests/OperatorTests/CombineLatestTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace R3.Tests.OperatorTests;

public class CombineLatestTest
{
[Fact]
public void CombineLatest()
{
// どちらかに値が入ってきた時点で他方の最新の値とペアにして吐き出す

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

using var list = source1.CombineLatest(source2, ValueTuple.Create).ToLiveList();

source1.OnNext(1);

list.AssertEmpty();

source1.OnNext(2);

list.AssertEmpty();

source2.OnNext("a");

list.AssertEqual([(2, "a")]);

source1.OnNext(3);

list.AssertEqual([(2, "a"), (3, "a")]);

source2.OnNext("b");

list.AssertEqual([(2, "a"), (3, "a"), (3, "b")]);

source2.OnNext("c");

list.AssertEqual([(2, "a"), (3, "a"), (3, "b"), (3, "c")]);

source1.OnCompleted();

list.AssertIsNotCompleted();

source1.OnNext(4);
source2.OnNext("d");

list.AssertEqual([(2, "a"), (3, "a"), (3, "b"), (3, "c"), (3, "d")]);

source2.OnCompleted();

list.AssertIsCompleted();
}
}
20 changes: 20 additions & 0 deletions tests/R3.Tests/OperatorTests/ContainsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace R3.Tests.OperatorTests;

public class ContainsTest
{
[Fact]
public async Task Positive()
{
var range = Observable.Range(1, 10);
var task = range.ContainsAsync(5);
(await task).Should().BeTrue();
}

[Fact]
public async Task Negative()
{
var range = Observable.Range(1, 10);
var task = range.ContainsAsync(0);
(await task).Should().BeFalse();
}
}
20 changes: 20 additions & 0 deletions tests/R3.Tests/OperatorTests/IsEmptyTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace R3.Tests.OperatorTests;

public class IsEmptyTest
{
[Fact]
public async Task Positive()
{
var range = Observable.Empty<int>();
var task = range.IsEmptyAsync();
(await task).Should().BeTrue();
}

[Fact]
public async Task Negative()
{
var range = Observable.Return(0);
var task = range.IsEmptyAsync();
(await task).Should().BeFalse();
}
}
13 changes: 13 additions & 0 deletions tests/R3.Tests/OperatorTests/MaxByTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace R3.Tests.OperatorTests;

public class MaxByTest
{
[Fact]
public async Task MaxBy()
{
var items = new[] { (1, 3), (2, 2), (3, 1) }.ToObservable();

var task = items.MaxByAsync(static x => x.Item1);
(await task).Should().Be((3, 1));
}
}
13 changes: 13 additions & 0 deletions tests/R3.Tests/OperatorTests/MinByTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace R3.Tests.OperatorTests;

public class MinByTest
{
[Fact]
public async Task MinBy()
{
var items = new[] { (1, 3), (2, 2), (3, 1) }.ToObservable();

var task = items.MinByAsync(static x => x.Item1);
(await task).Should().Be((1, 3));
}
}
24 changes: 24 additions & 0 deletions tests/R3.Tests/OperatorTests/SequenceEqualTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace R3.Tests.OperatorTests;

public class SequenceEqualTest
{
[Fact]
public async Task Positive()
{
var range1 = Observable.Range(1, 10);
var range2 = Observable.Range(1, 10);

var task = range1.SequenceEqualAsync(range2);
(await task).Should().BeTrue();
}

[Fact]
public async Task Negative()
{
var range1 = Observable.Range(1, 10);
var range2 = Observable.Range(1, 11);

var task = range1.SequenceEqualAsync(range2);
(await task).Should().BeFalse();
}
}
58 changes: 58 additions & 0 deletions tests/R3.Tests/OperatorTests/ToDictionaryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace R3.Tests.OperatorTests;

public class ToDictionaryTest
{
[Fact]
public async Task ToDictionary()
{
var publisher = new Subject<(int, string)>();

var task = publisher.ToDictionaryAsync(static x => x.Item1);

publisher.OnNext((1, "a"));
publisher.OnNext((2, "b"));
publisher.OnNext((3, "c"));

task.Status.Should().Be(TaskStatus.WaitingForActivation);

publisher.OnCompleted();

task.Status.Should().Be(TaskStatus.RanToCompletion);

var expected = new Dictionary<int, (int, string)>
{
[1] = (1, "a"),
[2] = (2, "b"),
[3] = (3, "c")
};

(await task).Should().Equal(expected);
}

[Fact]
public async Task ToDictionaryWithElementSelector()
{
var publisher = new Subject<(int, string)>();

var task = publisher.ToDictionaryAsync(static x => x.Item1, static x => x.Item2.ToUpperInvariant());

publisher.OnNext((1, "a"));
publisher.OnNext((2, "b"));
publisher.OnNext((3, "c"));

task.Status.Should().Be(TaskStatus.WaitingForActivation);

publisher.OnCompleted();

task.Status.Should().Be(TaskStatus.RanToCompletion);

var expected = new Dictionary<int, string>
{
[1] = "A",
[2] = "B",
[3] = "C"
};

(await task).Should().Equal(expected);
}
}
60 changes: 60 additions & 0 deletions tests/R3.Tests/OperatorTests/ToLookupTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace R3.Tests.OperatorTests;

public class ToLookupTest
{
[Fact]
public async Task ToLookup()
{
var publisher = new Subject<KeyValuePair<int, string>>();

var task = publisher.ToLookupAsync(static x => x.Key);

publisher.OnNext(new (1, "a"));
publisher.OnNext(new (2, "b"));
publisher.OnNext(new (3, "c"));

task.Status.Should().Be(TaskStatus.WaitingForActivation);

publisher.OnCompleted();

task.Status.Should().Be(TaskStatus.RanToCompletion);

var expected = new Dictionary<int, string>
{
[1] = "a",
[2] = "b",
[3] = "c"
}
.ToLookup(static x => x.Key);

(await task).Should().BeEquivalentTo(expected);
}

[Fact]
public async Task ToLookupWithElementSelector()
{
var publisher = new Subject<(int, string)>();

var task = publisher.ToLookupAsync(static x => x.Item1, static x => x.Item2.ToUpperInvariant());

publisher.OnNext((1, "a"));
publisher.OnNext((2, "b"));
publisher.OnNext((3, "c"));

task.Status.Should().Be(TaskStatus.WaitingForActivation);

publisher.OnCompleted();

task.Status.Should().Be(TaskStatus.RanToCompletion);

var expected = new Dictionary<int, string>
{
[1] = "A",
[2] = "B",
[3] = "C"
}
.ToLookup(static x => x.Key, static x => x.Value);

(await task).Should().BeEquivalentTo(expected);
}
}
Loading

0 comments on commit e707c64

Please sign in to comment.