-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(flow): improve coverage and minor refactoring (#139)
- Loading branch information
Showing
17 changed files
with
673 additions
and
146 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,4 @@ issues: | |
- errcheck | ||
- unparam | ||
- prealloc | ||
- funlen |
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,72 @@ | ||
package flow_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/reugn/go-streams" | ||
ext "github.com/reugn/go-streams/extension" | ||
"github.com/reugn/go-streams/flow" | ||
"github.com/reugn/go-streams/internal/assert" | ||
) | ||
|
||
func TestFilter(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
filterFlow streams.Flow | ||
ptr bool | ||
}{ | ||
{ | ||
name: "values", | ||
filterFlow: flow.NewFilter(func(e int) bool { | ||
return e%2 != 0 | ||
}, 1), | ||
ptr: false, | ||
}, | ||
{ | ||
name: "pointers", | ||
filterFlow: flow.NewFilter(func(e *int) bool { | ||
return *e%2 != 0 | ||
}, 1), | ||
ptr: true, | ||
}, | ||
} | ||
input := []int{1, 2, 3, 4, 5} | ||
expected := []int{1, 3, 5} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
in := make(chan any, 5) | ||
out := make(chan any, 5) | ||
|
||
source := ext.NewChanSource(in) | ||
sink := ext.NewChanSink(out) | ||
|
||
if tt.ptr { | ||
ingestSlice(ptrSlice(input), in) | ||
} else { | ||
ingestSlice(input, in) | ||
} | ||
close(in) | ||
|
||
source. | ||
Via(tt.filterFlow). | ||
To(sink) | ||
|
||
if tt.ptr { | ||
output := readSlicePtr[int](out) | ||
assert.Equal(t, ptrSlice(expected), output) | ||
} else { | ||
output := readSlice[int](out) | ||
assert.Equal(t, expected, output) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFilter_NonPositiveParallelism(t *testing.T) { | ||
assert.Panics(t, func() { | ||
flow.NewFilter(filterNotContainsA, 0) | ||
}) | ||
assert.Panics(t, func() { | ||
flow.NewFilter(filterNotContainsA, -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,94 @@ | ||
package flow_test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/reugn/go-streams" | ||
ext "github.com/reugn/go-streams/extension" | ||
"github.com/reugn/go-streams/flow" | ||
"github.com/reugn/go-streams/internal/assert" | ||
) | ||
|
||
func TestFlatMap(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
flatMapFlow streams.Flow | ||
inPtr bool | ||
outPtr bool | ||
}{ | ||
{ | ||
name: "val-val", | ||
inPtr: false, | ||
flatMapFlow: flow.NewFlatMap(func(in string) []string { | ||
return []string{in, strings.ToUpper(in)} | ||
}, 1), | ||
outPtr: false, | ||
}, | ||
{ | ||
name: "ptr-val", | ||
inPtr: true, | ||
flatMapFlow: flow.NewFlatMap(func(in *string) []string { | ||
return []string{*in, strings.ToUpper(*in)} | ||
}, 1), | ||
outPtr: false, | ||
}, | ||
{ | ||
name: "ptr-ptr", | ||
inPtr: true, | ||
flatMapFlow: flow.NewFlatMap(func(in *string) []*string { | ||
upper := strings.ToUpper(*in) | ||
return []*string{in, &upper} | ||
}, 1), | ||
outPtr: true, | ||
}, | ||
{ | ||
name: "val-ptr", | ||
inPtr: false, | ||
flatMapFlow: flow.NewFlatMap(func(in string) []*string { | ||
upper := strings.ToUpper(in) | ||
return []*string{&in, &upper} | ||
}, 1), | ||
outPtr: true, | ||
}, | ||
} | ||
input := []string{"a", "b", "c"} | ||
expected := []string{"a", "A", "b", "B", "c", "C"} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
in := make(chan any, 3) | ||
out := make(chan any, 6) | ||
|
||
source := ext.NewChanSource(in) | ||
sink := ext.NewChanSink(out) | ||
|
||
if tt.inPtr { | ||
ingestSlice(ptrSlice(input), in) | ||
} else { | ||
ingestSlice(input, in) | ||
} | ||
close(in) | ||
|
||
source. | ||
Via(tt.flatMapFlow). | ||
To(sink) | ||
|
||
if tt.outPtr { | ||
output := readSlicePtr[string](out) | ||
assert.Equal(t, ptrSlice(expected), output) | ||
} else { | ||
output := readSlice[string](out) | ||
assert.Equal(t, expected, output) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFlatMap_NonPositiveParallelism(t *testing.T) { | ||
assert.Panics(t, func() { | ||
flow.NewFlatMap(addAsterisk, 0) | ||
}) | ||
assert.Panics(t, func() { | ||
flow.NewFlatMap(addAsterisk, -1) | ||
}) | ||
} |
Oops, something went wrong.