summaryrefslogtreecommitdiff
path: root/roundtrip_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'roundtrip_test.go')
-rw-r--r--roundtrip_test.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/roundtrip_test.go b/roundtrip_test.go
new file mode 100644
index 0000000..71ca6d0
--- /dev/null
+++ b/roundtrip_test.go
@@ -0,0 +1,54 @@
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package lowmemjson_test
+
+import (
+ "bytes"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "git.lukeshu.com/go/lowmemjson"
+)
+
+type ScanDevicesResult map[string]ScanOneDeviceResult
+
+type ScanOneDeviceResult struct {
+ FoundExtentCSums []SysExtentCSum
+}
+
+type SysExtentCSum struct {
+ Generation int64
+ Sums SumRun
+}
+
+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) {
+ t.Parallel()
+ inBytes, err := os.ReadFile(filepath.Join("testdata", "roundtrip", filename))
+ require.NoError(t, err)
+ var obj ScanDevicesResult
+ require.NoError(t, lowmemjson.NewDecoder(bytes.NewReader(inBytes)).DecodeThenEOF(&obj))
+ 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.Equal(t, string(inBytes), outBytes.String())
+ })
+ }
+}