2021-02-15 03:27:00 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var reduceOperatorScenarios = []expressionScenario{
|
|
|
|
{
|
2021-02-15 05:38:53 +00:00
|
|
|
description: "Sum numbers",
|
|
|
|
document: `[10,2, 5, 3]`,
|
|
|
|
expression: `.[] as $item ireduce (0; . + $item)`,
|
2021-02-15 03:27:00 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!int)::20\n",
|
|
|
|
},
|
|
|
|
},
|
2021-02-15 05:38:53 +00:00
|
|
|
{
|
2021-02-15 07:23:50 +00:00
|
|
|
description: "Merge all yaml files together",
|
|
|
|
document: `a: cat`,
|
|
|
|
document2: `b: dog`,
|
|
|
|
expression: `. as $item ireduce ({}; . * $item )`,
|
2021-02-15 06:31:12 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::a: cat\nb: dog\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-02-15 07:23:50 +00:00
|
|
|
description: "Convert an array to an object",
|
|
|
|
document: `[{name: Cathy, has: apples},{name: Bob, has: bananas}]`,
|
|
|
|
expression: `.[] as $item ireduce ({}; .[$item | .name] = ($item | .has) )`,
|
2021-02-15 06:31:12 +00:00
|
|
|
expected: []string{
|
2021-02-15 07:23:50 +00:00
|
|
|
"D0, P[], (!!map)::Cathy: apples\nBob: bananas\n",
|
2021-02-15 06:31:12 +00:00
|
|
|
},
|
|
|
|
},
|
2021-02-15 03:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReduceOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range reduceOperatorScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
2021-02-15 05:38:53 +00:00
|
|
|
documentScenarios(t, "Reduce", reduceOperatorScenarios)
|
2021-02-15 03:27:00 +00:00
|
|
|
}
|