mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 13:48:06 +00:00
more tests
This commit is contained in:
parent
5ab584afac
commit
05520c2168
@ -47,6 +47,7 @@ var Pipe = &OperationType{Type: "PIPE", NumArgs: 2, Precedence: 45, Handler: Pip
|
|||||||
var Length = &OperationType{Type: "LENGTH", NumArgs: 0, Precedence: 50, Handler: LengthOperator}
|
var Length = &OperationType{Type: "LENGTH", NumArgs: 0, Precedence: 50, Handler: LengthOperator}
|
||||||
var Collect = &OperationType{Type: "COLLECT", NumArgs: 0, Precedence: 50, Handler: CollectOperator}
|
var Collect = &OperationType{Type: "COLLECT", NumArgs: 0, Precedence: 50, Handler: CollectOperator}
|
||||||
var GetStyle = &OperationType{Type: "GET_STYLE", NumArgs: 0, Precedence: 50, Handler: GetStyleOperator}
|
var GetStyle = &OperationType{Type: "GET_STYLE", NumArgs: 0, Precedence: 50, Handler: GetStyleOperator}
|
||||||
|
var GetComment = &OperationType{Type: "GET_COMMENT", NumArgs: 0, Precedence: 50, Handler: GetCommentsOperator}
|
||||||
|
|
||||||
var Explode = &OperationType{Type: "EXPLODE", NumArgs: 1, Precedence: 50, Handler: ExplodeOperator}
|
var Explode = &OperationType{Type: "EXPLODE", NumArgs: 1, Precedence: 50, Handler: ExplodeOperator}
|
||||||
|
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
package yqlib
|
package yqlib
|
||||||
|
|
||||||
import "container/list"
|
import (
|
||||||
|
"container/list"
|
||||||
|
"strings"
|
||||||
|
|
||||||
type AssignCommentPreferences struct {
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CommentOpPreferences struct {
|
||||||
LineComment bool
|
LineComment bool
|
||||||
HeadComment bool
|
HeadComment bool
|
||||||
FootComment bool
|
FootComment bool
|
||||||
@ -27,7 +32,7 @@ func AssignCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, path
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
preferences := pathNode.Operation.Preferences.(*AssignCommentPreferences)
|
preferences := pathNode.Operation.Preferences.(*CommentOpPreferences)
|
||||||
|
|
||||||
for el := lhs.Front(); el != nil; el = el.Next() {
|
for el := lhs.Front(); el != nil; el = el.Next() {
|
||||||
candidate := el.Value.(*CandidateNode)
|
candidate := el.Value.(*CandidateNode)
|
||||||
@ -45,3 +50,27 @@ func AssignCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, path
|
|||||||
}
|
}
|
||||||
return matchingNodes, nil
|
return matchingNodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
|
||||||
|
preferences := pathNode.Operation.Preferences.(*CommentOpPreferences)
|
||||||
|
log.Debugf("GetComments operator!")
|
||||||
|
var results = list.New()
|
||||||
|
|
||||||
|
for el := matchingNodes.Front(); el != nil; el = el.Next() {
|
||||||
|
candidate := el.Value.(*CandidateNode)
|
||||||
|
comment := ""
|
||||||
|
if preferences.LineComment {
|
||||||
|
comment = candidate.Node.LineComment
|
||||||
|
} else if preferences.HeadComment {
|
||||||
|
comment = candidate.Node.HeadComment
|
||||||
|
} else if preferences.FootComment {
|
||||||
|
comment = candidate.Node.FootComment
|
||||||
|
}
|
||||||
|
comment = strings.Replace(comment, "# ", "", 1)
|
||||||
|
|
||||||
|
node := &yaml.Node{Kind: yaml.ScalarNode, Value: comment, Tag: "!!str"}
|
||||||
|
lengthCand := &CandidateNode{Node: node, Document: candidate.Document, Path: candidate.Path}
|
||||||
|
results.PushBack(lengthCand)
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
|
|
||||||
var commentOperatorScenarios = []expressionScenario{
|
var commentOperatorScenarios = []expressionScenario{
|
||||||
{
|
{
|
||||||
description: "Add line comment",
|
description: "Set line comment",
|
||||||
document: `a: cat`,
|
document: `a: cat`,
|
||||||
expression: `.a lineComment="single"`,
|
expression: `.a lineComment="single"`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -14,7 +14,7 @@ var commentOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Add head comment",
|
description: "Set head comment",
|
||||||
document: `a: cat`,
|
document: `a: cat`,
|
||||||
expression: `. headComment="single"`,
|
expression: `. headComment="single"`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -22,7 +22,7 @@ var commentOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Add foot comment, using an expression",
|
description: "Set foot comment, using an expression",
|
||||||
document: `a: cat`,
|
document: `a: cat`,
|
||||||
expression: `. footComment=.a`,
|
expression: `. footComment=.a`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -45,6 +45,30 @@ var commentOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[], (!!map)::a: cat\n",
|
"D0, P[], (!!map)::a: cat\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCommentOperatorScenarios(t *testing.T) {
|
func TestCommentOperatorScenarios(t *testing.T) {
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
var multiplyOperatorScenarios = []expressionScenario{
|
var multiplyOperatorScenarios = []expressionScenario{
|
||||||
{
|
{
|
||||||
|
skipDoc: true,
|
||||||
document: `{a: {also: [1]}, b: {also: me}}`,
|
document: `{a: {also: [1]}, b: {also: me}}`,
|
||||||
expression: `. * {"a" : .b}`,
|
expression: `. * {"a" : .b}`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -13,6 +14,7 @@ var multiplyOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
skipDoc: true,
|
||||||
document: `{a: {also: me}, b: {also: [1]}}`,
|
document: `{a: {also: me}, b: {also: [1]}}`,
|
||||||
expression: `. * {"a":.b}`,
|
expression: `. * {"a":.b}`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -20,6 +22,7 @@ var multiplyOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
description: "Merge objects together",
|
||||||
document: `{a: {also: me}, b: {also: {g: wizz}}}`,
|
document: `{a: {also: me}, b: {also: {g: wizz}}}`,
|
||||||
expression: `. * {"a":.b}`,
|
expression: `. * {"a":.b}`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -27,6 +30,7 @@ var multiplyOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
skipDoc: true,
|
||||||
document: `{a: {also: {g: wizz}}, b: {also: me}}`,
|
document: `{a: {also: {g: wizz}}, b: {also: me}}`,
|
||||||
expression: `. * {"a":.b}`,
|
expression: `. * {"a":.b}`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -34,6 +38,7 @@ var multiplyOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
skipDoc: true,
|
||||||
document: `{a: {also: {g: wizz}}, b: {also: [1]}}`,
|
document: `{a: {also: {g: wizz}}, b: {also: [1]}}`,
|
||||||
expression: `. * {"a":.b}`,
|
expression: `. * {"a":.b}`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -41,6 +46,7 @@ var multiplyOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
skipDoc: true,
|
||||||
document: `{a: {also: [1]}, b: {also: {g: wizz}}}`,
|
document: `{a: {also: [1]}, b: {also: {g: wizz}}}`,
|
||||||
expression: `. * {"a":.b}`,
|
expression: `. * {"a":.b}`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -48,6 +54,7 @@ var multiplyOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
skipDoc: true,
|
||||||
document: `{a: {things: great}, b: {also: me}}`,
|
document: `{a: {things: great}, b: {also: me}}`,
|
||||||
expression: `. * {"a":.b}`,
|
expression: `. * {"a":.b}`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -55,6 +62,7 @@ var multiplyOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
description: "Merge keeps style of LHS",
|
||||||
document: `a: {things: great}
|
document: `a: {things: great}
|
||||||
b:
|
b:
|
||||||
also: "me"
|
also: "me"
|
||||||
@ -68,6 +76,7 @@ b:
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
description: "Merge arrays",
|
||||||
document: `{a: [1,2,3], b: [3,4,5]}`,
|
document: `{a: [1,2,3], b: [3,4,5]}`,
|
||||||
expression: `. * {"a":.b}`,
|
expression: `. * {"a":.b}`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -75,13 +84,15 @@ b:
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
document: `{a: cat}`,
|
description: "Merge to prefix an element",
|
||||||
|
document: `{a: cat, b: dog}`,
|
||||||
expression: `. * {"a": {"c": .a}}`,
|
expression: `. * {"a": {"c": .a}}`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!map)::{a: {c: cat}}\n",
|
"D0, P[], (!!map)::{a: {c: cat}, b: dog}\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
description: "Merge with simple aliases",
|
||||||
document: `{a: &cat {c: frog}, b: {f: *cat}, c: {g: thongs}}`,
|
document: `{a: &cat {c: frog}, b: {f: *cat}, c: {g: thongs}}`,
|
||||||
expression: `.c * .b`,
|
expression: `.c * .b`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -89,6 +100,7 @@ b:
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
description: "Merge does not copy anchor names",
|
||||||
document: `{a: {c: &cat frog}, b: {f: *cat}, c: {g: thongs}}`,
|
document: `{a: {c: &cat frog}, b: {f: *cat}, c: {g: thongs}}`,
|
||||||
expression: `.c * .a`,
|
expression: `.c * .a`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -96,6 +108,7 @@ b:
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
description: "Merge with merge anchors",
|
||||||
document: mergeDocSample,
|
document: mergeDocSample,
|
||||||
expression: `.foobar * .foobarList`,
|
expression: `.foobar * .foobarList`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -108,4 +121,5 @@ func TestMultiplyOperatorScenarios(t *testing.T) {
|
|||||||
for _, tt := range multiplyOperatorScenarios {
|
for _, tt := range multiplyOperatorScenarios {
|
||||||
testScenario(t, &tt)
|
testScenario(t, &tt)
|
||||||
}
|
}
|
||||||
|
documentScenarios(t, "Mulitply Operator", multiplyOperatorScenarios)
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ type expressionScenario struct {
|
|||||||
document string
|
document string
|
||||||
expression string
|
expression string
|
||||||
expected []string
|
expected []string
|
||||||
|
skipDoc bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func testScenario(t *testing.T, s *expressionScenario) {
|
func testScenario(t *testing.T, s *expressionScenario) {
|
||||||
@ -48,6 +49,8 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
|
|||||||
printer := NewPrinter(false, true, false, 2, true)
|
printer := NewPrinter(false, true, false, 2, true)
|
||||||
|
|
||||||
for index, s := range scenarios {
|
for index, s := range scenarios {
|
||||||
|
if !s.skipDoc {
|
||||||
|
|
||||||
if s.description != "" {
|
if s.description != "" {
|
||||||
w.WriteString(fmt.Sprintf("### %v\n", s.description))
|
w.WriteString(fmt.Sprintf("### %v\n", s.description))
|
||||||
} else {
|
} else {
|
||||||
@ -79,6 +82,7 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
w.Flush()
|
w.Flush()
|
||||||
|
@ -194,10 +194,16 @@ func initLexer() (*lex.Lexer, error) {
|
|||||||
lexer.Add([]byte(`style\s*=`), opToken(AssignStyle))
|
lexer.Add([]byte(`style\s*=`), opToken(AssignStyle))
|
||||||
lexer.Add([]byte(`style`), opToken(GetStyle))
|
lexer.Add([]byte(`style`), opToken(GetStyle))
|
||||||
|
|
||||||
lexer.Add([]byte(`lineComment\s*=`), opTokenWithPrefs(AssignComment, &AssignCommentPreferences{LineComment: true}))
|
lexer.Add([]byte(`lineComment\s*=`), opTokenWithPrefs(AssignComment, &CommentOpPreferences{LineComment: true}))
|
||||||
lexer.Add([]byte(`headComment\s*=`), opTokenWithPrefs(AssignComment, &AssignCommentPreferences{HeadComment: true}))
|
lexer.Add([]byte(`lineComment`), opTokenWithPrefs(GetComment, &CommentOpPreferences{LineComment: true}))
|
||||||
lexer.Add([]byte(`footComment\s*=`), opTokenWithPrefs(AssignComment, &AssignCommentPreferences{FootComment: true}))
|
|
||||||
lexer.Add([]byte(`comments\s*=`), opTokenWithPrefs(AssignComment, &AssignCommentPreferences{LineComment: true, HeadComment: true, FootComment: true}))
|
lexer.Add([]byte(`headComment\s*=`), opTokenWithPrefs(AssignComment, &CommentOpPreferences{HeadComment: true}))
|
||||||
|
lexer.Add([]byte(`headComment`), opTokenWithPrefs(GetComment, &CommentOpPreferences{HeadComment: true}))
|
||||||
|
|
||||||
|
lexer.Add([]byte(`footComment\s*=`), opTokenWithPrefs(AssignComment, &CommentOpPreferences{FootComment: true}))
|
||||||
|
lexer.Add([]byte(`footComment`), opTokenWithPrefs(GetComment, &CommentOpPreferences{FootComment: true}))
|
||||||
|
|
||||||
|
lexer.Add([]byte(`comments\s*=`), opTokenWithPrefs(AssignComment, &CommentOpPreferences{LineComment: true, HeadComment: true, FootComment: true}))
|
||||||
// lexer.Add([]byte(`style`), opToken(GetStyle))
|
// lexer.Add([]byte(`style`), opToken(GetStyle))
|
||||||
|
|
||||||
// lexer.Add([]byte(`and`), opToken())
|
// lexer.Add([]byte(`and`), opToken())
|
||||||
|
Loading…
Reference in New Issue
Block a user