Added some Regexp tests

This commit is contained in:
Brandon Dyck 2024-10-16 13:32:25 -06:00
parent 9c5e8fff0e
commit c22246b7de

View File

@ -4,6 +4,7 @@ package bytes_test
import ( import (
"bytes" "bytes"
"errors"
"strings" "strings"
"testing" "testing"
@ -21,20 +22,43 @@ func Todo(t *testing.T) {
} }
func TestRegexp(t *testing.T) { func TestRegexp(t *testing.T) {
t.Run("only searches the beginning of input", Todo) alpha := rapid.SliceOfBytesMatching(`[A-Za-z]{1,100}`)
t.Run("position is correct after match", Todo) t.Run("position and value are correct after match", rapid.MakeCheck(func(t *rapid.T) {
t.Run("fails on unexpected error", Todo) needle := alpha.Draw(t, "needle")
t.Run("returns a useful Got value", rapid.MakeCheck(func(t *rapid.T) { input := rapid.Map(alpha, func(suffix []byte) []byte { return append(needle, suffix...) }).
Draw(t, "input")
p := pbytes.Regexp(string(needle))
result, err := p(gigaparsec.MakeState(bytes.NewReader(input)))
succeeded, val, next := result.Status()
}))
t.Run("basically works", func(t *testing.T) {
result, err := pbytes.Regexp("a")(gigaparsec.MakeState(strings.NewReader("a")))
must.NoError(t, err) must.NoError(t, err)
success, value, _ := result.Status() test.True(t, succeeded)
test.True(t, success, test.Sprint(result.Message())) test.EqOp(t, string(needle), val)
test.EqOp(t, "a", value) ptest.StateIsAt(t, next, uint64(len(needle)))
test.True(t, result.Consumed()) }))
t.Run("only searches the beginning of input", rapid.MakeCheck(func(t *rapid.T) {
needle := alpha.Draw(t, "needle")
input := rapid.Map(alpha, func(prefix []byte) []byte { return append(prefix, needle...) }).
Filter(func(b []byte) bool { return !bytes.HasPrefix(b, needle) }).
Draw(t, "input")
p := pbytes.Regexp(string(needle))
result, err := p(gigaparsec.MakeState(bytes.NewReader(input)))
succeeded, _, _ := result.Status()
must.NoError(t, err)
test.False(t, succeeded)
}))
t.Run("fails on unexpected error", func(t *testing.T) {
expectedErr := errors.New("it broke")
p := pbytes.Regexp("nope")
result, err := p(gigaparsec.MakeState(ptest.ErrReaderAt(expectedErr)))
succeeded, _, _ := result.Status()
test.ErrorIs(t, err, expectedErr)
test.False(t, succeeded)
}) })
t.Run("returns a useful Got value", Todo)
} }
func TestRuneReader(t *testing.T) { func TestRuneReader(t *testing.T) {