summaryrefslogtreecommitdiff
path: root/spec/ruby/core/marshal/dump_spec.rb
blob: ff9b9214faffa49530fbde8359a46e677844933d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# encoding: binary
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'fixtures/marshal_data'

describe "Marshal.dump" do
  it "dumps nil" do
    Marshal.dump(nil).should == "\004\b0"
  end

  it "dumps true" do
    Marshal.dump(true).should == "\004\bT"
  end

  it "dumps false" do
    Marshal.dump(false).should == "\004\bF"
  end

  describe "with a Fixnum" do
    it "dumps a Fixnum" do
      [ [Marshal,  0,       "\004\bi\000"],
        [Marshal,  5,       "\004\bi\n"],
        [Marshal,  8,       "\004\bi\r"],
        [Marshal,  122,     "\004\bi\177"],
        [Marshal,  123,     "\004\bi\001{"],
        [Marshal,  1234,    "\004\bi\002\322\004"],
        [Marshal, -8,       "\004\bi\363"],
        [Marshal, -123,     "\004\bi\200"],
        [Marshal, -124,     "\004\bi\377\204"],
        [Marshal, -1234,    "\004\bi\376.\373"],
        [Marshal, -4516727, "\004\bi\375\211\024\273"],
        [Marshal,  2**8,    "\004\bi\002\000\001"],
        [Marshal,  2**16,   "\004\bi\003\000\000\001"],
        [Marshal,  2**24,   "\004\bi\004\000\000\000\001"],
        [Marshal, -2**8,    "\004\bi\377\000"],
        [Marshal, -2**16,   "\004\bi\376\000\000"],
        [Marshal, -2**24,   "\004\bi\375\000\000\000"],
      ].should be_computed_by(:dump)
    end

    platform_is c_long_size: 64 do
      it "dumps a positive Fixnum > 31 bits as a Bignum" do
        Marshal.dump(2**31 + 1).should == "\x04\bl+\a\x01\x00\x00\x80"
      end

      it "dumps a negative Fixnum > 31 bits as a Bignum" do
        Marshal.dump(-2**31 - 1).should == "\x04\bl-\a\x01\x00\x00\x80"
      end
    end

    it "does not use object links for objects repeatedly dumped" do
      Marshal.dump([0, 0]).should == "\x04\b[\ai\x00i\x00"
      Marshal.dump([2**16, 2**16]).should == "\x04\b[\ai\x03\x00\x00\x01i\x03\x00\x00\x01"
    end
  end

  describe "with a Symbol" do
    it "dumps a Symbol" do
      Marshal.dump(:symbol).should == "\004\b:\vsymbol"
    end

    it "dumps a big Symbol" do
      Marshal.dump(('big' * 100).to_sym).should == "\004\b:\002,\001#{'big' * 100}"
    end

    it "dumps an encoded Symbol" do
      s = "\u2192"
      [ [Marshal, s.encode("utf-8").to_sym,
            "\x04\bI:\b\xE2\x86\x92\x06:\x06ET"],
        [Marshal, s.encode("utf-16").to_sym,
            "\x04\bI:\t\xFE\xFF!\x92\x06:\rencoding\"\vUTF-16"],
        [Marshal, s.encode("utf-16le").to_sym,
            "\x04\bI:\a\x92!\x06:\rencoding\"\rUTF-16LE"],
        [Marshal, s.encode("utf-16be").to_sym,
            "\x04\bI:\a!\x92\x06:\rencoding\"\rUTF-16BE"],
        [Marshal, s.encode("euc-jp").to_sym,
            "\x04\bI:\a\xA2\xAA\x06:\rencoding\"\vEUC-JP"],
        [Marshal, s.encode("sjis").to_sym,
            "\x04\bI:\a\x81\xA8\x06:\rencoding\"\x10Windows-31J"]
      ].should be_computed_by(:dump)
    end

    it "dumps a binary encoded Symbol" do
      s = "\u2192".dup.force_encoding("binary").to_sym
      Marshal.dump(s).should == "\x04\b:\b\xE2\x86\x92"
    end

    it "dumps multiple Symbols sharing the same encoding" do
      # Note that the encoding is a link for the second Symbol
      symbol1 = "I:\t\xE2\x82\xACa\x06:\x06ET"
      symbol2 = "I:\t\xE2\x82\xACb\x06;\x06T"
      value = [
        "€a".dup.force_encoding(Encoding::UTF_8).to_sym,
        "€b".dup.force_encoding(Encoding::UTF_8).to_sym
      ]
      Marshal.dump(value).should == "\x04\b[\a#{symbol1}#{symbol2}"

      value = [*value, value[0]]
      Marshal.dump(value).should == "\x04\b[\b#{symbol1}#{symbol2};\x00"
    end

    it "uses symbol links for objects repeatedly dumped" do
      symbol = :foo
      Marshal.dump([symbol, symbol]).should == "\x04\b[\a:\bfoo;\x00" # ;\x00 is a link to the symbol object
    end
  end

  describe "with an object responding to #marshal_dump" do
    it "dumps the object returned by #marshal_dump" do
      Marshal.dump(UserMarshal.new).should == "\x04\bU:\x10UserMarshal:\tdata"
    end

    it "does not use Class#name" do
      UserMarshal.should_not_receive(:name)
      Marshal.dump(UserMarshal.new)
    end

    it "raises TypeError if an Object is an instance of an anonymous class" do
      -> { Marshal.dump(Class.new(UserMarshal).new) }.should raise_error(TypeError, /can't dump anonymous class/)
    end

    it "uses object links for objects repeatedly dumped" do
      obj = UserMarshal.new
      Marshal.dump([obj, obj]).should == "\x04\b[\aU:\x10UserMarshal:\tdata@\x06" # @\x06 is a link to the object
    end

    it "adds instance variables of a dumped object after the object itself into the objects table" do
      value = "<foo>"
      obj = MarshalSpec::UserMarshalDumpWithIvar.new("string", value)

      # expect a link to the object (@\x06, that means Integer 1) is smaller than a link
      # to the instance variable value (@\t, that means Integer 4)
      Marshal.dump([obj, obj, value]).should == "\x04\b[\bU:)MarshalSpec::UserMarshalDumpWithIvarI[\x06\"\vstring\x06:\t@foo\"\n<foo>@\x06@\t"
    end
  end

  describe "with an object responding to #_dump" do
    it "dumps the String returned by #_dump" do
      Marshal.dump(UserDefined.new).should == "\004\bu:\020UserDefined\022\004\b[\a:\nstuff;\000"
    end

    it "dumps the String in non US-ASCII and non UTF-8 encoding" do
      object = UserDefinedString.new("a".encode("windows-1251"))
      Marshal.dump(object).should == "\x04\bIu:\x16UserDefinedString\x06a\x06:\rencoding\"\x11Windows-1251"
    end

    it "dumps the String in multibyte encoding" do
      object = UserDefinedString.new("a".encode("utf-32le"))
      Marshal.dump(object).should == "\x04\bIu:\x16UserDefinedString\ta\x00\x00\x00\x06:\rencoding\"\rUTF-32LE"
    end

    it "ignores overridden name method" do
      obj = MarshalSpec::UserDefinedWithOverriddenName.new
      Marshal.dump(obj).should == "\x04\bu:/MarshalSpec::UserDefinedWithOverriddenName\x12\x04\b[\a:\nstuff;\x00"
    end

    it "raises a TypeError if _dump returns a non-string" do
      m = mock("marshaled")
      m.should_receive(:_dump).and_return(0)
      -> { Marshal.dump(m) }.should raise_error(TypeError)
    end

    it "raises TypeError if an Object is an instance of an anonymous class" do
      -> { Marshal.dump(Class.new(UserDefined).new) }.should raise_error(TypeError, /can't dump anonymous class/)
    end

    it "favors marshal_dump over _dump" do
      m = mock("marshaled")
      m.should_receive(:marshal_dump).and_return(0)
      m.should_not_receive(:_dump)
      Marshal.dump(m)
    end

    it "indexes instance variables of a String returned by #_dump at first and then indexes the object itself" do
      class MarshalSpec::M1::A
        def _dump(level)
          s = +"<dump>"
          s.instance_variable_set(:@foo, "bar")
          s
        end
      end

      a = MarshalSpec::M1::A.new

      # 0-based index of the object a = 2, that is encoded as \x07 and printed as "\a" character.
      # Objects are serialized in the following order: Array, a, "bar".
      # But they are indexed in different order: Array (index=0), "bar" (index=1), a (index=2)
      # So the second occurenc of the object a is encoded as an index 2.
      reference = "@\a"
      Marshal.dump([a, a]).should == "\x04\b[\aIu:\x17MarshalSpec::M1::A\v<dump>\x06:\t@foo\"\bbar#{reference}"
    end

    it "uses object links for objects repeatedly dumped" do
      obj = UserDefined.new
      Marshal.dump([obj, obj]).should == "\x04\b[\au:\x10UserDefined\x12\x04\b[\a:\nstuff;\x00@\x06" # @\x06 is a link to the object
    end

    it "adds instance variables of a dumped String before the object itself into the objects table" do
      value = "<foo>"
      obj = MarshalSpec::UserDefinedDumpWithIVars.new(+"string", value)

      # expect a link to the object (@\a, that means Integer 2) is greater than a link
      # to the instance variable value (@\x06, that means Integer 1)
      Marshal.dump([obj, obj, value]).should == "\x04\b[\bIu:*MarshalSpec::UserDefinedDumpWithIVars\vstring\x06:\t@foo\"\n<foo>@\a@\x06"
    end

    describe "Core library classes with #_dump returning a String with instance variables" do
      it "indexes instance variables and then a Time object itself" do
        t = Time.utc(2022)
        reference = "@\a"

        Marshal.dump([t, t]).should == "\x04\b[\aIu:\tTime\r \x80\x1E\xC0\x00\x00\x00\x00\x06:\tzoneI\"\bUTC\x06:\x06EF#{reference}"
      end
    end
  end

  describe "with a Class" do
    it "dumps a builtin Class" do
      Marshal.dump(String).should == "\004\bc\vString"
    end

    it "dumps a user Class" do
      Marshal.dump(UserDefined).should == "\x04\bc\x10UserDefined"
    end

    it "dumps a nested Class" do
      Marshal.dump(UserDefined::Nested).should == "\004\bc\030UserDefined::Nested"
    end

    it "ignores overridden name method" do
      Marshal.dump(MarshalSpec::ClassWithOverriddenName).should == "\x04\bc)MarshalSpec::ClassWithOverriddenName"
    end

    ruby_version_is "4.0" do
      it "dumps a class with multibyte characters in name" do
        source_object = eval("MarshalSpec::MultibyteぁあぃいClass".dup.force_encoding(Encoding::UTF_8))
        Marshal.dump(source_object).should == "\x04\bIc,MarshalSpec::Multibyte\xE3\x81\x81\xE3\x81\x82\xE3\x81\x83\xE3\x81\x84Class\x06:\x06ET"
        Marshal.load(Marshal.dump(source_object)) == source_object
      end
    end

    it "uses object links for objects repeatedly dumped" do
      Marshal.dump([String, String]).should == "\x04\b[\ac\vString@\x06" # @\x06 is a link to the object
    end

    it "raises TypeError with an anonymous Class" do
      -> { Marshal.dump(Class.new) }.should raise_error(TypeError, /can't dump anonymous class/)
    end

    it "raises TypeError with a singleton Class" do
      -> { Marshal.dump(class << self; self end) }.should raise_error(TypeError)
    end
  end

  describe "with a Module" do
    it "dumps a builtin Module" do
      Marshal.dump(Marshal).should == "\004\bm\fMarshal"
    end

    it "ignores overridden name method" do
      Marshal.dump(MarshalSpec::ModuleWithOverriddenName).should == "\x04\bc*MarshalSpec::ModuleWithOverriddenName"
    end

    ruby_version_is "4.0" do
      it "dumps a module with multibyte characters in name" do
        source_object = eval("MarshalSpec::MultibyteけげこごModule".dup.force_encoding(Encoding::UTF_8))
        Marshal.dump(source_object).should == "\x04\bIm-MarshalSpec::Multibyte\xE3\x81\x91\xE3\x81\x92\xE3\x81\x93\xE3\x81\x94Module\x06:\x06ET"
        Marshal.load(Marshal.dump(source_object)) == source_object
      end
    end

    it "uses object links for objects repeatedly dumped" do
      Marshal.dump([Marshal, Marshal]).should == "\x04\b[\am\fMarshal@\x06" # @\x06 is a link to the object
    end

    it "raises TypeError with an anonymous Module" do
      -> { Marshal.dump(Module.new) }.should raise_error(TypeError, /can't dump anonymous module/)
    end
  end

  describe "with a Float" do
    it "dumps a Float" do
      [ [Marshal,  0.0,            "\004\bf\0060"],
        [Marshal, -0.0,            "\004\bf\a-0"],
        [Marshal,  1.0,            "\004\bf\0061"],
        [Marshal,  123.4567,       "\004\bf\r123.4567"],
        [Marshal, -0.841,          "\x04\bf\v-0.841"],
        [Marshal, -9876.345,       "\x04\bf\x0E-9876.345"],
        [Marshal,  infinity_value, "\004\bf\binf"],
        [Marshal, -infinity_value, "\004\bf\t-inf"],
        [Marshal,  nan_value,      "\004\bf\bnan"],
      ].should be_computed_by(:dump)
    end

    it "may or may not use object links for objects repeatedly dumped" do
      # it's an MRI implementation detail - on x86 architecture object links
      # aren't used for Float values but on amd64 - object links are used

      dump = Marshal.dump([0.0, 0.0])
      ["\x04\b[\af\x060@\x06", "\x04\b[\af\x060f\x060"].should.include?(dump)

      # if object links aren't used - entries in the objects table are still
      # occupied by Float values
      if dump == "\x04\b[\af\x060f\x060"
        s = "string"
        # an index of "string" ("@\b") in the object table equals 3 (`"\b".ord - 5`),
        # so `0.0, 0,0` elements occupied indices 1 and 2
        Marshal.dump([0.0, 0.0, s, s]).should == "\x04\b[\tf\x060f\x060\"\vstring@\b"
      end
    end
  end

  describe "with a Bignum" do
    it "dumps a Bignum" do
      [ [Marshal, -4611686018427387903,    "\004\bl-\t\377\377\377\377\377\377\377?"],
        [Marshal, -2361183241434822606847, "\004\bl-\n\377\377\377\377\377\377\377\377\177\000"],
      ].should be_computed_by(:dump)
    end

    it "dumps a Bignum" do
      [ [Marshal,  2**64, "\004\bl+\n\000\000\000\000\000\000\000\000\001\000"],
        [Marshal,  2**90, "\004\bl+\v#{"\000" * 11}\004"],
        [Marshal, -2**63, "\004\bl-\t\000\000\000\000\000\000\000\200"],
        [Marshal, -2**64, "\004\bl-\n\000\000\000\000\000\000\000\000\001\000"],
      ].should be_computed_by(:dump)
    end

    it "uses object links for objects repeatedly dumped" do
      n = 2**64
      Marshal.dump([n, n]).should == "\x04\b[\al+\n\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00@\x06" # @\x06 is a link to the object
    end

    it "increases the object links counter" do
      obj = Object.new
      object_1_link = "\x06" # representing of (0-based) index=1 (by adding 5 for small Integers)
      object_2_link = "\x07" # representing of index=2

      # objects: Array, Object, Object
      Marshal.dump([obj, obj]).should == "\x04\b[\ao:\vObject\x00@#{object_1_link}"

      # objects: Array, Bignum, Object, Object
      Marshal.dump([2**64, obj, obj]).should == "\x04\b[\bl+\n\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00o:\vObject\x00@#{object_2_link}"
      Marshal.dump([2**48, obj, obj]).should == "\x04\b[\bl+\t\x00\x00\x00\x00\x00\x00\x01\x00o:\vObject\x00@#{object_2_link}"
      Marshal.dump([2**32, obj, obj]).should == "\x04\b[\bl+\b\x00\x00\x00\x00\x01\x00o:\vObject\x00@#{object_2_link}"
    end
  end

  describe "with a Rational" do
    it "dumps a Rational" do
      Marshal.dump(Rational(2, 3)).should == "\x04\bU:\rRational[\ai\ai\b"
    end

    it "uses object links for objects repeatedly dumped" do
      r = Rational(2, 3)
      Marshal.dump([r, r]).should == "\x04\b[\aU:\rRational[\ai\ai\b@\x06" # @\x06 is a link to the object
    end
  end

  describe "with a Complex" do
    it "dumps a Complex" do
      Marshal.dump(Complex(2, 3)).should == "\x04\bU:\fComplex[\ai\ai\b"
    end

    it "uses object links for objects repeatedly dumped" do
      c = Complex(2, 3)
      Marshal.dump([c, c]).should == "\x04\b[\aU:\fComplex[\ai\ai\b@\x06" # @\x06 is a link to the object
    end
  end

  describe "with a Data" do
    it "dumps a Data" do
      Marshal.dump(MarshalSpec::DataSpec::Measure.new(100, 'km')).should == "\x04\bS:#MarshalSpec::DataSpec::Measure\a:\vamountii:\tunit\"\akm"
    end

    it "dumps an extended Data" do
      obj = MarshalSpec::DataSpec::MeasureExtended.new(100, "km")
      Marshal.dump(obj).should == "\x04\bS:+MarshalSpec::DataSpec::MeasureExtended\a:\vamountii:\tunit\"\akm"
    end

    it "ignores overridden name method" do
      obj = MarshalSpec::DataSpec::MeasureWithOverriddenName.new(100, "km")
      Marshal.dump(obj).should == "\x04\bS:5MarshalSpec::DataSpec::MeasureWithOverriddenName\a:\vamountii:\tunit\"\akm"
    end

    it "uses object links for objects repeatedly dumped" do
      d = MarshalSpec::DataSpec::Measure.new(100, 'km')
      Marshal.dump([d, d]).should == "\x04\b[\aS:#MarshalSpec::DataSpec::Measure\a:\vamountii:\tunit\"\akm@\x06" # @\x06 is a link to the object
    end

    it "raises TypeError with an anonymous Struct" do
      -> { Marshal.dump(Data.define(:a).new(1)) }.should raise_error(TypeError, /can't dump anonymous class/)
    end
  end

  describe "with a String" do
    it "dumps a blank String" do
      Marshal.dump("".dup.force_encoding("binary")).should == "\004\b\"\000"
    end

    it "dumps a short String" do
      Marshal.dump("short".dup.force_encoding("binary")).should == "\004\b\"\012short"
    end

    it "dumps a long String" do
      Marshal.dump(("big" * 100).force_encoding("binary")).should == "\004\b\"\002,\001#{"big" * 100}"
    end

    it "dumps a String extended with a Module" do
      Marshal.dump("".dup.extend(Meths).force_encoding("binary")).should == "\004\be:\nMeths\"\000"
    end

    it "dumps a String subclass" do
      Marshal.dump(UserString.new.force_encoding("binary")).should == "\004\bC:\017UserString\"\000"
    end

    it "dumps a String subclass extended with a Module" do
      Marshal.dump(UserString.new.extend(Meths).force_encoding("binary")).should == "\004\be:\nMethsC:\017UserString\"\000"
    end

    it "ignores overridden name method when dumps a String subclass" do
      obj = MarshalSpec::StringWithOverriddenName.new
      Marshal.dump(obj).should == "\x04\bC:*MarshalSpec::StringWithOverriddenName\"\x00"
    end

    it "dumps a String with instance variables" do
      str = +""
      str.instance_variable_set("@foo", "bar")
      Marshal.dump(str.force_encoding("binary")).should == "\x04\bI\"\x00\x06:\t@foo\"\bbar"
    end

    it "dumps a US-ASCII String" do
      str = "abc".dup.force_encoding("us-ascii")
      Marshal.dump(str).should == "\x04\bI\"\babc\x06:\x06EF"
    end

    it "dumps a UTF-8 String" do
      str = "\x6d\xc3\xb6\x68\x72\x65".dup.force_encoding("utf-8")
      Marshal.dump(str).should == "\x04\bI\"\vm\xC3\xB6hre\x06:\x06ET"
    end

    it "dumps a String in another encoding" do
      str = "\x6d\x00\xf6\x00\x68\x00\x72\x00\x65\x00".dup.force_encoding("utf-16le")
      result = "\x04\bI\"\x0Fm\x00\xF6\x00h\x00r\x00e\x00\x06:\rencoding\"\rUTF-16LE"
      Marshal.dump(str).should == result
    end

    it "dumps multiple strings using symlinks for the :E (encoding) symbol" do
      Marshal.dump(["".encode("us-ascii"), "".encode("utf-8")]).should == "\x04\b[\aI\"\x00\x06:\x06EFI\"\x00\x06;\x00T"
    end

    it "uses object links for objects repeatedly dumped" do
      s = "string"
      Marshal.dump([s, s]).should == "\x04\b[\a\"\vstring@\x06" # @\x06 is a link to the object
    end

    it "adds instance variables after the object itself into the objects table" do
      obj = +"string"
      value = "<foo>"
      obj.instance_variable_set :@foo, value

      # expect a link to the object (@\x06, that means Integer 1) is smaller than a link
      # to the instance variable value (@\a, that means Integer 2)
      Marshal.dump([obj, obj, value]).should == "\x04\b[\bI\"\vstring\x06:\t@foo\"\n<foo>@\x06@\a"
    end
  end

  describe "with a Regexp" do
    it "dumps a Regexp" do
      Marshal.dump(/\A.\Z/).should == "\x04\bI/\n\\A.\\Z\x00\x06:\x06EF"
    end

    it "dumps a Regexp with flags" do
      Marshal.dump(//im).should == "\x04\bI/\x00\x05\x06:\x06EF"
    end

    it "dumps a Regexp with instance variables" do
      o = Regexp.new("")
      o.instance_variable_set(:@ivar, :ivar)
      Marshal.dump(o).should == "\x04\bI/\x00\x00\a:\x06EF:\n@ivar:\tivar"
    end

    it "dumps an extended Regexp" do
      Marshal.dump(Regexp.new("").extend(Meths)).should == "\x04\bIe:\nMeths/\x00\x00\x06:\x06EF"
    end

    it "dumps a Regexp subclass" do
      Marshal.dump(UserRegexp.new("")).should == "\x04\bIC:\x0FUserRegexp/\x00\x00\x06:\x06EF"
    end

    it "dumps a binary Regexp" do
      o = Regexp.new("".dup.force_encoding("binary"), Regexp::FIXEDENCODING)
      Marshal.dump(o).should == "\x04\b/\x00\x10"
    end

    it "dumps an ascii-compatible Regexp" do
      o = Regexp.new("a".encode("us-ascii"), Regexp::FIXEDENCODING)
      Marshal.dump(o).should == "\x04\bI/\x06a\x10\x06:\x06EF"

      o = Regexp.new("a".encode("us-ascii"))
      Marshal.dump(o).should == "\x04\bI/\x06a\x00\x06:\x06EF"

      o = Regexp.new("a".encode("windows-1251"), Regexp::FIXEDENCODING)
      Marshal.dump(o).should == "\x04\bI/\x06a\x10\x06:\rencoding\"\x11Windows-1251"

      o = Regexp.new("a".encode("windows-1251"))
      Marshal.dump(o).should == "\x04\bI/\x06a\x00\x06:\x06EF"
    end

    it "dumps a UTF-8 Regexp" do
      o = Regexp.new("".dup.force_encoding("utf-8"), Regexp::FIXEDENCODING)
      Marshal.dump(o).should == "\x04\bI/\x00\x10\x06:\x06ET"

      o = Regexp.new("a".dup.force_encoding("utf-8"), Regexp::FIXEDENCODING)
      Marshal.dump(o).should == "\x04\bI/\x06a\x10\x06:\x06ET"

      o = Regexp.new("\u3042".dup.force_encoding("utf-8"), Regexp::FIXEDENCODING)
      Marshal.dump(o).should == "\x04\bI/\b\xE3\x81\x82\x10\x06:\x06ET"
    end

    it "dumps a Regexp in another encoding" do
      o = Regexp.new("".dup.force_encoding("utf-16le"), Regexp::FIXEDENCODING)
      Marshal.dump(o).should == "\x04\bI/\x00\x10\x06:\rencoding\"\rUTF-16LE"

      o = Regexp.new("a".encode("utf-16le"), Regexp::FIXEDENCODING)
      Marshal.dump(o).should == "\x04\bI/\aa\x00\x10\x06:\rencoding\"\rUTF-16LE"
    end

    it "ignores overridden name method when dumps a Regexp subclass" do
      obj = MarshalSpec::RegexpWithOverriddenName.new("")
      Marshal.dump(obj).should == "\x04\bIC:*MarshalSpec::RegexpWithOverriddenName/\x00\x00\x06:\x06EF"
    end

    it "uses object links for objects repeatedly dumped" do
      r = /\A.\Z/
      Marshal.dump([r, r]).should == "\x04\b[\aI/\n\\A.\\Z\x00\x06:\x06EF@\x06" # @\x06 is a link to the object
    end
  end

  describe "with an Array" do
    it "dumps an empty Array" do
      Marshal.dump([]).should == "\004\b[\000"
    end

    it "dumps a non-empty Array" do
      Marshal.dump([:a, 1, 2]).should == "\004\b[\b:\006ai\006i\a"
    end

    it "dumps an Array subclass" do
      Marshal.dump(UserArray.new).should == "\004\bC:\016UserArray[\000"
    end

    it "dumps a recursive Array" do
      a = []
      a << a
      Marshal.dump(a).should == "\x04\b[\x06@\x00"
    end

    it "dumps an Array with instance variables" do
      a = []
      a.instance_variable_set(:@ivar, 1)
      Marshal.dump(a).should == "\004\bI[\000\006:\n@ivari\006"
    end

    it "dumps an extended Array" do
      Marshal.dump([].extend(Meths)).should == "\004\be:\nMeths[\000"
    end

    it "ignores overridden name method when dumps an Array subclass" do
      obj = MarshalSpec::ArrayWithOverriddenName.new
      Marshal.dump(obj).should == "\x04\bC:)MarshalSpec::ArrayWithOverriddenName[\x00"
    end

    it "uses object links for objects repeatedly dumped" do
      a = [1]
      Marshal.dump([a, a]).should == "\x04\b[\a[\x06i\x06@\x06" # @\x06 is a link to the object
    end

    it "adds instance variables after the object itself into the objects table" do
      obj = []
      value = "<foo>"
      obj.instance_variable_set :@foo, value

      # expect a link to the object (@\x06, that means Integer 1) is smaller than a link
      # to the instance variable value (@\a, that means Integer 2)
      Marshal.dump([obj, obj, value]).should == "\x04\b[\bI[\x00\x06:\t@foo\"\n<foo>@\x06@\a"
    end
  end

  describe "with a Hash" do
    it "dumps a Hash" do
      Marshal.dump({}).should == "\004\b{\000"
    end

    it "dumps a non-empty Hash" do
      Marshal.dump({a: 1}).should == "\x04\b{\x06:\x06ai\x06"
    end

    it "dumps a Hash subclass" do
      Marshal.dump(UserHash.new).should == "\004\bC:\rUserHash{\000"
    end

    it "dumps a Hash with a default value" do
      Marshal.dump(Hash.new(1)).should == "\004\b}\000i\006"
    end

    it "dumps a Hash with compare_by_identity" do
      h = {}
      h.compare_by_identity

      Marshal.dump(h).should == "\004\bC:\tHash{\x00"
    end

    it "dumps a Hash subclass with compare_by_identity" do
      h = UserHash.new
      h.compare_by_identity

      Marshal.dump(h).should == "\x04\bC:\rUserHashC:\tHash{\x00"
    end

    it "raises a TypeError with hash having default proc" do
      -> { Marshal.dump(Hash.new {}) }.should raise_error(TypeError, "can't dump hash with default proc")
    end

    it "dumps a Hash with instance variables" do
      a = {}
      a.instance_variable_set(:@ivar, 1)
      Marshal.dump(a).should == "\004\bI{\000\006:\n@ivari\006"
    end

    it "dumps an extended Hash" do
      Marshal.dump({}.extend(Meths)).should == "\004\be:\nMeths{\000"
    end

    it "dumps an Hash subclass with a parameter to initialize" do
      Marshal.dump(UserHashInitParams.new(1)).should == "\004\bIC:\027UserHashInitParams{\000\006:\a@ai\006"
    end

    it "ignores overridden name method when dumps a Hash subclass" do
      obj = MarshalSpec::HashWithOverriddenName.new
      Marshal.dump(obj).should == "\x04\bC:(MarshalSpec::HashWithOverriddenName{\x00"
    end

    it "uses object links for objects repeatedly dumped" do
      h = {a: 1}
      Marshal.dump([h, h]).should == "\x04\b[\a{\x06:\x06ai\x06@\x06" # @\x06 is a link to the object
    end

    it "adds instance variables after the object itself into the objects table" do
      obj = {}
      value = "<foo>"
      obj.instance_variable_set :@foo, value

      # expect a link to the object (@\x06, that means Integer 1) is smaller than a link
      # to the instance variable value (@\a, that means Integer 2)
      Marshal.dump([obj, obj, value]).should == "\x04\b[\bI{\x00\x06:\t@foo\"\n<foo>@\x06@\a"
    end
  end

  describe "with a Struct" do
    it "dumps a Struct" do
      Marshal.dump(Struct::Pyramid.new).should == "\004\bS:\024Struct::Pyramid\000"
    end

    it "dumps a Struct" do
      Marshal.dump(Struct::Useful.new(1, 2)).should == "\004\bS:\023Struct::Useful\a:\006ai\006:\006bi\a"
    end

    it "dumps a Struct with instance variables" do
      st = Struct.new("Thick").new
      st.instance_variable_set(:@ivar, 1)
      Marshal.dump(st).should == "\004\bIS:\022Struct::Thick\000\006:\n@ivari\006"
      Struct.send(:remove_const, :Thick)
    end

    it "dumps an extended Struct" do
      obj = Struct.new("Extended", :a, :b).new.extend(Meths)
      Marshal.dump(obj).should == "\004\be:\nMethsS:\025Struct::Extended\a:\006a0:\006b0"

      s = 'hi'
      obj.a = [:a, s]
      obj.b = [:Meths, s]
      Marshal.dump(obj).should == "\004\be:\nMethsS:\025Struct::Extended\a:\006a[\a;\a\"\ahi:\006b[\a;\000@\a"
      Struct.send(:remove_const, :Extended)
    end

    it "ignores overridden name method" do
      obj = MarshalSpec::StructWithOverriddenName.new("member")
      Marshal.dump(obj).should == "\x04\bS:*MarshalSpec::StructWithOverriddenName\x06:\x06a\"\vmember"
    end

    it "uses object links for objects repeatedly dumped" do
      s = Struct::Pyramid.new
      Marshal.dump([s, s]).should == "\x04\b[\aS:\x14Struct::Pyramid\x00@\x06" # @\x06 is a link to the object
    end

    it "raises TypeError with an anonymous Struct" do
      -> { Marshal.dump(Struct.new(:a).new(1)) }.should raise_error(TypeError, /can't dump anonymous class/)
    end

    it "adds instance variables after the object itself into the objects table" do
      obj = Struct::Pyramid.new
      value = "<foo>"
      obj.instance_variable_set :@foo, value

      # expect a link to the object (@\x06, that means Integer 1) is smaller than a link
      # to the instance variable value (@\a, that means Integer 2)
      Marshal.dump([obj, obj, value]).should == "\x04\b[\bIS:\x14Struct::Pyramid\x00\x06:\t@foo\"\n<foo>@\x06@\a"
    end
  end

  describe "with an Object" do
    it "dumps an Object" do
      Marshal.dump(Object.new).should == "\004\bo:\x0BObject\x00"
    end

    it "dumps an extended Object" do
      Marshal.dump(Object.new.extend(Meths)).should == "\004\be:\x0AMethso:\x0BObject\x00"
    end

    it "dumps an Object with an instance variable" do
      obj = Object.new
      obj.instance_variable_set(:@ivar, 1)
      Marshal.dump(obj).should == "\004\bo:\vObject\006:\n@ivari\006"
    end

    it "dumps an Object with a non-US-ASCII instance variable" do
      obj = Object.new
      ivar = "@é".dup.force_encoding(Encoding::UTF_8).to_sym
      obj.instance_variable_set(ivar, 1)
      Marshal.dump(obj).should == "\x04\bo:\vObject\x06I:\b@\xC3\xA9\x06:\x06ETi\x06"
    end

    it "dumps an Object that has had an instance variable added and removed as though it was never set" do
      obj = Object.new
      obj.instance_variable_set(:@ivar, 1)
      obj.send(:remove_instance_variable, :@ivar)
      Marshal.dump(obj).should == "\004\bo:\x0BObject\x00"
    end

    it "dumps an Object if it has a singleton class but no singleton methods and no singleton instance variables" do
      obj = Object.new
      obj.singleton_class
      Marshal.dump(obj).should == "\004\bo:\x0BObject\x00"
    end

    it "ignores overridden name method" do
      obj = MarshalSpec::ClassWithOverriddenName.new
      Marshal.dump(obj).should == "\x04\bo:)MarshalSpec::ClassWithOverriddenName\x00"
    end

    it "raises TypeError if an Object has a singleton class and singleton methods" do
      obj = Object.new
      def obj.foo; end
      -> {
        Marshal.dump(obj)
      }.should raise_error(TypeError, "singleton can't be dumped")
    end

    it "raises TypeError if an Object has a singleton class and singleton instance variables" do
      obj = Object.new
      class << obj
        @v = 1
      end

      -> {
        Marshal.dump(obj)
      }.should raise_error(TypeError, "singleton can't be dumped")
    end

    it "raises TypeError if an Object is an instance of an anonymous class" do
      anonymous_class = Class.new
      obj = anonymous_class.new

      -> { Marshal.dump(obj) }.should raise_error(TypeError, /can't dump anonymous class/)
    end

    it "raises TypeError if an Object extends an anonymous module" do
      anonymous_module = Module.new
      obj = Object.new
      obj.extend(anonymous_module)

      -> { Marshal.dump(obj) }.should raise_error(TypeError, /can't dump anonymous class/)
    end

    it "dumps a BasicObject subclass if it defines respond_to?" do
      obj = MarshalSpec::BasicObjectSubWithRespondToFalse.new
      Marshal.dump(obj).should == "\x04\bo:2MarshalSpec::BasicObjectSubWithRespondToFalse\x00"
    end

    it "dumps without marshaling any attached finalizer" do
      obj = Object.new
      finalizer = Object.new
      def finalizer.noop(_)
      end
      ObjectSpace.define_finalizer(obj, finalizer.method(:noop))
      Marshal.load(Marshal.dump(obj)).class.should == Object
    end

    it "uses object links for objects repeatedly dumped" do
      obj = Object.new
      Marshal.dump([obj, obj]).should == "\x04\b[\ao:\vObject\x00@\x06" # @\x06 is a link to the object
    end

    it "adds instance variables after the object itself into the objects table" do
      obj = Object.new
      value = "<foo>"
      obj.instance_variable_set :@foo, value

      # expect a link to the object (@\x06, that means Integer 1) is smaller than a link
      # to the instance variable value (@\a, that means Integer 2)
      Marshal.dump([obj, obj, value]).should == "\x04\b[\bo:\vObject\x06:\t@foo\"\n<foo>@\x06@\a"
    end
  end

  describe "with a Range" do
    it "dumps a Range inclusive of end" do
      dump = Marshal.dump(1..2)
      dump.should == "\x04\bo:\nRange\b:\texclF:\nbegini\x06:\bendi\a"

      load = Marshal.load(dump)
      load.should == (1..2)
    end

    it "dumps a Range exclusive of end" do
      dump = Marshal.dump(1...2)
      dump.should == "\x04\bo:\nRange\b:\texclT:\nbegini\x06:\bendi\a"

      load = Marshal.load(dump)
      load.should == (1...2)
    end

    it "uses object links for objects repeatedly dumped" do
      r = 1..2
      Marshal.dump([r, r]).should == "\x04\b[\ao:\nRange\b:\texclF:\nbegini\x06:\bendi\a@\x06" # @\x06 is a link to the object
    end

    it "raises TypeError with an anonymous Range subclass" do
      -> { Marshal.dump(Class.new(Range).new(1, 2)) }.should raise_error(TypeError, /can't dump anonymous class/)
    end
  end

  describe "with a Time" do
    before :each do
      @internal = Encoding.default_internal
      Encoding.default_internal = Encoding::UTF_8

      @utc = Time.utc(2012, 1, 1)
      @utc_dump = @utc.send(:_dump)

      with_timezone 'AST', 3 do
        @t = Time.local(2012, 1, 1)
        @fract = Time.local(2012, 1, 1, 1, 59, 56.2)
        @t_dump = @t.send(:_dump)
        @fract_dump = @fract.send(:_dump)
      end
    end

    after :each do
      Encoding.default_internal = @internal
    end

    it "dumps the zone and the offset" do
      with_timezone 'AST', 3 do
        dump = Marshal.dump(@t)
        base = "\x04\bIu:\tTime\r#{@t_dump}\a"
        offset = ":\voffseti\x020*"
        zone = ":\tzoneI\"\bAST\x06:\x06EF" # Last is 'F' (US-ASCII)
        [ "#{base}#{offset}#{zone}", "#{base}#{zone}#{offset}" ].should include(dump)
      end
    end

    it "dumps the zone, but not the offset if zone is UTC" do
      dump = Marshal.dump(@utc)
      zone = ":\tzoneI\"\bUTC\x06:\x06EF" # Last is 'F' (US-ASCII)
      dump.should == "\x04\bIu:\tTime\r#{@utc_dump}\x06#{zone}"
    end

    it "ignores overridden name method" do
      obj = MarshalSpec::TimeWithOverriddenName.new
      Marshal.dump(obj).should include("MarshalSpec::TimeWithOverriddenName")
    end

    ruby_version_is "4.0" do
      it "dumps a Time subclass with multibyte characters in name" do
        source_object = eval("MarshalSpec::MultibyteぁあぃいTime".dup.force_encoding(Encoding::UTF_8))
        Marshal.dump(source_object).should == "\x04\bIc+MarshalSpec::Multibyte\xE3\x81\x81\xE3\x81\x82\xE3\x81\x83\xE3\x81\x84Time\x06:\x06ET"
        Marshal.load(Marshal.dump(source_object)) == source_object
      end
    end

    it "uses object links for objects repeatedly dumped" do
      # order of the offset and zone instance variables is a subject to change
      # and may be different on different CRuby versions
      base = Regexp.quote("\x04\b[\aIu:\tTime\r\xF5\xEF\e\x80\x00\x00\x00\x00\a")
      offset = Regexp.quote(":\voffseti\x020*:\tzoneI\"\bAST\x06:\x06EF")
      zone = Regexp.quote(":\tzoneI\"\bAST\x06:\x06EF:\voffseti\x020*")
      instance_variables = /#{offset}|#{zone}/
      Marshal.dump([@t, @t]).should =~ /\A#{base}#{instance_variables}@\a\Z/ # @\a is a link to the object
    end

    it "adds instance variables before the object itself into the objects table" do
      obj = @utc
      value = "<foo>"
      obj.instance_variable_set :@foo, value

      # expect a link to the object (@\b, that means Integer 3) is greater than a link
      # to the instance variable value (@\x06, that means Integer 1)
      Marshal.dump([obj, obj, value]).should == "\x04\b[\bIu:\tTime\r \x00\x1C\xC0\x00\x00\x00\x00\a:\t@foo\"\n<foo>:\tzoneI\"\bUTC\x06:\x06EF@\b@\x06"
    end

    it "raises TypeError with an anonymous Time subclass" do
      -> { Marshal.dump(Class.new(Time).now) }.should raise_error(TypeError)
    end
  end

  describe "with an Exception" do
    it "dumps an empty Exception" do
      Marshal.dump(Exception.new).should == "\x04\bo:\x0EException\a:\tmesg0:\abt0"
    end

    it "dumps the message for the exception" do
      Marshal.dump(Exception.new("foo")).should == "\x04\bo:\x0EException\a:\tmesg\"\bfoo:\abt0"
    end

    it "contains the filename in the backtrace" do
      obj = Exception.new("foo")
      obj.set_backtrace(["foo/bar.rb:10"])
      Marshal.dump(obj).should == "\x04\bo:\x0EException\a:\tmesg\"\bfoo:\abt[\x06\"\x12foo/bar.rb:10"
    end

    it "dumps instance variables if they exist" do
      obj = Exception.new("foo")
      obj.instance_variable_set(:@ivar, 1)
      Marshal.dump(obj).should == "\x04\bo:\x0EException\b:\tmesg\"\bfoo:\abt0:\n@ivari\x06"
    end

    it "dumps the cause for the exception" do
      exc = nil
      begin
        raise StandardError, "the cause"
      rescue StandardError => cause
        begin
          raise RuntimeError, "the consequence"
        rescue RuntimeError => e
          e.cause.should equal(cause)
          exc = e
        end
      end

      reloaded = Marshal.load(Marshal.dump(exc))
      reloaded.cause.should be_an_instance_of(StandardError)
      reloaded.cause.message.should == "the cause"
    end

    # NoMethodError uses an exception formatter on TruffleRuby and computes a message lazily
    it "dumps the message for the raised NoMethodError exception" do
      begin
        "".foo
      rescue => e
      end

      Marshal.dump(e).should =~ /undefined method [`']foo' for ("":String|an instance of String)/
    end

    it "uses object links for objects repeatedly dumped" do
      e = Exception.new
      Marshal.dump([e, e]).should == "\x04\b[\ao:\x0EException\a:\tmesg0:\abt0@\x06" # @\x\a is a link to the object
    end

    it "adds instance variables after the object itself into the objects table" do
      obj = Exception.new
      value = "<foo>"
      obj.instance_variable_set :@foo, value

      # expect a link to the object (@\x06, that means Integer 1) is smaller than a link
      # to the instance variable value (@\a, that means Integer 2)
      Marshal.dump([obj, obj, value]).should == "\x04\b[\bo:\x0EException\b:\tmesg0:\abt0:\t@foo\"\n<foo>@\x06@\a"
    end

    it "raises TypeError if an Object is an instance of an anonymous class" do
      anonymous_class = Class.new(Exception)
      obj = anonymous_class.new

      -> { Marshal.dump(obj) }.should raise_error(TypeError, /can't dump anonymous class/)
    end
  end

  it "dumps subsequent appearances of a symbol as a link" do
    Marshal.dump([:a, :a]).should == "\004\b[\a:\006a;\000"
  end

  it "dumps subsequent appearances of an object as a link" do
    o = Object.new
    Marshal.dump([o, o]).should == "\004\b[\ao:\vObject\000@\006"
  end

  MarshalSpec::DATA_19.each do |description, (object, marshal, attributes)|
    it "#{description} returns a binary string" do
      Marshal.dump(object).encoding.should == Encoding::BINARY
    end
  end

  it "raises an ArgumentError when the recursion limit is exceeded" do
    h = {'one' => {'two' => {'three' => 0}}}
    -> { Marshal.dump(h, 3) }.should raise_error(ArgumentError)
    -> { Marshal.dump([h], 4) }.should raise_error(ArgumentError)
    -> { Marshal.dump([], 0) }.should raise_error(ArgumentError)
    -> { Marshal.dump([[[]]], 1) }.should raise_error(ArgumentError)
  end

  it "ignores the recursion limit if the limit is negative" do
    Marshal.dump([], -1).should == "\004\b[\000"
    Marshal.dump([[]], -1).should == "\004\b[\006[\000"
    Marshal.dump([[[]]], -1).should == "\004\b[\006[\006[\000"
  end

  describe "when passed an IO" do
    it "writes the serialized data to the IO-Object" do
      (obj = mock('test')).should_receive(:write).at_least(1)
      Marshal.dump("test", obj)
    end

    it "returns the IO-Object" do
      (obj = mock('test')).should_receive(:write).at_least(1)
      Marshal.dump("test", obj).should == obj
    end

    it "raises an Error when the IO-Object does not respond to #write" do
      obj = mock('test')
      -> { Marshal.dump("test", obj) }.should raise_error(TypeError)
    end


    it "calls binmode when it's defined" do
      obj = mock('test')
      obj.should_receive(:write).at_least(1)
      obj.should_receive(:binmode).at_least(1)
      Marshal.dump("test", obj)
    end
  end

  describe "when passed a StringIO" do
    it "should raise an error" do
      require "stringio"
      -> { Marshal.dump(StringIO.new) }.should raise_error(TypeError)
    end
  end

  it "raises a TypeError if marshalling a Method instance" do
    -> { Marshal.dump(Marshal.method(:dump)) }.should raise_error(TypeError)
  end

  it "raises a TypeError if marshalling a Proc" do
    -> { Marshal.dump(proc {}) }.should raise_error(TypeError)
  end

  it "raises a TypeError if dumping a IO/File instance" do
    -> { Marshal.dump(STDIN) }.should raise_error(TypeError)
    -> { File.open(__FILE__) { |f| Marshal.dump(f) } }.should raise_error(TypeError)
  end

  it "raises a TypeError if dumping a MatchData instance" do
    -> { Marshal.dump(/(.)/.match("foo")) }.should raise_error(TypeError)
  end

  it "raises a TypeError if dumping a Mutex instance" do
    m = Mutex.new
    -> { Marshal.dump(m) }.should raise_error(TypeError)
  end
end