1# Contains all static code examples of all constants behavior in language and
2# library specs. The specs include language/constants_spec.rb and the specs
3# for Module#const_defined?, Module#const_get, Module#const_set,
4# Module#remove_const, Module#const_missing and Module#constants.
5#
6# Rather than defining a class structure for each example, a canonical set of
7# classes is used along with numerous constants, in most cases, a unique
8# constant for each facet of behavior. This potentially leads to some
9# redundancy but hopefully the minimal redundancy that includes reasonable
10# variety in class and module configurations, including hierarchy,
11# containment, inclusion, singletons and toplevel.
12#
13# Constants are numbered for for uniqueness. The CS_ prefix is uniformly used
14# and is to minimize clashes with other toplevel constants (see e.g. ModuleA
15# which is included in Object). Constant values are symbols. A numbered suffix
16# is used to distinguish constants with the same name defined in different
17# areas (e.g. CS_CONST10 has values :const10_1, :const10_2, etc.).
18#
19# Methods are named after the constants they reference (e.g. ClassA.const10
20# references CS_CONST10). Where it is reasonable to do so, both class and
21# instance methods are defined. This is an instance of redundancy (class
22# methods should behave no differently than instance methods) but is useful
23# for ensuring compliance in implementations.
24
25
26# This constant breaks the rule of defining all constants, classes, modules
27# inside a module namespace for the particular specs, however, it is needed
28# for completeness. No other constant of this name should be defined in the
29# specs.
30CS_CONST1 = :const1   # only defined here
31
32module ConstantSpecs
33
34  # Included at toplevel
35  module ModuleA
36    CS_CONST10 = :const10_1
37    CS_CONST12 = :const12_2
38    CS_CONST13 = :const13
39    CS_CONST21 = :const21_2
40  end
41
42  # Included in ParentA
43  module ModuleB
44    CS_CONST10 = :const10_9
45    CS_CONST11 = :const11_2
46    CS_CONST12 = :const12_1
47  end
48
49  # Included in ChildA
50  module ModuleC
51    CS_CONST10 = :const10_4
52    CS_CONST15 = :const15_1
53  end
54
55  # Included in ChildA metaclass
56  module ModuleH
57    CS_CONST10 = :const10_7
58  end
59
60  # Included in ModuleD
61  module ModuleM
62    CS_CONST10 = :const10_11
63    CS_CONST24 = :const24
64  end
65
66  # Included in ContainerA
67  module ModuleD
68    include ModuleM
69
70    CS_CONST10 = :const10_8
71  end
72
73  # The following classes/modules have all the constants set "statically".
74  # Contrast with the classes below where the constants are set as the specs
75  # are run.
76
77  class ClassA
78    CS_CONST10 = :const10_10
79    CS_CONST16 = :const16
80    CS_CONST17 = :const17_2
81    CS_CONST22 = :const22_1
82
83    def self.const_missing(const)
84      const
85    end
86
87    def self.constx;  CS_CONSTX;       end
88    def self.const10; CS_CONST10;      end
89    def self.const16; ParentA.const16; end
90    def self.const22; ParentA.const22 { CS_CONST22 }; end
91
92    def const10; CS_CONST10; end
93    def constx;  CS_CONSTX;  end
94  end
95
96  class ParentA
97    include ModuleB
98
99    CS_CONST4 = :const4
100    CS_CONST10 = :const10_5
101    CS_CONST11 = :const11_1
102    CS_CONST15 = :const15_2
103    CS_CONST20 = :const20_2
104    CS_CONST21 = :const21_1
105    CS_CONST22 = :const22_2
106
107    def self.constx;  CS_CONSTX;  end
108    def self.const10; CS_CONST10; end
109    def self.const16; CS_CONST16; end
110    def self.const22; yield;      end
111
112    def const10; CS_CONST10; end
113    def constx;  CS_CONSTX;  end
114  end
115
116  class ContainerA
117    include ModuleD
118
119    CS_CONST5 = :const5
120    CS_CONST10 = :const10_2
121    CS_CONST23 = :const23
122
123    class ChildA < ParentA
124      include ModuleC
125
126      class << self
127        include ModuleH
128
129        CS_CONST10 = :const10_6
130        CS_CONST14 = :const14_1
131        CS_CONST19 = :const19_1
132
133        def const19; CS_CONST19; end
134      end
135
136      CS_CONST6 = :const6
137      CS_CONST10 = :const10_3
138      CS_CONST19 = :const19_2
139
140      def self.const10; CS_CONST10; end
141      def self.const11; CS_CONST11; end
142      def self.const12; CS_CONST12; end
143      def self.const13; CS_CONST13; end
144      def self.const15; CS_CONST15; end
145      def self.const21; CS_CONST21; end
146
147      def const10; CS_CONST10; end
148      def const11; CS_CONST11; end
149      def const12; CS_CONST12; end
150      def const13; CS_CONST13; end
151      def const15; CS_CONST15; end
152    end
153
154    def self.const10; CS_CONST10; end
155
156    def const10; CS_CONST10; end
157  end
158
159  class ContainerA::ChildA
160    def self.const23; CS_CONST23; end
161  end
162
163  class ::Object
164    CS_CONST20 = :const20_1
165
166    module ConstantSpecs
167      class ContainerA
168        class ChildA
169          def self.const20; CS_CONST20; end
170        end
171      end
172    end
173  end
174
175  # Included in ParentB
176  module ModuleE
177  end
178
179  # Included in ChildB
180  module ModuleF
181  end
182
183  # Included in ContainerB
184  module ModuleG
185  end
186
187  # The following classes/modules have the same structure as the ones above
188  # but the constants are set as the specs are run.
189
190  class ClassB
191    def self.const201; CS_CONST201; end
192    def self.const209; ParentB.const209; end
193    def self.const210; ParentB.const210 { CS_CONST210 }; end
194
195    def const201; CS_CONST201; end
196  end
197
198  class ParentB
199    include ModuleE
200
201    def self.const201; CS_CONST201; end
202    def self.const209; CS_CONST209; end
203    def self.const210; yield;       end
204
205    def const201; CS_CONST201; end
206  end
207
208  class ContainerB
209    include ModuleG
210
211    class ChildB < ParentB
212      include ModuleF
213
214      class << self
215        def const206; CS_CONST206; end
216      end
217
218      def self.const201; CS_CONST201; end
219      def self.const202; CS_CONST202; end
220      def self.const203; CS_CONST203; end
221      def self.const204; CS_CONST204; end
222      def self.const205; CS_CONST205; end
223      def self.const212; CS_CONST212; end
224      def self.const213; CS_CONST213; end
225
226      def const201; CS_CONST201; end
227      def const202; CS_CONST202; end
228      def const203; CS_CONST203; end
229      def const204; CS_CONST204; end
230      def const205; CS_CONST205; end
231      def const213; CS_CONST213; end
232    end
233
234    def self.const201; CS_CONST201; end
235  end
236
237  class ContainerB::ChildB
238    def self.const214; CS_CONST214; end
239  end
240
241  class ::Object
242    module ConstantSpecs
243      class ContainerB
244        class ChildB
245          def self.const211; CS_CONST211; end
246        end
247      end
248    end
249  end
250
251  # Constants
252  CS_CONST2 = :const2   # only defined here
253  CS_CONST17 = :const17_1
254
255  class << self
256    CS_CONST14 = :const14_2
257  end
258
259  # Singleton
260  a = ClassA.new
261  def a.const17; CS_CONST17; end
262  CS_CONST18 = a
263
264  b = ClassB.new
265  def b.const207; CS_CONST207; end
266  CS_CONST208 = b
267
268  # Methods
269  def self.get_const; self; end
270
271  def const10; CS_CONST10; end
272
273  class ClassC
274    CS_CONST1 = 1
275
276    class ClassE
277      CS_CONST2 = 2
278    end
279  end
280
281  class ClassD < ClassC
282  end
283
284  CS_PRIVATE = :cs_private
285  private_constant :CS_PRIVATE
286end
287
288include ConstantSpecs::ModuleA
289