2019-12-03 04:50:32 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-12-09 02:44:53 +00:00
|
|
|
"github.com/mikefarah/yq/v3/test"
|
2020-01-08 21:17:56 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2019-12-03 04:50:32 +00:00
|
|
|
)
|
|
|
|
|
2020-04-17 01:03:43 +00:00
|
|
|
var parseStyleTests = []struct {
|
|
|
|
customStyle string
|
|
|
|
expectedStyle yaml.Style
|
|
|
|
}{
|
|
|
|
{"", 0},
|
|
|
|
{"tagged", yaml.TaggedStyle},
|
2020-04-17 01:24:45 +00:00
|
|
|
{"double", yaml.DoubleQuotedStyle},
|
|
|
|
{"single", yaml.SingleQuotedStyle},
|
2020-04-17 01:03:43 +00:00
|
|
|
{"folded", yaml.FoldedStyle},
|
2020-06-10 23:58:10 +00:00
|
|
|
{"flow", yaml.FlowStyle},
|
2020-04-17 01:03:43 +00:00
|
|
|
{"literal", yaml.LiteralStyle},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValueParserStyleTag(t *testing.T) {
|
|
|
|
for _, tt := range parseStyleTests {
|
2020-06-11 03:57:13 +00:00
|
|
|
actual := NewValueParser().Parse("cat", "", tt.customStyle, "", false)
|
2020-04-17 01:03:43 +00:00
|
|
|
test.AssertResultWithContext(t, tt.expectedStyle, actual.Style, tt.customStyle)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 04:50:32 +00:00
|
|
|
var parseValueTests = []struct {
|
|
|
|
argument string
|
2020-01-08 21:17:56 +00:00
|
|
|
customTag string
|
|
|
|
expectedTag string
|
2019-12-03 04:50:32 +00:00
|
|
|
testDescription string
|
|
|
|
}{
|
2020-01-10 11:01:59 +00:00
|
|
|
{"true", "!!str", "!!str", "boolean forced as string"},
|
2020-01-19 21:42:08 +00:00
|
|
|
{"3", "!!int", "!!int", "int"},
|
|
|
|
{"cat", "", "", "default"},
|
2019-12-03 04:50:32 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 21:17:56 +00:00
|
|
|
func TestValueParserParse(t *testing.T) {
|
2019-12-03 04:50:32 +00:00
|
|
|
for _, tt := range parseValueTests {
|
2020-06-11 03:57:13 +00:00
|
|
|
actual := NewValueParser().Parse(tt.argument, tt.customTag, "", "", false)
|
2020-01-08 21:17:56 +00:00
|
|
|
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)
|
2019-12-03 04:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-08 21:17:56 +00:00
|
|
|
|
|
|
|
func TestValueParserParseEmptyArray(t *testing.T) {
|
2020-06-11 03:57:13 +00:00
|
|
|
actual := NewValueParser().Parse("[]", "", "", "", false)
|
2020-01-08 21:17:56 +00:00
|
|
|
test.AssertResult(t, "!!seq", actual.Tag)
|
|
|
|
test.AssertResult(t, yaml.SequenceNode, actual.Kind)
|
|
|
|
}
|
2020-06-11 03:57:13 +00:00
|
|
|
|
|
|
|
func TestValueParserParseAlias(t *testing.T) {
|
|
|
|
actual := NewValueParser().Parse("bob", "", "", "", true)
|
|
|
|
test.AssertResult(t, "bob", actual.Value)
|
|
|
|
test.AssertResult(t, yaml.AliasNode, actual.Kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValueParserAnchorname(t *testing.T) {
|
|
|
|
actual := NewValueParser().Parse("caterpillar", "", "", "foo", false)
|
|
|
|
test.AssertResult(t, "foo", actual.Anchor)
|
|
|
|
}
|