-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
51 lines (41 loc) · 1.52 KB
/
utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package collections
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func assertBijective[T any](t *testing.T, encoder KeyEncoder[T], key T) {
encodedKey := encoder.Encode(key)
read, decodedKey := encoder.Decode(encodedKey)
require.Equal(t, len(encodedKey), read, "encoded key and read bytes must have same size")
require.Equal(t, key, decodedKey, "encoding and decoding produces different keys")
}
func assertValueBijective[T any](t *testing.T, encoder ValueEncoder[T], value T) {
encodedValue := encoder.Encode(value)
decodedValue := encoder.Decode(encodedValue)
require.Equal(t, value, decodedValue, "encoding and decoding produces different values")
}
// stringValue is a ValueEncoder for string, used for testing.
type stringValue struct{}
func (s stringValue) Encode(value string) []byte { return []byte(value) }
func (s stringValue) Decode(b []byte) string { return string(b) }
func (s stringValue) Stringify(value string) string { return value }
func (s stringValue) Name() string { return "test string" }
// jsonValue is a ValueEncoder for objects to be turned into json.
// used for testing.
type jsonValue[T any] struct{}
func (jsonValue[T]) Encode(value T) []byte {
b, _ := json.Marshal(value)
return b
}
func (jsonValue[T]) Decode(b []byte) T {
v := new(T)
_ = json.Unmarshal(b, v)
return *v
}
func (jsonValue[T]) Stringify(v T) string { return fmt.Sprintf("%#v", v) }
func (jsonValue[T]) Name() string {
var t T
return fmt.Sprintf("json-value-%T", t)
}