Skip to content

Commit 6a22ec3

Browse files
committed
Improve Module#to_s specs
* Ensure there is no extra output before or after. For example, TruffleRuby had an extra leading `#<Class:`.
1 parent a2e33ba commit 6a22ec3

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

spec/ruby/core/class/to_s_spec.rb

Lines changed: 0 additions & 23 deletions
This file was deleted.

spec/ruby/core/module/fixtures/classes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ def self.without_test_modules(modules)
66

77
CONST = :plain_constant
88

9+
class NamedClass
10+
end
11+
912
module PrivConstModule
1013
PRIVATE_CONSTANT = 1
1114
private_constant :PRIVATE_CONSTANT

spec/ruby/core/module/to_s_spec.rb

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,44 @@
22
require_relative 'fixtures/classes'
33

44
describe "Module#to_s" do
5+
it 'returns the name of the module if it has a name' do
6+
Enumerable.to_s.should == 'Enumerable'
7+
String.to_s.should == 'String'
8+
end
9+
510
it "returns the full constant path leading to the module" do
611
ModuleSpecs::LookupMod.to_s.should == "ModuleSpecs::LookupMod"
712
end
813

914
it "works with an anonymous module" do
1015
m = Module.new
11-
m.to_s.should =~ /#<Module:0x[0-9a-f]+>/
16+
m.to_s.should =~ /\A#<Module:0x\h+>\z/
1217
end
1318

1419
it "works with an anonymous class" do
1520
c = Class.new
16-
c.to_s.should =~ /#<Class:0x[0-9a-f]+>/
21+
c.to_s.should =~ /\A#<Class:0x\h+>\z/
22+
end
23+
24+
it 'for the singleton class of an object of an anonymous class' do
25+
klass = Class.new
26+
obj = klass.new
27+
sclass = obj.singleton_class
28+
sclass.to_s.should == "#<Class:#{obj}>"
29+
sclass.to_s.should =~ /\A#<Class:#<#{klass}:0x\h+>>\z/
30+
sclass.to_s.should =~ /\A#<Class:#<#<Class:0x\h+>:0x\h+>>\z/
31+
end
32+
33+
it 'for a singleton class of a module includes the module name' do
34+
ModuleSpecs.singleton_class.to_s.should == '#<Class:ModuleSpecs>'
35+
end
36+
37+
it 'for a metaclass includes the class name' do
38+
ModuleSpecs::NamedClass.singleton_class.to_s.should == '#<Class:ModuleSpecs::NamedClass>'
39+
end
40+
41+
it 'for objects includes class name and object ID' do
42+
obj = ModuleSpecs::NamedClass.new
43+
obj.singleton_class.to_s.should =~ /\A#<Class:#<ModuleSpecs::NamedClass:0x\h+>>\z/
1744
end
1845
end

spec/tags/core/module/to_s_tags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fails:Module#to_s for the singleton class of an object of an anonymous class

0 commit comments

Comments
 (0)