summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-03 22:53:53 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-03 22:53:53 -0700
commit69e4520942a27d7e8f8cdaf8f4611bf24fb73e7b (patch)
tree8ef5ed67a2e126ffd60c666ec0f0a42e3e810da1
parent34fac20f675f28c61bd84ea469a6843a25035559 (diff)
Add more formatter tests
-rw-r--r--roundtrip_test.go54
-rw-r--r--testdata/roundtrip/scandevices.json14
2 files changed, 68 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())
+ })
+ }
+}
diff --git a/testdata/roundtrip/scandevices.json b/testdata/roundtrip/scandevices.json
new file mode 100644
index 0000000..c62f7df
--- /dev/null
+++ b/testdata/roundtrip/scandevices.json
@@ -0,0 +1,14 @@
+{
+ "dev-1234": {
+ "FoundExtentCSums": [
+ {
+ "Generation": 6596005,
+ "Sums": {
+ "ChecksumSize": 4,
+ "Addr": 52626001920,
+ "Sums": "d69e7384e04333e332f32f8ecfd874ea1ea3f66bd54143f826f740b995105d0e8b2d25a21709109c4930e526a68a89e48d3ebdb6f5191bca3e867eba57110e36d885ca46666168ad782b4fe9af483561"
+ }
+ }
+ ]
+ }
+}