Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,16 @@ func TestEncoder(t *testing.T) {
map[string]string{"a": " b "},
nil,
},
{
"a: \"\\tb\"\n",
map[string]string{"a": "\tb"},
nil,
},
{
"a: \"b\\t\"\n",
map[string]string{"a": "b\t"},
nil,
},
{
"a: \"`b` c\"\n",
map[string]string{"a": "`b` c"},
Expand Down
4 changes: 2 additions & 2 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,12 +690,12 @@ func IsNeedQuoted(value string) bool {
}
first := value[0]
switch first {
case '*', '&', '[', '{', '}', ']', ',', '!', '|', '>', '%', '\'', '"', '@', ' ', '`':
case '*', '&', '[', '{', '}', ']', ',', '!', '|', '>', '%', '\'', '"', '@', ' ', '\t', '\n', '`':
return true
}
last := value[len(value)-1]
switch last {
case ':', ' ':
case ':', ' ', '\t':
return true
}
if isTimestamp(value) {
Expand Down
3 changes: 3 additions & 0 deletions token/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ func TestIsNeedQuoted(t *testing.T) {
" a",
" a ",
"a ",
"\ta",
"a\t",
"\na",
"null",
"Null",
"NULL",
Expand Down
18 changes: 17 additions & 1 deletion yaml_test_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"

"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/testdata/yaml-test-suite"
yamltestsuite "github.com/goccy/go-yaml/testdata/yaml-test-suite"
)

var failureTestNames = []string{
Expand Down Expand Up @@ -53,6 +53,7 @@ var failureTestNames = []string{
"invalid-comment-after-end-of-flow-sequence",
"invalid-comma-in-tag",
"plain-dashes-in-flow-sequence",
"spec-example-8-17-explicit-block-mapping-entries",
"spec-example-9-3-bare-documents",
"spec-example-9-6-stream",
"spec-example-9-6-stream-1-3",
Expand Down Expand Up @@ -122,6 +123,21 @@ func TestYAMLTestSuite(t *testing.T) {
if !bytes.Equal(expected, got) {
t.Fatalf("json mismatch [%s]:\n[expected]\n%s\n[got]\n%s\n", test.Name, string(expected), string(got))
}
marshaled, err := yaml.Marshal(v)
if err != nil {
t.Fatalf("failed to remarshal value: %v", err)
}
var v2 any
if err := yaml.Unmarshal(marshaled, &v2); err != nil {
t.Fatalf("failed to round-trip value:\n[yaml]\n%s\n[err]\n%v", marshaled, err)
}
got, err = json.Marshal(v)
if err != nil {
t.Fatalf("failed to encode json value: %v", err)
}
if !bytes.Equal(expected, got) {
t.Fatalf("round-trip json mismatch [%s]:\n[expected]\n%s\n[got]\n%s\n", test.Name, string(expected), string(got))
}
idx++
}
})
Expand Down
Loading