yq/yaml_test.go

51 lines
938 B
Go
Raw Normal View History

2015-09-28 01:39:16 +00:00
package main
import (
2015-09-28 02:00:38 +00:00
"fmt"
"gopkg.in/yaml.v2"
"os"
"testing"
2015-09-28 01:39:16 +00:00
)
var rawData = `
2015-09-28 01:39:16 +00:00
a: Easy!
b:
c: 2
d: [3, 4]
`
var parsedData map[interface{}]interface{}
2015-09-28 01:39:16 +00:00
func TestMain(m *testing.M) {
err := yaml.Unmarshal([]byte(rawData), &parsedData)
2015-09-28 02:00:38 +00:00
if err != nil {
fmt.Println("Error parsing yaml: %v", err)
os.Exit(1)
}
2015-09-28 01:39:16 +00:00
2015-09-28 02:00:38 +00:00
os.Exit(m.Run())
2015-09-28 01:39:16 +00:00
}
func TestReadMap_simple(t *testing.T) {
2015-10-01 04:43:57 +00:00
assertResult(t, 2, readMap(parsedData, "b", []string{"c"}))
2015-09-28 01:39:16 +00:00
}
func TestReadMap_array(t *testing.T) {
2015-10-01 04:43:57 +00:00
assertResult(t, 4, readMap(parsedData, "b", []string{"d", "1"}))
2015-09-28 01:39:16 +00:00
}
func TestWrite_simple(t *testing.T) {
write(parsedData, "b", []string{"c"}, "4")
b := parsedData["b"].(map[interface{}]interface{})
2015-10-01 04:43:57 +00:00
assertResult(t, "4", b["c"].(string))
}
func assertResult(t *testing.T, expectedValue interface{}, actualValue interface{}) {
if (expectedValue != actualValue) {
t.Error("Expected <", expectedValue, "> but got <", actualValue, ">")
}
}