summaryrefslogtreecommitdiff
path: root/test/psych
diff options
context:
space:
mode:
Diffstat (limited to 'test/psych')
-rw-r--r--test/psych/test_data.rb26
-rw-r--r--test/psych/test_parser.rb42
-rw-r--r--test/psych/test_scalar_scanner.rb5
-rw-r--r--test/psych/visitors/test_to_ruby.rb6
4 files changed, 78 insertions, 1 deletions
diff --git a/test/psych/test_data.rb b/test/psych/test_data.rb
index a67a037b9e..5e340c580a 100644
--- a/test/psych/test_data.rb
+++ b/test/psych/test_data.rb
@@ -64,6 +64,30 @@ module Psych
assert_equal "hello", obj.bar
assert_equal "bar", obj.foo
end
+
+ def test_members_must_be_identical
+ TestData.const_set :D, Data.define(:a, :b)
+ d = Psych.dump(TestData::D.new(1, 2))
+
+ # more members
+ TestData.send :remove_const, :D
+ TestData.const_set :D, Data.define(:a, :b, :c)
+ e = assert_raise(ArgumentError) { Psych.unsafe_load d }
+ assert_equal 'missing keyword: :c', e.message
+
+ # less members
+ TestData.send :remove_const, :D
+ TestData.const_set :D, Data.define(:a)
+ e = assert_raise(ArgumentError) { Psych.unsafe_load d }
+ assert_equal 'unknown keyword: :b', e.message
+
+ # completely different members
+ TestData.send :remove_const, :D
+ TestData.const_set :D, Data.define(:a, :c)
+ e = assert_raise(ArgumentError) { Psych.unsafe_load d }
+ assert_include e.message, 'keyword:'
+ ensure
+ TestData.send :remove_const, :D
+ end
end
end
-
diff --git a/test/psych/test_parser.rb b/test/psych/test_parser.rb
index c1e0abb89d..4ca4d63d80 100644
--- a/test/psych/test_parser.rb
+++ b/test/psych/test_parser.rb
@@ -198,6 +198,48 @@ module Psych
assert_called :end_stream
end
+ def test_parse_io_returns_more_bytes_than_requested
+ # An IO-like source whose #read returns more bytes than the size it was
+ # asked for must not overflow libyaml's read buffer.
+ io = Object.new
+ def io.external_encoding; Encoding::UTF_8 end
+ def io.read len
+ return nil if @done
+ @done = true
+ "--- a\n" + ("#" * (len + (1 << 20)))
+ end
+
+ # CRuby clamps the over-read and parses; JRuby's parser rejects the
+ # over-reading IO with an IOError. Either way there is no overflow.
+ begin
+ @parser.parse io
+ rescue IOError
+ return
+ end
+ assert_called :start_stream
+ assert_called :scalar
+ assert_called :end_stream
+ end
+
+ def test_parse_io_returns_more_bytes_than_requested_multibyte
+ # The over-read is rounded down to a character boundary so a multibyte
+ # character is never split when the copy is clamped.
+ io = Object.new
+ def io.external_encoding; Encoding::UTF_8 end
+ def io.read len
+ return nil if @done
+ @done = true
+ "--- a\n#" + ("あ" * (len + (1 << 20)))
+ end
+
+ begin
+ @parser.parse io
+ rescue IOError
+ return
+ end
+ assert_called :scalar
+ end
+
def test_syntax_error
assert_raise(Psych::SyntaxError) do
@parser.parse("---\n\"foo\"\n\"bar\"\n")
diff --git a/test/psych/test_scalar_scanner.rb b/test/psych/test_scalar_scanner.rb
index 2637a74df8..bc6a74ad8b 100644
--- a/test/psych/test_scalar_scanner.rb
+++ b/test/psych/test_scalar_scanner.rb
@@ -138,6 +138,11 @@ module Psych
assert_equal '-0b___', scanner.tokenize('-0b___')
end
+ def test_scan_without_parse_symbols
+ scanner = Psych::ScalarScanner.new ClassLoader.new, parse_symbols: false
+ assert_equal ':foo', scanner.tokenize(':foo')
+ end
+
def test_scan_int_commas_and_underscores
# NB: This test is to ensure backward compatibility with prior Psych versions,
# not to test against any actual YAML specification.
diff --git a/test/psych/visitors/test_to_ruby.rb b/test/psych/visitors/test_to_ruby.rb
index 89c3676651..c9b501dfa2 100644
--- a/test/psych/visitors/test_to_ruby.rb
+++ b/test/psych/visitors/test_to_ruby.rb
@@ -328,6 +328,12 @@ description:
mapping.children << Nodes::Scalar.new('bar')
assert_equal({'foo' => 'bar'}, mapping.to_ruby)
end
+
+ def test_parse_symbols
+ node = Nodes::Scalar.new(':foo')
+ assert_equal :foo, node.to_ruby
+ assert_equal ':foo', node.to_ruby(parse_symbols: false)
+ end
end
end
end