mirror of
				https://github.com/taigrr/yq
				synced 2025-01-18 04:53:17 -08: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 Collect = &OperationType{Type: "COLLECT", NumArgs: 0, Precedence: 50, Handler: CollectOperator} | ||||
| 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} | ||||
| 
 | ||||
|  | ||||
| @ -1,8 +1,13 @@ | ||||
| package yqlib | ||||
| 
 | ||||
| import "container/list" | ||||
| import ( | ||||
| 	"container/list" | ||||
| 	"strings" | ||||
| 
 | ||||
| type AssignCommentPreferences struct { | ||||
| 	"gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| type CommentOpPreferences struct { | ||||
| 	LineComment bool | ||||
| 	HeadComment bool | ||||
| 	FootComment bool | ||||
| @ -27,7 +32,7 @@ func AssignCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, path | ||||
| 		return nil, err | ||||
| 	} | ||||
| 
 | ||||
| 	preferences := pathNode.Operation.Preferences.(*AssignCommentPreferences) | ||||
| 	preferences := pathNode.Operation.Preferences.(*CommentOpPreferences) | ||||
| 
 | ||||
| 	for el := lhs.Front(); el != nil; el = el.Next() { | ||||
| 		candidate := el.Value.(*CandidateNode) | ||||
| @ -45,3 +50,27 @@ func AssignCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, path | ||||
| 	} | ||||
| 	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{ | ||||
| 	{ | ||||
| 		description: "Add line comment", | ||||
| 		description: "Set line comment", | ||||
| 		document:    `a: cat`, | ||||
| 		expression:  `.a lineComment="single"`, | ||||
| 		expected: []string{ | ||||
| @ -14,7 +14,7 @@ var commentOperatorScenarios = []expressionScenario{ | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		description: "Add head comment", | ||||
| 		description: "Set head comment", | ||||
| 		document:    `a: cat`, | ||||
| 		expression:  `. headComment="single"`, | ||||
| 		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`, | ||||
| 		expression:  `. footComment=.a`, | ||||
| 		expected: []string{ | ||||
| @ -45,6 +45,30 @@ var commentOperatorScenarios = []expressionScenario{ | ||||
| 			"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) { | ||||
|  | ||||
| @ -6,6 +6,7 @@ import ( | ||||
| 
 | ||||
| var multiplyOperatorScenarios = []expressionScenario{ | ||||
| 	{ | ||||
| 		skipDoc:    true, | ||||
| 		document:   `{a: {also: [1]}, b: {also: me}}`, | ||||
| 		expression: `. * {"a" : .b}`, | ||||
| 		expected: []string{ | ||||
| @ -13,6 +14,7 @@ var multiplyOperatorScenarios = []expressionScenario{ | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		skipDoc:    true, | ||||
| 		document:   `{a: {also: me}, b: {also: [1]}}`, | ||||
| 		expression: `. * {"a":.b}`, | ||||
| 		expected: []string{ | ||||
| @ -20,6 +22,7 @@ var multiplyOperatorScenarios = []expressionScenario{ | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		description: "Merge objects together", | ||||
| 		document:    `{a: {also: me}, b: {also: {g: wizz}}}`, | ||||
| 		expression:  `. * {"a":.b}`, | ||||
| 		expected: []string{ | ||||
| @ -27,6 +30,7 @@ var multiplyOperatorScenarios = []expressionScenario{ | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		skipDoc:    true, | ||||
| 		document:   `{a: {also: {g: wizz}}, b: {also: me}}`, | ||||
| 		expression: `. * {"a":.b}`, | ||||
| 		expected: []string{ | ||||
| @ -34,6 +38,7 @@ var multiplyOperatorScenarios = []expressionScenario{ | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		skipDoc:    true, | ||||
| 		document:   `{a: {also: {g: wizz}}, b: {also: [1]}}`, | ||||
| 		expression: `. * {"a":.b}`, | ||||
| 		expected: []string{ | ||||
| @ -41,6 +46,7 @@ var multiplyOperatorScenarios = []expressionScenario{ | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		skipDoc:    true, | ||||
| 		document:   `{a: {also: [1]}, b: {also: {g: wizz}}}`, | ||||
| 		expression: `. * {"a":.b}`, | ||||
| 		expected: []string{ | ||||
| @ -48,6 +54,7 @@ var multiplyOperatorScenarios = []expressionScenario{ | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		skipDoc:    true, | ||||
| 		document:   `{a: {things: great}, b: {also: me}}`, | ||||
| 		expression: `. * {"a":.b}`, | ||||
| 		expected: []string{ | ||||
| @ -55,6 +62,7 @@ var multiplyOperatorScenarios = []expressionScenario{ | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		description: "Merge keeps style of LHS", | ||||
| 		document: `a: {things: great} | ||||
| b: | ||||
|   also: "me" | ||||
| @ -68,6 +76,7 @@ b: | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		description: "Merge arrays", | ||||
| 		document:    `{a: [1,2,3], b: [3,4,5]}`, | ||||
| 		expression:  `. * {"a":.b}`, | ||||
| 		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}}`, | ||||
| 		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}}`, | ||||
| 		expression:  `.c * .b`, | ||||
| 		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}}`, | ||||
| 		expression:  `.c * .a`, | ||||
| 		expected: []string{ | ||||
| @ -96,6 +108,7 @@ b: | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		description: "Merge with merge anchors", | ||||
| 		document:    mergeDocSample, | ||||
| 		expression:  `.foobar * .foobarList`, | ||||
| 		expected: []string{ | ||||
| @ -108,4 +121,5 @@ func TestMultiplyOperatorScenarios(t *testing.T) { | ||||
| 	for _, tt := range multiplyOperatorScenarios { | ||||
| 		testScenario(t, &tt) | ||||
| 	} | ||||
| 	documentScenarios(t, "Mulitply Operator", multiplyOperatorScenarios) | ||||
| } | ||||
|  | ||||
| @ -15,6 +15,7 @@ type expressionScenario struct { | ||||
| 	document    string | ||||
| 	expression  string | ||||
| 	expected    []string | ||||
| 	skipDoc     bool | ||||
| } | ||||
| 
 | ||||
| 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) | ||||
| 
 | ||||
| 	for index, s := range scenarios { | ||||
| 		if !s.skipDoc { | ||||
| 
 | ||||
| 			if s.description != "" { | ||||
| 				w.WriteString(fmt.Sprintf("### %v\n", s.description)) | ||||
| 			} else { | ||||
| @ -79,6 +82,7 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari | ||||
| 			if err != nil { | ||||
| 				panic(err) | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 	} | ||||
| 	w.Flush() | ||||
|  | ||||
| @ -194,10 +194,16 @@ func initLexer() (*lex.Lexer, error) { | ||||
| 	lexer.Add([]byte(`style\s*=`), opToken(AssignStyle)) | ||||
| 	lexer.Add([]byte(`style`), opToken(GetStyle)) | ||||
| 
 | ||||
| 	lexer.Add([]byte(`lineComment\s*=`), opTokenWithPrefs(AssignComment, &AssignCommentPreferences{LineComment: true})) | ||||
| 	lexer.Add([]byte(`headComment\s*=`), opTokenWithPrefs(AssignComment, &AssignCommentPreferences{HeadComment: 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(`lineComment\s*=`), opTokenWithPrefs(AssignComment, &CommentOpPreferences{LineComment: true})) | ||||
| 	lexer.Add([]byte(`lineComment`), opTokenWithPrefs(GetComment, &CommentOpPreferences{LineComment: 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(`and`), opToken()) | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user