yq/pkg/yqlib/operator_entries_test.go

57 lines
1.3 KiB
Go
Raw Normal View History

2021-05-09 03:59:23 +00:00
package yqlib
import (
"testing"
)
var entriesOperatorScenarios = []expressionScenario{
{
description: "to_entries Map",
2021-05-09 05:12:50 +00:00
document: `{a: 1, b: 2}`,
expression: `to_entries`,
2021-05-09 03:59:23 +00:00
expected: []string{
"D0, P[], (!!seq)::- key: a\n value: 1\n- key: b\n value: 2\n",
},
},
{
description: "to_entries Array",
2021-05-09 05:12:50 +00:00
document: `[a, b]`,
expression: `to_entries`,
2021-05-09 03:59:23 +00:00
expected: []string{
"D0, P[], (!!seq)::- key: 0\n value: a\n- key: 1\n value: b\n",
},
},
2021-05-09 04:18:25 +00:00
{
description: "from_entries map",
2021-05-09 05:12:50 +00:00
document: `{a: 1, b: 2}`,
expression: `to_entries | from_entries`,
2021-05-09 04:18:25 +00:00
expected: []string{
"D0, P[], (!!map)::a: 1\nb: 2\n",
},
},
{
2021-05-09 05:12:50 +00:00
description: "from_entries with numeric key indexes",
2021-05-09 04:18:25 +00:00
subdescription: "from_entries always creates a map, even for numeric keys",
2021-05-09 05:12:50 +00:00
document: `[a,b]`,
expression: `to_entries | from_entries`,
2021-05-09 04:18:25 +00:00
expected: []string{
"D0, P[], (!!map)::0: a\n1: b\n",
},
},
2021-05-09 05:12:50 +00:00
{
description: "Use with_entries to update keys",
document: `{a: 1, b: 2}`,
expression: `with_entries(.key |= "KEY_" + .)`,
expected: []string{
"D0, P[], (!!map)::KEY_a: 1\nKEY_b: 2\n",
},
},
2021-05-09 03:59:23 +00:00
}
func TestEntriesOperatorScenarios(t *testing.T) {
for _, tt := range entriesOperatorScenarios {
testScenario(t, &tt)
}
documentScenarios(t, "Entries", entriesOperatorScenarios)
}