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 distinct operator (#164)
- Loading branch information
1 parent
754ec36
commit c8aa638
Showing
4 changed files
with
331 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
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("Distinct", func() { | ||
When("duplicates present", func() { | ||
It("🧪 should: suppress duplicates", func() { | ||
// rxgo: Test_Observable_Distinct | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
obs := testObservable[int](ctx, 1, 2, 2, 1, 3).Distinct( | ||
func(_ context.Context, item int) (int, error) { | ||
return item, nil | ||
}, | ||
) | ||
|
||
rx.Assert(ctx, obs, | ||
rx.HasItems[int]{ | ||
Expected: []int{1, 2, 3}, | ||
}, | ||
rx.HasNoError[int]{}, | ||
) | ||
}) | ||
}) | ||
|
||
Context("Errors", func() { | ||
When("error present", func() { | ||
It("🧪 should: emit values before error and has error", func() { | ||
// rxgo: Test_Observable_Distinct_Error | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
obs := testObservable[int](ctx, 1, 2, 2, errFoo, 3).Distinct( | ||
func(_ context.Context, item int) (int, error) { | ||
return item, nil | ||
}, | ||
) | ||
|
||
rx.Assert(ctx, obs, | ||
rx.HasItems[int]{ | ||
Expected: []int{1, 2}, | ||
}, | ||
rx.HasError[int]{ | ||
Expected: []error{errFoo}, | ||
}, | ||
) | ||
}) | ||
}) | ||
|
||
When("error present", func() { | ||
It("🧪 should: emit values before error and has error", func() { | ||
// rxgo: Test_Observable_Distinct_Error2 | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
obs := testObservable[int](ctx, 1, 2, 2, 2, 3, 4).Distinct( | ||
func(_ context.Context, v int) (int, error) { | ||
if v == 3 { | ||
return 0, errFoo | ||
} | ||
|
||
return v, nil | ||
}, | ||
) | ||
|
||
rx.Assert(ctx, obs, | ||
rx.HasItems[int]{ | ||
Expected: []int{1, 2}, | ||
}, rx.HasError[int]{ | ||
Expected: []error{errFoo}, | ||
}, | ||
) | ||
}) | ||
}) | ||
}) | ||
|
||
Context("Parallel", func() { | ||
Context("foo", func() { | ||
It("🧪 should: ", func() { | ||
// rxgo: Test_Observable_Distinct_Parallel | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
/* | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
obs := testObservable[int](ctx, 1, 2, 2, 1, 3).Distinct( | ||
func(_ context.Context, item int) (int, error) { | ||
return item, nil | ||
}, rx.WithCPUPool[int]()) | ||
rx.Assert(ctx, obs, | ||
rx.HasItemsNoOrder[int]{ | ||
Expected: []int{1, 2, 3}, | ||
}, | ||
rx.HasNoError[int]{}, | ||
) | ||
*/ | ||
}) | ||
}) | ||
}) | ||
|
||
Context("Parallel/Error", func() { | ||
When("given: foo", func() { | ||
It("🧪 should: ", func() { | ||
// rxgo: Test_Observable_Distinct_Parallel_Error | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
/* | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
obs := testObservable[int](ctx, 1, 2, 2, errFoo).Distinct( | ||
func(_ context.Context, item int) (int, error) { | ||
return item, nil | ||
}, rx.WithContext[int](ctx), rx.WithCPUPool[int]()) | ||
rx.Assert(ctx, obs, rx.HasError[int]{ | ||
Expected: []error{errFoo}, | ||
}) | ||
*/ | ||
}) | ||
}) | ||
|
||
When("given: foo", func() { | ||
It("🧪 should: ", func() { | ||
// rxgo: Test_Observable_Distinct_Parallel_Error2 | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
/* | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
obs := testObservable[int](ctx, 1, 2, 2, 2, 3, 4).Distinct( | ||
func(_ context.Context, item int) (int, error) { | ||
if item == 3 { | ||
return 0, errFoo | ||
} | ||
return item, nil | ||
}, rx.WithContext[int](ctx), rx.WithCPUPool[int](), | ||
) | ||
rx.Assert[int](ctx, obs, rx.HasError[int]{ | ||
Expected: []error{errFoo}, | ||
}) | ||
*/ | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
Context("DistinctUntilChanged", func() { | ||
Context("principle", func() { | ||
It("🧪 should: ", func() { | ||
// rxgo: Test_Observable_DistinctUntilChanged | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
obs := testObservable[int](ctx, 1, 2, 2, 1, 3).DistinctUntilChanged( | ||
func(_ context.Context, v int) (int, error) { | ||
return v, nil | ||
}, rx.LimitComparator, rx.WithCPUPool[int]()) | ||
|
||
rx.Assert(ctx, obs, | ||
rx.HasItems[int]{ | ||
Expected: []int{1, 2, 1, 3}, | ||
}) | ||
}) | ||
}) | ||
|
||
Context("Parallel", func() { | ||
Context("given: foo", func() { | ||
It("🧪 should: ", func() { | ||
// rxgo: Test_Observable_DistinctUntilChanged_Parallel | ||
defer leaktest.Check(GinkgoT())() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
obs := testObservable[int](ctx, 1, 2, 2, 1, 3).DistinctUntilChanged( | ||
func(_ context.Context, item int) (int, error) { | ||
return item, nil | ||
}, rx.LimitComparator, rx.WithCPUPool[int]()) | ||
|
||
rx.Assert(ctx, obs, rx.HasItems[int]{ | ||
Expected: []int{1, 2, 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