flabk/pkg/ld/internal/parse/chunk/chunk_test.go

109 lines
2.3 KiB
Go
Raw Permalink Normal View History

package chunk
2022-08-03 15:20:03 +01:00
import (
"testing"
"github.com/stretchr/testify/require"
"sectorinf.com/emilis/flabk/pkg/coll"
2022-08-03 15:20:03 +01:00
)
func TestSpace(t *testing.T) {
tests := []byte{0x9, 0xa, 0xd, 0x20}
2022-08-03 15:20:03 +01:00
that := require.New(t)
for _, test := range tests {
b := []byte("xhello world")
b[0] = test
that.True(New(b).AtSpace())
}
2022-08-03 15:20:03 +01:00
}
func TestAlign(t *testing.T) {
tests := map[string]struct {
c func() Chunk
v func(Chunk) Chunk
}{
"same-size": {
c: func() Chunk {
c := New(coll.Vector[byte](`{"heres some test data": "hi mom"}`))
c.posLeft = 15
c.globLeft = 15
return c
},
v: func(c Chunk) Chunk {
c.posLeft = 20
c.globLeft = 20
c.globRight = 22
c.posRight = 22
return c
},
},
"cookie-cutter": {
c: func() Chunk {
c := New(coll.Vector[byte](`{"heres some test data": "hi mom"}`))
c.posLeft = 15
c.globLeft = 15
return c
},
v: func(c Chunk) Chunk {
return c.CookieCutter().CookieCutter()
},
},
"no-change": {
c: func() Chunk {
c := New(coll.Vector[byte](`{"heres some test data": "hi mom"}`))
c.posLeft = 15
c.globLeft = 15
return c
},
v: func(c Chunk) Chunk {
c.posRight = 15
c.globRight = 15
return c.Sub()
},
},
"contains": {
c: func() Chunk {
return New(coll.Vector[byte](`0123456789`))
},
v: func(c Chunk) Chunk {
return c.Child(5, 8)
},
},
2022-08-03 15:20:03 +01:00
}
for name, test := range tests {
test := test
t.Run(name, func(tt *testing.T) {
that := require.New(tt)
c := test.c()
v := test.v(c)
out := c.After(v)
that.Equal(v.globRight+1, out.globLeft)
that.Equal(string(c.vector[v.globRight+1]), string(out.LeftByte()))
})
2022-08-03 15:20:03 +01:00
}
}
2022-08-03 15:20:03 +01:00
func TestRow(t *testing.T) {
c := New(coll.Vector[byte](`{
"heres some test data": "hi mom",
"another field": "get it right",
"integer": -12345
}`))
that := require.New(t)
row, err := c.CookieCutter().Seek().Row()
that.NoError(err)
that.Equal("heres some test data", row.Name)
that.Equal(`"hi mom"`, row.Value.String())
row, err = c.After(row.Value).StepIf(',').Seek().Row()
that.NoError(err)
that.Equal("another field", row.Name)
that.Equal(`"get it right"`, row.Value.String())
row, err = c.After(row.Value).StepIf(',').Seek().Row()
that.NoError(err)
that.Equal("integer", row.Name)
that.Equal("-12345", row.Value.String())
that.True(c.After(row.Value).Seek().EOF())
2022-08-03 15:20:03 +01:00
}