Skip to content

Commit

Permalink
issue-281: add tests and fix panic for *
Browse files Browse the repository at this point in the history
Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com>
  • Loading branch information
ivankatliarchuk committed Jan 12, 2025
1 parent df5e06f commit cd2020c
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
bin/
cover.out
.DS_Store
.idea/
3 changes: 3 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,9 @@ func (n *SequenceNode) blockStyleString() string {
}

for idx, value := range n.Values {
if value == nil {
continue
}
valueStr := value.String()
newLinePrefix := ""
if strings.HasPrefix(valueStr, "\n") {
Expand Down
167 changes: 167 additions & 0 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,36 @@ store:
path: builder().Root().Child("store").Child("book").IndexAll().Child("author").Build(),
expected: []interface{}{"john", "ken"},
},
{
name: "$.store.book[*]",
path: builder().Root().Child("store").Child("book").IndexAll().Build(),
expected: []interface{}{
map[string]interface{}{
"author": "john",
"price": uint64(10),
},
map[string]interface{}{
"author": "ken",
"price": uint64(12),
},
},
},
{
name: "$..book[*]",
path: builder().Root().Recursive("book").IndexAll().Build(),
expected: []interface{}{
[]interface{}{
map[string]interface{}{
"author": "john",
"price": uint64(10),
},
map[string]interface{}{
"author": "ken",
"price": uint64(12),
},
},
},
},
{
name: "$.store.book[0]",
path: builder().Root().Child("store").Child("book").Index(0).Build(),
Expand Down Expand Up @@ -305,6 +335,143 @@ func TestPath_Invalid(t *testing.T) {
}
}

func TestPath_ReadNode(t *testing.T) {
tests := []struct {
name string
path string
src string
expected interface{}
}{
{
name: "nested array sequence",
path: `$.a.b[0].c`,
src: `
a:
b:
- c: 123
e: |
Line1
Line2
`,
expected: uint64(123),
},
{
name: "nested array sequence issue#281",
path: `$..a.c`,
src: `
s:
- a:
b: u1
c: get1
d: i1
- w:
c: bad
e:
- a:
b: u2
c: get2
d: i2
`,
// The expected values are
// - get1
// - get2
expected: []interface{}{
map[string]interface{}{
"b": "u1",
"c": "get1",
"d": "i1",
},
map[string]interface{}{
"b": "u2",
"c": "get2",
"d": "i2",
},
},
},
{
name: "nested array sequence issue#281",
path: `$..c`,
src: `
s:
- a:
b: u1
c: get1
d: i1
- w:
c: bad
e:
- a:
b: u2
c: get2
d: i2
`,
expected: []interface{}{"get1", "bad", "get2"},
},
{
name: "nested array sequence issue#281",
path: `$.s[0].a.c`,
src: `
s:
- a:
b: u1
c: get1
d: i1
- w:
c: bad
e:
- a:
b: u2
c: get2
d: i2
`,
expected: "get1",
},
{
name: "nested array sequence issue#281",
path: "$.s[*].a.c",
src: `
s:
- a:
b: u1
c: get1
d: i1
- w:
c: bad
e:
- a:
b: u2
c: get2
d: i2
`,
expected: []interface{}{"get1"},
},
}
for _, test := range tests {
path, err := yaml.PathString(test.path)
if err != nil {
t.Fatal(err)
}
t.Run(fmt.Sprintf("path.ReadNode %s path %s", test.name, test.path), func(t *testing.T) {
file, err := parser.ParseBytes([]byte(test.src), 0)
if err != nil {
t.Fatal(err)
}
n, err := path.ReadNode(file)
if err != nil {
t.Fatal("expected error", err)
}
var v interface{}
err = yaml.Unmarshal([]byte(n.String()), &v)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(test.expected, v) {
t.Fatalf("expected %v(%T) but got %v(%T)", test.expected, test.expected, v, v)
}
})
}
}

func TestPath_Merge(t *testing.T) {
tests := []struct {
path string
Expand Down

0 comments on commit cd2020c

Please sign in to comment.