summaryrefslogtreecommitdiff
path: root/spec/ruby/library/objectspace
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/objectspace')
-rw-r--r--spec/ruby/library/objectspace/dump_all_spec.rb112
-rw-r--r--spec/ruby/library/objectspace/dump_spec.rb70
-rw-r--r--spec/ruby/library/objectspace/fixtures/trace.rb6
-rw-r--r--spec/ruby/library/objectspace/reachable_objects_from_spec.rb2
-rw-r--r--spec/ruby/library/objectspace/trace_object_allocations_spec.rb18
-rw-r--r--spec/ruby/library/objectspace/trace_spec.rb15
6 files changed, 221 insertions, 2 deletions
diff --git a/spec/ruby/library/objectspace/dump_all_spec.rb b/spec/ruby/library/objectspace/dump_all_spec.rb
new file mode 100644
index 0000000000..e9b449a905
--- /dev/null
+++ b/spec/ruby/library/objectspace/dump_all_spec.rb
@@ -0,0 +1,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
diff --git a/spec/ruby/library/objectspace/dump_spec.rb b/spec/ruby/library/objectspace/dump_spec.rb
new file mode 100644
index 0000000000..e22ee3df1e
--- /dev/null
+++ b/spec/ruby/library/objectspace/dump_spec.rb
@@ -0,0 +1,70 @@
+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
+
+ 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
+
+ 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
+
+ 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
+
+ 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
diff --git a/spec/ruby/library/objectspace/fixtures/trace.rb b/spec/ruby/library/objectspace/fixtures/trace.rb
new file mode 100644
index 0000000000..e53a7a0cac
--- /dev/null
+++ b/spec/ruby/library/objectspace/fixtures/trace.rb
@@ -0,0 +1,6 @@
+# frozen_string_literal: false
+require "objspace/trace"
+a = "foo"
+b = "b" + "a" + "r"
+c = 42
+p a, b, c
diff --git a/spec/ruby/library/objectspace/reachable_objects_from_spec.rb b/spec/ruby/library/objectspace/reachable_objects_from_spec.rb
index 7e70bc8569..dee5961663 100644
--- a/spec/ruby/library/objectspace/reachable_objects_from_spec.rb
+++ b/spec/ruby/library/objectspace/reachable_objects_from_spec.rb
@@ -38,7 +38,6 @@ describe "ObjectSpace.reachable_objects_from" do
end
it "finds an object stored in a Queue" do
- require 'thread'
o = Object.new
q = Queue.new
q << o
@@ -49,7 +48,6 @@ describe "ObjectSpace.reachable_objects_from" do
end
it "finds an object stored in a SizedQueue" do
- require 'thread'
o = Object.new
q = SizedQueue.new(3)
q << o
diff --git a/spec/ruby/library/objectspace/trace_object_allocations_spec.rb b/spec/ruby/library/objectspace/trace_object_allocations_spec.rb
index 3100511dc9..612430e067 100644
--- a/spec/ruby/library/objectspace/trace_object_allocations_spec.rb
+++ b/spec/ruby/library/objectspace/trace_object_allocations_spec.rb
@@ -128,4 +128,22 @@ describe "ObjectSpace.trace_object_allocations" do
ObjectSpace.trace_object_allocations_stop
end
end
+
+ it "returns nil for class_path, generation, method_id, sourcefile, and sourceline for immutable objects" do
+ ObjectSpace.trace_object_allocations_start
+ begin
+ one = nil
+ two = 42
+ three = :foo
+ [one, two, three].each do |i|
+ ObjectSpace.allocation_class_path(i).should == nil
+ ObjectSpace.allocation_generation(i).should == nil
+ ObjectSpace.allocation_method_id(i).should == nil
+ ObjectSpace.allocation_sourcefile(i).should == nil
+ ObjectSpace.allocation_sourceline(i).should == nil
+ end
+ ensure
+ ObjectSpace.trace_object_allocations_stop
+ end
+ end
end
diff --git a/spec/ruby/library/objectspace/trace_spec.rb b/spec/ruby/library/objectspace/trace_spec.rb
new file mode 100644
index 0000000000..532c282ce4
--- /dev/null
+++ b/spec/ruby/library/objectspace/trace_spec.rb
@@ -0,0 +1,15 @@
+require_relative '../../spec_helper'
+
+ruby_version_is "3.1" do
+ describe 'require "objspace/trace"' do
+ it "shows object allocation sites" do
+ file = fixture(__FILE__ , "trace.rb")
+ ruby_exe(file, args: "2>&1").lines(chomp: true).should == [
+ "objspace/trace is enabled",
+ "\"foo\" @ #{file}:3",
+ "\"bar\" @ #{file}:4",
+ "42"
+ ]
+ end
+ end
+end