Skip to content

Commit 6b135a4

Browse files
committed
Add test for no ending newline
1 parent ae14d3f commit 6b135a4

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/BlockScalars.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ function block(str::AbstractString, block_scalar::AbstractString="")
5656
end
5757

5858
if chomp == 'c'
59-
str = rstrip(str, '\n') * '\n'
59+
suffix = endswith(str, '\n') ? "\n" : ""
60+
str = rstrip(str, '\n') * suffix
6061
elseif chomp == 's'
6162
str = rstrip(str, '\n')
6263
elseif chomp != 'k'

test/runtests.jl

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,28 @@ function yaml_block(str, block_scalar)
1010
end
1111

1212
# https://yaml-multiline.info/
13-
const TEXT = """
14-
Several lines of text,
15-
with some "quotes" of various 'types'
16-
and also two blank lines:
13+
const TEST_STRINGS = [
14+
"paragraph" => """
15+
Several lines of text,
16+
with some "quotes" of various 'types'
17+
and also two blank lines:
1718
1819
19-
plus another line at the end.
20+
plus another line at the end.
2021
2122
22-
"""
23-
24-
const WHITESPACE = "a\nb\n c \nd\n"
23+
""",
24+
"whitespace" => "a\nb\n c \nd\n",
25+
"no ending newline" => "foo",
26+
]
2527

2628
# Validate `yaml_block` function
27-
@assert yaml_block(TEXT, "|+") == TEXT
28-
@assert yaml_block(TEXT, "|+") == TEXT
29+
for (test, str) in TEST_STRINGS
30+
@assert yaml_block(str, "|+") == str
31+
end
2932

3033
@testset "BlockScalars.jl" begin
31-
@testset "block: $name" for (name, str) in ("TEXT" => TEXT, "WHITESPACE" => WHITESPACE)
34+
@testset "block: $test" for (test, str) in TEST_STRINGS
3235
@testset "literal" begin
3336
@test block(str, "lk") == yaml_block(str, "|+")
3437
@test block(str, "lc") == yaml_block(str, "|")

0 commit comments

Comments
 (0)