diff --git a/core/marshal.rbs b/core/marshal.rbs index 6e162d1e9..552def479 100644 --- a/core/marshal.rbs +++ b/core/marshal.rbs @@ -108,6 +108,16 @@ # which is Marshal.loaded in _load for complex objects. # module Marshal + # + # major version + # + MAJOR_VERSION: Integer + + # + # minor version + # + MINOR_VERSION: Integer + # -# major version -# -Marshal::MAJOR_VERSION: Integer - -# -# minor version -# -Marshal::MINOR_VERSION: Integer diff --git a/test/stdlib/Marshal_test.rb b/test/stdlib/Marshal_test.rb index d45dbe515..bfd626f10 100644 --- a/test/stdlib/Marshal_test.rb +++ b/test/stdlib/Marshal_test.rb @@ -6,51 +6,105 @@ class MarshalSingletonTest < Test::Unit::TestCase include TypeAssertions testing "singleton(::Marshal)" + def test_MAJOR_VERSION + assert_const_type 'Integer', 'Marshal::MAJOR_VERSION' + end + + def test_MINOR_VERSION + assert_const_type 'Integer', 'Marshal::MINOR_VERSION' + end + def test_dump - assert_send_type "(::String) -> ::String", - Marshal, :dump, "" + obj = Object.new - assert_send_type "(::String, ::Integer) -> ::String", - Marshal, :dump, "", 3 + assert_send_type '(untyped) -> String', + Marshal, :dump, obj + assert_send_type '(untyped, Integer) -> String', + Marshal, :dump, obj, 123 - io = (Pathname(Dir.mktmpdir) + "foo").open("w") + writer = Writer.new + assert_send_type '(untyped, Writer) -> Writer', + Marshal, :dump, obj, writer - assert_send_type "(::String, ::File) -> ::File", - Marshal, :dump, "", io + with_int.chain([nil]).each do |limit| + assert_send_type '(untyped, Writer, int?) -> Writer', + Marshal, :dump, obj, writer, limit + end end - def test_load - dump = Marshal.dump([1,2,3]) + def with_source(src = Marshal.dump(Object.new)) + with_string src do |string| + def string.reset!; end + yield string + end + + src = src.chars + source = Struct.new(:source).new(src.dup) + source.define_singleton_method(:reset!) do + self.source = src.dup + end + + # String, String + def source.getbyte; source.shift end + def source.read(x) source.shift(x).join end + yield source + + # int, string + source.reset! + def source.getbyte; ToInt.new(source.shift.ord) end + def source.read(x) ToStr.new(source.shift(x).join) end + yield source + end - assert_send_type( - "(::String) -> ::Array[::Integer]", - Marshal, :load, dump - ) + def test_load(meth = :load) + result_proc = Object.new + def result_proc.call(loaded) 1r end - assert_send_type( - "(::String, freeze: bool) -> ::Array[::Integer]", - Marshal, :load, dump, freeze: true - ) - assert_send_type( - "(::String, freeze: Symbol) -> ::Array[::Integer]", - Marshal, :load, dump, freeze: :true - ) + with_source do |source| + assert_send_type '(string | Marshal::_Source) -> untyped', + Marshal, meth, source + source.reset! - assert_send_type( - "(::String, ^(untyped) -> void) -> ::Integer", - Marshal, :load, dump, -> (_x) { 123 } - ) + assert_send_type '(string | Marshal::_Source, Marshal::_Proc[Rational]) -> Rational', + Marshal, meth, source, result_proc + source.reset! - name = Pathname(Dir.mktmpdir) + "foo" + [nil, :yep, true, "hello"].each do |freeze| + assert_send_type '(string | Marshal::_Source, freeze: boolish) -> untyped', + Marshal, meth, source, freeze: freeze + source.reset! - File.open(name, "w") do |io| - Marshal.dump([1,2,3], io) + assert_send_type '(string | Marshal::_Source, Marshal::_Proc[Rational], freeze: boolish) -> Rational', + Marshal, meth, source, result_proc, freeze: freeze + source.reset! + end end - File.open(name) do |io| - assert_send_type( - "(IO) -> ::Array[::Integer]", - Marshal, :load, io - ) + end + + def test_restore + test_load :restore + end +end + +class MarshalIncludeTest < Test::Unit::TestCase + include TypeAssertions + testing "::Marshal" + + def test_dump + obj = Object.new + + assert_send_type '(untyped) -> String', + Marshal, :dump, obj + assert_send_type '(untyped, Integer) -> String', + Marshal, :dump, obj, 123 + + writer = Writer.new + assert_send_type '(untyped, Writer) -> Writer', + Marshal, :dump, obj, writer + + with_int.chain([nil]).each do |limit| + assert_send_type '(untyped, Writer, int?) -> Writer', + Marshal, :dump, obj, writer, limit end end end diff --git a/test/stdlib/test_helper.rb b/test/stdlib/test_helper.rb index 28db50224..3d544a16f 100644 --- a/test/stdlib/test_helper.rb +++ b/test/stdlib/test_helper.rb @@ -625,6 +625,18 @@ def each(&block) end end +class Writer + attr_reader :buffer + + def initialize + @buffer = "" + end + + def write(*vals) + @buffer.concat vals.join + end +end + class ToJson end