Skip to content

Commit

Permalink
expfmt: add test cases for TextParser startOfLine error handling
Browse files Browse the repository at this point in the history
The test cases cover the various scenarios that can occur when the
TextParser `startOfLine` method encounters an error. The first subtest
ensures that the method handles the `io.EOF` error correctly when the
input `io.Reader` is empty.

The second subtest uses a custom implementation of `io.Reader` to test
that `startOfLine` handles errors other than `io.EOF` correctly.

See prometheus#444

Signed-off-by: Julien Pivotto <[email protected]>
  • Loading branch information
roidelapluie committed Feb 20, 2023
1 parent 2c9877d commit 69ed1ea
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions expfmt/text_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package expfmt

import (
"errors"
"math"
"strings"
"testing"
Expand Down Expand Up @@ -667,3 +668,39 @@ func BenchmarkParseError(b *testing.B) {
testTextParseError(b)
}
}

func TestTextParserStartOfLine(t *testing.T) {
t.Run("EOF", func(t *testing.T) {
p := TextParser{}
in := strings.NewReader("")
p.reset(in)
fn := p.startOfLine()
if fn != nil {
t.Errorf("Unexpected non-nil function: %v", fn)
}
if p.err != nil {
t.Errorf("Unexpected error: %v", p.err)
}
})

t.Run("OtherError", func(t *testing.T) {
p := TextParser{}
in := &errReader{err: errors.New("unexpected error")}
p.reset(in)
fn := p.startOfLine()
if fn != nil {
t.Errorf("Unexpected non-nil function: %v", fn)
}
if p.err != in.err {
t.Errorf("Unexpected error: %v, expected %v", p.err, in.err)
}
})
}

type errReader struct {
err error
}

func (r *errReader) Read(p []byte) (int, error) {
return 0, r.err
}

0 comments on commit 69ed1ea

Please sign in to comment.