nix-amer/internal/cnf/yaml_test.go

193 lines
3.9 KiB
Go
Raw Permalink Normal View History

package cnf
import (
"bytes"
"testing"
)
func TestYamlGet(t *testing.T) {
tests := []struct {
raw string
key string
}{
{"key: value\n", "key"},
{"ignore: xxx\nkey: value\n", "key"},
{"parent:\n child: value\n", "parent.child"},
{"ignore: xxx\nparent:\n child: value\n", "parent.child"},
}
for _, test := range tests {
parser := new(yaml)
reader := bytes.NewBufferString(test.raw)
// try to extract value
_, err := parser.ReadFrom(reader)
if err != nil {
t.Errorf("parse error: %s", err)
continue
}
// extract value
value, found := parser.Get(test.key)
if !found {
t.Errorf("expected a result, got none")
continue
}
// check value
if value != "value" {
t.Errorf("expected 'value' got '%s'", value)
}
}
}
func TestYamlGetNotString(t *testing.T) {
tests := []struct {
raw string
key string
}{
{"key:\n- value\n", "key"},
{"key:\n subkey: value\n", "key"},
{"parent:\n child:\n - value\n", "parent.child"},
{"parent:\n child:\n subkey: value\n", "parent.child"},
}
for i, test := range tests {
parser := new(yaml)
reader := bytes.NewBufferString(test.raw)
// try to extract value
_, err := parser.ReadFrom(reader)
if err != nil {
t.Errorf("[%d] parse error: %s", i, err)
continue
}
// extract value
value, found := parser.Get(test.key)
if found || len(value) > 0 {
t.Errorf("[%d] expected no result, got '%s'", i, value)
continue
}
}
}
func TestYamlSetPathExistsAndIsString(t *testing.T) {
tests := []struct {
raw string
key string
value string
}{
{"key: value\n", "key", "newvalue"},
{"ignore: xxx\nkey: value\n", "key", "newvalue"},
{"parent:\n child: value\n", "parent.child", "newvalue"},
{"ignore: xxx\nparent:\n child: value\n", "parent.child", "newvalue"},
}
for i, test := range tests {
parser := new(yaml)
reader := bytes.NewBufferString(test.raw)
// try to extract value
_, err := parser.ReadFrom(reader)
if err != nil {
t.Errorf("parse error: %s", err)
continue
}
// update value
if !parser.Set(test.key, test.value) {
t.Errorf("[%d] cannot set '%s' to '%s'", i, test.key, test.value)
continue
}
// check new value
value, found := parser.Get(test.key)
if !found {
t.Errorf("[%d] expected a result, got none", i)
continue
}
// check value
if value != test.value {
t.Errorf("[%d] expected '%s' got '%s'", i, test.value, value)
}
}
}
func TestYamlSetCreatePath(t *testing.T) {
tests := []struct {
raw string
key string
ignore string // path to field that must be present after transformation
value string
}{
{"ignore: xxx\n", "key", "ignore", "newvalue"},
{"ignore: xxx\n", "parent.child", "ignore", "newvalue"},
{"ignore: xxx\n", "parent.child.subchild", "ignore", "newvalue"},
{"ignore: xxx\n", "key", "ignore", "newvalue"},
{"parent:\n ignore: xxx\n", "parent.child", "parent.ignore", "newvalue"},
{"ignore: xxx\nparent: {}\n", "parent.child", "ignore", "newvalue"},
}
for i, test := range tests {
parser := new(yaml)
reader := bytes.NewBufferString(test.raw)
// try to extract value
_, err := parser.ReadFrom(reader)
if err != nil {
t.Errorf("[%d] parse error: %s", i, err)
continue
}
// update value
if !parser.Set(test.key, test.value) {
t.Errorf("[%d] cannot set '%s' to '%s'", i, test.key, test.value)
continue
}
// check new value
value, found := parser.Get(test.key)
if !found {
t.Errorf("[%d] expected a result, got none", i)
continue
}
// check value
if value != test.value {
t.Errorf("[%d] expected '%s' got '%s'", i, test.value, value)
continue
}
// check that ignore field is still there
value, found = parser.Get(test.ignore)
if !found {
t.Errorf("[%d] expected ignore field, got none", i)
continue
}
// check value
if value != "xxx" {
t.Errorf("[%d] expected ignore value to be '%s' got '%s'", i, "xxx", value)
continue
}
}
}