2020-11-22 01:19:57 +00:00
package yqlib
import (
"testing"
)
2022-11-09 23:17:08 +00:00
var documentToPrune = `
parentA : bob
parentB :
child1 : i am child1
child2 : i am child2
parentC :
child1 : me child1
child2 : me child2
`
2020-11-22 01:19:57 +00:00
var pathOperatorScenarios = [ ] expressionScenario {
{
description : "Map path" ,
document : ` { a: { b: cat}} ` ,
expression : ` .a.b | path ` ,
expected : [ ] string {
"D0, P[a b], (!!seq)::- a\n- b\n" ,
} ,
} ,
2024-06-16 00:22:42 +00:00
{
description : "Map path" ,
document : ` { a: { b: cat}} ` ,
skipDoc : true ,
expression : ` .a.b | path[] ` ,
expected : [ ] string {
"D0, P[a b 0], (!!str)::a\n" ,
"D0, P[a b 1], (!!str)::b\n" ,
} ,
} ,
2020-12-25 01:46:08 +00:00
{
skipDoc : true ,
document : ` a :
b :
c :
- 0
- 1
- 2
- 3 ` ,
expression : ` .a.b.c.[] ` ,
expected : [ ] string {
"D0, P[a b c 0], (!!int)::0\n" ,
"D0, P[a b c 1], (!!int)::1\n" ,
"D0, P[a b c 2], (!!int)::2\n" ,
"D0, P[a b c 3], (!!int)::3\n" ,
} ,
} ,
2020-11-26 00:20:53 +00:00
{
description : "Get map key" ,
document : ` { a: { b: cat}} ` ,
expression : ` .a.b | path | .[-1] ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[a b 1], (!!str)::b\n" ,
2020-11-26 00:20:53 +00:00
} ,
} ,
2020-11-22 01:19:57 +00:00
{
description : "Array path" ,
document : ` { a: [cat, dog]} ` ,
expression : ` .a.[] | select(. == "dog") | path ` ,
expected : [ ] string {
"D0, P[a 1], (!!seq)::- a\n- 1\n" ,
} ,
} ,
2020-11-26 00:20:53 +00:00
{
description : "Get array index" ,
document : ` { a: [cat, dog]} ` ,
expression : ` .a.[] | select(. == "dog") | path | .[-1] ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[a 1 1], (!!int)::1\n" ,
2020-11-26 00:20:53 +00:00
} ,
} ,
2020-11-22 01:19:57 +00:00
{
description : "Print path and value" ,
document : ` { a: [cat, dog, frog]} ` ,
2021-11-30 02:19:30 +00:00
expression : ` .a[] | select(. == "*og") | [ { "path":path, "value":.}] ` ,
expected : [ ] string {
"D0, P[a 1], (!!seq)::- path:\n - a\n - 1\n value: dog\n" ,
"D0, P[a 2], (!!seq)::- path:\n - a\n - 2\n value: frog\n" ,
} ,
2020-11-22 01:19:57 +00:00
} ,
2022-10-05 03:12:08 +00:00
{
description : "Set path" ,
document : ` { a: { b: cat}} ` ,
expression : ` setpath(["a", "b"]; "things") ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::{a: {b: things}}\n" ,
2022-10-05 03:12:08 +00:00
} ,
} ,
{
description : "Set on empty document" ,
expression : ` setpath(["a", "b"]; "things") ` ,
expected : [ ] string {
"D0, P[], ()::a:\n b: things\n" ,
} ,
} ,
2022-11-09 23:17:08 +00:00
{
2022-11-09 23:28:29 +00:00
description : "Set path to prune deep paths" ,
2023-03-16 02:39:36 +00:00
subdescription : "Like pick but recursive. This uses `ireduce` to deeply set the selected paths into an empty object." ,
2022-11-09 23:17:08 +00:00
document : documentToPrune ,
2022-11-09 23:22:08 +00:00
expression : "(.parentB.child2, .parentC.child1) as $i\n ireduce({}; setpath($i | path; $i))" ,
2022-11-09 23:17:08 +00:00
expected : [ ] string {
"D0, P[], (!!map)::parentB:\n child2: i am child2\nparentC:\n child1: me child1\n" ,
} ,
} ,
2022-10-05 03:12:08 +00:00
{
description : "Set array path" ,
document : ` a: [cat, frog] ` ,
expression : ` setpath(["a", 0]; "things") ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: [things, frog]\n" ,
2022-10-05 03:12:08 +00:00
} ,
} ,
{
description : "Set array path empty" ,
expression : ` setpath(["a", 0]; "things") ` ,
expected : [ ] string {
"D0, P[], ()::a:\n - things\n" ,
} ,
} ,
2022-10-05 09:09:53 +00:00
{
description : "Delete path" ,
subdescription : "Notice delpaths takes an _array_ of paths." ,
document : ` { a: { b: cat, c: dog, d: frog}} ` ,
expression : ` delpaths([["a", "c"], ["a", "d"]]) ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::{a: {b: cat}}\n" ,
2022-10-05 09:09:53 +00:00
} ,
} ,
{
description : "Delete array path" ,
document : ` a: [cat, frog] ` ,
expression : ` delpaths([["a", 0]]) ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: [frog]\n" ,
2022-10-05 09:09:53 +00:00
} ,
} ,
2024-06-16 00:48:01 +00:00
{
description : "Delete splat" ,
skipDoc : true ,
document : ` a: [cat, frog] ` ,
expression : ` delpaths([["a", 0]])[] ` ,
expected : [ ] string {
"D0, P[a], (!!seq)::[frog]\n" ,
} ,
} ,
2022-10-05 09:09:53 +00:00
{
description : "Delete - wrong parameter" ,
subdescription : "delpaths does not work with a single path array" ,
document : ` a: [cat, frog] ` ,
expression : ` delpaths(["a", 0]) ` ,
expectedError : "DELPATHS: expected entry [0] to be a sequence, but its a !!str. Note that delpaths takes an array of path arrays, e.g. [[\"a\", \"b\"]]" ,
} ,
2020-11-22 01:19:57 +00:00
}
func TestPathOperatorsScenarios ( t * testing . T ) {
for _ , tt := range pathOperatorScenarios {
testScenario ( t , & tt )
}
2021-12-21 04:02:07 +00:00
documentOperatorScenarios ( t , "path" , pathOperatorScenarios )
2020-11-22 01:19:57 +00:00
}