generated from snivilised/astrolib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rx): add first/first-or-default operators (#176)
- Loading branch information
1 parent
b302d80
commit e087538
Showing
3 changed files
with
221 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package rx_test | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/fortytw2/leaktest" | ||
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo ok | ||
"github.com/snivilised/lorax/rx" | ||
) | ||
|
||
var _ = Describe("Observable operator", func() { | ||
Context("First", func() { | ||
When("not empty", func() { | ||
It("🧪 should: return first item", func() { | ||
// rxgo: Test_Observable_First_NotEmpty | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
obs := testObservable[int](ctx, 1, 2, 3).First() | ||
rx.Assert(ctx, obs, rx.HasItem[int]{ | ||
Expected: 1, | ||
}) | ||
}) | ||
}) | ||
|
||
When("empty", func() { | ||
It("🧪 should: return nothing", func() { | ||
// rxgo: Test_Observable_First_Empty | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
obs := rx.Empty[int]().First() | ||
rx.Assert(ctx, obs, rx.IsEmpty[int]{}) | ||
}) | ||
}) | ||
|
||
Context("Parallel", func() { | ||
When("not empty", func() { | ||
It("🧪 should: return first item", func() { | ||
// rxgo: Test_Observable_First_Parallel_NotEmpty | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
obs := testObservable[int](ctx, 1, 2, 3).First(rx.WithCPUPool[int]()) | ||
rx.Assert(ctx, obs, rx.HasItem[int]{ | ||
Expected: 1, | ||
}) | ||
}) | ||
}) | ||
|
||
When("empty", func() { | ||
It("🧪 should: return nothing", func() { | ||
// rxgo: Test_Observable_First_Parallel_Empty | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
obs := rx.Empty[int]().First(rx.WithCPUPool[int]()) | ||
rx.Assert(ctx, obs, rx.IsEmpty[int]{}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
Context("FirstOrDefault", func() { | ||
When("not empty", func() { | ||
It("🧪 should: return first item", func() { | ||
// rxgo: Test_Observable_First_NotEmpty | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
obs := testObservable[int](ctx, 1, 2, 3).FirstOrDefault(10) | ||
rx.Assert(ctx, obs, rx.HasItem[int]{ | ||
Expected: 1, | ||
}) | ||
}) | ||
}) | ||
|
||
When("empty", func() { | ||
It("🧪 should: return default", func() { | ||
// rxgo: Test_Observable_First_Empty | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
obs := rx.Empty[int]().FirstOrDefault(10) | ||
rx.Assert(ctx, obs, rx.HasItem[int]{ | ||
Expected: 10, | ||
}) | ||
}) | ||
}) | ||
|
||
Context("Parallel", func() { | ||
When("not empty", func() { | ||
It("🧪 should: return first item", func() { | ||
// rxgo: Test_Observable_First_Parallel_NotEmpty | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
obs := testObservable[int](ctx, 1, 2, 3).FirstOrDefault(10, rx.WithCPUPool[int]()) | ||
rx.Assert(ctx, obs, rx.HasItem[int]{ | ||
Expected: 1, | ||
}) | ||
}) | ||
}) | ||
|
||
When("empty", func() { | ||
It("🧪 should: return default ", func() { | ||
// rxgo: Test_Observable_First_Parallel_Empty | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
obs := rx.Empty[int]().FirstOrDefault(10, rx.WithCPUPool[int]()) | ||
rx.Assert(ctx, obs, rx.HasItem[int]{ | ||
Expected: 10, | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
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