diff --git a/pkg/yqlib/operator_explode_test.go b/pkg/yqlib/operator_explode_test.go index e7c609ca..1bd9fe40 100644 --- a/pkg/yqlib/operator_explode_test.go +++ b/pkg/yqlib/operator_explode_test.go @@ -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, diff --git a/pkg/yqlib/operator_multiply_test.go b/pkg/yqlib/operator_multiply_test.go index 3da2858f..a12a0220 100644 --- a/pkg/yqlib/operator_multiply_test.go +++ b/pkg/yqlib/operator_multiply_test.go @@ -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", }, }, { @@ -62,7 +70,8 @@ var multiplyOperatorScenarios = []expressionScenario{ }, }, { - description: "Merge keeps style of LHS", + 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) } diff --git a/pkg/yqlib/operator_not_test.go b/pkg/yqlib/operator_not_test.go index 1c441644..3bac2087 100644 --- a/pkg/yqlib/operator_not_test.go +++ b/pkg/yqlib/operator_not_test.go @@ -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) } diff --git a/pkg/yqlib/operators_test.go b/pkg/yqlib/operators_test.go index b550071a..625997b8 100644 --- a/pkg/yqlib/operators_test.go +++ b/pkg/yqlib/operators_test.go @@ -14,11 +14,12 @@ import ( ) type expressionScenario struct { - description string - document string - expression string - expected []string - skipDoc bool + description string + document string + 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) }