Skip to content

Commit

Permalink
example tests
Browse files Browse the repository at this point in the history
  • Loading branch information
koron committed Jul 30, 2024
1 parent 7cfb47f commit 803ab1b
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 15 deletions.
104 changes: 104 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package inflater_test

import (
"fmt"

"github.com/koron-go/inflater"
)

func ExampleSlice() {
slice := inflater.Slice[string]{"foo", "bar", "baz"}
for s := range slice.Inflate("IGNORED") {
fmt.Println(s)
}
// Output:
// foo
// bar
// baz
}

func ExamplePrefix() {
prefix := inflater.Prefix("1st ", "2nd ", "3rd ")
for s := range prefix.Inflate("item") {
fmt.Println(s)
}
// Output:
// 1st item
// 2nd item
// 3rd item
}

func ExampleSuffix() {
suffix := inflater.Suffix("-san", "-sama", "-dono")
for s := range suffix.Inflate("mina") {
fmt.Println(s)
}
// Output:
// mina-san
// mina-sama
// mina-dono
}

func ExampleParallel() {
parallel := inflater.Parallel(
inflater.Prefix("1st ", "2nd ", "3rd "),
inflater.Suffix("-san", "-sama", "-dono"),
)
for s := range parallel.Inflate("foo") {
fmt.Println(s)
}
// Output:
// 1st foo
// 2nd foo
// 3rd foo
// foo-san
// foo-sama
// foo-dono
}

func ExampleSerial() {
serial := inflater.Serial(
inflater.Prefix("1st ", "2nd ", "3rd "),
inflater.Suffix("-san", "-sama", "-dono"),
)
for s := range serial.Inflate("foo") {
fmt.Println(s)
}
// Output:
// 1st foo-san
// 1st foo-sama
// 1st foo-dono
// 2nd foo-san
// 2nd foo-sama
// 2nd foo-dono
// 3rd foo-san
// 3rd foo-sama
// 3rd foo-dono
}

func ExampleKeep() {
parallel := inflater.Parallel(
inflater.Keep[string](),
inflater.Prefix("1st ", "2nd ", "3rd "),
)
for s := range parallel.Inflate("foo") {
fmt.Println(s)
}
// Output:
// foo
// 1st foo
// 2nd foo
// 3rd foo
}

func ExampleNone() {
serial := inflater.Serial(
inflater.Prefix("1st ", "2nd ", "3rd "),
inflater.Suffix("-san", "-sama", "-dono"),
inflater.None[string](),
)
for s := range serial.Inflate("foo") {
fmt.Println(s)
}
// Output:
}
15 changes: 15 additions & 0 deletions inflater.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ func (f InflaterFunc[V]) Inflate(seed V) iter.Seq[V] {
return f(seed)
}

// Slice is a wrapper for a slice, which creates an Inflater that returns the
// elements of the slice. This Inflater always ignores the input.
type Slice[V any] []V

// Inflate inflates all elements of the slice. It ignores the input seed.
func (slice Slice[V]) Inflate(seed V) iter.Seq[V] {
return func(yield func(V) bool) {
for _, v := range slice {
if !yield(v) {
return
}
}
}
}

// None provides an Inflater which not inflate anything.
func None[V any]() Inflater[V] {
return InflaterFunc[V](func(seed V) iter.Seq[V] {
Expand Down
17 changes: 2 additions & 15 deletions inflater_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
package inflater_test

import (
"iter"
"strings"
"testing"

"github.com/koron-go/inflater"
)

type staticInflater []string

func (iter staticInflater) Inflate(string) iter.Seq[string] {
return func(yield func(string) bool) {
for _, s := range iter {
if !yield(s) {
return
}
}
}
}

var static1 = staticInflater{"aaa", "bbb", "ccc"}
var static2 = staticInflater{"111", "222", "333"}
var static1 = inflater.Slice[string]{"aaa", "bbb", "ccc"}
var static2 = inflater.Slice[string]{"111", "222", "333"}

func testInflater[T ~string | ~int](t *testing.T, target inflater.Inflater[T], seed T, wants ...T) {
i := 0
Expand Down

0 comments on commit 803ab1b

Please sign in to comment.