summaryrefslogtreecommitdiff
path: root/spec/ruby/library/objectspace/dump_all_spec.rb
blob: e9b449a90519b3bc007dae18978f9c874ac912d6 (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
require_relative '../../spec_helper'
require 'objspace'

describe "ObjectSpace.dump_all" do
  it "dumps Ruby heap to string when passed output: :string" do
    stdout = ruby_exe(<<~RUBY, options: "-robjspace")
      string = "abc"
      dump = ObjectSpace.dump_all(output: :string)
      puts dump.class
      puts dump.include?('"value":"abc"')
    RUBY

    stdout.should == "String\ntrue\n"
  end

  it "dumps Ruby heap to a temporary file when passed output: :file" do
    stdout = ruby_exe(<<~RUBY, options: "-robjspace")
      string = "abc"
      file = ObjectSpace.dump_all(output: :file)

      begin
        file.flush
        file.rewind
        content = file.read

        puts file.class
        puts content.include?('"value":"abc"')
      ensure
        file.close
        File.unlink file.path
      end
    RUBY

    stdout.should == "File\ntrue\n"
  end

  it "dumps Ruby heap to a temporary file when :output not specified" do
    stdout = ruby_exe(<<~RUBY, options: "-robjspace")
      string = "abc"
      file = ObjectSpace.dump_all

      begin
        file.flush
        file.rewind
        content = file.read

        puts file.class
        puts content.include?('"value":"abc"')
      ensure
        file.close
        File.unlink file.path
      end
    RUBY

    stdout.should == "File\ntrue\n"
  end

  it "dumps Ruby heap to a temporary file when passed output: :nil" do
    stdout = ruby_exe(<<~RUBY, options: "-robjspace")
    string = "abc"
    file = ObjectSpace.dump_all(output: nil)

    begin
      file.flush
      file.rewind
      content = file.read

      puts file.class
      puts content.include?('"value":"abc"')
    ensure
      file.close
      File.unlink file.path
    end
    RUBY

    stdout.should == "File\ntrue\n"
  end

  it "dumps Ruby heap to stdout when passed output: :stdout" do
    stdout = ruby_exe(<<~RUBY, options: "-robjspace")
      string = "abc"
      ObjectSpace.dump_all(output: :stdout)
    RUBY

    stdout.should include('"value":"abc"')
  end

  it "dumps Ruby heap to provided IO when passed output: IO" do
    stdout = ruby_exe(<<~RUBY, options: "-robjspace -rtempfile")
      string = "abc"
      io = Tempfile.create("object_space_dump_all")

      begin
        result = ObjectSpace.dump_all(output: io)
        io.rewind
        content = io.read

        puts result.equal?(io)
        puts content.include?('"value":"abc"')
      ensure
        io.close
        File.unlink io.path
      end
    RUBY

    stdout.should == "true\ntrue\n"
  end

  it "raises ArgumentError when passed not supported :output value" do
    -> { ObjectSpace.dump_all(output: Object.new) }.should raise_error(ArgumentError, /wrong output option/)
  end
end