Skip to content
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

Add ? to the list of things to quote when it's an exact value. #178

Merged
merged 6 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Support for escape and unicode characters [#98](https://github.com/ufirstgroup/ymlr/pull/98)
- Refactor `Ymlr.Encode` to make it faster.

### Fixed

- Put single quotes around '?' when it is the entire value.

<!--------------------- Don't add new entries after this line --------------------->

### Added
Expand Down
2 changes: 2 additions & 0 deletions lib/ymlr/encode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ defmodule Ymlr.Encode do
@single_quote_when_exact [
"",
"~",
"?",
"-",
mruoss marked this conversation as resolved.
Show resolved Hide resolved
"null",
"yes",
"no",
Expand Down
32 changes: 32 additions & 0 deletions test/ymlr/encode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ defmodule Ymlr.EncodeTest do
test "plain strings" do
assert_identity_and_output("hello world", "hello world")
assert_identity_and_output("that's it", "that's it")

# Strings where the ":", "?", or "-" indicator character is used as the
# first character may be encoded in Plain Style (without being quoted)
# if followed by a non-space “safe” character.
# https://yaml.org/spec/1.2.2/#733-plain-style
assert_identity_and_output("?x", ~S(?x))
assert_identity_and_output(":x", ~S(:x))
assert_identity_and_output("-x", ~S(-x))
mruoss marked this conversation as resolved.
Show resolved Hide resolved
end

# see http://blogs.perl.org/users/tinita/2018/03/strings-in-yaml---to-quote-or-not-to-quote.html
Expand Down Expand Up @@ -48,6 +56,30 @@ defmodule Ymlr.EncodeTest do
assert_identity_and_output("hello #world", ~S('hello #world'))
end

test "quoted strings - indicator characters on their own" do
assert_identity_and_output("-", ~S('-'))
assert_identity_and_output("?", ~S('?'))
assert_identity_and_output(":", ~S(':'))
assert_identity_and_output("[", ~S('['))
assert_identity_and_output("]", ~S(']'))
assert_identity_and_output("{", ~S('{'))
assert_identity_and_output("}", ~S('}'))
assert_identity_and_output("#", ~S('#'))
assert_identity_and_output("&", ~S('&'))
assert_identity_and_output("&", ~S('&'))
assert_identity_and_output("*", ~S('*'))
assert_identity_and_output("*", ~S('*'))
assert_identity_and_output("!", ~S('!'))
assert_identity_and_output("!", ~S('!'))
assert_identity_and_output("|", ~S('|'))
assert_identity_and_output("|", ~S('|'))
assert_identity_and_output(">", ~S('>'))
assert_identity_and_output(">", ~S('>'))
assert_identity_and_output("%", ~S(%))
assert_identity_and_output("@", ~S('@'))
assert_identity_and_output("`", ~S('`'))
end

test "quoted strings - starts with special char" do
assert_identity_and_output("!tag", ~S('!tag'))
assert_identity_and_output("&anchor", ~S('&anchor'))
Expand Down
Loading