From 93b7b6823a59ad6e46f34bd00002f44c015a97f9 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 10 Feb 2022 11:57:37 +1100 Subject: [PATCH] Switched formatScenario parameter order for more readablity --- pkg/yqlib/decoder_test.go | 60 ++++++++++++++++++++++++++++++++++++ pkg/yqlib/json_test.go | 47 ++-------------------------- pkg/yqlib/properties_test.go | 12 ++++---- pkg/yqlib/xml_test.go | 23 ++++---------- 4 files changed, 74 insertions(+), 68 deletions(-) create mode 100644 pkg/yqlib/decoder_test.go diff --git a/pkg/yqlib/decoder_test.go b/pkg/yqlib/decoder_test.go new file mode 100644 index 00000000..462eb4d1 --- /dev/null +++ b/pkg/yqlib/decoder_test.go @@ -0,0 +1,60 @@ +package yqlib + +import ( + "bufio" + "bytes" + "strings" +) + +type formatScenario struct { + input string + indent int + expression string + expected string + description string + subdescription string + skipDoc bool + scenarioType string +} + +func processFormatScenario(s formatScenario, decoder Decoder, encoder Encoder) string { + + var output bytes.Buffer + writer := bufio.NewWriter(&output) + + if decoder == nil { + decoder = NewYamlDecoder() + } + + inputs, err := readDocuments(strings.NewReader(s.input), "sample.yml", 0, decoder) + if err != nil { + panic(err) + } + + expression := s.expression + if expression == "" { + expression = "." + } + + exp, err := getExpressionParser().ParseExpression(expression) + + if err != nil { + panic(err) + } + + context, err := NewDataTreeNavigator().GetMatchingNodes(Context{MatchingNodes: inputs}, exp) + + if err != nil { + panic(err) + } + + printer := NewPrinter(encoder, NewSinglePrinterWriter(writer)) + err = printer.PrintResults(context.MatchingNodes) + if err != nil { + panic(err) + } + writer.Flush() + + return output.String() + +} diff --git a/pkg/yqlib/json_test.go b/pkg/yqlib/json_test.go index 91c5f2d3..9e1ac66e 100644 --- a/pkg/yqlib/json_test.go +++ b/pkg/yqlib/json_test.go @@ -4,7 +4,6 @@ import ( "bufio" "bytes" "fmt" - "strings" "testing" "github.com/mikefarah/yq/v4/test" @@ -98,55 +97,13 @@ func decodeJSON(t *testing.T, jsonString string) *CandidateNode { func testJSONScenario(t *testing.T, s formatScenario) { if s.scenarioType == "encode" || s.scenarioType == "roundtrip" { - test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewJONEncoder(s.indent), NewYamlDecoder()), s.description) + test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewJONEncoder(s.indent)), s.description) } else { var actual = resultToString(t, decodeJSON(t, s.input)) test.AssertResultWithContext(t, s.expected, actual, s.description) } } -func processFormatScenario(s formatScenario, encoder Encoder, decoder Decoder) string { - - var output bytes.Buffer - writer := bufio.NewWriter(&output) - - if decoder == nil { - decoder = NewYamlDecoder() - } - - inputs, err := readDocuments(strings.NewReader(s.input), "sample.yml", 0, decoder) - if err != nil { - panic(err) - } - - expression := s.expression - if expression == "" { - expression = "." - } - - exp, err := getExpressionParser().ParseExpression(expression) - - if err != nil { - panic(err) - } - - context, err := NewDataTreeNavigator().GetMatchingNodes(Context{MatchingNodes: inputs}, exp) - - if err != nil { - panic(err) - } - - printer := NewPrinter(encoder, NewSinglePrinterWriter(writer)) - err = printer.PrintResults(context.MatchingNodes) - if err != nil { - panic(err) - } - writer.Flush() - - return output.String() - -} - func documentJSONDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario) { writeOrPanic(w, fmt.Sprintf("## %v\n", s.description)) @@ -214,7 +171,7 @@ func documentJSONEncodeScenario(w *bufio.Writer, s formatScenario) { } writeOrPanic(w, "will output\n") - writeOrPanic(w, fmt.Sprintf("```json\n%v```\n\n", processFormatScenario(s, NewJONEncoder(s.indent), NewYamlDecoder()))) + writeOrPanic(w, fmt.Sprintf("```json\n%v```\n\n", processFormatScenario(s, NewYamlDecoder(), NewJONEncoder(s.indent)))) } func TestJSONScenarios(t *testing.T) { diff --git a/pkg/yqlib/properties_test.go b/pkg/yqlib/properties_test.go index 9716d98f..f271dd20 100644 --- a/pkg/yqlib/properties_test.go +++ b/pkg/yqlib/properties_test.go @@ -121,7 +121,7 @@ func documentEncodePropertyScenario(w *bufio.Writer, s formatScenario) { } writeOrPanic(w, "will output\n") - writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewPropertiesEncoder(), NewYamlDecoder()))) + writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewYamlDecoder(), NewPropertiesEncoder()))) } func documentDecodePropertyScenario(w *bufio.Writer, s formatScenario) { @@ -146,7 +146,7 @@ func documentDecodePropertyScenario(w *bufio.Writer, s formatScenario) { writeOrPanic(w, "will output\n") - writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewYamlEncoder(s.indent, false, true, true), NewPropertiesDecoder()))) + writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewPropertiesDecoder(), NewYamlEncoder(s.indent, false, true, true)))) } func documentRoundTripPropertyScenario(w *bufio.Writer, s formatScenario) { @@ -171,7 +171,7 @@ func documentRoundTripPropertyScenario(w *bufio.Writer, s formatScenario) { writeOrPanic(w, "will output\n") - writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewPropertiesEncoder(), NewPropertiesDecoder()))) + writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder()))) } func documentPropertyScenario(t *testing.T, w *bufio.Writer, i interface{}) { @@ -189,11 +189,11 @@ func documentPropertyScenario(t *testing.T, w *bufio.Writer, i interface{}) { func TestPropertyScenarios(t *testing.T) { for _, s := range propertyScenarios { if s.scenarioType == "decode" { - test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlEncoder(2, false, true, true), NewPropertiesDecoder()), s.description) + test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesDecoder(), NewYamlEncoder(2, false, true, true)), s.description) } else if s.scenarioType == "roundtrip" { - test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesEncoder(), NewPropertiesDecoder()), s.description) + test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder()), s.description) } else { - test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesEncoder(), NewYamlDecoder()), s.description) + test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewPropertiesEncoder()), s.description) } } genericScenarios := make([]interface{}, len(propertyScenarios)) diff --git a/pkg/yqlib/xml_test.go b/pkg/yqlib/xml_test.go index 53835128..adf09374 100644 --- a/pkg/yqlib/xml_test.go +++ b/pkg/yqlib/xml_test.go @@ -8,17 +8,6 @@ import ( "github.com/mikefarah/yq/v4/test" ) -type formatScenario struct { - input string - indent int - expression string - expected string - description string - subdescription string - skipDoc bool - scenarioType string -} - var inputXMLWithComments = ` @@ -295,11 +284,11 @@ var xmlScenarios = []formatScenario{ func testXMLScenario(t *testing.T, s formatScenario) { if s.scenarioType == "encode" { - test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLEncoder(2, "+", "+content"), NewYamlDecoder()), s.description) + test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewXMLEncoder(2, "+", "+content")), s.description) } else if s.scenarioType == "roundtrip" { - test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLEncoder(2, "+", "+content"), NewXMLDecoder("+", "+content")), s.description) + test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content"), NewXMLEncoder(2, "+", "+content")), s.description) } else { - test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlEncoder(4, false, true, true), NewXMLDecoder("+", "+content")), s.description) + test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content"), NewYamlEncoder(4, false, true, true)), s.description) } } @@ -338,7 +327,7 @@ func documentXMLDecodeScenario(w *bufio.Writer, s formatScenario) { writeOrPanic(w, fmt.Sprintf("```bash\nyq -p=xml '%v' sample.xml\n```\n", expression)) writeOrPanic(w, "will output\n") - writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewYamlEncoder(2, false, true, true), NewXMLDecoder("+", "+content")))) + writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder("+", "+content"), NewYamlEncoder(2, false, true, true)))) } func documentXMLEncodeScenario(w *bufio.Writer, s formatScenario) { @@ -356,7 +345,7 @@ func documentXMLEncodeScenario(w *bufio.Writer, s formatScenario) { writeOrPanic(w, "```bash\nyq -o=xml '.' sample.yml\n```\n") writeOrPanic(w, "will output\n") - writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processFormatScenario(s, NewXMLEncoder(2, "+", "+content"), NewYamlDecoder()))) + writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processFormatScenario(s, NewYamlDecoder(), NewXMLEncoder(2, "+", "+content")))) } func documentXMLRoundTripScenario(w *bufio.Writer, s formatScenario) { @@ -374,7 +363,7 @@ func documentXMLRoundTripScenario(w *bufio.Writer, s formatScenario) { writeOrPanic(w, "```bash\nyq -p=xml -o=xml '.' sample.xml\n```\n") writeOrPanic(w, "will output\n") - writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processFormatScenario(s, NewXMLEncoder(2, "+", "+content"), NewXMLDecoder("+", "+content")))) + writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder("+", "+content"), NewXMLEncoder(2, "+", "+content")))) } func TestXMLScenarios(t *testing.T) {