mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-13 11:55:38 +00:00
Individual tests now have their own test data
This commit is contained in:
parent
805729e4f7
commit
6a696c81db
@ -7,14 +7,66 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var rawData = `
|
func parseData(rawData string) map[interface{}]interface{} {
|
||||||
|
var parsedData map[interface{}]interface{}
|
||||||
|
err := yaml.Unmarshal([]byte(rawData), &parsedData)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error parsing yaml: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
return parsedData
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadMap_simple(t *testing.T) {
|
||||||
|
var data = parseData(`
|
||||||
---
|
---
|
||||||
a: Easy!
|
|
||||||
b:
|
b:
|
||||||
c: 2
|
c: 2
|
||||||
|
`)
|
||||||
|
assertResult(t, 2, readMap(data, "b", []string{"c"}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadMap_key_doesnt_exist(t *testing.T) {
|
||||||
|
var data = parseData(`
|
||||||
|
---
|
||||||
|
b:
|
||||||
|
c: 2
|
||||||
|
`)
|
||||||
|
assertResult(t, nil, readMap(data, "b.x.f", []string{"c"}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadMap_recurse_against_string(t *testing.T) {
|
||||||
|
var data = parseData(`
|
||||||
|
---
|
||||||
|
a: cat
|
||||||
|
`)
|
||||||
|
assertResult(t, nil, readMap(data, "a", []string{"b"}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadMap_with_array(t *testing.T) {
|
||||||
|
var data = parseData(`
|
||||||
|
---
|
||||||
|
b:
|
||||||
d:
|
d:
|
||||||
- 3
|
- 3
|
||||||
- 4
|
- 4
|
||||||
|
`)
|
||||||
|
assertResult(t, 4, readMap(data, "b", []string{"d", "1"}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadMap_with_array_out_of_bounds(t *testing.T) {
|
||||||
|
var data = parseData(`
|
||||||
|
---
|
||||||
|
b:
|
||||||
|
d:
|
||||||
|
- 3
|
||||||
|
- 4
|
||||||
|
`)
|
||||||
|
assertResult(t, nil, readMap(data, "b", []string{"d", "3"}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadMap_with_array_splat(t *testing.T) {
|
||||||
|
var data = parseData(`
|
||||||
e:
|
e:
|
||||||
-
|
-
|
||||||
name: Fred
|
name: Fred
|
||||||
@ -22,57 +74,30 @@ e:
|
|||||||
-
|
-
|
||||||
name: Sam
|
name: Sam
|
||||||
thing: dog
|
thing: dog
|
||||||
`
|
`)
|
||||||
|
assertResult(t, "[Fred Sam]", fmt.Sprintf("%v", readMap(data, "e", []string{"*", "name"})))
|
||||||
var parsedData map[interface{}]interface{}
|
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
|
||||||
err := yaml.Unmarshal([]byte(rawData), &parsedData)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error parsing yaml: %v", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
os.Exit(m.Run())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadMap_simple(t *testing.T) {
|
|
||||||
assertResult(t, 2, readMap(parsedData, "b", []string{"c"}))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadMap_key_doesnt_exist(t *testing.T) {
|
|
||||||
assertResult(t, nil, readMap(parsedData, "b.x.f", []string{"c"}))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadMap_recurse_against_string(t *testing.T) {
|
|
||||||
assertResult(t, nil, readMap(parsedData, "a", []string{"b"}))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadMap_with_array(t *testing.T) {
|
|
||||||
assertResult(t, 4, readMap(parsedData, "b", []string{"d", "1"}))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadMap_with_array_out_of_bounds(t *testing.T) {
|
|
||||||
assertResult(t, nil, readMap(parsedData, "b", []string{"d", "3"}))
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
func TestWrite_simple(t *testing.T) {
|
||||||
|
var data = parseData(`
|
||||||
|
b:
|
||||||
|
c: 2
|
||||||
|
`)
|
||||||
|
|
||||||
write(parsedData, "b", []string{"c"}, "4")
|
write(data, "b", []string{"c"}, "4")
|
||||||
|
|
||||||
b := parsedData["b"].(map[interface{}]interface{})
|
b := data["b"].(map[interface{}]interface{})
|
||||||
assertResult(t, "4", b["c"].(string))
|
assertResult(t, "4", b["c"].(string))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWrite_with_no_tail(t *testing.T) {
|
func TestWrite_with_no_tail(t *testing.T) {
|
||||||
|
var data = parseData(`
|
||||||
|
b:
|
||||||
|
c: 2
|
||||||
|
`)
|
||||||
|
write(data, "b", []string{}, "4")
|
||||||
|
|
||||||
write(parsedData, "b", []string{}, "4")
|
b := data["b"]
|
||||||
|
|
||||||
b := parsedData["b"]
|
|
||||||
assertResult(t, "4", fmt.Sprintf("%v", b))
|
assertResult(t, "4", fmt.Sprintf("%v", b))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user