mirror of https://git.jolheiser.com/cfg.git
68 lines
1.1 KiB
Go
68 lines
1.1 KiB
Go
|
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,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
encoders := []Encoding{JSON, JSONC, YAML, TOML}
|
||
|
// 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
|
||
|
}
|