// 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()) }) } }