2022-03-10 11:12:55 +03:00
|
|
|
package metadatautil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-11-15 11:58:58 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-03-10 11:12:55 +03:00
|
|
|
)
|
|
|
|
|
2022-04-21 14:54:14 +03:00
|
|
|
var blackhole []byte
|
|
|
|
|
|
|
|
func BenchmarkJSONToYAML(b *testing.B) {
|
|
|
|
funcs := []struct {
|
|
|
|
name string
|
|
|
|
f func([]byte) ([]byte, error)
|
|
|
|
}{
|
|
|
|
{"cuelang/encoding", JSONToYAML},
|
|
|
|
}
|
|
|
|
for _, f := range funcs {
|
|
|
|
b.Run(f.name, func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
input, err := ioutil.ReadFile("testdata/json/t2/metadata.json")
|
|
|
|
assert.NoError(b, err)
|
|
|
|
blackhole, err = f.f(input)
|
|
|
|
assert.NoError(b, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 11:12:55 +03:00
|
|
|
func TestJSONToYAML(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
inputFile string
|
|
|
|
wantGoldenFile string
|
|
|
|
wantErr bool
|
2022-11-15 11:58:58 +03:00
|
|
|
assertErr require.ErrorAssertionFunc
|
2022-03-10 11:12:55 +03:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"can preserve order of json",
|
|
|
|
"testdata/json/t1/metadata.json",
|
|
|
|
"testdata/json/t1/want.metadata.yaml",
|
|
|
|
false,
|
2022-11-15 11:58:58 +03:00
|
|
|
require.NoError,
|
2022-03-10 11:12:55 +03:00
|
|
|
},
|
2022-04-21 14:54:14 +03:00
|
|
|
{
|
|
|
|
"can preserve order of json in largish metadata",
|
|
|
|
"testdata/json/t2/metadata.json",
|
|
|
|
"testdata/json/t2/want.metadata.yaml",
|
|
|
|
false,
|
2022-11-15 11:58:58 +03:00
|
|
|
require.NoError,
|
2022-04-21 14:54:14 +03:00
|
|
|
},
|
2022-03-10 11:12:55 +03:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
input, err := ioutil.ReadFile(tt.inputFile)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
got, err := JSONToYAML(input)
|
2022-11-15 11:58:58 +03:00
|
|
|
tt.assertErr(t, err)
|
|
|
|
if !tt.wantErr {
|
2022-03-10 11:12:55 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// uncomment to update golden file
|
|
|
|
// assert.NoError(t, ioutil.WriteFile(tt.wantGoldenFile, got, os.ModePerm))
|
|
|
|
|
|
|
|
want, err := ioutil.ReadFile(tt.wantGoldenFile)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, string(want), string(got))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|