From 584ec998bc17b3218fa2a82c8ed6814dfefff915 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 6 Jun 2023 11:06:23 -0700 Subject: [PATCH] Unit tests pass! --- pkg/yqlib/doc/usage/properties.md | 1 - pkg/yqlib/encoder_xml.go | 2 +- pkg/yqlib/operator_unique_test.go | 14 +++++++------- pkg/yqlib/properties_test.go | 16 +++++++++++++--- pkg/yqlib/toml_test.go | 2 +- pkg/yqlib/xml_test.go | 7 ------- 6 files changed, 22 insertions(+), 20 deletions(-) diff --git a/pkg/yqlib/doc/usage/properties.md b/pkg/yqlib/doc/usage/properties.md index 3548ad43..f3d01235 100644 --- a/pkg/yqlib/doc/usage/properties.md +++ b/pkg/yqlib/doc/usage/properties.md @@ -83,7 +83,6 @@ yq -o=props '... comments = ""' sample.yml ``` will output ```properties -# block comments come through person.name = Mike Wazowski person.pets.0 = cat person.food.0 = pizza diff --git a/pkg/yqlib/encoder_xml.go b/pkg/yqlib/encoder_xml.go index 5490e4e3..4a1432af 100644 --- a/pkg/yqlib/encoder_xml.go +++ b/pkg/yqlib/encoder_xml.go @@ -93,7 +93,7 @@ func (e *xmlEncoder) Encode(writer io.Writer, node *CandidateNode) error { } return encoder.Flush() default: - return fmt.Errorf("unsupported type %v", node.Tag) + return fmt.Errorf("cannot encode %v to XML - only maps can be encoded", node.Tag) } return encoder.EncodeToken(newLine) diff --git a/pkg/yqlib/operator_unique_test.go b/pkg/yqlib/operator_unique_test.go index f0c44533..fe28de56 100644 --- a/pkg/yqlib/operator_unique_test.go +++ b/pkg/yqlib/operator_unique_test.go @@ -11,7 +11,7 @@ var uniqueOperatorScenarios = []expressionScenario{ document: `[2,1,3,2]`, expression: `unique`, expected: []string{ - "D0, P[], (!!seq)::- 2\n- 1\n- 3\n", + "D0, P[], (!!seq)::[2, 1, 3]\n", }, }, { @@ -20,7 +20,7 @@ var uniqueOperatorScenarios = []expressionScenario{ document: `[~,null, ~, null]`, expression: `unique`, expected: []string{ - "D0, P[], (!!seq)::- ~\n- null\n", + "D0, P[], (!!seq)::[~, null]\n", }, }, { @@ -29,7 +29,7 @@ var uniqueOperatorScenarios = []expressionScenario{ document: `[~,null, ~, null]`, expression: `unique_by(tag)`, expected: []string{ - "D0, P[], (!!seq)::- ~\n", + "D0, P[], (!!seq)::[~]\n", }, }, { @@ -37,7 +37,7 @@ var uniqueOperatorScenarios = []expressionScenario{ document: `[{name: harry, pet: cat}, {name: billy, pet: dog}, {name: harry, pet: dog}]`, expression: `unique_by(.name)`, expected: []string{ - "D0, P[], (!!seq)::- {name: harry, pet: cat}\n- {name: billy, pet: dog}\n", + "D0, P[], (!!seq)::[{name: harry, pet: cat}, {name: billy, pet: dog}]\n", }, }, { @@ -45,7 +45,7 @@ var uniqueOperatorScenarios = []expressionScenario{ document: `[{name: harry, pet: cat}, {pet: fish}, {name: harry, pet: dog}]`, expression: `unique_by(.name)`, expected: []string{ - "D0, P[], (!!seq)::- {name: harry, pet: cat}\n- {pet: fish}\n", + "D0, P[], (!!seq)::[{name: harry, pet: cat}, {pet: fish}]\n", }, }, { @@ -53,7 +53,7 @@ var uniqueOperatorScenarios = []expressionScenario{ document: `[{name: harry, pet: cat}, {pet: fish}, {name: harry, pet: dog}]`, expression: `unique_by(.cat.dog)`, expected: []string{ - "D0, P[], (!!seq)::- {name: harry, pet: cat}\n", + "D0, P[], (!!seq)::[{name: harry, pet: cat}]\n", }, }, { @@ -61,7 +61,7 @@ var uniqueOperatorScenarios = []expressionScenario{ document: "# abc\n[{name: harry, pet: cat}, {pet: fish}, {name: harry, pet: dog}]\n# xyz", expression: `unique_by(.name)`, expected: []string{ - "D0, P[], (!!seq)::# abc\n- {name: harry, pet: cat}\n- {pet: fish}\n# xyz\n", + "D0, P[], (!!seq)::# abc\n[{name: harry, pet: cat}, {pet: fish}]\n# xyz\n", }, }, } diff --git a/pkg/yqlib/properties_test.go b/pkg/yqlib/properties_test.go index bf67d043..8ca996b0 100644 --- a/pkg/yqlib/properties_test.go +++ b/pkg/yqlib/properties_test.go @@ -98,6 +98,16 @@ const expectedDecodedYaml = `person: - pizza ` +const expectedDecodedPersonYaml = `# block comments come through +# comments on values appear +name: Mike Wazowski +pets: + # comments on array values appear + - cat +food: + - pizza +` + const expectedPropertiesNoComments = `person.name = Mike Wazowski person.pets.0 = cat person.food.0 = pizza @@ -153,7 +163,7 @@ var propertyScenarios = []formatScenario{ description: "Decode properties - keeps key information", input: expectedPropertiesUnwrapped, expression: ".person.name | key", - expected: "name", + expected: "name\n", scenarioType: "decode", }, { @@ -161,7 +171,7 @@ var propertyScenarios = []formatScenario{ description: "Decode properties - keeps parent information", input: expectedPropertiesUnwrapped, expression: ".person.name | parent", - expected: "name", + expected: expectedDecodedPersonYaml, scenarioType: "decode", }, { @@ -169,7 +179,7 @@ var propertyScenarios = []formatScenario{ description: "Decode properties - keeps path information", input: expectedPropertiesUnwrapped, expression: ".person.name | path", - expected: "name", + expected: "- person\n- name\n", scenarioType: "decode", }, diff --git a/pkg/yqlib/toml_test.go b/pkg/yqlib/toml_test.go index b4d3ef6f..2e0765dc 100644 --- a/pkg/yqlib/toml_test.go +++ b/pkg/yqlib/toml_test.go @@ -107,7 +107,7 @@ var tomlScenarios = []formatScenario{ description: "Parse: include parent information", input: "person.name = \"hello\"\nperson.address = \"12 cat st\"\n", expression: ".person.name | parent", - expected: "person\n", + expected: "name: hello\naddress: 12 cat st\n", scenarioType: "decode", }, { diff --git a/pkg/yqlib/xml_test.go b/pkg/yqlib/xml_test.go index a86e25ab..a0c75c65 100644 --- a/pkg/yqlib/xml_test.go +++ b/pkg/yqlib/xml_test.go @@ -523,13 +523,6 @@ var xmlScenarios = []formatScenario{ expectedError: "cannot encode !!seq to XML - only maps can be encoded", scenarioType: "encode-error", }, - { - description: "scalars cannot be encoded", - skipDoc: true, - input: "mike", - expectedError: "cannot encode !!str to XML - only maps can be encoded", - scenarioType: "encode-error", - }, { description: "Encode xml: attributes with content", subdescription: "Fields with the matching xml-content-name is assumed to be content.",