76 lines
1.5 KiB
Go
76 lines
1.5 KiB
Go
package ident_test
|
|
|
|
import (
|
|
"encoding"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
// rTestCases describes multiple representation test cases.
|
|
type rTestCases[V any, S interface {
|
|
encoding.TextMarshaler
|
|
encoding.TextUnmarshaler
|
|
fmt.Stringer
|
|
|
|
*V
|
|
}] []struct {
|
|
name string
|
|
ident V
|
|
want []byte
|
|
err error
|
|
}
|
|
|
|
// checkRepresentation runs tests described by testCases.
|
|
func (testCases rTestCases[V, S]) run(t *testing.T) {
|
|
t.Helper()
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Helper()
|
|
t.Parallel()
|
|
|
|
t.Run("decode", func(t *testing.T) {
|
|
t.Helper()
|
|
t.Parallel()
|
|
|
|
var got V
|
|
if err := S(&got).UnmarshalText(tc.want); !reflect.DeepEqual(err, tc.err) {
|
|
t.Fatalf("UnmarshalText: error = %v, want %v", err, tc.err)
|
|
}
|
|
|
|
if tc.err != nil {
|
|
return
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tc.ident) {
|
|
t.Errorf("UnmarshalText: %#v, want %#v", got, tc.ident)
|
|
}
|
|
})
|
|
|
|
if tc.err != nil {
|
|
return
|
|
}
|
|
|
|
t.Run("encode", func(t *testing.T) {
|
|
t.Helper()
|
|
t.Parallel()
|
|
|
|
if got, err := S(&tc.ident).MarshalText(); err != nil {
|
|
t.Fatalf("MarshalText: error = %v", err)
|
|
} else if string(got) != string(tc.want) {
|
|
raw, decodeErr := base64.URLEncoding.AppendDecode(nil, got)
|
|
t.Logf("AppendDecode: %#v, error = %v", raw, decodeErr)
|
|
|
|
t.Errorf("MarshalText: %#v, want %#v", got, tc.want)
|
|
}
|
|
|
|
if got := S(&tc.ident).String(); got != string(tc.want) {
|
|
t.Errorf("String: %#v, want %#v", []byte(got), tc.want)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|