Skip to content

Commit

Permalink
add failing multiline tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mruoss committed Nov 17, 2023
1 parent abb41dd commit 3ca629e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/ymlr/encode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ defmodule Ymlr.Encode do

@escape_chars ~c"\b\f\r\v\0\"\\"
@escape_char_mapping Enum.zip(@escape_chars, ~c"bfrv0\"\\")
@unicode_char_mapping (Enum.to_list(0x00..0x1F) ++ Enum.to_list(0x7F..0xFF)) |> Enum.reject(&Kernel.in(&1, '\n\t' ++ @escape_chars))
@require_double_quotes Enum.map(~c"\b\f\r\v\0"++@unicode_char_mapping, &(<< &1 >>)) |>dbg
@unicode_chars Enum.to_list(0x00..0x1F) ++ Enum.to_list(0x7F..0xFF)
@unicode_char_mapping Enum.reject(@unicode_chars, &Kernel.in(&1, ~c"\n\t" ++ @escape_chars))
@require_double_quotes Enum.map(~c"\b\f\r\v\0" ++ @unicode_char_mapping, &<<&1>>)

@doc ~S"""
Encodes the given data as YAML string. Raises if it cannot be encoded.
Expand Down Expand Up @@ -198,20 +199,21 @@ defmodule Ymlr.Encode do
with_single_quotes(data)
end
end

defp with_double_quotes(data) do
~s("#{escape(data)}")
end

defp with_single_quotes(data), do: ~s('#{data}')

defp escape(data) do
for << char::utf8 <- data >> do
for <<char::utf8 <- data>> do
escape_char(char)
end
end

for {char, escaped} <- @escape_char_mapping do
defp escape_char(unquote(char)), do: << ?\\, unquote(escaped) >>
defp escape_char(unquote(char)), do: <<?\\, unquote(escaped)>>
end

for uchar <- @unicode_char_mapping do
Expand Down
7 changes: 7 additions & 0 deletions test/ymlr/encode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,12 @@ defmodule Ymlr.EncodeTest do
test "quoted strings - example-escaped-characters from 1.2.2 spec" do
assert_identity_and_output("Fun with \\", "Fun with \\")
assert_identity_and_output("\r \t \u000b \u0000", "\"\\r \t \\v \\0\"")

assert_identity_and_output(
"\u0020 \u00a0 \u0085 \u2028 \u2029",
"\" \\u00A0 \\u0085 \u2028 \u2029\""
)

assert_identity_and_output("\" \u0007 \b \u001b \f", "\"\\\" \\u0007 \\b \\u001B \\f\"")
assert_identity_and_output("\r \t \u000b \u0000", "\"\\r \t \\v \\0\"")
end
Expand Down Expand Up @@ -313,6 +315,11 @@ defmodule Ymlr.EncodeTest do
end

# see https://yaml-multiline.info/
@tag skip: "still buggy"
test "multiline strings - starting with spaces" do
assert_identity_and_output("\n abc", "|-\n\n abc")
assert_identity_and_output(" abc\nabc", "|-\n abc\n abc")
end

test "multiline strings - base cases" do
assert_identity_and_output("a\n b\nc", "|-\n a\n b\n c")
Expand Down

0 comments on commit 3ca629e

Please sign in to comment.