2020-11-06 00:23:26 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var commentOperatorScenarios = []expressionScenario{
|
|
|
|
{
|
2020-11-06 00:45:18 +00:00
|
|
|
description: "Set line comment",
|
2020-11-06 00:23:26 +00:00
|
|
|
document: `a: cat`,
|
|
|
|
expression: `.a lineComment="single"`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: cat # single\n",
|
|
|
|
},
|
|
|
|
},
|
2021-01-06 09:22:50 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "a: cat\nb: dog",
|
|
|
|
expression: `.a lineComment=.b`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: cat # dog\nb: dog\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "a: cat\n---\na: dog",
|
|
|
|
expression: `.a lineComment |= documentIndex`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: cat # 0\n",
|
|
|
|
"D1, P[], (doc)::a: dog # 1\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Use update assign to perform relative updates",
|
|
|
|
document: "a: cat\nb: dog",
|
|
|
|
expression: `.. lineComment |= .`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::a: cat # cat\nb: dog # dog\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "a: cat\nb: dog",
|
|
|
|
expression: `.. comments |= .`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::a: cat # cat\n# cat\n\n# cat\nb: dog # dog\n# dog\n\n# dog\n",
|
|
|
|
},
|
|
|
|
},
|
2020-11-06 00:23:26 +00:00
|
|
|
{
|
2020-11-06 00:45:18 +00:00
|
|
|
description: "Set head comment",
|
2020-11-06 00:23:26 +00:00
|
|
|
document: `a: cat`,
|
|
|
|
expression: `. headComment="single"`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::# single\n\na: cat\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-11-06 00:45:18 +00:00
|
|
|
description: "Set foot comment, using an expression",
|
2020-11-06 00:23:26 +00:00
|
|
|
document: `a: cat`,
|
|
|
|
expression: `. footComment=.a`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: cat\n\n# cat\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Remove comment",
|
|
|
|
document: "a: cat # comment\nb: dog # leave this",
|
|
|
|
expression: `.a lineComment=""`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: cat\nb: dog # leave this\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-01-13 22:12:14 +00:00
|
|
|
description: "Remove all comments",
|
|
|
|
subdescription: "Note the use of `...` to ensure key nodes are included.",
|
|
|
|
document: "# hi\n\na: cat # comment\n\n# great\n\nb: # key comment",
|
|
|
|
expression: `... comments=""`,
|
2020-11-06 00:23:26 +00:00
|
|
|
expected: []string{
|
2021-01-13 22:12:14 +00:00
|
|
|
"D0, P[], (!!map)::a: cat\nb:\n",
|
2020-11-06 00:23:26 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-06 00:45:18 +00:00
|
|
|
{
|
|
|
|
description: "Get line comment",
|
|
|
|
document: "# welcome!\n\na: cat # meow\n\n# have a great day",
|
|
|
|
expression: `.a | lineComment`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[a], (!!str)::meow\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Get head comment",
|
|
|
|
document: "# welcome!\n\na: cat # meow\n\n# have a great day",
|
|
|
|
expression: `. | headComment`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!str)::welcome!\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Get foot comment",
|
|
|
|
document: "# welcome!\n\na: cat # meow\n\n# have a great day",
|
|
|
|
expression: `. | footComment`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!str)::have a great day\n",
|
|
|
|
},
|
|
|
|
},
|
2020-11-06 00:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommentOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range commentOperatorScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
2020-11-22 02:16:54 +00:00
|
|
|
documentScenarios(t, "Comment Operators", commentOperatorScenarios)
|
2020-11-06 00:23:26 +00:00
|
|
|
}
|