2015-09-28 01:39:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-09-28 02:00:38 +00:00
|
|
|
"fmt"
|
2015-10-05 23:01:33 +00:00
|
|
|
"sort"
|
2015-09-28 02:00:38 +00:00
|
|
|
"testing"
|
2017-09-20 23:40:33 +00:00
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
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
|
|
|
|
`)
|
2017-09-22 19:58:12 +00:00
|
|
|
got, _ := readMap(data, "b", []string{"c"})
|
|
|
|
assertResult(t, 2, got)
|
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
|
|
|
|
`)
|
2017-09-22 19:58:12 +00:00
|
|
|
res, _ := readMap(data, "mapSplat", []string{"*"})
|
|
|
|
result := res.([]interface{})
|
2015-10-07 02:36:39 +00:00
|
|
|
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
|
|
|
|
`)
|
|
|
|
|
2017-09-22 19:58:12 +00:00
|
|
|
res, _ := readMap(data, "mapSplatDeep", []string{"*", "cats"})
|
|
|
|
result := res.([]interface{})
|
2015-10-05 23:01:33 +00:00
|
|
|
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
|
|
|
|
`)
|
2017-09-22 19:58:12 +00:00
|
|
|
got, _ := readMap(data, "b.x.f", []string{"c"})
|
|
|
|
assertResult(t, nil, got)
|
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
|
|
|
|
`)
|
2017-09-22 19:58:12 +00:00
|
|
|
got, _ := readMap(data, "a", []string{"b"})
|
|
|
|
assertResult(t, nil, got)
|
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
|
|
|
|
`)
|
2017-09-22 19:58:12 +00:00
|
|
|
got, _ := readMap(data, "b", []string{"d", "1"})
|
|
|
|
assertResult(t, 4, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadMap_with_array_and_bad_index(t *testing.T) {
|
|
|
|
var data = parseData(`
|
|
|
|
---
|
|
|
|
b:
|
|
|
|
d:
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
`)
|
|
|
|
_, err := readMap(data, "b", []string{"d", "x"})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error due to invalid path")
|
|
|
|
}
|
|
|
|
expectedOutput := `Error accessing array: strconv.ParseInt: parsing "x": invalid syntax`
|
|
|
|
assertResult(t, expectedOutput, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadMap_with_mapsplat_array_and_bad_index(t *testing.T) {
|
|
|
|
var data = parseData(`
|
|
|
|
---
|
|
|
|
b:
|
|
|
|
d:
|
|
|
|
e:
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
f:
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
`)
|
|
|
|
_, err := readMap(data, "b", []string{"d", "*", "x"})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error due to invalid path")
|
|
|
|
}
|
|
|
|
expectedOutput := `Error accessing array: strconv.ParseInt: parsing "x": invalid syntax`
|
|
|
|
assertResult(t, expectedOutput, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadMap_with_arraysplat_map_array_and_bad_index(t *testing.T) {
|
|
|
|
var data = parseData(`
|
|
|
|
---
|
|
|
|
b:
|
|
|
|
d:
|
|
|
|
- names:
|
|
|
|
- fred
|
|
|
|
- smith
|
|
|
|
- names:
|
|
|
|
- sam
|
|
|
|
- bo
|
|
|
|
`)
|
|
|
|
_, err := readMap(data, "b", []string{"d", "*", "names", "x"})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error due to invalid path")
|
|
|
|
}
|
|
|
|
expectedOutput := `Error accessing array: strconv.ParseInt: parsing "x": invalid syntax`
|
|
|
|
assertResult(t, expectedOutput, err.Error())
|
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
|
|
|
|
`)
|
2017-09-22 19:58:12 +00:00
|
|
|
got, _ := readMap(data, "b", []string{"d", "3"})
|
|
|
|
assertResult(t, nil, got)
|
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
|
|
|
|
`)
|
2017-09-22 19:58:12 +00:00
|
|
|
got, _ := readMap(data, "b", []string{"d", "2"})
|
|
|
|
assertResult(t, nil, got)
|
2016-03-16 05:11:06 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
`)
|
2017-09-22 19:58:12 +00:00
|
|
|
got, _ := readMap(data, "e", []string{"*", "name"})
|
|
|
|
assertResult(t, "[Fred Sam]", fmt.Sprintf("%v", got))
|
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
|
|
|
|
`)
|
|
|
|
|
2017-04-11 23:16:54 +00:00
|
|
|
updated := writeMap(data, []string{"b"}, "4")
|
|
|
|
b := entryInSlice(updated, "b").Value
|
2017-02-26 22:01:52 +00:00
|
|
|
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
|
|
|
|
2017-04-11 23:16:54 +00:00
|
|
|
updated := writeMap(data, []string{"b", "c"}, "4")
|
|
|
|
b := entryInSlice(updated, "b").Value.(yaml.MapSlice)
|
2017-02-26 22:01:52 +00:00
|
|
|
c := entryInSlice(b, "c").Value
|
|
|
|
assertResult(t, "4", c)
|
2015-10-01 04:43:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 23:16:54 +00:00
|
|
|
func TestWrite_new(t *testing.T) {
|
|
|
|
var data = parseData(`
|
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
`)
|
|
|
|
|
|
|
|
updated := writeMap(data, []string{"b", "d"}, "4")
|
|
|
|
b := entryInSlice(updated, "b").Value.(yaml.MapSlice)
|
|
|
|
d := entryInSlice(b, "d").Value
|
|
|
|
assertResult(t, "4", d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_new_deep(t *testing.T) {
|
|
|
|
var data = parseData(`
|
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
`)
|
|
|
|
|
|
|
|
updated := writeMap(data, []string{"b", "d", "f"}, "4")
|
2017-09-22 19:58:12 +00:00
|
|
|
got, _ := readMap(updated, "b", []string{"d", "f"})
|
|
|
|
assertResult(t, "4", got)
|
2017-04-11 23:16:54 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 02:57:05 +00:00
|
|
|
func TestWrite_array(t *testing.T) {
|
|
|
|
var data = parseData(`
|
|
|
|
b:
|
|
|
|
- aa
|
|
|
|
`)
|
|
|
|
|
2017-04-11 23:16:54 +00:00
|
|
|
updated := writeMap(data, []string{"b", "0"}, "bb")
|
2017-02-09 02:57:05 +00:00
|
|
|
|
2017-04-11 23:16:54 +00:00
|
|
|
b := entryInSlice(updated, "b").Value.([]interface{})
|
2017-02-09 02:57:05 +00:00
|
|
|
assertResult(t, "bb", b[0].(string))
|
|
|
|
}
|
|
|
|
|
2017-04-11 23:16:54 +00:00
|
|
|
func TestWrite_new_array(t *testing.T) {
|
|
|
|
var data = parseData(`
|
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
`)
|
|
|
|
|
|
|
|
updated := writeMap(data, []string{"b", "0"}, "4")
|
2017-09-22 19:58:12 +00:00
|
|
|
got, _ := readMap(updated, "b", []string{"0"})
|
|
|
|
assertResult(t, "4", got)
|
2017-04-11 23:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_new_array_deep(t *testing.T) {
|
|
|
|
var data = parseData(`
|
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
`)
|
|
|
|
|
|
|
|
var expected = `b:
|
|
|
|
- c: "4"`
|
|
|
|
|
|
|
|
updated := writeMap(data, []string{"b", "0", "c"}, "4")
|
2017-09-22 19:58:12 +00:00
|
|
|
got, _ := yamlToString(updated)
|
|
|
|
assertResult(t, expected, got)
|
2017-04-11 23:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_new_map_array_deep(t *testing.T) {
|
|
|
|
var data = parseData(`
|
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
`)
|
|
|
|
|
|
|
|
updated := writeMap(data, []string{"b", "d", "0"}, "4")
|
2017-09-22 19:58:12 +00:00
|
|
|
got, _ := readMap(updated, "b", []string{"d", "0"})
|
|
|
|
assertResult(t, "4", got)
|
2017-04-11 23:16:54 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 22:53:27 +00:00
|
|
|
func TestWrite_add_to_array(t *testing.T) {
|
2017-04-17 23:14:06 +00:00
|
|
|
var data = parseData(`
|
2017-04-17 22:53:27 +00:00
|
|
|
b:
|
|
|
|
- aa
|
|
|
|
`)
|
|
|
|
|
2017-04-17 23:14:06 +00:00
|
|
|
var expected = `b:
|
2017-04-17 22:53:27 +00:00
|
|
|
- aa
|
|
|
|
- bb`
|
|
|
|
|
2017-04-17 23:14:06 +00:00
|
|
|
updated := writeMap(data, []string{"b", "1"}, "bb")
|
2017-09-22 19:58:12 +00:00
|
|
|
got, _ := yamlToString(updated)
|
|
|
|
assertResult(t, expected, got)
|
2017-04-17 22:53:27 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
`)
|
2017-04-11 23:16:54 +00:00
|
|
|
updated := writeMap(data, []string{"b"}, "4")
|
2015-10-05 04:59:59 +00:00
|
|
|
|
2017-04-11 23:16:54 +00:00
|
|
|
b := entryInSlice(updated, "b").Value
|
2015-10-05 04:59:59 +00:00
|
|
|
assertResult(t, "4", fmt.Sprintf("%v", b))
|
|
|
|
}
|
2017-04-14 02:39:55 +00:00
|
|
|
|
|
|
|
func TestWriteMap_no_paths(t *testing.T) {
|
|
|
|
var data = parseData(`
|
|
|
|
b: 5
|
|
|
|
`)
|
|
|
|
|
|
|
|
result := writeMap(data, []string{}, 4)
|
|
|
|
assertResult(t, fmt.Sprintf("%v", data), fmt.Sprintf("%v", result))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteArray_no_paths(t *testing.T) {
|
|
|
|
var data = make([]interface{}, 1)
|
|
|
|
data[0] = "mike"
|
|
|
|
result := writeArray(data, []string{}, 4)
|
|
|
|
assertResult(t, fmt.Sprintf("%v", data), fmt.Sprintf("%v", result))
|
|
|
|
}
|