summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-04 17:48:56 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-04 17:48:56 -0700
commit0599269e3f7abd82d97892f45b82891003d19139 (patch)
treef3af488852988fbfe80a7dfe4ec7424110df5c5b
parent862657e44d21e5c36adbc2660045a41e3aca083e (diff)
fixup! Add more formatter tests
-rw-r--r--roundtrip_test.go91
-rw-r--r--testdata/roundtrip/mappings.json15
-rw-r--r--testdata/roundtrip/scandevices.json2
3 files changed, 89 insertions, 19 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())
})
}
diff --git a/testdata/roundtrip/mappings.json b/testdata/roundtrip/mappings.json
new file mode 100644
index 0000000..69e596f
--- /dev/null
+++ b/testdata/roundtrip/mappings.json
@@ -0,0 +1,15 @@
+[
+ {"LAddr":1048576,"PAddr":{"Dev":1,"Addr":1048576},"Size":4194304,"Flags":2},
+ {"LAddr":22020096,"PAddr":{"Dev":1,"Addr":22020096},"Size":1073741824,"Flags":4},
+ {"LAddr":1095761920,"PAddr":{"Dev":1,"Addr":1095761920},"Size":1073741824,"Flags":1},
+ {"LAddr":2169503744,"PAddr":{"Dev":1,"Addr":2169503744},"Size":1073741824,"Flags":1},
+ {"LAddr":3243245568,"PAddr":{"Dev":1,"Addr":3243245568},"Size":1073741824,"Flags":1},
+ {"LAddr":4316987392,"PAddr":{"Dev":1,"Addr":4316987392},"Size":1073741824,"Flags":1},
+ {"LAddr":5390729216,"PAddr":{"Dev":1,"Addr":5390729216},"Size":1073741824,"Flags":1},
+ {"LAddr":6464471040,"PAddr":{"Dev":1,"Addr":6464471040},"Size":1073741824,"Flags":1},
+ {"LAddr":7538212864,"PAddr":{"Dev":1,"Addr":7538212864},"Size":1073741824,"Flags":1},
+ {"LAddr":8611954688,"PAddr":{"Dev":1,"Addr":8611954688},"Size":1073741824,"Flags":1},
+ {"LAddr":9685696512,"PAddr":{"Dev":1,"Addr":9685696512},"Size":1073741824,"Flags":1},
+ {"LAddr":413900537856,"PAddr":{"Dev":1,"Addr":227655286784},"Size":1073741824,"Flags":1},
+ {"LAddr":414974279680,"PAddr":{"Dev":1,"Addr":243761414144},"Size":1073741824,"Flags":1}
+]
diff --git a/testdata/roundtrip/scandevices.json b/testdata/roundtrip/scandevices.json
index c62f7df..f56a312 100644
--- a/testdata/roundtrip/scandevices.json
+++ b/testdata/roundtrip/scandevices.json
@@ -1,5 +1,5 @@
{
- "dev-1234": {
+ "1234": {
"FoundExtentCSums": [
{
"Generation": 6596005,