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 on error xxx and observe operators (#184)
- Loading branch information
1 parent
cd2dffd
commit 7b9765d
Showing
4 changed files
with
220 additions
and
8 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,29 @@ | ||
package rx_test | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/fortytw2/leaktest" | ||
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo ok | ||
. "github.com/onsi/gomega" //nolint:revive // gomega ok | ||
) | ||
|
||
var _ = Describe("Observable operator", func() { | ||
Context("Observe", func() { | ||
When("observable", func() { | ||
It("🧪 should: receive all emitted items", func() { | ||
// rxgo: Test_Observable_Observe | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
got := make([]int, 0) | ||
ch := testObservable[int](ctx, 1, 2, 3).Observe() | ||
for item := range ch { | ||
got = append(got, item.V) | ||
} | ||
Expect(got).To(ContainElements([]int{1, 2, 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,79 @@ | ||
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("OnErrorResumeNext", func() { | ||
When("error occurs", func() { | ||
It("🧪 should: continue with next iterable", func() { | ||
// rxgo: Test_Observable_OnErrorResumeNext | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
obs := testObservable[int](ctx, 1, 2, errFoo, 4).OnErrorResumeNext( | ||
func(_ error) rx.Observable[int] { | ||
return testObservable[int](ctx, 10, 20) | ||
}, | ||
) | ||
rx.Assert(ctx, obs, | ||
rx.HasItems[int]{ | ||
Expected: []int{1, 2, 10, 20}, | ||
}, | ||
rx.HasNoError[int]{}, | ||
) | ||
}) | ||
}) | ||
}) | ||
|
||
Context("OnErrorReturn", func() { | ||
When("error occurs", func() { | ||
It("🧪 should: emit translated error and continue", func() { | ||
// rxgo: Test_Observable_OnErrorReturn | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
obs := testObservable[int](ctx, 1, 2, errFoo, 4, errBar, 6).OnErrorReturn( | ||
func(_ error) int { | ||
return -1 | ||
}, | ||
) | ||
rx.Assert(ctx, obs, | ||
rx.HasItems[int]{ | ||
Expected: []int{1, 2, -1, 4, -1, 6}, | ||
}, | ||
rx.HasNoError[int]{}, | ||
) | ||
}) | ||
}) | ||
}) | ||
|
||
Context("OnErrorReturnItem", func() { | ||
When("error occurs", func() { | ||
It("🧪 should: emit translated error and continue", func() { | ||
// rxgo: Test_Observable_OnErrorReturnItem | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
obs := testObservable[int](ctx, 1, 2, errFoo, 4, errBar, 6).OnErrorReturnItem(-1) | ||
rx.Assert(ctx, obs, | ||
rx.HasItems[int]{ | ||
Expected: []int{1, 2, -1, 4, -1, 6}, | ||
}, | ||
rx.HasNoError[int]{}, | ||
) | ||
}) | ||
}) | ||
}) | ||
}) |
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