mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-01 14:14:52 +08:00
fix tests
This commit is contained in:
+28
-41
@@ -32,7 +32,6 @@ var iniScenarios = []formatScenario{
|
||||
{
|
||||
description: "Encode INI: simple",
|
||||
input: `section: {key: value}`,
|
||||
indent: 0,
|
||||
expected: expectedSimpleINIOutput,
|
||||
scenarioType: "encode",
|
||||
},
|
||||
@@ -41,7 +40,6 @@ var iniScenarios = []formatScenario{
|
||||
input: simpleINIInput,
|
||||
expected: expectedSimpleINIOutput,
|
||||
scenarioType: "roundtrip",
|
||||
indent: 0,
|
||||
},
|
||||
{
|
||||
description: "bad ini",
|
||||
@@ -51,7 +49,7 @@ var iniScenarios = []formatScenario{
|
||||
},
|
||||
}
|
||||
|
||||
func documentRoundtripINIScenario(w *bufio.Writer, s formatScenario, indent int) {
|
||||
func documentRoundtripINIScenario(w *bufio.Writer, s formatScenario) {
|
||||
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
||||
|
||||
if s.subdescription != "" {
|
||||
@@ -66,17 +64,13 @@ func documentRoundtripINIScenario(w *bufio.Writer, s formatScenario, indent int)
|
||||
|
||||
expression := s.expression
|
||||
if expression != "" {
|
||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq -p=ini -o=ini -I=%v '%v' sample.ini\n```\n", indent, expression))
|
||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq -p=ini -o=ini '%v' sample.ini\n```\n", expression))
|
||||
} else {
|
||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq -p=ini -o=ini -I=%v sample.ini\n```\n", indent))
|
||||
writeOrPanic(w, "```bash\nyq -p=ini -o=ini sample.ini\n```\n")
|
||||
}
|
||||
|
||||
writeOrPanic(w, "will output\n")
|
||||
prefs := ConfiguredINIPreferences.Copy()
|
||||
prefs.Indent = indent
|
||||
|
||||
// Pass prefs.Indent instead of prefs
|
||||
writeOrPanic(w, fmt.Sprintf("```ini\n%v```\n\n", mustProcessFormatScenario(s, NewINIDecoder(), NewINIEncoder(prefs.Indent))))
|
||||
writeOrPanic(w, fmt.Sprintf("```ini\n%v```\n\n", mustProcessFormatScenario(s, NewINIDecoder(), NewINIEncoder())))
|
||||
}
|
||||
|
||||
func documentDecodeINIScenario(w *bufio.Writer, s formatScenario) {
|
||||
@@ -100,25 +94,19 @@ func documentDecodeINIScenario(w *bufio.Writer, s formatScenario) {
|
||||
}
|
||||
|
||||
writeOrPanic(w, "will output\n")
|
||||
|
||||
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewINIDecoder(), NewYamlEncoder(ConfiguredYamlPreferences))))
|
||||
}
|
||||
|
||||
func testINIScenario(t *testing.T, s formatScenario) {
|
||||
prefs := ConfiguredINIPreferences.Copy()
|
||||
prefs.Indent = s.indent
|
||||
switch s.scenarioType {
|
||||
case "encode":
|
||||
// Pass prefs.Indent instead of prefs
|
||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewINIEncoder(prefs.Indent)), s.description)
|
||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewINIEncoder()), s.description)
|
||||
case "decode":
|
||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewINIDecoder(), NewYamlEncoder(ConfiguredYamlPreferences)), s.description)
|
||||
case "roundtrip":
|
||||
// Pass prefs.Indent instead of prefs
|
||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewINIDecoder(), NewINIEncoder(prefs.Indent)), s.description)
|
||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewINIDecoder(), NewINIEncoder()), s.description)
|
||||
case "decode-error":
|
||||
// Pass prefs.Indent instead of prefs
|
||||
result, err := processFormatScenario(s, NewINIDecoder(), NewINIEncoder(prefs.Indent))
|
||||
result, err := processFormatScenario(s, NewINIDecoder(), NewINIEncoder())
|
||||
if err == nil {
|
||||
t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result)
|
||||
} else {
|
||||
@@ -129,7 +117,7 @@ func testINIScenario(t *testing.T, s formatScenario) {
|
||||
}
|
||||
}
|
||||
|
||||
func documentINIScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
||||
func documentINIScenario(_ *testing.T, w *bufio.Writer, i interface{}) {
|
||||
s := i.(formatScenario)
|
||||
if s.skipDoc {
|
||||
return
|
||||
@@ -140,18 +128,9 @@ func documentINIScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
||||
case "decode":
|
||||
documentDecodeINIScenario(w, s)
|
||||
case "roundtrip":
|
||||
documentRoundtripINIScenario(w, s, s.indent)
|
||||
documentRoundtripINIScenario(w, s)
|
||||
case "decode-error":
|
||||
// Add handling for decode-error scenario type to prevent panic
|
||||
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
||||
if s.subdescription != "" {
|
||||
writeOrPanic(w, s.subdescription)
|
||||
writeOrPanic(w, "\n\n")
|
||||
}
|
||||
writeOrPanic(w, "Given a sample.ini file of:\n")
|
||||
writeOrPanic(w, fmt.Sprintf("```ini\n%v\n```\n", s.input))
|
||||
writeOrPanic(w, "then an error is expected:\n")
|
||||
writeOrPanic(w, fmt.Sprintf("```\n%v\n```\n\n", s.expectedError))
|
||||
documentDecodeErrorINIScenario(w, s)
|
||||
default:
|
||||
panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
|
||||
}
|
||||
@@ -175,17 +154,25 @@ func documentINIEncodeScenario(w *bufio.Writer, s formatScenario) {
|
||||
expression = "."
|
||||
}
|
||||
|
||||
if s.indent == 2 {
|
||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=ini '%v' sample.yml\n```\n", expression))
|
||||
} else {
|
||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=ini -I=%v '%v' sample.yml\n```\n", s.indent, expression))
|
||||
}
|
||||
writeOrPanic(w, "will output\n")
|
||||
prefs := ConfiguredINIPreferences.Copy()
|
||||
prefs.Indent = s.indent
|
||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=ini '%v' sample.yml\n```\n", expression))
|
||||
|
||||
// Pass prefs.Indent instead of prefs
|
||||
writeOrPanic(w, fmt.Sprintf("```ini\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewINIEncoder(prefs.Indent))))
|
||||
writeOrPanic(w, "will output\n")
|
||||
writeOrPanic(w, fmt.Sprintf("```ini\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewINIEncoder())))
|
||||
}
|
||||
|
||||
func documentDecodeErrorINIScenario(w *bufio.Writer, s formatScenario) {
|
||||
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
||||
|
||||
if s.subdescription != "" {
|
||||
writeOrPanic(w, s.subdescription)
|
||||
writeOrPanic(w, "\n\n")
|
||||
}
|
||||
|
||||
writeOrPanic(w, "Given a sample.ini file of:\n")
|
||||
writeOrPanic(w, fmt.Sprintf("```ini\n%v\n```\n", s.input))
|
||||
|
||||
writeOrPanic(w, "then an error is expected:\n")
|
||||
writeOrPanic(w, fmt.Sprintf("```\n%v\n```\n\n", s.expectedError))
|
||||
}
|
||||
|
||||
func TestINIScenarios(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user