mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-11 09:17:06 +00:00
Add tests
This commit is contained in:
parent
ab906ae760
commit
6afa3a29d7
@ -192,6 +192,42 @@ cat:
|
|||||||
# after cat
|
# after cat
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Parse xml: keep attribute namespace
|
||||||
|
Given a sample.xml file of:
|
||||||
|
```xml
|
||||||
|
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url">
|
||||||
|
</map>
|
||||||
|
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq -p=xml -o=xml --xml-keep-namespace '.' sample.xml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```xml
|
||||||
|
<map xmlns="some-namespace" xmlns:xsi="some-instance" some-instance:schemaLocation="some-url"></map>
|
||||||
|
```
|
||||||
|
|
||||||
|
## Parse xml: keep raw attribute namespace
|
||||||
|
Given a sample.xml file of:
|
||||||
|
```xml
|
||||||
|
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url">
|
||||||
|
</map>
|
||||||
|
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq -p=xml -o=xml --xml-keep-namespace --xml-raw-token '.' sample.xml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```xml
|
||||||
|
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url"></map>
|
||||||
|
```
|
||||||
|
|
||||||
## Encode xml: simple
|
## Encode xml: simple
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
```yaml
|
```yaml
|
||||||
|
|||||||
@ -153,6 +153,24 @@ var expectedXMLWithComments = `<!-- above_cat inline_cat --><cat><!-- above_arra
|
|||||||
</cat><!-- below_cat -->
|
</cat><!-- below_cat -->
|
||||||
`
|
`
|
||||||
|
|
||||||
|
var inputXMLWithNamespacedAttr = `
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url">
|
||||||
|
</map>
|
||||||
|
`
|
||||||
|
|
||||||
|
var expectedYAMLWithNamespacedAttr = `map:
|
||||||
|
+xmlns: some-namespace
|
||||||
|
+xmlns:xsi: some-instance
|
||||||
|
+some-instance:schemaLocation: some-url
|
||||||
|
`
|
||||||
|
|
||||||
|
var expectedYAMLWithRawNamespacedAttr = `map:
|
||||||
|
+xmlns: some-namespace
|
||||||
|
+xmlns:xsi: some-instance
|
||||||
|
+xsi:schemaLocation: some-url
|
||||||
|
`
|
||||||
|
|
||||||
var xmlWithCustomDtd = `
|
var xmlWithCustomDtd = `
|
||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
<!DOCTYPE root [
|
<!DOCTYPE root [
|
||||||
@ -254,6 +272,20 @@ var xmlScenarios = []formatScenario{
|
|||||||
expected: expectedDecodeYamlWithArray,
|
expected: expectedDecodeYamlWithArray,
|
||||||
scenarioType: "decode",
|
scenarioType: "decode",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "Parse xml: keep attribute namespace",
|
||||||
|
skipDoc: false,
|
||||||
|
input: inputXMLWithNamespacedAttr,
|
||||||
|
expected: expectedYAMLWithNamespacedAttr,
|
||||||
|
scenarioType: "decode-keep-ns",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Parse xml: keep raw attribute namespace",
|
||||||
|
skipDoc: false,
|
||||||
|
input: inputXMLWithNamespacedAttr,
|
||||||
|
expected: expectedYAMLWithRawNamespacedAttr,
|
||||||
|
scenarioType: "decode-raw-token",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "Encode xml: simple",
|
description: "Encode xml: simple",
|
||||||
input: "cat: purrs",
|
input: "cat: purrs",
|
||||||
@ -303,11 +335,16 @@ var xmlScenarios = []formatScenario{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testXMLScenario(t *testing.T, s formatScenario) {
|
func testXMLScenario(t *testing.T, s formatScenario) {
|
||||||
if s.scenarioType == "encode" {
|
switch s.scenarioType {
|
||||||
|
case "encode":
|
||||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewXMLEncoder(2, "+", "+content")), s.description)
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewXMLEncoder(2, "+", "+content")), s.description)
|
||||||
} else if s.scenarioType == "roundtrip" {
|
case "roundtrip":
|
||||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content", false, false, false), NewXMLEncoder(2, "+", "+content")), s.description)
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content", false, false, false), NewXMLEncoder(2, "+", "+content")), s.description)
|
||||||
} else {
|
case "decode-keep-ns":
|
||||||
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content", false, true, false), NewYamlEncoder(2, false, true, true)), s.description)
|
||||||
|
case "decode-raw-token":
|
||||||
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content", false, true, true), NewYamlEncoder(2, false, true, true)), s.description)
|
||||||
|
default:
|
||||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content", false, false, false), NewYamlEncoder(4, false, true, true)), s.description)
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content", false, false, false), NewYamlEncoder(4, false, true, true)), s.description)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -318,11 +355,16 @@ func documentXMLScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
|||||||
if s.skipDoc {
|
if s.skipDoc {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if s.scenarioType == "encode" {
|
switch s.scenarioType {
|
||||||
|
case "encode":
|
||||||
documentXMLEncodeScenario(w, s)
|
documentXMLEncodeScenario(w, s)
|
||||||
} else if s.scenarioType == "roundtrip" {
|
case "roundtrip":
|
||||||
documentXMLRoundTripScenario(w, s)
|
documentXMLRoundTripScenario(w, s)
|
||||||
} else {
|
case "decode-keep-ns":
|
||||||
|
documentXMLDecodeKeepNsScenario(w, s)
|
||||||
|
case "decode-raw-token":
|
||||||
|
documentXMLDecodeKeepNsRawTokenScenario(w, s)
|
||||||
|
default:
|
||||||
documentXMLDecodeScenario(w, s)
|
documentXMLDecodeScenario(w, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -350,6 +392,42 @@ func documentXMLDecodeScenario(w *bufio.Writer, s formatScenario) {
|
|||||||
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder("+", "+content", false, false, false), NewYamlEncoder(2, false, true, true))))
|
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder("+", "+content", false, false, false), NewYamlEncoder(2, false, true, true))))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func documentXMLDecodeKeepNsScenario(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.xml file of:\n")
|
||||||
|
writeOrPanic(w, fmt.Sprintf("```xml\n%v\n```\n", s.input))
|
||||||
|
|
||||||
|
writeOrPanic(w, "then\n")
|
||||||
|
writeOrPanic(w, "```bash\nyq -p=xml -o=xml --xml-keep-namespace '.' sample.xml\n```\n")
|
||||||
|
writeOrPanic(w, "will output\n")
|
||||||
|
|
||||||
|
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder("+", "+content", false, true, false), NewXMLEncoder(2, "+", "+content"))))
|
||||||
|
}
|
||||||
|
|
||||||
|
func documentXMLDecodeKeepNsRawTokenScenario(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.xml file of:\n")
|
||||||
|
writeOrPanic(w, fmt.Sprintf("```xml\n%v\n```\n", s.input))
|
||||||
|
|
||||||
|
writeOrPanic(w, "then\n")
|
||||||
|
writeOrPanic(w, "```bash\nyq -p=xml -o=xml --xml-keep-namespace --xml-raw-token '.' sample.xml\n```\n")
|
||||||
|
writeOrPanic(w, "will output\n")
|
||||||
|
|
||||||
|
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder("+", "+content", false, true, true), NewXMLEncoder(2, "+", "+content"))))
|
||||||
|
}
|
||||||
|
|
||||||
func documentXMLEncodeScenario(w *bufio.Writer, s formatScenario) {
|
func documentXMLEncodeScenario(w *bufio.Writer, s formatScenario) {
|
||||||
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user