summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jirutka <jakub@jirutka.cz>2015-01-13 01:25:09 +0100
committerJakub Jirutka <jakub@jirutka.cz>2015-01-13 01:25:09 +0100
commitaf668b4c83692ca8556d0aed97a3c542f7176e75 (patch)
treeeeb76f7ca4855dd9cc9743439c142b60ec294da8
parent9a811d65852fa3fd556c475652c321263d148edd (diff)
downloadpsych-af668b4c83692ca8556d0aed97a3c542f7176e75.tar.gz
Fix block chomping and add more tests
When no [chomping indicator][1] is specified for a folded or literal block, then YAML parser should preserve the final line break (i.e. the string should end with \n). This implies that when dumping a string *without* the trailing newline to YAML, we should specify the stripping indicator (-). [1]: http://yaml.org/spec/1.2/2009-07-21/spec.html#id2593651
-rw-r--r--lib/psych/visitors/yaml_tree.rb1
-rw-r--r--test/psych/test_string.rb38
2 files changed, 30 insertions, 9 deletions
diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb
index ba1ac13..f6427ce 100644
--- a/lib/psych/visitors/yaml_tree.rb
+++ b/lib/psych/visitors/yaml_tree.rb
@@ -319,7 +319,6 @@ module Psych
quote = false
elsif @line_width && o.length > @line_width
style = Nodes::Scalar::FOLDED
- o += "\n" unless o =~ /\n\Z/ # to avoid non-default chomping indicator
elsif o =~ /^[^[:word:]][^"]*$/
style = Nodes::Scalar::DOUBLE_QUOTED
elsif not String === @ss.tokenize(o)
diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb
index 3188b5c..df8fb98 100644
--- a/test/psych/test_string.rb
+++ b/test/psych/test_string.rb
@@ -30,32 +30,54 @@ module Psych
end
def test_doublequotes_when_there_is_a_single
- yaml = Psych.dump "@123'abc"
- assert_match(/---\s*"/, yaml)
+ str = "@123'abc"
+ yaml = Psych.dump str
+ assert_match /---\s*"/, yaml
+ assert_equal str, Psych.load(yaml)
end
- def test_plain_when_shorten_than_line_width
+ def test_plain_when_shorten_than_line_width_and_no_final_line_break
str = "Lorem ipsum"
yaml = Psych.dump str, line_width: 12
assert_match /---\s*[^>|]+\n/, yaml
+ assert_equal str, Psych.load(yaml)
end
- def test_folded_when_longer_than_line_width_and_no_newlines
- str = "Lorem ipsum dolor sit amet, consectetur"
+ def test_plain_when_shorten_than_line_width_and_with_final_line_break
+ str = "Lorem ipsum\n"
yaml = Psych.dump str, line_width: 12
- assert_match /---\s*>\n(.*\n){3}\Z/, yaml
+ assert_match /---\s*[^>|]+\n/, yaml
+ assert_equal str, Psych.load(yaml)
end
- def test_folded_when_longer_than_line_width_and_trailing_newline
+ def test_folded_when_longer_than_line_width_and_with_final_line_break
str = "Lorem ipsum dolor sit\n"
yaml = Psych.dump str, line_width: 12
assert_match /---\s*>\n(.*\n){2}\Z/, yaml
+ assert_equal str, Psych.load(yaml)
end
- def test_literal_when_inner_newline
+ # http://yaml.org/spec/1.2/2009-07-21/spec.html#id2593651
+ def test_folded_strip_when_longer_than_line_width_and_no_newlines
+ str = "Lorem ipsum dolor sit amet, consectetur"
+ yaml = Psych.dump str, line_width: 12
+ assert_match /---\s*>-\n(.*\n){3}\Z/, yaml
+ assert_equal str, Psych.load(yaml)
+ end
+
+ def test_literal_when_inner_and_final_line_break
str = "Lorem ipsum\ndolor\n"
yaml = Psych.dump str, line_width: 12
assert_match /---\s*|\n(.*\n){2}\Z/, yaml
+ assert_equal str, Psych.load(yaml)
+ end
+
+ # http://yaml.org/spec/1.2/2009-07-21/spec.html#id2593651
+ def test_literal_strip_when_inner_line_break_and_no_final_line_break
+ str = "Lorem ipsum\ndolor"
+ yaml = Psych.dump str, line_width: 12
+ assert_match /---\s*|-\n(.*\n){2}\Z/, yaml
+ assert_equal str, Psych.load(yaml)
end
def test_cycle_x