2024-06-06 01:11:19 +00:00
|
|
|
package cfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/matryer/is"
|
|
|
|
"go.jolheiser.com/nixfig"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestData struct {
|
|
|
|
Foo string
|
|
|
|
Baz bool
|
|
|
|
Bux int
|
|
|
|
Qux TestSubData
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestSubData struct {
|
|
|
|
Honk string
|
|
|
|
Chonk int
|
|
|
|
Gonk bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEncoding(t *testing.T) {
|
|
|
|
assert := is.New(t)
|
|
|
|
|
|
|
|
// Starting with dhall since it can't be encoded
|
|
|
|
dhall := `
|
|
|
|
{
|
|
|
|
Foo = "bar"
|
|
|
|
, Baz = True
|
|
|
|
, Bux = 10
|
|
|
|
, Qux = {
|
|
|
|
Honk = "bonk"
|
|
|
|
, Chonk = 50
|
|
|
|
, Gonk = False
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
final := TestData{
|
|
|
|
Foo: "bar",
|
|
|
|
Baz: true,
|
|
|
|
Bux: 10,
|
|
|
|
Qux: TestSubData{
|
|
|
|
Honk: "bonk",
|
|
|
|
Chonk: 50,
|
|
|
|
Gonk: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-08 00:05:59 +00:00
|
|
|
encoders := []Encoding{JSON, JSONC, YAML, TOML, KDL}
|
2024-06-06 01:11:19 +00:00
|
|
|
// Only test nix if it's available
|
|
|
|
if nixfig.Nix != "" {
|
|
|
|
encoders = append(encoders, NIX)
|
|
|
|
}
|
|
|
|
|
|
|
|
var data TestData
|
|
|
|
err := DHALL.Unmarshal([]byte(dhall), &data)
|
|
|
|
assert.NoErr(err) // Should be able to unmarshal dhall
|
|
|
|
|
|
|
|
for _, e := range encoders {
|
|
|
|
out, err := e.Marshal(data)
|
|
|
|
assert.NoErr(err) // Should be able to marshal
|
|
|
|
err = e.Unmarshal(out, &data)
|
|
|
|
assert.NoErr(err) // Should be able to unmarshal
|
|
|
|
}
|
|
|
|
assert.Equal(data, final) // Final structs should be equal
|
|
|
|
}
|