-
Notifications
You must be signed in to change notification settings - Fork 72
Quote multiline scalars that start with tabs #388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2984,29 +2984,39 @@ func TestScalarStyleWithTabs(t *testing.T) { | |||||
| }, | ||||||
| { | ||||||
| "\tThis starts with tab\nand is long enough\nfor literal style", | ||||||
| "|-\n \tThis starts with tab\n and is long enough\n for literal style\n", | ||||||
| "\"\\tThis starts with tab\\nand is long enough\\nfor literal style\"\n", | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you could use string literal (here and everywhere) to improve readability
Suggested change
|
||||||
| "Multiline starting with tab", | ||||||
| }, | ||||||
| { | ||||||
| "\tB\n\tC\n", | ||||||
| "|\n \tB\n \tC\n", | ||||||
| "\"\\tB\\n\\tC\\n\"\n", | ||||||
| "Tab B newline tab C newline", | ||||||
| }, | ||||||
| { | ||||||
| "\ta\n", | ||||||
| "|\n \ta\n", | ||||||
| "\"\\ta\\n\"\n", | ||||||
| "Tab + char + newline", | ||||||
| }, | ||||||
| { | ||||||
| "\thello\n", | ||||||
| "|\n \thello\n", | ||||||
| "\"\\thello\\n\"\n", | ||||||
| "Tab + text + newline", | ||||||
| }, | ||||||
| { | ||||||
| "\t\nhello", | ||||||
| "|-\n \t\n hello\n", | ||||||
| "\"\\t\\nhello\"\n", | ||||||
| "Tab + newline + text", | ||||||
| }, | ||||||
| { | ||||||
| "\n\tthis\nnext", | ||||||
| "\"\\n\\tthis\\nnext\"\n", | ||||||
| "Tab after initial newline", | ||||||
| }, | ||||||
| { | ||||||
| "first\n\tsecond", | ||||||
| "|-\n first\n \tsecond\n", | ||||||
| "Tab after content line", | ||||||
| }, | ||||||
| } | ||||||
|
|
||||||
| for i, testCase := range testCases { | ||||||
|
|
@@ -3018,6 +3028,53 @@ func TestScalarStyleWithTabs(t *testing.T) { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| func TestLeadingTabScalarRoundTrip(t *testing.T) { | ||||||
| testCases := []struct { | ||||||
| value string | ||||||
| mapYAML string | ||||||
| scalarYAML string | ||||||
| }{ | ||||||
| { | ||||||
| "\tthis\nis\nmultiline", | ||||||
| "text: \"\\tthis\\nis\\nmultiline\"\n", | ||||||
| "\"\\tthis\\nis\\nmultiline\"\n", | ||||||
| }, | ||||||
| { | ||||||
| "\n\tthis\nnext", | ||||||
| "text: \"\\n\\tthis\\nnext\"\n", | ||||||
| "\"\\n\\tthis\\nnext\"\n", | ||||||
| }, | ||||||
| { | ||||||
| "first\n\tsecond", | ||||||
| "text: |-\n first\n \tsecond\n", | ||||||
| "|-\n first\n \tsecond\n", | ||||||
| }, | ||||||
| } | ||||||
|
|
||||||
| for _, testCase := range testCases { | ||||||
| wantMap := map[string]string{"text": testCase.value} | ||||||
| data, err := yaml.Marshal(wantMap) | ||||||
| assert.NoError(t, err) | ||||||
| assert.Equal(t, testCase.mapYAML, string(data)) | ||||||
|
|
||||||
| var gotMap map[string]string | ||||||
| err = yaml.Unmarshal(data, &gotMap) | ||||||
| assert.NoError(t, err) | ||||||
| assert.DeepEqual(t, wantMap, gotMap) | ||||||
|
|
||||||
| var node yaml.Node | ||||||
| node.SetString(testCase.value) | ||||||
| data, err = yaml.Marshal(&node) | ||||||
| assert.NoError(t, err) | ||||||
| assert.Equal(t, testCase.scalarYAML, string(data)) | ||||||
|
|
||||||
| var gotScalar string | ||||||
| err = yaml.Unmarshal(data, &gotScalar) | ||||||
| assert.NoError(t, err) | ||||||
| assert.Equal(t, testCase.value, gotScalar) | ||||||
| } | ||||||
| } | ||||||
|
Comment on lines
+3031
to
+3076
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we have something in the test suite that only uses .yaml files to handle this round trip check. I think it's in emitter.yaml Could you please check? Thanks |
||||||
|
|
||||||
| func TestUnicodeWhitespaceHandling(t *testing.T) { | ||||||
| // Test cases for Unicode whitespace characters that should be properly handled | ||||||
| // by the shouldUseLiteralStyle function using unicode.IsSpace() | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm surprised to see the Unicode separators in addition of
\r\nCould you cover this with tests?
Also I would like to see these Unicode character to constants with clear name and comment.
I'm a bit surprised by the fact it trims everything. It could have consequences for multi line maybe.
Something like \n\n\n\t
Maybe I'm wrong.
I feel like the issue you try to fix is about having some characters in front of \t
I feel like using strings.HasPrefix with iterative checks such as
\r\n\t,\n\t,\uxxxx\t