diff options
-rw-r--r-- | lib/psych/visitors/yaml_tree.rb | 18 | ||||
-rw-r--r-- | test/psych/test_array.rb | 6 | ||||
-rw-r--r-- | test/psych/test_scalar.rb | 5 | ||||
-rw-r--r-- | test/psych/test_string.rb | 10 | ||||
-rw-r--r-- | test/psych/visitors/test_yaml_tree.rb | 2 |
5 files changed, 25 insertions, 16 deletions
diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb index 671bf5d..cfed8f1 100644 --- a/lib/psych/visitors/yaml_tree.rb +++ b/lib/psych/visitors/yaml_tree.rb @@ -378,14 +378,18 @@ module Psych def visit_Array o if o.class == ::Array - register o, @emitter.start_sequence(nil, nil, true, Nodes::Sequence::BLOCK) - o.each { |c| accept c } - @emitter.end_sequence + visit_Enumerator o else visit_array_subclass o end end + def visit_Enumerator o + register o, @emitter.start_sequence(nil, nil, true, Nodes::Sequence::BLOCK) + o.each { |c| accept c } + @emitter.end_sequence + end + def visit_NilClass o @emitter.scalar('', nil, 'tag:yaml.org,2002:null', true, false, Nodes::Scalar::ANY) end @@ -411,15 +415,9 @@ module Psych end private - # FIXME: Remove the index and count checks in Psych 3.0 - NULL = "\x00" - BINARY_RANGE = "\x00-\x7F" - WS_RANGE = "^ -~\t\r\n" def binary? string - (string.encoding == Encoding::ASCII_8BIT && !string.ascii_only?) || - string.index(NULL) || - string.count(BINARY_RANGE, WS_RANGE).fdiv(string.length) > 0.3 + string.encoding == Encoding::ASCII_8BIT && !string.ascii_only? end def visit_array_subclass o diff --git a/test/psych/test_array.rb b/test/psych/test_array.rb index 11f5f23..f2bbdca 100644 --- a/test/psych/test_array.rb +++ b/test/psych/test_array.rb @@ -15,6 +15,12 @@ module Psych @list = [{ :a => 'b' }, 'foo'] end + def test_enumerator + x = [1, 2, 3, 4] + y = Psych.load Psych.dump x.to_enum + assert_equal x, y + end + def test_another_subclass_with_attributes y = Y.new.tap {|o| o.val = 1} y << "foo" << "bar" diff --git a/test/psych/test_scalar.rb b/test/psych/test_scalar.rb index 4e45991..e2f9ec7 100644 --- a/test/psych/test_scalar.rb +++ b/test/psych/test_scalar.rb @@ -8,5 +8,10 @@ module Psych def test_utf_8 assert_equal "日本語", Psych.load("--- 日本語") end + + def test_some_bytes # Ticket #278 + x = "\xEF\xBF\xBD\x1F" + assert_cycle x + end end end diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb index 7d14d16..973f38b 100644 --- a/test/psych/test_string.rb +++ b/test/psych/test_string.rb @@ -174,7 +174,7 @@ string: &70121654388580 !ruby/string end def test_binary_string_null - string = "\x00" + string = "\x00\x92".b yml = Psych.dump string assert_match(/binary/, yml) assert_equal string, Psych.load(yml) @@ -187,8 +187,8 @@ string: &70121654388580 !ruby/string assert_equal string, Psych.load(yml) end - def test_non_binary_string - string = binary_string(0.29) + def test_ascii_only_binary_string + string = "non bnry string".b yml = Psych.dump string refute_match(/binary/, yml) assert_equal string, Psych.load(yml) @@ -220,9 +220,9 @@ string: &70121654388580 !ruby/string end def binary_string percentage = 0.31, length = 100 - string = ''.dup + string = ''.b (percentage * length).to_i.times do |i| - string << "\b" + string << "\x92".b end string << 'a' * (length - string.length) string diff --git a/test/psych/visitors/test_yaml_tree.rb b/test/psych/visitors/test_yaml_tree.rb index 503a3e4..01f1aec 100644 --- a/test/psych/visitors/test_yaml_tree.rb +++ b/test/psych/visitors/test_yaml_tree.rb @@ -37,7 +37,7 @@ module Psych end def test_binary_formatting - gif = "GIF89a\f\x00\f\x00\x84\x00\x00\xFF\xFF\xF7\xF5\xF5\xEE\xE9\xE9\xE5fff\x00\x00\x00\xE7\xE7\xE7^^^\xF3\xF3\xED\x8E\x8E\x8E\xE0\xE0\xE0\x9F\x9F\x9F\x93\x93\x93\xA7\xA7\xA7\x9E\x9E\x9Eiiiccc\xA3\xA3\xA3\x84\x84\x84\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9!\xFE\x0EMade with GIMP\x00,\x00\x00\x00\x00\f\x00\f\x00\x00\x05, \x8E\x810\x9E\xE3@\x14\xE8i\x10\xC4\xD1\x8A\b\x1C\xCF\x80M$z\xEF\xFF0\x85p\xB8\xB01f\r\e\xCE\x01\xC3\x01\x1E\x10' \x82\n\x01\x00;" + gif = "GIF89a\f\x00\f\x00\x84\x00\x00\xFF\xFF\xF7\xF5\xF5\xEE\xE9\xE9\xE5fff\x00\x00\x00\xE7\xE7\xE7^^^\xF3\xF3\xED\x8E\x8E\x8E\xE0\xE0\xE0\x9F\x9F\x9F\x93\x93\x93\xA7\xA7\xA7\x9E\x9E\x9Eiiiccc\xA3\xA3\xA3\x84\x84\x84\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9!\xFE\x0EMade with GIMP\x00,\x00\x00\x00\x00\f\x00\f\x00\x00\x05, \x8E\x810\x9E\xE3@\x14\xE8i\x10\xC4\xD1\x8A\b\x1C\xCF\x80M$z\xEF\xFF0\x85p\xB8\xB01f\r\e\xCE\x01\xC3\x01\x1E\x10' \x82\n\x01\x00;".b @v << gif scalar = @v.tree.children.first.children.first assert_equal Psych::Nodes::Scalar::LITERAL, scalar.style |