File: test_multiton.rb

package info (click to toggle)
ruby-facets 2.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,824 kB
  • sloc: ruby: 25,483; xml: 90; makefile: 20
file content (184 lines) | stat: -rw-r--r-- 3,565 bytes parent folder | download | duplicates (2)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
require 'test/unit'
require 'facets/multiton'

#
# EXAMPLE A - STANDARD USAGE
#
class TC_Multiton_A < Test::Unit::TestCase

  class SomeMultitonClass
    include Multiton

    attr :arg
    def initialize(arg)
      @arg = arg
    end
  end

  def test_standard

    a = SomeMultitonClass.instance(4)
    b = SomeMultitonClass.instance(4)     # a and b are same object
    c = SomeMultitonClass.instance(2)     # c is a different object

    assert_equal( a, b )
    assert_equal( 42, [a.arg,b.arg].max * 10 + c.arg )

  end

end

#
# EXAMPLE B - MODIFY AN EXISTING CLASS, SHARED FILEHANDLES
#
class TC_Multiton_B < Test::Unit::TestCase

  class ::File
    include Multiton
  end

  def test_modify_existing

    lineno = __LINE__
    # HERE1
    # HERE2

    a = File.instance(__FILE__)
    b = File.instance(__FILE__)

    assert_equal( a, b )

    lineno.times{ a.gets }

    assert_equal( "# HERE1", a.gets.strip )
    assert_equal( "# HERE2", b.gets.strip )

  end

end

#
# EXAMPLE C - INHERITENCE
#
class TC_Multiton_C < Test::Unit::TestCase

  class A < String
    include Multiton
  end

  # B is also a multiton - with it's OWN object cache
  class B < A; end

  def test_inheritence

    # w is the same object as x, y is the same object as z
    w,x,y,z = A.instance('A'), A.instance('A'), B.instance('B'), B.instance('B')

    assert_equal( w.object_id, x.object_id ) # -> true
    assert_equal( y.object_id, z.object_id ) # -> true

    a = B.instance('A')
    b = B.instance('A')

    assert_not_equal( a.object_id, w.object_id ) # -> false (each class has it's own pool)
    assert_equal( a.object_id, b.object_id )     # -> true

  end

end


#
# EXAMPLE D - MULTIPLE MODULE INCLUSION (does nothing)
#
class TC_Multiton_D < Test::Unit::TestCase

  class A < String
    include Multiton
  end

  # B is also a multiton - with it's OWN object cache
  class B < A; end

  def test_multiple

    # w is the same object as x, y is the same object as z
    w,x,y,z = A.instance('A'), A.instance('A'), B.instance('B'), B.instance('B')

    yz_id = y.object_id || z.object_id

    B.class_eval {
      include Multiton
    }

    # y is not only the same object as z, but they are both the same object(s)
    # as from EXAMPLE C

    y,z = B.instance('B'), B.instance('B')

    assert_equal( y.object_id, yz_id )   # -> true
    assert_equal( z.object_id, yz_id ) # -> true

  end

end

#
# EXAMPLE E - SO YOU WANNA USE NEW INSTEAD OF INSTANCE...
#
class TC_Multiton_E < Test::Unit::TestCase

  module K
    # use an inner class which is itself a multiton
    class K < String; include Multiton; end

    # define a new which returns a mutltion using #instance...
    class << self
      def new(*args, &block)
        K.instance(*args, &block)
      end
    end
  end

  def test_new

    the = K.new '4'
    answer = K.new '2'

    assert_equal( "42", sprintf( "%s%s", the, answer ) )  #-> 42
    assert_equal( K::K, the.class )  #-> K::K

  end

end

#
# EXAMPLE F - using Klass.multiton_id
#
class TC_Multiton_F < Test::Unit::TestCase

  class Klass
    include Multiton

    def initialize( important, not_important )
      @important, @not_important = important, not_important
    end

    def Klass.multiton_id(*args, &block)
      # we consider only the first arg
      important, not_important = *args
      important
    end
  end

  def test_using_id
    a = Klass.instance( :a, :b )
    b = Klass.instance( :a, :c )
    c = Klass.instance( :b, :b )

    assert_equal( a, b )
    assert_not_equal( a, c )
    assert_not_equal( b, c )
  end

end