This commit is contained in:
Mike Farah 2023-04-13 14:44:39 +10:00
parent 08c93f0f1f
commit 27fa4e10b8
3 changed files with 91 additions and 35 deletions

View File

@ -173,7 +173,6 @@ func (n *CandidateNode) AsList() *list.List {
} }
func (n *CandidateNode) GetValueRep() (interface{}, error) { func (n *CandidateNode) GetValueRep() (interface{}, error) {
// TODO: handle booleans, ints, etc
log.Debugf("GetValueRep for %v value: %v", n.GetNicePath(), n.Value) log.Debugf("GetValueRep for %v value: %v", n.GetNicePath(), n.Value)
realTag := n.guessTagFromCustomType() realTag := n.guessTagFromCustomType()
@ -186,6 +185,8 @@ func (n *CandidateNode) GetValueRep() (interface{}, error) {
return strconv.ParseFloat(n.Value, 64) return strconv.ParseFloat(n.Value, 64)
case "!!bool": case "!!bool":
return isTruthyNode(n) return isTruthyNode(n)
case "!!null":
return nil, nil
} }
return n.Value, nil return n.Value, nil

View File

@ -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)
}
}

View File

@ -7,54 +7,56 @@ import (
) )
var yamlScenarios = []formatScenario{ var yamlScenarios = []formatScenario{
// {
// description: "basic - null",
// skipDoc: true,
// input: "null",
// expected: "null\n",
// },
{ {
description: "basic - ~", description: "basic - null",
skipDoc: true, skipDoc: true,
input: "~", input: "null",
expected: "~\n", expected: "null\n",
}, },
// { // {
// description: "basic - [null]", // description: "basic - ~",
// skipDoc: true, // skipDoc: true,
// input: "[null]", // input: "~",
// expected: "[null]\n", // expected: "~\n",
// },
// {
// description: "basic - [~]",
// skipDoc: true,
// input: "[~]",
// expected: "[~]\n",
// },
// {
// description: "basic - null map value",
// skipDoc: true,
// input: "a: null",
// expected: "a: null\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) { 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) 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) { func TestYamlScenarios(t *testing.T) {
for _, tt := range yamlScenarios { for _, tt := range yamlScenarios {
testYamlScenario(t, tt) testYamlScenario(t, tt)
} }
// genericScenarios := make([]interface{}, len(yamlScenarios))
// for i, s := range yamlScenarios {
// genericScenarios[i] = s
// }
// documentScenarios(t, "usage", "convert", genericScenarios, documentJSONScenario)
} }