~tim/natalie

ref: f5a482b5ccd757b3cd29779e40922a6db95e541a natalie/test/natalie/singleton_class_test.rb -rw-r--r-- 1.2 KiB
f5a482b5Tim Morgan Add Method#owner and Class#singleton_class? 2 months ago
                                                                                
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
require_relative '../spec_helper'

class Foo
end

class Bar < Foo
end

describe 'singleton_class' do
  it 'has the correct hierarchy' do
    klass = Bar.singleton_class
    klass.inspect.should == '#<Class:Bar>'
    klass = klass.superclass
    klass.inspect.should == '#<Class:Foo>'
    klass = klass.superclass
    klass.inspect.should == '#<Class:Object>'
    klass = klass.superclass
    klass.inspect.should == '#<Class:BasicObject>'
    klass = klass.superclass
    klass.should == Class
    klass = klass.superclass
    klass.should == Module
    klass = klass.superclass
    klass.should == Object
    klass = klass.superclass
    klass.should == BasicObject
    klass = klass.superclass
    klass.should == nil

    klass = Class.singleton_class
    klass.inspect.should == '#<Class:Class>'
    klass = klass.superclass
    klass.inspect.should == '#<Class:Module>'
    klass = klass.superclass
    klass.inspect.should == '#<Class:Object>'
    klass = klass.superclass
    klass.inspect.should == '#<Class:BasicObject>'
    klass = klass.superclass
    klass.should == Class
  end

  it 'knows it is a singleton class' do
    Bar.singleton_class.singleton_class?.should == true
    Bar.singleton_class?.should == false
  end
end