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`,
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `.a line_comment="single"`,
|
2020-11-06 00:23:26 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: cat # single\n",
|
|
|
|
},
|
|
|
|
},
|
2021-01-06 09:22:50 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "a: cat\nb: dog",
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `.a line_comment=.b`,
|
2021-01-06 09:22:50 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: cat # dog\nb: dog\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "a: cat\n---\na: dog",
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `.a line_comment |= documentIndex`,
|
2021-01-06 09:22:50 +00:00
|
|
|
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",
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `.. line_comment |= .`,
|
2021-01-06 09:22:50 +00:00
|
|
|
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`,
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `. head_comment="single"`,
|
2020-11-06 00:23:26 +00:00
|
|
|
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`,
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `. foot_comment=.a`,
|
2020-11-06 00:23:26 +00:00
|
|
|
expected: []string{
|
2022-05-25 00:54:03 +00:00
|
|
|
"D0, P[], (doc)::a: cat\n# cat\n",
|
2020-11-06 00:23:26 +00:00
|
|
|
},
|
|
|
|
},
|
2021-05-16 04:17:13 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `a: cat`,
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `. foot_comment=.b.d`,
|
2021-05-16 04:17:13 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: cat\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `a: cat`,
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `. foot_comment|=.b.d`,
|
2021-05-16 04:17:13 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: cat\n",
|
|
|
|
},
|
|
|
|
},
|
2020-11-06 00:23:26 +00:00
|
|
|
{
|
|
|
|
description: "Remove comment",
|
|
|
|
document: "a: cat # comment\nb: dog # leave this",
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `.a line_comment=""`,
|
2020-11-06 00:23:26 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: cat\nb: dog # leave this\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-04-29 03:18:57 +00:00
|
|
|
description: "Remove (strip) all comments",
|
2021-01-13 22:12:14 +00:00
|
|
|
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",
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `.a | line_comment`,
|
2020-11-06 00:45:18 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[a], (!!str)::meow\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-11-03 04:00:58 +00:00
|
|
|
description: "Get head comment",
|
|
|
|
dontFormatInputForDoc: true,
|
|
|
|
document: "# welcome!\n\na: cat # meow\n\n# have a great day",
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `. | head_comment`,
|
2020-11-06 00:45:18 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!str)::welcome!\n",
|
|
|
|
},
|
|
|
|
},
|
2021-11-12 04:02:28 +00:00
|
|
|
{
|
2021-11-23 23:59:19 +00:00
|
|
|
description: "Head comment with document split",
|
2021-11-12 04:02:28 +00:00
|
|
|
dontFormatInputForDoc: true,
|
|
|
|
document: "# welcome!\n---\n# bob\na: cat # meow\n\n# have a great day",
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `head_comment`,
|
2021-11-12 04:02:28 +00:00
|
|
|
expected: []string{
|
2021-11-13 23:18:02 +00:00
|
|
|
"D0, P[], (!!str)::welcome!\nbob\n",
|
2021-11-12 04:02:28 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-06 00:45:18 +00:00
|
|
|
{
|
2021-11-03 04:00:58 +00:00
|
|
|
description: "Get foot comment",
|
|
|
|
dontFormatInputForDoc: true,
|
2021-11-12 04:02:28 +00:00
|
|
|
document: "# welcome!\n\na: cat # meow\n\n# have a great day\n# no really",
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `. | foot_comment`,
|
2020-11-06 00:45:18 +00:00
|
|
|
expected: []string{
|
2021-11-12 04:02:28 +00:00
|
|
|
"D0, P[], (!!str)::have a great day\nno really\n",
|
2020-11-06 00:45:18 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-06 00:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommentOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range commentOperatorScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
2021-12-21 04:02:07 +00:00
|
|
|
documentOperatorScenarios(t, "comment-operators", commentOperatorScenarios)
|
2020-11-06 00:23:26 +00:00
|
|
|
}
|