miniflux/vendor/github.com/tdewolff/parse/position_test.go

43 lines
822 B
Go
Raw Normal View History

2017-11-20 06:10:04 +01:00
package parse
import (
"bytes"
"fmt"
"io"
"testing"
"github.com/tdewolff/test"
)
func TestPosition(t *testing.T) {
var newlineTests = []struct {
offset int
buf string
line int
col int
err error
}{
{0, "x", 1, 1, nil},
{1, "xx", 1, 2, nil},
{2, "x\nx", 2, 1, nil},
{2, "\n\nx", 3, 1, nil},
{3, "\nxxx", 2, 3, nil},
{2, "\r\nx", 2, 1, nil},
// edge cases
{0, "", 1, 1, io.EOF},
{0, "\n", 1, 1, nil},
{1, "\r\n", 1, 2, nil},
{-1, "x", 1, 2, io.EOF}, // continue till the end
}
for _, tt := range newlineTests {
t.Run(fmt.Sprint(tt.buf, " ", tt.offset), func(t *testing.T) {
r := bytes.NewBufferString(tt.buf)
line, col, _, err := Position(r, tt.offset)
test.T(t, err, tt.err)
test.T(t, line, tt.line, "line")
test.T(t, col, tt.col, "column")
})
}
}