diff options
-rw-r--r-- | lib/psych/visitors/yaml_tree.rb | 8 | ||||
-rw-r--r-- | test/psych/test_psych.rb | 12 |
2 files changed, 19 insertions, 1 deletions
diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb index 63e14da..6ad30d2 100644 --- a/lib/psych/visitors/yaml_tree.rb +++ b/lib/psych/visitors/yaml_tree.rb @@ -70,6 +70,14 @@ module Psych @ss = ss @options = options @line_width = options[:line_width] + if @line_width && @line_width < 0 + if @line_width == -1 + # Treat -1 as unlimited line-width, same as libyaml does. + @line_width = nil + else + fail(ArgumentError, "Invalid line_width #{@line_width}, must be non-negative or -1 for unlimited.") + end + end @coders = [] @dispatch_cache = Hash.new do |h,klass| diff --git a/test/psych/test_psych.rb b/test/psych/test_psych.rb index 8054bd6..05a9343 100644 --- a/test/psych/test_psych.rb +++ b/test/psych/test_psych.rb @@ -8,7 +8,17 @@ class TestPsych < Psych::TestCase Psych.domain_types.clear end - def test_line_width + def test_line_width_invalid + assert_raises(ArgumentError) { Psych.dump('x', { :line_width => -2 }) } + end + + def test_line_width_no_limit + data = { 'a' => 'a b' * 50} + expected = "---\na: #{'a b' * 50}\n" + assert_equal(expected, Psych.dump(data, { :line_width => -1 })) + end + + def test_line_width_limit yml = Psych.dump('123456 7', { :line_width => 5 }) assert_match(/^\s*7/, yml) end |