From 803ab1baf6dc7cba5a4f5f76c52c9c842e3cb0ee Mon Sep 17 00:00:00 2001 From: MURAOKA Taro Date: Tue, 30 Jul 2024 23:58:35 +0900 Subject: [PATCH] example tests --- example_test.go | 104 +++++++++++++++++++++++++++++++++++++++++++++++ inflater.go | 15 +++++++ inflater_test.go | 17 +------- 3 files changed, 121 insertions(+), 15 deletions(-) create mode 100644 example_test.go diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..bc3efff --- /dev/null +++ b/example_test.go @@ -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: +} diff --git a/inflater.go b/inflater.go index c89f61a..54619ca 100644 --- a/inflater.go +++ b/inflater.go @@ -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] { diff --git a/inflater_test.go b/inflater_test.go index 573191c..1bf9e82 100644 --- a/inflater_test.go +++ b/inflater_test.go @@ -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