yq/pkg/yqlib/operator_strings_test.go

71 lines
1.8 KiB
Go
Raw Normal View History

2021-01-14 03:46:50 +00:00
package yqlib
import (
"testing"
)
var stringsOperatorScenarios = []expressionScenario{
{
description: "Join strings",
document: `[cat, meow, 1, null, true]`,
expression: `join("; ")`,
expected: []string{
"D0, P[], (!!str)::cat; meow; 1; ; true\n",
},
},
2021-04-15 00:09:41 +00:00
{
description: "Substitute / Replace string",
2021-04-16 06:07:40 +00:00
subdescription: "This uses golang regex, described [here](https://github.com/google/re2/wiki/Syntax)\nNote the use of `|=` to run in context of the current string value.",
2021-04-15 00:09:41 +00:00
document: `a: dogs are great`,
expression: `.a |= sub("dogs", "cats")`,
expected: []string{
"D0, P[], (doc)::a: cats are great\n",
},
},
{
description: "Substitute / Replace string with regex",
2021-04-16 06:07:40 +00:00
subdescription: "This uses golang regex, described [here](https://github.com/google/re2/wiki/Syntax)\nNote the use of `|=` to run in context of the current string value.",
2021-04-15 00:09:41 +00:00
document: "a: cat\nb: heat",
2021-04-16 06:07:40 +00:00
expression: `.[] |= sub("(a)", "${1}r")`,
2021-04-15 00:09:41 +00:00
expected: []string{
"D0, P[], (doc)::a: cart\nb: heart\n",
},
},
2021-01-14 04:05:50 +00:00
{
description: "Split strings",
document: `"cat; meow; 1; ; true"`,
expression: `split("; ")`,
expected: []string{
"D0, P[], (!!seq)::- cat\n- meow\n- \"1\"\n- \"\"\n- \"true\"\n",
},
},
{
description: "Split strings one match",
document: `"word"`,
expression: `split("; ")`,
expected: []string{
"D0, P[], (!!seq)::- word\n",
},
},
{
skipDoc: true,
document: `""`,
expression: `split("; ")`,
expected: []string{
"D0, P[], (!!seq)::[]\n", // dont actually want this, just not to error
},
},
{
skipDoc: true,
expression: `split("; ")`,
expected: []string{},
},
2021-01-14 03:46:50 +00:00
}
func TestStringsOperatorScenarios(t *testing.T) {
for _, tt := range stringsOperatorScenarios {
testScenario(t, &tt)
}
documentScenarios(t, "String Operators", stringsOperatorScenarios)
}