2019-11-23 03:52:29 +00:00
|
|
|
package yqlib
|
2015-10-03 05:10:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-11-23 03:52:29 +00:00
|
|
|
"github.com/mikefarah/yq/test"
|
2015-10-03 05:10:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var parsePathsTests = []struct {
|
|
|
|
path string
|
|
|
|
expectedPaths []string
|
|
|
|
}{
|
|
|
|
{"a.b", []string{"a", "b"}},
|
|
|
|
{"a.b[0]", []string{"a", "b", "0"}},
|
2017-09-24 00:21:16 +00:00
|
|
|
{"a.b.d[+]", []string{"a", "b", "d", "+"}},
|
2015-10-03 05:10:29 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 23:40:33 +00:00
|
|
|
func TestParsePath(t *testing.T) {
|
2015-10-03 05:10:29 +00:00
|
|
|
for _, tt := range parsePathsTests {
|
2019-11-23 03:52:29 +00:00
|
|
|
test.AssertResultComplex(t, tt.expectedPaths, ParsePath(tt.path))
|
2015-10-03 05:10:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var nextYamlPathTests = []struct {
|
|
|
|
path string
|
|
|
|
expectedElement string
|
|
|
|
expectedRemaining string
|
|
|
|
}{
|
|
|
|
{"a.b", "a", "b"},
|
|
|
|
{"a", "a", ""},
|
|
|
|
{"a.b.c", "a", "b.c"},
|
|
|
|
{"\"a.b\".c", "a.b", "c"},
|
|
|
|
{"a.\"b.c\".d", "a", "\"b.c\".d"},
|
|
|
|
{"[1].a.d", "1", "a.d"},
|
|
|
|
{"a[0].c", "a", "[0].c"},
|
|
|
|
{"[0]", "0", ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNextYamlPath(t *testing.T) {
|
|
|
|
for _, tt := range nextYamlPathTests {
|
|
|
|
var element, remaining = nextYamlPath(tt.path)
|
2019-11-23 03:52:29 +00:00
|
|
|
test.AssertResultWithContext(t, tt.expectedElement, element, tt)
|
|
|
|
test.AssertResultWithContext(t, tt.expectedRemaining, remaining, tt)
|
2015-10-03 05:10:29 +00:00
|
|
|
}
|
|
|
|
}
|