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

describe "ObjectSpace.dump" do
  it "dumps the content of object as JSON" do
    require 'json'
    string = ObjectSpace.dump("abc")
    dump = JSON.parse(string)

    dump['type'].should == "STRING"
    dump['value'].should == "abc"
  end

  it "dumps to string when passed output: :string" do
    string = ObjectSpace.dump("abc", output: :string)
    string.should be_kind_of(String)
    string.should include('"value":"abc"')
  end

  it "dumps to string when :output not specified" do
    string = ObjectSpace.dump("abc")
    string.should be_kind_of(String)
    string.should include('"value":"abc"')
  end

  ruby_version_is "3.0" do
    it "dumps to a temporary file when passed output: :file" do
      file = ObjectSpace.dump("abc", output: :file)
      file.should be_kind_of(File)

      file.rewind
      content = file.read
      content.should include('"value":"abc"')
    ensure
      file.close
      File.unlink file.path
    end

    it "dumps to a temporary file when passed output: :nil" do
      file = ObjectSpace.dump("abc", output: nil)
      file.should be_kind_of(File)

      file.rewind
      file.read.should include('"value":"abc"')
    ensure
      file.close
      File.unlink file.path
    end
  end

  it "dumps to stdout when passed output: :stdout" do
    stdout = ruby_exe('ObjectSpace.dump("abc", output: :stdout)', options: "-robjspace").chomp
    stdout.should include('"value":"abc"')
  end

  ruby_version_is "3.0" do
    it "dumps to provided IO when passed output: IO" do
      filename = tmp("io_read.txt")
      io = File.open(filename, "w+")
      result = ObjectSpace.dump("abc", output: io)
      result.should.equal? io

      io.rewind
      io.read.should include('"value":"abc"')
    ensure
      io.close
      rm_r filename
    end
  end

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