yq/pkg/yqlib/lib_test.go

167 lines
3.1 KiB
Go
Raw Normal View History

package yqlib
import (
"fmt"
"testing"
2019-12-01 20:10:42 +00:00
2019-12-04 05:10:28 +00:00
"github.com/mikefarah/yq/v2/test"
logging "gopkg.in/op/go-logging.v1"
)
func TestLib(t *testing.T) {
var log = logging.MustGetLogger("yq")
subject := NewYqLib(log)
t.Run("TestReadPath", func(t *testing.T) {
var data = test.ParseData(`
---
b:
2: c
`)
2019-12-01 20:10:42 +00:00
got, _ := subject.ReadPath(data, "b.2")
test.AssertResult(t, `c`, got)
})
2019-12-01 20:10:42 +00:00
t.Run("TestReadPath_WithError", func(t *testing.T) {
var data = test.ParseData(`
---
b:
- c
`)
2019-12-01 20:10:42 +00:00
_, err := subject.ReadPath(data, "b.[a]")
if err == nil {
t.Fatal("Expected error due to invalid path")
}
})
2019-12-01 20:10:42 +00:00
t.Run("TestWritePath", func(t *testing.T) {
var data = test.ParseData(`
---
b:
2: c
`)
2019-12-01 20:10:42 +00:00
got := subject.WritePath(data, "b.3", "a")
test.AssertResult(t, `[{b [{2 c} {3 a}]}]`, fmt.Sprintf("%v", got))
})
2019-12-01 20:10:42 +00:00
t.Run("TestPrefixPath", func(t *testing.T) {
var data = test.ParseData(`
---
b:
2: c
`)
2019-12-01 20:10:42 +00:00
got := subject.PrefixPath(data, "a.d")
test.AssertResult(t, `[{a [{d [{b [{2 c}]}]}]}]`, fmt.Sprintf("%v", got))
})
2019-12-01 20:10:42 +00:00
t.Run("TestDeletePath", func(t *testing.T) {
var data = test.ParseData(`
---
b:
2: c
3: a
`)
2019-12-01 20:10:42 +00:00
got, _ := subject.DeletePath(data, "b.2")
test.AssertResult(t, `[{b [{3 a}]}]`, fmt.Sprintf("%v", got))
})
2019-12-01 20:10:42 +00:00
t.Run("TestDeletePath_WithError", func(t *testing.T) {
var data = test.ParseData(`
---
b:
- c
`)
2019-12-01 20:10:42 +00:00
_, err := subject.DeletePath(data, "b.[a]")
if err == nil {
t.Fatal("Expected error due to invalid path")
}
})
2019-12-01 20:10:42 +00:00
t.Run("TestMerge", func(t *testing.T) {
var dst = test.ParseData(`
---
a: b
c: d
`)
var src = test.ParseData(`
---
a: 1
b: 2
`)
2019-12-01 20:10:42 +00:00
var mergedData = make(map[interface{}]interface{})
mergedData["root"] = dst
var mapDataBucket = make(map[interface{}]interface{})
mapDataBucket["root"] = src
2019-12-01 20:10:42 +00:00
err := subject.Merge(&mergedData, mapDataBucket, false, false)
if err != nil {
t.Fatal("Unexpected error")
}
test.AssertResult(t, `[{a b} {c d}]`, fmt.Sprintf("%v", mergedData["root"]))
})
2019-12-01 20:10:42 +00:00
t.Run("TestMerge_WithOverwrite", func(t *testing.T) {
var dst = test.ParseData(`
---
a: b
c: d
`)
var src = test.ParseData(`
---
a: 1
b: 2
`)
2019-12-01 20:10:42 +00:00
var mergedData = make(map[interface{}]interface{})
mergedData["root"] = dst
var mapDataBucket = make(map[interface{}]interface{})
mapDataBucket["root"] = src
2019-12-01 20:10:42 +00:00
err := subject.Merge(&mergedData, mapDataBucket, true, false)
if err != nil {
t.Fatal("Unexpected error")
}
test.AssertResult(t, `[{a 1} {b 2}]`, fmt.Sprintf("%v", mergedData["root"]))
})
2019-12-01 20:10:42 +00:00
t.Run("TestMerge_WithAppend", func(t *testing.T) {
var dst = test.ParseData(`
---
a: b
c: d
`)
var src = test.ParseData(`
---
a: 1
b: 2
`)
2019-12-01 20:10:42 +00:00
var mergedData = make(map[interface{}]interface{})
mergedData["root"] = dst
var mapDataBucket = make(map[interface{}]interface{})
mapDataBucket["root"] = src
2019-12-01 20:10:42 +00:00
err := subject.Merge(&mergedData, mapDataBucket, false, true)
if err != nil {
t.Fatal("Unexpected error")
}
test.AssertResult(t, `[{a b} {c d} {a 1} {b 2}]`, fmt.Sprintf("%v", mergedData["root"]))
})
2019-12-01 20:10:42 +00:00
t.Run("TestMerge_WithError", func(t *testing.T) {
err := subject.Merge(nil, nil, false, false)
if err == nil {
t.Fatal("Expected error due to nil")
}
})
2019-12-01 20:10:42 +00:00
}