mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-24 14:45:39 +00:00
more docs
This commit is contained in:
parent
db4762ef7c
commit
af2aa9ad91
@ -15,10 +15,10 @@ var explodeTest = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Explode with no aliases or anchors",
|
description: "Explode with no aliases or anchors",
|
||||||
document: `{a: mike}`,
|
document: `a: mike`,
|
||||||
expression: `explode(.a)`,
|
expression: `explode(.a)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (doc)::{a: mike}\n",
|
"D0, P[], (doc)::a: mike\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -29,6 +29,29 @@ var explodeTest = []expressionScenario{
|
|||||||
"D0, P[], (doc)::{f: {a: cat, cat: b}}\n",
|
"D0, P[], (doc)::{f: {a: cat, cat: b}}\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "Explode with merge anchors",
|
||||||
|
document: mergeDocSample,
|
||||||
|
expression: `explode(.)`,
|
||||||
|
expected: []string{`D0, P[], (doc)::foo:
|
||||||
|
a: foo_a
|
||||||
|
thing: foo_thing
|
||||||
|
c: foo_c
|
||||||
|
bar:
|
||||||
|
b: bar_b
|
||||||
|
thing: bar_thing
|
||||||
|
c: bar_c
|
||||||
|
foobarList:
|
||||||
|
b: bar_b
|
||||||
|
a: foo_a
|
||||||
|
thing: bar_thing
|
||||||
|
c: foobarList_c
|
||||||
|
foobar:
|
||||||
|
c: foo_c
|
||||||
|
a: foo_a
|
||||||
|
thing: foobar_thing
|
||||||
|
`},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
skipDoc: true,
|
skipDoc: true,
|
||||||
document: mergeDocSample,
|
document: mergeDocSample,
|
||||||
|
@ -22,11 +22,19 @@ var multiplyOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Merge objects together",
|
description: "Merge objects together, returning merged result only",
|
||||||
document: `{a: {also: me}, b: {also: {g: wizz}}}`,
|
document: `{a: {field: me, fieldA: cat}, b: {field: {g: wizz}, fieldB: dog}}`,
|
||||||
|
expression: `.a * .b`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[a], (!!map)::{field: {g: wizz}, fieldA: cat, fieldB: dog}\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Merge objects together, returning parent object",
|
||||||
|
document: `{a: {field: me, fieldA: cat}, b: {field: {g: wizz}, fieldB: dog}}`,
|
||||||
expression: `. * {"a":.b}`,
|
expression: `. * {"a":.b}`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!map)::{a: {also: {g: wizz}}, b: {also: {g: wizz}}}\n",
|
"D0, P[], (!!map)::{a: {field: {g: wizz}, fieldA: cat, fieldB: dog}, b: {field: {g: wizz}, fieldB: dog}}\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -63,6 +71,7 @@ var multiplyOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Merge keeps style of LHS",
|
description: "Merge keeps style of LHS",
|
||||||
|
dontFormatInputForDoc: true,
|
||||||
document: `a: {things: great}
|
document: `a: {things: great}
|
||||||
b:
|
b:
|
||||||
also: "me"
|
also: "me"
|
||||||
@ -121,5 +130,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)
|
documentScenarios(t, "Multiply Operator", multiplyOperatorScenarios)
|
||||||
}
|
}
|
||||||
|
@ -5,52 +5,61 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var notOperatorScenarios = []expressionScenario{
|
var notOperatorScenarios = []expressionScenario{
|
||||||
// {
|
|
||||||
// document: `cat`,
|
|
||||||
// expression: `. | not`,
|
|
||||||
// expected: []string{
|
|
||||||
// "D0, P[], (!!bool)::false\n",
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// document: `1`,
|
|
||||||
// expression: `. | not`,
|
|
||||||
// expected: []string{
|
|
||||||
// "D0, P[], (!!bool)::false\n",
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// document: `0`,
|
|
||||||
// expression: `. | not`,
|
|
||||||
// expected: []string{
|
|
||||||
// "D0, P[], (!!bool)::false\n",
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
{
|
{
|
||||||
document: `~`,
|
description: "Not true is false",
|
||||||
expression: `. | not`,
|
expression: `true | not`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!bool)::false\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Not false is true",
|
||||||
|
expression: `false | not`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!bool)::true\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "String values considered to be true",
|
||||||
|
expression: `"cat" | not`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!bool)::false\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Empty string value considered to be true",
|
||||||
|
expression: `"" | not`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!bool)::false\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Numbers are considered to be true",
|
||||||
|
expression: `1 | not`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!bool)::false\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Zero is considered to be true",
|
||||||
|
expression: `0 | not`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!bool)::false\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
|
||||||
|
description: "Null is considered to be false",
|
||||||
|
expression: `~ | not`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!bool)::true\n",
|
"D0, P[], (!!bool)::true\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
// {
|
|
||||||
// document: `false`,
|
|
||||||
// expression: `. | not`,
|
|
||||||
// expected: []string{
|
|
||||||
// "D0, P[], (!!bool)::true\n",
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// document: `true`,
|
|
||||||
// expression: `. | not`,
|
|
||||||
// expected: []string{
|
|
||||||
// "D0, P[], (!!bool)::false\n",
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNotOperatorScenarios(t *testing.T) {
|
func TestNotOperatorScenarios(t *testing.T) {
|
||||||
for _, tt := range notOperatorScenarios {
|
for _, tt := range notOperatorScenarios {
|
||||||
testScenario(t, &tt)
|
testScenario(t, &tt)
|
||||||
}
|
}
|
||||||
|
documentScenarios(t, "Not Operator", notOperatorScenarios)
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ type expressionScenario struct {
|
|||||||
expression string
|
expression string
|
||||||
expected []string
|
expected []string
|
||||||
skipDoc bool
|
skipDoc bool
|
||||||
|
dontFormatInputForDoc bool // dont format input doc for documentation generation
|
||||||
}
|
}
|
||||||
|
|
||||||
func testScenario(t *testing.T, s *expressionScenario) {
|
func testScenario(t *testing.T, s *expressionScenario) {
|
||||||
@ -113,11 +114,17 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
|
|||||||
} else {
|
} else {
|
||||||
writeOrPanic(w, fmt.Sprintf("### Example %v\n", index))
|
writeOrPanic(w, fmt.Sprintf("### Example %v\n", index))
|
||||||
}
|
}
|
||||||
|
formattedDoc := ""
|
||||||
if s.document != "" {
|
if s.document != "" {
|
||||||
|
if s.dontFormatInputForDoc {
|
||||||
|
formattedDoc = s.document
|
||||||
|
} else {
|
||||||
|
formattedDoc = formatYaml(s.document)
|
||||||
|
}
|
||||||
//TODO: pretty here
|
//TODO: pretty here
|
||||||
writeOrPanic(w, "Given a sample.yml file of:\n")
|
writeOrPanic(w, "Given a sample.yml file of:\n")
|
||||||
|
|
||||||
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n", formatYaml(s.document)))
|
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n", formattedDoc))
|
||||||
writeOrPanic(w, "then\n")
|
writeOrPanic(w, "then\n")
|
||||||
if s.expression != "" {
|
if s.expression != "" {
|
||||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq eval '%v' sample.yml\n```\n", s.expression))
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq eval '%v' sample.yml\n```\n", s.expression))
|
||||||
@ -140,7 +147,7 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
err = EvaluateStream("sample.yaml", strings.NewReader(s.document), node, printer)
|
err = EvaluateStream("sample.yaml", strings.NewReader(formattedDoc), node, printer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user