diff --git a/pkg/yqlib/candidate_node.go b/pkg/yqlib/candidate_node.go index ce321f12..5cd3c77a 100644 --- a/pkg/yqlib/candidate_node.go +++ b/pkg/yqlib/candidate_node.go @@ -173,7 +173,6 @@ func (n *CandidateNode) AsList() *list.List { } func (n *CandidateNode) GetValueRep() (interface{}, error) { - // TODO: handle booleans, ints, etc log.Debugf("GetValueRep for %v value: %v", n.GetNicePath(), n.Value) realTag := n.guessTagFromCustomType() @@ -186,6 +185,8 @@ func (n *CandidateNode) GetValueRep() (interface{}, error) { return strconv.ParseFloat(n.Value, 64) case "!!bool": return isTruthyNode(n) + case "!!null": + return nil, nil } return n.Value, nil diff --git a/pkg/yqlib/candidate_node_test.go b/pkg/yqlib/candidate_node_test.go new file mode 100644 index 00000000..1b3641c9 --- /dev/null +++ b/pkg/yqlib/candidate_node_test.go @@ -0,0 +1,53 @@ +package yqlib + +import ( + "testing" + + "github.com/mikefarah/yq/v4/test" +) + +type valueRepScenario struct { + input string + tag string + expected interface{} +} + +var valueRepScenarios = []valueRepScenario{ + { + input: `"cat"`, + expected: `"cat"`, + }, + { + input: `3`, + expected: int64(3), + }, + { + input: `3.1`, + expected: float64(3.1), + }, + { + input: `true`, + expected: true, + }, + { + input: `y`, + tag: "!!bool", + expected: true, + }, + { + tag: "!!null", + expected: nil, + }, +} + +func TestCandidateNodeGetValueRepScenarios(t *testing.T) { + for _, tt := range valueRepScenarios { + node := CandidateNode{Value: tt.input, Tag: tt.tag} + actual, err := node.GetValueRep() + if err != nil { + t.Error(err) + return + } + test.AssertResult(t, tt.expected, actual) + } +} diff --git a/pkg/yqlib/yaml_test.go b/pkg/yqlib/yaml_test.go index b056e4a1..6388718a 100644 --- a/pkg/yqlib/yaml_test.go +++ b/pkg/yqlib/yaml_test.go @@ -7,54 +7,56 @@ import ( ) var yamlScenarios = []formatScenario{ - // { - // description: "basic - null", - // skipDoc: true, - // input: "null", - // expected: "null\n", - // }, { - description: "basic - ~", + description: "basic - null", skipDoc: true, - input: "~", - expected: "~\n", + input: "null", + expected: "null\n", }, // { - // description: "basic - [null]", + // description: "basic - ~", // skipDoc: true, - // input: "[null]", - // expected: "[null]\n", - // }, - // { - // description: "basic - [~]", - // skipDoc: true, - // input: "[~]", - // expected: "[~]\n", - // }, - // { - // description: "basic - null map value", - // skipDoc: true, - // input: "a: null", - // expected: "a: null\n", + // input: "~", + // expected: "~\n", // }, + { + description: "basic - [null]", + skipDoc: true, + input: "[null]", + expected: "[null]\n", + }, + { + description: "basic - [~]", + skipDoc: true, + input: "[~]", + expected: "[~]\n", + }, + { + description: "basic - null map value", + skipDoc: true, + input: "a: null", + expected: "a: null\n", + }, + { + description: "basic - number", + skipDoc: true, + input: "3", + expected: "3\n", + }, + { + description: "basic - float", + skipDoc: true, + input: "3.1", + expected: "3.1\n", + }, } func testYamlScenario(t *testing.T, s formatScenario) { - // switch s.scenarioType { - // case "decode": test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description) - // default: - // panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType)) - // } } func TestYamlScenarios(t *testing.T) { for _, tt := range yamlScenarios { testYamlScenario(t, tt) } - // genericScenarios := make([]interface{}, len(yamlScenarios)) - // for i, s := range yamlScenarios { - // genericScenarios[i] = s - // } - // documentScenarios(t, "usage", "convert", genericScenarios, documentJSONScenario) }