more docs

This commit is contained in:
Mike Farah 2020-11-15 10:50:30 +11:00
parent db4762ef7c
commit af2aa9ad91
4 changed files with 99 additions and 51 deletions

View File

@ -15,10 +15,10 @@ var explodeTest = []expressionScenario{
},
{
description: "Explode with no aliases or anchors",
document: `{a: mike}`,
document: `a: mike`,
expression: `explode(.a)`,
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",
},
},
{
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,
document: mergeDocSample,

View File

@ -22,11 +22,19 @@ var multiplyOperatorScenarios = []expressionScenario{
},
},
{
description: "Merge objects together",
document: `{a: {also: me}, b: {also: {g: wizz}}}`,
description: "Merge objects together, returning merged result only",
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}`,
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",
dontFormatInputForDoc: true,
document: `a: {things: great}
b:
also: "me"
@ -121,5 +130,5 @@ func TestMultiplyOperatorScenarios(t *testing.T) {
for _, tt := range multiplyOperatorScenarios {
testScenario(t, &tt)
}
documentScenarios(t, "Mulitply Operator", multiplyOperatorScenarios)
documentScenarios(t, "Multiply Operator", multiplyOperatorScenarios)
}

View File

@ -5,52 +5,61 @@ import (
)
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: `~`,
expression: `. | not`,
description: "Not true is false",
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{
"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) {
for _, tt := range notOperatorScenarios {
testScenario(t, &tt)
}
documentScenarios(t, "Not Operator", notOperatorScenarios)
}

View File

@ -19,6 +19,7 @@ type expressionScenario struct {
expression string
expected []string
skipDoc bool
dontFormatInputForDoc bool // dont format input doc for documentation generation
}
func testScenario(t *testing.T, s *expressionScenario) {
@ -113,11 +114,17 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
} else {
writeOrPanic(w, fmt.Sprintf("### Example %v\n", index))
}
formattedDoc := ""
if s.document != "" {
if s.dontFormatInputForDoc {
formattedDoc = s.document
} else {
formattedDoc = formatYaml(s.document)
}
//TODO: pretty here
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")
if 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 {
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 {
t.Error(err)
}