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 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,13 +22,15 @@ var multiplyOperatorScenarios = []expressionScenario{ | |||||||
| 		}, | 		}, | ||||||
| 	}, | 	}, | ||||||
| 	{ | 	{ | ||||||
| 		document:   `{a: {also: me}, b: {also: {g: wizz}}}`, | 		description: "Merge objects together", | ||||||
| 		expression: `. * {"a":.b}`, | 		document:    `{a: {also: me}, b: {also: {g: wizz}}}`, | ||||||
|  | 		expression:  `. * {"a":.b}`, | ||||||
| 		expected: []string{ | 		expected: []string{ | ||||||
| 			"D0, P[], (!!map)::{a: {also: {g: wizz}}, b: {also: {g: wizz}}}\n", | 			"D0, P[], (!!map)::{a: {also: {g: wizz}}, b: {also: {g: wizz}}}\n", | ||||||
| 		}, | 		}, | ||||||
| 	}, | 	}, | ||||||
| 	{ | 	{ | ||||||
|  | 		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,36 +76,41 @@ b: | |||||||
| 		}, | 		}, | ||||||
| 	}, | 	}, | ||||||
| 	{ | 	{ | ||||||
| 		document:   `{a: [1,2,3], b: [3,4,5]}`, | 		description: "Merge arrays", | ||||||
| 		expression: `. * {"a":.b}`, | 		document:    `{a: [1,2,3], b: [3,4,5]}`, | ||||||
|  | 		expression:  `. * {"a":.b}`, | ||||||
| 		expected: []string{ | 		expected: []string{ | ||||||
| 			"D0, P[], (!!map)::{a: [3, 4, 5], b: [3, 4, 5]}\n", | 			"D0, P[], (!!map)::{a: [3, 4, 5], b: [3, 4, 5]}\n", | ||||||
| 		}, | 		}, | ||||||
| 	}, | 	}, | ||||||
| 	{ | 	{ | ||||||
| 		document:   `{a: cat}`, | 		description: "Merge to prefix an element", | ||||||
| 		expression: `. * {"a": {"c": .a}}`, | 		document:    `{a: cat, b: dog}`, | ||||||
|  | 		expression:  `. * {"a": {"c": .a}}`, | ||||||
| 		expected: []string{ | 		expected: []string{ | ||||||
| 			"D0, P[], (!!map)::{a: {c: cat}}\n", | 			"D0, P[], (!!map)::{a: {c: cat}, b: dog}\n", | ||||||
| 		}, | 		}, | ||||||
| 	}, | 	}, | ||||||
| 	{ | 	{ | ||||||
| 		document:   `{a: &cat {c: frog}, b: {f: *cat}, c: {g: thongs}}`, | 		description: "Merge with simple aliases", | ||||||
| 		expression: `.c * .b`, | 		document:    `{a: &cat {c: frog}, b: {f: *cat}, c: {g: thongs}}`, | ||||||
|  | 		expression:  `.c * .b`, | ||||||
| 		expected: []string{ | 		expected: []string{ | ||||||
| 			"D0, P[c], (!!map)::{g: thongs, f: *cat}\n", | 			"D0, P[c], (!!map)::{g: thongs, f: *cat}\n", | ||||||
| 		}, | 		}, | ||||||
| 	}, | 	}, | ||||||
| 	{ | 	{ | ||||||
| 		document:   `{a: {c: &cat frog}, b: {f: *cat}, c: {g: thongs}}`, | 		description: "Merge does not copy anchor names", | ||||||
| 		expression: `.c * .a`, | 		document:    `{a: {c: &cat frog}, b: {f: *cat}, c: {g: thongs}}`, | ||||||
|  | 		expression:  `.c * .a`, | ||||||
| 		expected: []string{ | 		expected: []string{ | ||||||
| 			"D0, P[c], (!!map)::{g: thongs, c: frog}\n", | 			"D0, P[c], (!!map)::{g: thongs, c: frog}\n", | ||||||
| 		}, | 		}, | ||||||
| 	}, | 	}, | ||||||
| 	{ | 	{ | ||||||
| 		document:   mergeDocSample, | 		description: "Merge with merge anchors", | ||||||
| 		expression: `.foobar * .foobarList`, | 		document:    mergeDocSample, | ||||||
|  | 		expression:  `.foobar * .foobarList`, | ||||||
| 		expected: []string{ | 		expected: []string{ | ||||||
| 			"D0, P[foobar], (!!map)::c: foobarList_c\n<<: [*foo, *bar]\nthing: foobar_thing\nb: foobarList_b\n", | 			"D0, P[foobar], (!!map)::c: foobarList_c\n<<: [*foo, *bar]\nthing: foobar_thing\nb: foobarList_b\n", | ||||||
| 		}, | 		}, | ||||||
| @ -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,36 +49,39 @@ 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.description != "" { | 		if !s.skipDoc { | ||||||
| 			w.WriteString(fmt.Sprintf("### %v\n", s.description)) |  | ||||||
| 		} else { |  | ||||||
| 			w.WriteString(fmt.Sprintf("### Example %v\n", index)) |  | ||||||
| 		} |  | ||||||
| 		if s.document != "" { |  | ||||||
| 			w.WriteString(fmt.Sprintf("sample.yml:\n")) |  | ||||||
| 			w.WriteString(fmt.Sprintf("```yaml\n%v\n```\n", s.document)) |  | ||||||
| 		} |  | ||||||
| 		if s.expression != "" { |  | ||||||
| 			w.WriteString(fmt.Sprintf("Expression\n")) |  | ||||||
| 			w.WriteString(fmt.Sprintf("```bash\nyq '%v' < sample.yml\n```\n", s.expression)) |  | ||||||
| 		} |  | ||||||
| 
 | 
 | ||||||
| 		w.WriteString(fmt.Sprintf("Result\n")) | 			if s.description != "" { | ||||||
|  | 				w.WriteString(fmt.Sprintf("### %v\n", s.description)) | ||||||
|  | 			} else { | ||||||
|  | 				w.WriteString(fmt.Sprintf("### Example %v\n", index)) | ||||||
|  | 			} | ||||||
|  | 			if s.document != "" { | ||||||
|  | 				w.WriteString(fmt.Sprintf("sample.yml:\n")) | ||||||
|  | 				w.WriteString(fmt.Sprintf("```yaml\n%v\n```\n", s.document)) | ||||||
|  | 			} | ||||||
|  | 			if s.expression != "" { | ||||||
|  | 				w.WriteString(fmt.Sprintf("Expression\n")) | ||||||
|  | 				w.WriteString(fmt.Sprintf("```bash\nyq '%v' < sample.yml\n```\n", s.expression)) | ||||||
|  | 			} | ||||||
| 
 | 
 | ||||||
| 		nodes := readDoc(t, s.document) | 			w.WriteString(fmt.Sprintf("Result\n")) | ||||||
| 		path, errPath := treeCreator.ParsePath(s.expression) |  | ||||||
| 		if errPath != nil { |  | ||||||
| 			t.Error(errPath) |  | ||||||
| 			return |  | ||||||
| 		} |  | ||||||
| 		var output bytes.Buffer |  | ||||||
| 		results, err := treeNavigator.GetMatchingNodes(nodes, path) |  | ||||||
| 		printer.PrintResults(results, bufio.NewWriter(&output)) |  | ||||||
| 
 | 
 | ||||||
| 		w.WriteString(fmt.Sprintf("```yaml\n%v```\n", output.String())) | 			nodes := readDoc(t, s.document) | ||||||
|  | 			path, errPath := treeCreator.ParsePath(s.expression) | ||||||
|  | 			if errPath != nil { | ||||||
|  | 				t.Error(errPath) | ||||||
|  | 				return | ||||||
|  | 			} | ||||||
|  | 			var output bytes.Buffer | ||||||
|  | 			results, err := treeNavigator.GetMatchingNodes(nodes, path) | ||||||
|  | 			printer.PrintResults(results, bufio.NewWriter(&output)) | ||||||
| 
 | 
 | ||||||
| 		if err != nil { | 			w.WriteString(fmt.Sprintf("```yaml\n%v```\n", output.String())) | ||||||
| 			panic(err) | 
 | ||||||
|  | 			if err != nil { | ||||||
|  | 				panic(err) | ||||||
|  | 			} | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 	} | 	} | ||||||
|  | |||||||
| @ -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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user