corso/src/internal/kopia/path_encoder_test.go
Keepers 9d73d0c8e2
replace errors.* with clues.* (#2924)
Mostly find/replace on errors.N and errors.W. Also turns all wrapf into wrap, and removes as many errorf calls as possible.

Might follow up with a linter to enforce this change.

---

#### Does this PR need a docs update or release note?

- [x]  No

#### Type of change

- [x] 🧹 Tech Debt/Cleanup

#### Issue(s)

* #1970

#### Test Plan

- [x]  Unit test
- [x] 💚 E2E
2023-03-28 04:01:54 +00:00

102 lines
2.3 KiB
Go

package kopia
import (
"strings"
"testing"
"github.com/alcionai/clues"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/alcionai/corso/src/internal/tester"
)
type PathEncoderSuite struct {
tester.Suite
}
func TestPathEncoderSuite(t *testing.T) {
suite.Run(t, &PathEncoderSuite{Suite: tester.NewUnitSuite(t)})
}
func (suite *PathEncoderSuite) TestEncodeDecode() {
t := suite.T()
elements := []string{"these", "are", "some", "path", "elements"}
encoded := encodeElements(elements...)
decoded := make([]string, 0, len(elements))
for _, e := range encoded {
dec, err := decodeElement(e)
require.NoError(t, err, clues.ToCore(err))
decoded = append(decoded, dec)
}
assert.Equal(t, elements, decoded)
}
func (suite *PathEncoderSuite) TestEncodeAsPathDecode() {
table := []struct {
name string
elements []string
expected []string
}{
{
name: "MultipleElements",
elements: []string{"these", "are", "some", "path", "elements"},
expected: []string{"these", "are", "some", "path", "elements"},
},
{
name: "SingleElement",
elements: []string{"elements"},
expected: []string{"elements"},
},
{
name: "EmptyPath",
elements: []string{""},
expected: []string{""},
},
{
name: "NilPath",
elements: nil,
// Gets "" back because individual elements are decoded and "" is the 0
// value for the decoder.
expected: []string{""},
},
}
for _, test := range table {
suite.Run(test.name, func() {
t := suite.T()
encoded := encodeAsPath(test.elements...)
// Sanity check, first and last character should not be '/'.
assert.Equal(t, strings.Trim(encoded, "/"), encoded)
decoded := make([]string, 0, len(test.elements))
for _, e := range strings.Split(encoded, "/") {
dec, err := decodeElement(e)
require.NoError(t, err, clues.ToCore(err))
decoded = append(decoded, dec)
}
assert.Equal(t, test.expected, decoded)
})
}
}
func FuzzEncodeDecodeSingleString(f *testing.F) {
f.Fuzz(func(t *testing.T, in string) {
encoded := encodeElements(in)
assert.Len(t, encoded, 1)
assert.False(t, strings.ContainsRune(encoded[0], '/'))
decoded, err := decodeElement(encoded[0])
require.NoError(t, err, clues.ToCore(err))
assert.Equal(t, in, decoded)
})
}