-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from Cysharp/add-unit-tests
Add unit tests
- Loading branch information
Showing
18 changed files
with
458 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.