Skip to content

Instantly share code, notes, and snippets.

@elskwid
Created January 13, 2011 18:39
Show Gist options
  • Save elskwid/778366 to your computer and use it in GitHub Desktop.
Save elskwid/778366 to your computer and use it in GitHub Desktop.
Test some aspects of marshalling using JRuby 1.6RC1
require 'test/unit'
require 'tempfile'
require 'ostruct'
class Test::Unit::TestCase
def marshalled(obj)
tmp = Tempfile.new('marshal')
tmp.puts Marshal.dump(obj)
tmp.close
File.open(tmp.path, 'r'){|f| Marshal.load(f)}
end
end
class BasicClass
attr_accessor :name
end
class TestMarshal < Test::Unit::TestCase
def setup
#
end
def test_string
s = "the string"
assert_equal s, marshalled(s)
end
def test_integer
n = 2
assert_equal n, marshalled(n)
end
def test_float
n = 1.2
assert_equal n, marshalled(n)
end
def test_basic_class
bc = BasicClass.new
bc.name = "basic_class"
mbc = marshalled(bc)
assert mbc.is_a? BasicClass
assert_equal bc.name, mbc.name
end
def test_basic_openstruct
os = OpenStruct.new
os.name ="openstruct"
os.num = 5
mos = marshalled(os)
assert mos.is_a? OpenStruct
assert_equal os.name, mos.name
assert_equal os.num, mos.num
end
def test_less_basic_openstruct
os = OpenStruct.new
os.name = "openstruct2"
os.obj = BasicClass.new
os.obj.name = "enclosed_object"
mos = marshalled(os)
assert mos.is_a? OpenStruct
assert_equal os.name, mos.name
assert mos.obj.is_a? BasicClass
assert_equal os.obj.name, mos.obj.name
end
end
@elskwid
Copy link
Author

elskwid commented Jan 14, 2011

I can get this to pass on Windows if I change the marshalled helper to look like this:

def marshalled(obj)
  tmp = Tempfile.new('marshal')
  tmp.puts Marshal.dump(obj)
  # tmp.write Marshal.dump(obj) # <<-- both puts and write are ok
  tmp.close

  # File.open(tmp.path, 'r'){|f| Marshal.load(f)} # <<-- Fails on Windows
  File.open(tmp.path, 'r'){ |f| Marshal.load(f.read) } # <<-- Works all around
end

Notice that passing the file to Marshal.load is no good while doing file.read works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment