yq/data_navigator_test.go

80 lines
1.7 KiB
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-10-05 03:41:01 +00:00
---
2015-09-28 01:39:16 +00:00
a: Easy!
b:
c: 2
2015-10-05 03:41:01 +00:00
d:
- 3
- 4
e:
-
name: Fred
thing: cat
-
name: Sam
thing: dog
2015-09-28 01:39:16 +00:00
`
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
}
2015-10-03 07:06:33 +00:00
func TestReadMap_key_doesnt_exist(t *testing.T) {
assertResult(t, nil, readMap(parsedData, "b.x.f", []string{"c"}))
}
func TestReadMap_with_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
}
2015-10-03 07:06:33 +00:00
func TestReadMap_with_array_out_of_bounds(t *testing.T) {
assertResult(t, nil, readMap(parsedData, "b", []string{"d", "3"}))
}
2015-10-05 03:41:01 +00:00
func TestReadMap_with_array_splat(t *testing.T) {
assertResult(t, "[Fred Sam]", fmt.Sprintf("%v", readMap(parsedData, "e", []string{"*", "name"})))
}
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{}) {
2015-10-01 05:20:07 +00:00
if expectedValue != actualValue {
2015-10-05 03:41:01 +00:00
t.Error("Expected <", expectedValue, "> but got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
}
}
2015-10-01 05:20:07 +00:00
2015-10-03 05:10:29 +00:00
func assertResultWithContext(t *testing.T, expectedValue interface{}, actualValue interface{}, context interface{}) {
2015-10-05 03:41:01 +00:00
2015-10-01 05:20:07 +00:00
if expectedValue != actualValue {
2015-10-03 05:10:29 +00:00
t.Error(context)
t.Error(": expected <", expectedValue, "> but got <", actualValue, ">")
2015-10-01 05:20:07 +00:00
}
}