Skip to content

Commit

Permalink
Test SaveObjectToFile.
Browse files Browse the repository at this point in the history
  • Loading branch information
winder committed Nov 13, 2023
1 parent 231494e commit 6a7af22
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions util/codecs/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package codecs

import (
"bytes"
"os"
"path"
"testing"
"time"

Expand Down Expand Up @@ -58,6 +60,44 @@ func TestIsDefaultValue(t *testing.T) {
a.True(isDefaultValue("Missing", objectValues, defaultValues))
}

func TestSaveObjectToFile(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

type TestType struct {
A uint64
B string
}

obj := TestType{1024, "test"}

// prettyFormat = false
{
filename := path.Join(t.TempDir(), "test.json")
SaveObjectToFile(filename, obj, false)
data, err := os.ReadFile(filename)
require.NoError(t, err)
expected := `{"A":1024,"B":"test"}
`
require.Equal(t, expected, string(data))
}

// prettyFormat = true
{
filename := path.Join(t.TempDir(), "test.json")
SaveObjectToFile(filename, obj, true)
data, err := os.ReadFile(filename)
require.NoError(t, err)
expected := `{
"A": 1024,
"B": "test"
}
`
require.Equal(t, expected, string(data))
}

}

func TestWriteNonDefaultValue(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()
Expand Down

0 comments on commit 6a7af22

Please sign in to comment.