diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2015-01-13 12:19:10 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2015-01-13 12:19:10 -0800 |
commit | 47adf879591570daebde62895b3712c216710661 (patch) | |
tree | eeb76f7ca4855dd9cc9743439c142b60ec294da8 | |
parent | 24055a152492123b7516e9b4e54af5710ffed247 (diff) | |
parent | af668b4c83692ca8556d0aed97a3c542f7176e75 (diff) | |
download | psych-47adf879591570daebde62895b3712c216710661.tar.gz |
Merge pull request #215 from jirutka/patch-2
Use appropriate style for serialized strings (replaces #195)
-rw-r--r-- | lib/psych/visitors/yaml_tree.rb | 32 | ||||
-rw-r--r-- | test/psych/test_string.rb | 50 |
2 files changed, 64 insertions, 18 deletions
diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb index 3a6bd1d..f6427ce 100644 --- a/lib/psych/visitors/yaml_tree.rb +++ b/lib/psych/visitors/yaml_tree.rb @@ -62,13 +62,14 @@ module Psych def initialize emitter, ss, options super() - @started = false - @finished = false - @emitter = emitter - @st = Registrar.new - @ss = ss - @options = options - @coders = [] + @started = false + @finished = false + @emitter = emitter + @st = Registrar.new + @ss = ss + @options = options + @line_width = options[:line_width] + @coders = [] @dispatch_cache = Hash.new do |h,klass| method = "visit_#{(klass.name || '').split('::').join('_')}" @@ -301,28 +302,27 @@ module Psych quote = true style = Nodes::Scalar::PLAIN tag = nil - str = o if binary?(o) - str = [o].pack('m').chomp + o = [o].pack('m').chomp tag = '!binary' # FIXME: change to below when syck is removed #tag = 'tag:yaml.org,2002:binary' style = Nodes::Scalar::LITERAL plain = false quote = false - elsif o =~ /\n/ + elsif o =~ /\n[^\Z]/ # match \n except blank line at the end of string style = Nodes::Scalar::LITERAL elsif o == '<<' style = Nodes::Scalar::SINGLE_QUOTED tag = 'tag:yaml.org,2002:str' plain = false quote = false + elsif @line_width && o.length > @line_width + style = Nodes::Scalar::FOLDED elsif o =~ /^[^[:word:]][^"]*$/ style = Nodes::Scalar::DOUBLE_QUOTED - else - unless String === @ss.tokenize(o) - style = Nodes::Scalar::SINGLE_QUOTED - end + elsif not String === @ss.tokenize(o) + style = Nodes::Scalar::SINGLE_QUOTED end ivars = find_ivars o @@ -333,14 +333,14 @@ module Psych plain = false quote = false end - @emitter.scalar str, nil, tag, plain, quote, style + @emitter.scalar o, nil, tag, plain, quote, style else maptag = '!ruby/string' maptag << ":#{o.class}" unless o.class == ::String register o, @emitter.start_mapping(nil, maptag, false, Nodes::Mapping::BLOCK) @emitter.scalar 'str', nil, nil, true, false, Nodes::Scalar::ANY - @emitter.scalar str, nil, tag, plain, quote, style + @emitter.scalar o, nil, tag, plain, quote, style dump_ivars o diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb index 26a4e20..df8fb98 100644 --- a/test/psych/test_string.rb +++ b/test/psych/test_string.rb @@ -30,8 +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_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_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/, yaml + assert_equal str, Psych.load(yaml) + end + + 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 + + # 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 |