summaryrefslogtreecommitdiff
path: root/roundtrip_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'roundtrip_test.go')
-rw-r--r--roundtrip_test.go91
1 files changed, 73 insertions, 18 deletions
diff --git a/roundtrip_test.go b/roundtrip_test.go
index 71ca6d0..01d6f13 100644
--- a/roundtrip_test.go
+++ b/roundtrip_test.go
@@ -6,9 +6,9 @@ package lowmemjson_test
import (
"bytes"
+ "encoding/json"
"os"
"path/filepath"
- "strings"
"testing"
"github.com/stretchr/testify/require"
@@ -16,7 +16,7 @@ import (
"git.lukeshu.com/go/lowmemjson"
)
-type ScanDevicesResult map[string]ScanOneDeviceResult
+type ScanDevicesResult map[uint64]ScanOneDeviceResult
type ScanOneDeviceResult struct {
FoundExtentCSums []SysExtentCSum
@@ -27,27 +27,82 @@ type SysExtentCSum struct {
Sums SumRun
}
+type Optional[T any] struct {
+ OK bool
+ Val T
+}
+
+var (
+ _ json.Marshaler = Optional[bool]{}
+ _ json.Unmarshaler = (*Optional[bool])(nil)
+)
+
+func (o Optional[T]) MarshalJSON() ([]byte, error) {
+ if o.OK {
+ return json.Marshal(o.Val)
+ } else {
+ return []byte("null"), nil
+ }
+}
+
+func (o *Optional[T]) UnmarshalJSON(dat []byte) error {
+ if string(dat) == "null" {
+ *o = Optional[T]{}
+ return nil
+ }
+ o.OK = true
+ return json.Unmarshal(dat, &o.Val)
+}
+
+type QualifiedPhysicalAddr struct {
+ Dev uint64
+ Addr int64
+}
+
+type Mapping struct {
+ LAddr int64
+ PAddr QualifiedPhysicalAddr
+ Size int64
+ SizeLocked bool `json:",omitempty"`
+ Flags Optional[uint64] `json:",omitempty"`
+}
+
func TestRoundTrip(t *testing.T) {
t.Parallel()
- dents, err := os.ReadDir(filepath.Join("testdata", "roundtrip"))
- require.NoError(t, err)
- for _, dent := range dents {
- filename := dent.Name()
- if !strings.HasSuffix(filename, ".json") {
- continue
- }
- t.Run(strings.TrimSuffix(filename, ".json"), func(t *testing.T) {
+
+ type testcase struct {
+ ObjPtr any
+ Cfg lowmemjson.ReEncoderConfig
+ }
+ testcases := map[string]testcase{
+ "scandevices": {
+ ObjPtr: new(ScanDevicesResult),
+ Cfg: lowmemjson.ReEncoderConfig{
+ Indent: "\t",
+ ForceTrailingNewlines: true,
+ CompactIfUnder: 16,
+ },
+ },
+ "mappings": {
+ ObjPtr: new([]Mapping),
+ Cfg: lowmemjson.ReEncoderConfig{
+ Indent: "\t",
+ ForceTrailingNewlines: true,
+ CompactIfUnder: 120,
+ },
+ },
+ }
+
+ for tcName, tc := range testcases {
+ tcName := tcName
+ tc := tc
+ t.Run(tcName, func(t *testing.T) {
t.Parallel()
- inBytes, err := os.ReadFile(filepath.Join("testdata", "roundtrip", filename))
+ inBytes, err := os.ReadFile(filepath.Join("testdata", "roundtrip", tcName+".json")) // #nosec G304
require.NoError(t, err)
- var obj ScanDevicesResult
- require.NoError(t, lowmemjson.NewDecoder(bytes.NewReader(inBytes)).DecodeThenEOF(&obj))
+ require.NoError(t, lowmemjson.NewDecoder(bytes.NewReader(inBytes)).DecodeThenEOF(tc.ObjPtr))
var outBytes bytes.Buffer
- require.NoError(t, lowmemjson.NewEncoder(lowmemjson.NewReEncoder(&outBytes, lowmemjson.ReEncoderConfig{
- Indent: "\t",
- ForceTrailingNewlines: true,
- CompactIfUnder: 16, //nolint:gomnd // This is what looks nice.
- })).Encode(obj))
+ require.NoError(t, lowmemjson.NewEncoder(lowmemjson.NewReEncoder(&outBytes, tc.Cfg)).Encode(tc.ObjPtr))
require.Equal(t, string(inBytes), outBytes.String())
})
}