1
0
mirror of https://github.com/taigrr/yq synced 2025-01-18 04:53:17 -08:00

Added customStyle flag, split command tests

This commit is contained in:
Mike Farah
2020-04-17 11:03:43 +10:00
parent e15633023f
commit 06d8715cbe
15 changed files with 2838 additions and 2608 deletions

View File

@@ -5,7 +5,7 @@ import (
)
type ValueParser interface {
Parse(argument string, customTag string) *yaml.Node
Parse(argument string, customTag string, customStyle string) *yaml.Node
}
type valueParser struct {
@@ -15,9 +15,22 @@ func NewValueParser() ValueParser {
return &valueParser{}
}
func (v *valueParser) Parse(argument string, customTag string) *yaml.Node {
if argument == "[]" {
return &yaml.Node{Tag: "!!seq", Kind: yaml.SequenceNode}
func (v *valueParser) Parse(argument string, customTag string, customStyle string) *yaml.Node {
var style yaml.Style
if customStyle == "tagged" {
style = yaml.TaggedStyle
} else if customStyle == "doubleQuoted" {
style = yaml.DoubleQuotedStyle
} else if customStyle == "singleQuoted" {
style = yaml.SingleQuotedStyle
} else if customStyle == "literal" {
style = yaml.LiteralStyle
} else if customStyle == "folded" {
style = yaml.FoldedStyle
}
return &yaml.Node{Value: argument, Tag: customTag, Kind: yaml.ScalarNode}
if argument == "[]" {
return &yaml.Node{Tag: "!!seq", Kind: yaml.SequenceNode, Style: style}
}
return &yaml.Node{Value: argument, Tag: customTag, Kind: yaml.ScalarNode, Style: style}
}

View File

@@ -7,6 +7,25 @@ import (
yaml "gopkg.in/yaml.v3"
)
var parseStyleTests = []struct {
customStyle string
expectedStyle yaml.Style
}{
{"", 0},
{"tagged", yaml.TaggedStyle},
{"doubleQuoted", yaml.DoubleQuotedStyle},
{"singleQuoted", yaml.SingleQuotedStyle},
{"folded", yaml.FoldedStyle},
{"literal", yaml.LiteralStyle},
}
func TestValueParserStyleTag(t *testing.T) {
for _, tt := range parseStyleTests {
actual := NewValueParser().Parse("cat", "", tt.customStyle)
test.AssertResultWithContext(t, tt.expectedStyle, actual.Style, tt.customStyle)
}
}
var parseValueTests = []struct {
argument string
customTag string
@@ -20,7 +39,7 @@ var parseValueTests = []struct {
func TestValueParserParse(t *testing.T) {
for _, tt := range parseValueTests {
actual := NewValueParser().Parse(tt.argument, tt.customTag)
actual := NewValueParser().Parse(tt.argument, tt.customTag, "")
test.AssertResultWithContext(t, tt.argument, actual.Value, tt.testDescription)
test.AssertResultWithContext(t, tt.expectedTag, actual.Tag, tt.testDescription)
test.AssertResult(t, yaml.ScalarNode, actual.Kind)
@@ -28,7 +47,7 @@ func TestValueParserParse(t *testing.T) {
}
func TestValueParserParseEmptyArray(t *testing.T) {
actual := NewValueParser().Parse("[]", "")
actual := NewValueParser().Parse("[]", "", "")
test.AssertResult(t, "!!seq", actual.Tag)
test.AssertResult(t, yaml.SequenceNode, actual.Kind)
}