dart-lang / yaml_edit

A library for YAML manipulation with comment and whitespace preservation.
https://pub.dev/packages/yaml_edit
BSD 3-Clause "New" or "Revised" License
27 stars 16 forks source link

Code coverage for `>+` #44

Closed jonasfj closed 2 months ago

jonasfj commented 1 year ago

We have no test that exercise this code: https://coveralls.io/builds/57565406/source?filename=lib%2Fsrc%2Fstrings.dart#L103

That might be a good idea to have some tests for. Maybe there is bugs?

MikiPAUL commented 1 year ago

Hi @jonasfj, I think this code won't execute under any circumstances. Because the string has leading or trailing spaces, it is interpreted as PLAIN. https://github.com/dart-lang/yaml_edit/blob/aaa1d53590d4ae3b70b2529bfec533df7c0abd9b/lib/src/strings.dart#L183-L197

jonasfj commented 1 year ago

Hmm, that might make sense. I guess it's rare that something starts with whitespace -- and in such circumstances it might be simpler to just fallback to a safer string style.

I wonder if we should refactor the string handling. I probably don't have too good an overview.

Perhaps a good start is to write down what string styles we have, where they can be used, and what limits they have.

Some strings styles can't represent any string.

setcy commented 1 year ago

I think if a string does not contain special characters, it can be representable as both a folded string and a literal string.

For strings that begin with spaces or line breaks, we need to use block styles with indication indicators to implement it. By using this method, even a single space can be represented.

Therefore, if we improve _tryYamlEncodeFolded and _tryYamlEncodeLiteral, the sentence if (value.value.trim().length == value.value.length && value.value.length != 0) will be unnecessary.

However, cases where strings begin with spaces or line breaks are relatively rare, and some interpreters may parse them incorrectly. If considering a safer approach, falling back to a more secure string style will never be wrong.