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
|
package nestedtext_test
import (
"testing"
"github.com/knadh/koanf/parsers/nestedtext"
)
func TestParser(t *testing.T) {
// test input in NestedText format
ntsource := `ports:
- 6483
- 8020
- 9332
timeout: 20
`
// Test decoder
nt := nestedtext.Parser()
c, err := nt.Unmarshal([]byte(ntsource))
if err != nil {
t.Fatal("Unmarshal of NestedText input failed")
}
timeout := c["timeout"]
if timeout != "20" {
t.Logf("config-tree: %#v", c)
t.Errorf("expected timeout-parameter to be 20, is %q", timeout)
}
// test encoder
out, err := nt.Marshal(c)
if err != nil {
t.Fatal("Marshal of config to NestedText failed")
}
if string(out) != ntsource {
t.Logf("config-text: %q", string(out))
t.Errorf("expected output of Marshal(…) to equal input to Unmarshal(…); didn't")
}
}
|