yq/data_navigator_test.go

74 lines
1.6 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"
2015-10-01 05:20:07 +00:00
"strings"
2015-09-28 02:00:38 +00:00
"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))
}
2015-10-01 05:20:07 +00:00
var getValueTests = []struct {
argument string
expectedResult interface{}
testDescription string
}{
{"true", true, "boolean"},
{"3.4", 3.4, "number"},
}
func TestGetValue(t *testing.T) {
for _, tt := range getValueTests {
assertResultWithContext(t, tt.expectedResult, getValue(tt.argument, false), tt.testDescription)
assertResultWithContext(t, tt.argument, getValue(tt.argument, true), strings.Join([]string{tt.testDescription, "with forceString"}, " "))
}
}
2015-10-01 04:43:57 +00:00
func assertResult(t *testing.T, expectedValue interface{}, actualValue interface{}) {
2015-10-01 05:20:07 +00:00
if expectedValue != actualValue {
2015-10-01 04:43:57 +00:00
t.Error("Expected <", expectedValue, "> but got <", 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-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
}
}