From 69e4520942a27d7e8f8cdaf8f4611bf24fb73e7b Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 3 Feb 2023 22:53:53 -0700 Subject: Add more formatter tests --- roundtrip_test.go | 54 +++++++++++++++++++++++++++++++++++++ testdata/roundtrip/scandevices.json | 14 ++++++++++ 2 files changed, 68 insertions(+) create mode 100644 roundtrip_test.go create mode 100644 testdata/roundtrip/scandevices.json 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 +// +// 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" + } + } + ] + } +} -- cgit v1.2.3-54-g00ecf