Skip to content

Commit 2795fdc

Browse files
committed
+All, +Any, +Count, +ElementAt, +IsEmpty, Fixes
1 parent 4923aa7 commit 2795fdc

25 files changed

+1025
-39
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ mono: none
55
sudo: required
66

77
script:
8-
- dotnet test ./async-enumerable-dotnet-test/async-enumerable-dotnet-test.csproj
8+
- dotnet test ./async-enumerable-dotnet-test/async-enumerable-dotnet-test.csproj -v m

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,27 @@ finally
6060

6161
## Intermediate operators
6262

63+
- `Any` - signals true if any of the source items matched a predicate
64+
- `All` - signals true if all of the source items matched a predicate
6365
- `Buffer` - collect some number of items into buffer(s) and emit those buffers
6466
- `Collect` - collect items into a custom collection and emit the collection at the end
6567
- `ConcatMap` - concatenate in order the inner async sequences mapped from the main sequence
6668
- `ConcatMapEager` - run the async sources at once but relay elements in order similar to `ConcatMap`
6769
- `ConcatWith` - concatenate in order with another async sequence
70+
- `Count` - count the number of items in the async sequence
6871
- `Distinct` - makes sure only distinct elements get relayed
6972
- `DistinctUntilChanged` - relays an element only if it is distinct from the previous item
7073
- `Debounce` - wait a bit after each item and emit them if no newer item arrived from the source
7174
- `DefaultIfEmpty` - return a fallback value if the source async sequence turns out to be empty
7275
- `DoOnNext` - execute an action when an item becomes available
7376
- `DoOnDispose` - execute an action when the async sequence gets disposed.
77+
- `ElementAt` - get the element at a specified index or an error/default value
7478
- `Filter` - prevent items from passing through which don't pass a predicate
7579
- `First` - signals the first item of the async sequence
7680
- `FlatMap` - map the source items into `IAsyncEnumerable`s and merge their values into a single async sequence
7781
- `GroupBy` - groups the source elements into distinct async groups
7882
- `IgnoreElements` - ignores items and ends when the source async sequence ends
83+
- `IsEmpty` - signals a single true if the source is empty
7984
- `Last` - signals the last item of the async sequence
8085
- `Latest` - runs the source async sequence as fast as it can and samples it with the frequency of the consumer
8186
- `Map` - transform one source value into some other value
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright (c) David Karnok & Contributors.
2+
// Licensed under the Apache 2.0 License.
3+
// See LICENSE file in the project root for full license information.
4+
5+
using System;
6+
using System.Threading.Tasks;
7+
using Xunit;
8+
using async_enumerable_dotnet;
9+
10+
namespace async_enumerable_dotnet_test
11+
{
12+
public class AllTest
13+
{
14+
[Fact]
15+
public async void Sync_None()
16+
{
17+
await AsyncEnumerable.Range(1, 5)
18+
.All(v => v > 6)
19+
.AssertResult(false);
20+
}
21+
22+
[Fact]
23+
public async void Sync_Empty()
24+
{
25+
await AsyncEnumerable.Empty<int>()
26+
.All(v => v > 6)
27+
.AssertResult(true);
28+
}
29+
30+
[Fact]
31+
public async void Sync_Match()
32+
{
33+
await AsyncEnumerable.Range(1, 5)
34+
.All(v => v > 0)
35+
.AssertResult(true);
36+
}
37+
38+
[Fact]
39+
public async void Sync_Error()
40+
{
41+
await AsyncEnumerable.Error<int>(new InvalidOperationException())
42+
.All(v => v > 6)
43+
.AssertFailure(typeof(InvalidOperationException));
44+
}
45+
46+
[Fact]
47+
public async void Sync_Crash()
48+
{
49+
await AsyncEnumerable.Range(1, 5)
50+
.All((Func<int, bool>)(v => throw new InvalidOperationException()))
51+
.AssertFailure(typeof(InvalidOperationException));
52+
}
53+
54+
[Fact]
55+
public async void Async_None()
56+
{
57+
await AsyncEnumerable.Range(1, 5)
58+
.All(async v =>
59+
{
60+
await Task.Delay(100);
61+
return v > 6;
62+
})
63+
.AssertResult(false);
64+
}
65+
66+
[Fact]
67+
public async void Async_Empty()
68+
{
69+
await AsyncEnumerable.Empty<int>()
70+
.All(async v =>
71+
{
72+
await Task.Delay(100);
73+
return v > 6;
74+
})
75+
.AssertResult(true);
76+
}
77+
78+
[Fact]
79+
public async void Async_Match()
80+
{
81+
await AsyncEnumerable.Range(1, 5)
82+
.All(async v =>
83+
{
84+
await Task.Delay(100);
85+
return v > 0;
86+
})
87+
.AssertResult(true);
88+
}
89+
90+
[Fact]
91+
public async void Async_Error()
92+
{
93+
await AsyncEnumerable.Error<int>(new InvalidOperationException())
94+
.All(async v =>
95+
{
96+
await Task.Delay(100);
97+
return v > 6;
98+
})
99+
.AssertFailure(typeof(InvalidOperationException));
100+
}
101+
102+
[Fact]
103+
public async void Async_Crash()
104+
{
105+
await AsyncEnumerable.Range(1, 5)
106+
.All(async v =>
107+
{
108+
await Task.Delay(100);
109+
throw new InvalidOperationException();
110+
})
111+
.AssertFailure(typeof(InvalidOperationException));
112+
}
113+
114+
}
115+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright (c) David Karnok & Contributors.
2+
// Licensed under the Apache 2.0 License.
3+
// See LICENSE file in the project root for full license information.
4+
5+
using System;
6+
using System.Threading.Tasks;
7+
using Xunit;
8+
using async_enumerable_dotnet;
9+
10+
namespace async_enumerable_dotnet_test
11+
{
12+
public class AnyTest
13+
{
14+
[Fact]
15+
public async void Sync_None()
16+
{
17+
await AsyncEnumerable.Range(1, 5)
18+
.Any(v => v > 6)
19+
.AssertResult(false);
20+
}
21+
22+
[Fact]
23+
public async void Sync_Empty()
24+
{
25+
await AsyncEnumerable.Empty<int>()
26+
.Any(v => v > 6)
27+
.AssertResult(false);
28+
}
29+
30+
[Fact]
31+
public async void Sync_Match()
32+
{
33+
await AsyncEnumerable.Range(1, 5)
34+
.Any(v => v > 4)
35+
.AssertResult(true);
36+
}
37+
38+
[Fact]
39+
public async void Sync_Error()
40+
{
41+
await AsyncEnumerable.Error<int>(new InvalidOperationException())
42+
.Any(v => v > 6)
43+
.AssertFailure(typeof(InvalidOperationException));
44+
}
45+
46+
[Fact]
47+
public async void Sync_Crash()
48+
{
49+
await AsyncEnumerable.Range(1, 5)
50+
.Any((Func<int, bool>)(v => throw new InvalidOperationException()))
51+
.AssertFailure(typeof(InvalidOperationException));
52+
}
53+
54+
[Fact]
55+
public async void Async_None()
56+
{
57+
await AsyncEnumerable.Range(1, 5)
58+
.Any(async v =>
59+
{
60+
await Task.Delay(100);
61+
return v > 6;
62+
})
63+
.AssertResult(false);
64+
}
65+
66+
[Fact]
67+
public async void Async_Empty()
68+
{
69+
await AsyncEnumerable.Empty<int>()
70+
.Any(async v =>
71+
{
72+
await Task.Delay(100);
73+
return v > 6;
74+
})
75+
.AssertResult(false);
76+
}
77+
78+
[Fact]
79+
public async void Async_Match()
80+
{
81+
await AsyncEnumerable.Range(1, 5)
82+
.Any(async v =>
83+
{
84+
await Task.Delay(100);
85+
return v > 4;
86+
})
87+
.AssertResult(true);
88+
}
89+
90+
[Fact]
91+
public async void Async_Error()
92+
{
93+
await AsyncEnumerable.Error<int>(new InvalidOperationException())
94+
.Any(async v =>
95+
{
96+
await Task.Delay(100);
97+
return v > 6;
98+
})
99+
.AssertFailure(typeof(InvalidOperationException));
100+
}
101+
102+
[Fact]
103+
public async void Async_Crash()
104+
{
105+
await AsyncEnumerable.Range(1, 5)
106+
.Any(async v =>
107+
{
108+
await Task.Delay(100);
109+
throw new InvalidOperationException();
110+
})
111+
.AssertFailure(typeof(InvalidOperationException));
112+
}
113+
114+
}
115+
}

async-enumerable-dotnet-test/ConcatMapEagerTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ await AsyncEnumerable.FromArray(
8888
[Fact]
8989
public async void Main_Error()
9090
{
91-
await AsyncEnumerable.Range(1, 5).ConcatWith(AsyncEnumerable.Error<int>(new InvalidOperationException()))
91+
await AsyncEnumerable.Range(1, 5).WithError(new InvalidOperationException())
9292
.ConcatMapEager(v => AsyncEnumerable.Range(v * 10, 5))
9393
.AssertFailure(typeof(InvalidOperationException),
9494
10, 11, 12, 13, 14,
@@ -107,7 +107,7 @@ await AsyncEnumerable.Range(1, 5)
107107
var res = AsyncEnumerable.Range(v * 10, 5);
108108
if (v == 3)
109109
{
110-
res = res.ConcatWith(AsyncEnumerable.Error<int>(new InvalidOperationException()));
110+
res = res.WithError(new InvalidOperationException());
111111
}
112112
return res;
113113
})
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) David Karnok & Contributors.
2+
// Licensed under the Apache 2.0 License.
3+
// See LICENSE file in the project root for full license information.
4+
5+
using System;
6+
using Xunit;
7+
using async_enumerable_dotnet;
8+
9+
namespace async_enumerable_dotnet_test
10+
{
11+
public class CountTest
12+
{
13+
[Fact]
14+
public async void Empty()
15+
{
16+
await AsyncEnumerable.Empty<int>()
17+
.Count()
18+
.AssertResult(0L);
19+
}
20+
21+
[Fact]
22+
public async void Just()
23+
{
24+
await AsyncEnumerable.Just(1)
25+
.Count()
26+
.AssertResult(1L);
27+
}
28+
29+
[Fact]
30+
public async void Range()
31+
{
32+
await AsyncEnumerable.Range(1, 100)
33+
.Count()
34+
.AssertResult(100L);
35+
}
36+
37+
38+
[Fact]
39+
public async void Error()
40+
{
41+
await AsyncEnumerable.Range(1, 100).WithError(new InvalidOperationException())
42+
.Count()
43+
.AssertFailure(typeof(InvalidOperationException));
44+
}
45+
46+
}
47+
}

async-enumerable-dotnet-test/DoOnNextTest.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public async void Sync_Normal()
1717
var list = new List<int>();
1818
await AsyncEnumerable.Range(1, 5)
1919
.DoOnNext(v => list.Add(v))
20-
.ForEach();
20+
.AssertResult(1, 2, 3, 4, 5);
2121

2222
Assert.Equal(new[] { 1, 2, 3, 4, 5 }, list);
2323
}
@@ -27,11 +27,12 @@ public async void Async_Normal()
2727
{
2828
var list = new List<int>();
2929
await AsyncEnumerable.Range(1, 5)
30-
.DoOnNext(async v => {
30+
.DoOnNext(async v =>
31+
{
3132
await Task.Delay(100);
3233
list.Add(v);
3334
})
34-
.ForEach();
35+
.AssertResult(1, 2, 3, 4, 5);
3536

3637
Assert.Equal(new[] { 1, 2, 3, 4, 5 }, list);
3738
}

0 commit comments

Comments
 (0)