summaryrefslogtreecommitdiff
path: root/roundtrip_test.go
blob: 71ca6d03e3ba92391bd69b86402e65b123508cf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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())
		})
	}
}