Skip to content

Commit

Permalink
new failing tests for char escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
jlgeering committed Aug 26, 2022
1 parent a730ebc commit 38c5d22
Showing 1 changed file with 49 additions and 22 deletions.
71 changes: 49 additions & 22 deletions lib/ymlr/encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ defmodule Ymlr.EncoderTest do
assert MUT.to_s!("") == "''"
end

test "simple string" do
test "plain strings" do
assert MUT.to_s!("hello world") == "hello world"
assert MUT.to_s!("that's it") == "that's it"
end

# see http://blogs.perl.org/users/tinita/2018/03/strings-in-yaml---to-quote-or-not-to-quote.html
Expand Down Expand Up @@ -71,11 +72,6 @@ defmodule Ymlr.EncoderTest do
assert MUT.to_s!("some:entry:") == ~S('some:entry:')
end

test "quoted strings - escape seq forces double quotes (tab char)" do
assert MUT.to_s!("a\tb") == ~s("a\tb")
assert MUT.to_s!("!a\tb") == ~s("!a\tb")
end

test "quoted strings - listy and mappy things" do
# ... (prefer single quotes)
assert MUT.to_s!("[]") == ~S('[]')
Expand All @@ -84,7 +80,7 @@ defmodule Ymlr.EncoderTest do
assert MUT.to_s!("{}") == ~S('{}')
assert MUT.to_s!("[{}]") == ~S('[{}]')
# ... (use double quotes if string contains single quotes)
assert MUT.to_s!(~S(["I don't know!\nRea|\y?"])) == ~S("[\"I don't know!\\nRea|\\y?\"]")
assert MUT.to_s!(~S(["I don't know!\nRea|\y?"])) == ~S("[\"I don't know!\nRea|\\y?\"]")
end

test "quoted strings - in map key" do
Expand All @@ -93,25 +89,40 @@ defmodule Ymlr.EncoderTest do
assert MUT.to_s!(%{"true" => "value"}) == ~S('true': value)
end

@tag skip: "not sure about those => to be reviewed"
# https://yaml.org/spec/1.2.2/#example-escaped-characters
test "quoted strings - example-escaped-characters from 1.2.2 spec" do
assert MUT.to_s!("Fun with \\") == ~S("Fun with \\")
assert MUT.to_s!("\" \u0007 \b \u001b \f") == ~S("\" \a \b \e \f")
# assert MUT.to_s!("\n \r \t \u000b \u0000") == ~S("\n \r \t \v \0")
# or we use | when string contains newlines => rewrite the example to:
assert MUT.to_s!("\r \t \u000b \u0000") == ~S("\r \t \v \0")
assert MUT.to_s!("\u0020 \u00a0 \u0085 \u2028 \u2029") == ~S("\ \_ \N \L \P")
test "backslash" do
# in plain string
assert MUT.to_s!(~S(a\b)) == ~S(a\b)
# in single quote string
assert MUT.to_s!(~S(!a\b)) == ~S('!a\b')
# double quotes because of single quote
assert MUT.to_s!(~s(!a'b\\c)) == ~S("!a'b\\c")
# double quotes because of tab
assert MUT.to_s!(~s(a\tb\\c)) == ~S("a\tbc\\c")
end

@tag skip: "not sure about those => review the spec"
test "quoted strings - in map key (requires escape char)" do
assert MUT.to_s!(%{"a\tb" => "value"}) == ~s("a\tb": value)
assert MUT.to_s!(%{"a\rb" => "value"}) == ~s("a\rb": value)
test "backslash in map key" do
# in plain string
assert MUT.to_s!(%{~S(a\b) => "value"}) == ~S(a\b: value)
# in single quote string
assert MUT.to_s!(%{~S(!a\b) => "value"}) == ~S('!a\b': value)
# double quotes because of single quote
assert MUT.to_s!(%{~s(a'b\\c) => "value"}) == ~S("a'b\\c": value)
# double quotes because of tab
assert MUT.to_s!(%{~s(a\tb\\c) => "value"}) == ~S("a\tbc\\c": value)
end

test "newline in map key" do
assert MUT.to_s!(%{"a\nb" => "value"}) == ~S("a\nb": value)
test "tab" do
# would be plain string without the tab
assert MUT.to_s!("a\tb") == ~S("a\tb")
# would be single quoted string without the tab
assert MUT.to_s!("!a\tb") == ~S("!a\tb")
end

test "tab in map key" do
# would be plain string without the tab
assert MUT.to_s!(%{"a\tb" => "value"}) == ~S("a\tb": value)
# would be single quoted string without the tab
assert MUT.to_s!(%{"!a\tb" => "value"}) == ~S("!a\tb": value)
end

test "integers" do
Expand Down Expand Up @@ -217,6 +228,7 @@ defmodule Ymlr.EncoderTest do
assert MUT.to_s!("hello\nworld") == "|-\nhello\nworld"
assert MUT.to_s!("hello\nworld\n") == "|\nhello\nworld"
end

# see https://yaml.org/spec/1.2.2/#example-tabs-and-spaces
test "multiline strings - mix spaces and tabs" do
given = "void main() {\n\tprintf(\"Hello, world!\\n\");\n}\n"
Expand All @@ -228,9 +240,11 @@ defmodule Ymlr.EncoderTest do
""" |> String.trim()
assert MUT.to_s!(%{block: given}) == expected
end

test "nested: list / multiline string" do
assert MUT.to_s!(["a\nb\n", "c"]) == "- |\n a\n b\n- c"
end

test "nested: map / multiline string" do
expected = """
a: |-
Expand All @@ -246,6 +260,19 @@ defmodule Ymlr.EncoderTest do
assert MUT.to_s!(%{a: "a1\na2", b: "b1", c: "c1\nc2\n", d: "d1"}) == expected
end

@tag :skip
test "tab(s) and newline(s) in the same string" do
assert MUT.to_s!("a\tb\nc") == ~s("a\\tb\nc")
# with extra whitespaces around the newline
assert MUT.to_s!("a\tb \n c") == ~S("a\tb \n c")
# with backslash
assert MUT.to_s!(~s(a\tb\nc\\w)) == ~s("a\\tb\nc\\\\w")
end

test "newline in map key" do
assert MUT.to_s!(%{"a\nb" => "value"}) == ~S("a\nb": value)
end

test "date" do
assert MUT.to_s!(~D[2016-05-24]) == "2016-05-24"
end
Expand Down

0 comments on commit 38c5d22

Please sign in to comment.