Skip to content

Commit 00822e0

Browse files
committed
Support Stringer for Lines iters
1 parent 18460cf commit 00822e0

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

iter/lines.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,21 @@ func (iter *LinesIter) Next() option.Option[result.Result[[]byte]] {
4949
return option.Some(result.Ok(bytes.TrimRight(content, "\r\n")))
5050
}
5151

52+
// String implements the [fmt.Stringer] interface
53+
func (iter LinesIter) String() string {
54+
return "Iterator<Lines, type=Result<[]byte>>"
55+
}
56+
5257
// CollectResults is a convenience method for [CollectResults], providing this
5358
// iterator as an argument.
5459
func (iter *LinesIter) CollectResults() result.Result[[][]byte] {
5560
return CollectResults[[]byte](iter)
5661
}
5762

58-
var _ Iterator[result.Result[[]byte]] = new(LinesIter)
63+
var (
64+
_ fmt.Stringer = new(LinesIter)
65+
_ Iterator[result.Result[[]byte]] = new(LinesIter)
66+
)
5967

6068
type LinesStringIter struct {
6169
BaseIter[result.Result[string]]
@@ -75,6 +83,11 @@ func (iter *LinesStringIter) Next() option.Option[result.Result[string]] {
7583
}
7684
}
7785

86+
// String implements the [fmt.Stringer] interface
87+
func (iter LinesStringIter) String() string {
88+
return "Iterator<LinesString, type=Result<string>>"
89+
}
90+
7891
// CollectResults is a convenience method for [CollectResults], providing this
7992
// iterator as an argument.
8093
func (iter *LinesStringIter) CollectResults() result.Result[[]string] {
@@ -89,4 +102,7 @@ func LinesString(r io.Reader) *LinesStringIter {
89102
return iter
90103
}
91104

92-
var _ Iterator[result.Result[string]] = new(LinesStringIter)
105+
var (
106+
_ fmt.Stringer = new(LinesStringIter)
107+
_ Iterator[result.Result[string]] = new(LinesStringIter)
108+
)

iter/lines_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ func ExampleLinesString() {
2222
// Output: [hello there]
2323
}
2424

25+
func ExampleLinesStringIter_String() {
26+
fmt.Println(iter.LinesString(bytes.NewBufferString("hello\nthere")))
27+
// Output: Iterator<LinesString, type=Result<string>>
28+
}
29+
2530
func ExampleLines() {
2631
lines := iter.Lines(bytes.NewBufferString("hello\nthere"))
2732
unwrapped := iter.Map[result.Result[[]byte]](lines, ops.UnwrapResult[[]byte])
@@ -30,6 +35,11 @@ func ExampleLines() {
3035
// Output: [[104 101 108 108 111] [116 104 101 114 101]]
3136
}
3237

38+
func ExampleLinesIter_String() {
39+
fmt.Println(iter.Lines(bytes.NewBufferString("hello\nthere")))
40+
// Output: Iterator<Lines, type=Result<[]byte>>
41+
}
42+
3343
func TestLines(t *testing.T) {
3444
file, err := os.Open("fixtures/lines.txt")
3545
assert.Nil(t, err)
@@ -92,6 +102,14 @@ func TestLinesCollectResults(t *testing.T) {
92102
assert.Empty[byte](t, lines[4])
93103
}
94104

105+
func TestLinesIter_String(t *testing.T) {
106+
lines := iter.Lines(new(bytes.Buffer))
107+
expected := "Iterator<Lines, type=Result<[]byte>>"
108+
109+
assert.Equal(t, fmt.Sprintf("%s", lines), expected) //nolint:gosimple
110+
assert.Equal(t, fmt.Sprintf("%s", *lines), expected) //nolint:gosimple
111+
}
112+
95113
func TestLinesString(t *testing.T) {
96114
file, err := os.Open("fixtures/lines.txt")
97115
assert.Nil(t, err)
@@ -154,3 +172,11 @@ func TestLinesStringCollectResults(t *testing.T) {
154172
[]string{"This is", "a file", "with", "a trailing newline", ""},
155173
)
156174
}
175+
176+
func TestLinesStringIter_String(t *testing.T) {
177+
lines := iter.LinesString(new(bytes.Buffer))
178+
expected := "Iterator<LinesString, type=Result<string>>"
179+
180+
assert.Equal(t, fmt.Sprintf("%s", lines), expected) //nolint:gosimple
181+
assert.Equal(t, fmt.Sprintf("%s", *lines), expected) //nolint:gosimple
182+
}

0 commit comments

Comments
 (0)