2015-09-28 01:39:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-09-28 02:00:38 +00:00
|
|
|
"fmt"
|
2017-02-26 22:01:52 +00:00
|
|
|
"github.com/mikefarah/yaml/Godeps/_workspace/src/gopkg.in/yaml.v2"
|
2015-10-05 23:01:33 +00:00
|
|
|
"sort"
|
2015-09-28 02:00:38 +00:00
|
|
|
"testing"
|
2015-09-28 01:39:16 +00:00
|
|
|
)
|
|
|
|
|
2015-09-28 01:58:56 +00:00
|
|
|
func TestReadMap_simple(t *testing.T) {
|
2015-10-05 23:01:45 +00:00
|
|
|
var data = parseData(`
|
|
|
|
---
|
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
`)
|
|
|
|
assertResult(t, 2, readMap(data, "b", []string{"c"}))
|
2015-09-28 01:39:16 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 23:01:33 +00:00
|
|
|
func TestReadMap_splat(t *testing.T) {
|
|
|
|
var data = parseData(`
|
|
|
|
---
|
|
|
|
mapSplat:
|
|
|
|
item1: things
|
|
|
|
item2: whatever
|
|
|
|
`)
|
2015-10-07 02:36:39 +00:00
|
|
|
var result = readMap(data, "mapSplat", []string{"*"}).([]interface{})
|
|
|
|
var actual = []string{result[0].(string), result[1].(string)}
|
|
|
|
sort.Strings(actual)
|
|
|
|
assertResult(t, "[things whatever]", fmt.Sprintf("%v", actual))
|
2015-10-05 23:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadMap_deep_splat(t *testing.T) {
|
|
|
|
var data = parseData(`
|
|
|
|
---
|
|
|
|
mapSplatDeep:
|
|
|
|
item1:
|
|
|
|
cats: bananas
|
|
|
|
item2:
|
|
|
|
cats: apples
|
|
|
|
`)
|
|
|
|
|
|
|
|
var result = readMap(data, "mapSplatDeep", []string{"*", "cats"}).([]interface{})
|
|
|
|
var actual = []string{result[0].(string), result[1].(string)}
|
|
|
|
sort.Strings(actual)
|
|
|
|
assertResult(t, "[apples bananas]", fmt.Sprintf("%v", actual))
|
|
|
|
}
|
|
|
|
|
2015-10-03 07:06:33 +00:00
|
|
|
func TestReadMap_key_doesnt_exist(t *testing.T) {
|
2015-10-05 23:01:45 +00:00
|
|
|
var data = parseData(`
|
|
|
|
---
|
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
`)
|
|
|
|
assertResult(t, nil, readMap(data, "b.x.f", []string{"c"}))
|
2015-10-03 07:06:33 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 03:47:24 +00:00
|
|
|
func TestReadMap_recurse_against_string(t *testing.T) {
|
2015-10-05 23:01:45 +00:00
|
|
|
var data = parseData(`
|
|
|
|
---
|
|
|
|
a: cat
|
|
|
|
`)
|
|
|
|
assertResult(t, nil, readMap(data, "a", []string{"b"}))
|
2015-10-05 03:47:24 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 07:06:33 +00:00
|
|
|
func TestReadMap_with_array(t *testing.T) {
|
2015-10-05 23:01:45 +00:00
|
|
|
var data = parseData(`
|
|
|
|
---
|
|
|
|
b:
|
|
|
|
d:
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
`)
|
|
|
|
assertResult(t, 4, readMap(data, "b", []string{"d", "1"}))
|
2015-09-28 01:39:16 +00:00
|
|
|
}
|
2015-09-29 00:56:28 +00:00
|
|
|
|
2015-10-03 07:06:33 +00:00
|
|
|
func TestReadMap_with_array_out_of_bounds(t *testing.T) {
|
2015-10-05 23:01:45 +00:00
|
|
|
var data = parseData(`
|
|
|
|
---
|
|
|
|
b:
|
|
|
|
d:
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
`)
|
|
|
|
assertResult(t, nil, readMap(data, "b", []string{"d", "3"}))
|
2015-10-03 06:50:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-16 05:11:06 +00:00
|
|
|
func TestReadMap_with_array_out_of_bounds_by_1(t *testing.T) {
|
|
|
|
var data = parseData(`
|
|
|
|
---
|
|
|
|
b:
|
|
|
|
d:
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
`)
|
|
|
|
assertResult(t, nil, readMap(data, "b", []string{"d", "2"}))
|
|
|
|
}
|
|
|
|
|
2015-10-05 03:41:01 +00:00
|
|
|
func TestReadMap_with_array_splat(t *testing.T) {
|
2015-10-05 23:01:45 +00:00
|
|
|
var data = parseData(`
|
|
|
|
e:
|
|
|
|
-
|
|
|
|
name: Fred
|
|
|
|
thing: cat
|
|
|
|
-
|
|
|
|
name: Sam
|
|
|
|
thing: dog
|
|
|
|
`)
|
|
|
|
assertResult(t, "[Fred Sam]", fmt.Sprintf("%v", readMap(data, "e", []string{"*", "name"})))
|
2015-10-05 03:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 22:01:52 +00:00
|
|
|
func TestWrite_really_simple(t *testing.T) {
|
|
|
|
var data = parseData(`
|
|
|
|
b: 2
|
|
|
|
`)
|
|
|
|
|
|
|
|
write(data, "b", []string{}, "4")
|
|
|
|
b := entryInSlice(data, "b").Value
|
|
|
|
assertResult(t, "4", b)
|
|
|
|
}
|
|
|
|
|
2015-09-29 00:56:28 +00:00
|
|
|
func TestWrite_simple(t *testing.T) {
|
2015-10-05 23:01:45 +00:00
|
|
|
var data = parseData(`
|
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
`)
|
2015-09-29 00:56:28 +00:00
|
|
|
|
2015-10-05 23:01:45 +00:00
|
|
|
write(data, "b", []string{"c"}, "4")
|
2017-02-26 22:01:52 +00:00
|
|
|
b := entryInSlice(data, "b").Value.(yaml.MapSlice)
|
|
|
|
c := entryInSlice(b, "c").Value
|
|
|
|
assertResult(t, "4", c)
|
2015-10-01 04:43:57 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 02:57:05 +00:00
|
|
|
func TestWrite_array(t *testing.T) {
|
|
|
|
var data = parseData(`
|
|
|
|
b:
|
|
|
|
- aa
|
|
|
|
`)
|
|
|
|
|
|
|
|
write(data, "b", []string{"0"}, "bb")
|
|
|
|
|
2017-02-26 22:01:52 +00:00
|
|
|
b := entryInSlice(data, "b").Value.([]interface{})
|
2017-02-09 02:57:05 +00:00
|
|
|
assertResult(t, "bb", b[0].(string))
|
|
|
|
}
|
|
|
|
|
2015-10-05 04:59:59 +00:00
|
|
|
func TestWrite_with_no_tail(t *testing.T) {
|
2015-10-05 23:01:45 +00:00
|
|
|
var data = parseData(`
|
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
`)
|
|
|
|
write(data, "b", []string{}, "4")
|
2015-10-05 04:59:59 +00:00
|
|
|
|
2017-02-26 22:01:52 +00:00
|
|
|
b := entryInSlice(data, "b").Value
|
2015-10-05 04:59:59 +00:00
|
|
|
assertResult(t, "4", fmt.Sprintf("%v", b))
|
|
|
|
}
|