1module EnumerableSpecs
2
3  class Numerous
4    include Enumerable
5    def initialize(*list)
6      @list = list.empty? ? [2, 5, 3, 6, 1, 4] : list
7    end
8
9    def each
10      @list.each { |i| yield i }
11    end
12  end
13
14  class NumerousWithSize < Numerous
15    def size
16      @list.size
17    end
18  end
19
20  class EachCounter < Numerous
21    attr_reader :times_called, :times_yielded, :arguments_passed
22    def initialize(*list)
23      super(*list)
24      @times_yielded = @times_called = 0
25    end
26
27    def each(*arg)
28      @times_called += 1
29      @times_yielded = 0
30      @arguments_passed = arg
31      @list.each do |i|
32        @times_yielded +=1
33        yield i
34      end
35    end
36  end
37
38  class Empty
39    include Enumerable
40    def each
41    end
42  end
43
44  class EmptyWithSize
45    include Enumerable
46    def each
47    end
48    def size
49      0
50    end
51  end
52
53  class ThrowingEach
54    include Enumerable
55    def each
56      raise "from each"
57    end
58  end
59
60  class NoEach
61    include Enumerable
62  end
63
64  # (Legacy form rubycon)
65  class EachDefiner
66
67    include Enumerable
68
69    attr_reader :arr
70
71    def initialize(*arr)
72      @arr = arr
73    end
74
75    def each
76      i = 0
77      loop do
78        break if i == @arr.size
79        yield @arr[i]
80        i += 1
81      end
82    end
83
84  end
85
86  class SortByDummy
87    def initialize(s)
88      @s = s
89    end
90
91    def s
92      @s
93    end
94  end
95
96  class ComparesByVowelCount
97
98    attr_accessor :value, :vowels
99
100    def self.wrap(*args)
101      args.map {|element| ComparesByVowelCount.new(element)}
102    end
103
104    def initialize(string)
105      self.value = string
106      self.vowels = string.gsub(/[^aeiou]/, '').size
107    end
108
109    def <=>(other)
110      self.vowels <=> other.vowels
111    end
112
113  end
114
115  class InvalidComparable
116    def <=>(other)
117      "Not Valid"
118    end
119  end
120
121  class ArrayConvertable
122    attr_accessor :called
123    def initialize(*values)
124      @values = values
125    end
126
127    def to_a
128      self.called = :to_a
129      @values
130    end
131
132    def to_ary
133      self.called = :to_ary
134      @values
135    end
136  end
137
138  class EnumConvertable
139    attr_accessor :called
140    attr_accessor :sym
141    def initialize(delegate)
142      @delegate = delegate
143    end
144
145    def to_enum(sym)
146      self.called = :to_enum
147      self.sym = sym
148      @delegate.to_enum(sym)
149    end
150
151    def respond_to_missing?(*args)
152      @delegate.respond_to?(*args)
153    end
154  end
155
156  class Equals
157    def initialize(obj)
158      @obj = obj
159    end
160    def ==(other)
161      @obj == other
162    end
163  end
164
165  class YieldsMulti
166    include Enumerable
167    def each
168      yield 1,2
169      yield 3,4,5
170      yield 6,7,8,9
171    end
172  end
173
174  class YieldsMultiWithFalse
175    include Enumerable
176    def each
177      yield false,2
178      yield false,4,5
179      yield false,7,8,9
180    end
181  end
182
183  class YieldsMultiWithSingleTrue
184    include Enumerable
185    def each
186      yield false,2
187      yield true,4,5
188      yield false,7,8,9
189    end
190  end
191
192  class YieldsMixed
193    include Enumerable
194    def each
195      yield 1
196      yield [2]
197      yield 3,4
198      yield 5,6,7
199      yield [8,9]
200      yield nil
201      yield []
202    end
203  end
204
205  class YieldsMixed2
206    include Enumerable
207
208    def self.first_yields
209      [nil, 0, 0, 0, 0, nil, :default_arg, [], [], [0], [0, 1], [0, 1, 2]]
210    end
211
212    def self.gathered_yields
213      [nil, 0, [0, 1], [0, 1, 2], [0, 1, 2], nil, :default_arg, [], [], [0], [0, 1], [0, 1, 2]]
214    end
215
216    def self.gathered_yields_with_args(arg, *args)
217      [nil, 0, [0, 1], [0, 1, 2], [0, 1, 2], nil, arg, args, [], [0], [0, 1], [0, 1, 2]]
218    end
219
220    def self.greedy_yields
221      [[], [0], [0, 1], [0, 1, 2], [0, 1, 2], [nil], [:default_arg], [[]], [[]], [[0]], [[0, 1]], [[0, 1, 2]]]
222    end
223
224    def each(arg=:default_arg, *args)
225      yield
226      yield 0
227      yield 0, 1
228      yield 0, 1, 2
229      yield(*[0, 1, 2])
230      yield nil
231      yield arg
232      yield args
233      yield []
234      yield [0]
235      yield [0, 1]
236      yield [0, 1, 2]
237    end
238  end
239
240  class ReverseComparable
241    include Comparable
242    def initialize(num)
243      @num = num
244    end
245
246    attr_accessor :num
247
248    # Reverse comparison
249    def <=>(other)
250      other.num <=> @num
251    end
252  end
253
254  class ComparableWithFixnum
255    include Comparable
256    def initialize(num)
257      @num = num
258    end
259
260    def <=>(fixnum)
261      @num <=> fixnum
262    end
263  end
264
265  class Uncomparable
266    def <=>(obj)
267      nil
268    end
269  end
270
271  class Undupable
272    attr_reader :initialize_called, :initialize_dup_called
273    def dup
274      raise "Can't, sorry"
275    end
276
277    def clone
278      raise "Can't, either, sorry"
279    end
280
281    def initialize
282      @initialize_dup = true
283    end
284
285    def initialize_dup(arg)
286      @initialize_dup_called = true
287    end
288  end
289
290  class Freezy
291    include Enumerable
292
293    def each
294      yield 1
295      yield 2
296    end
297
298    def to_a
299      super.freeze
300    end
301  end
302
303  class MapReturnsEnumerable
304    include Enumerable
305
306    class EnumerableMapping
307      include Enumerable
308
309      def initialize(items, block)
310        @items = items
311        @block = block
312      end
313
314      def each
315        @items.each do |i|
316          yield @block.call(i)
317        end
318      end
319    end
320
321    def each
322      yield 1
323      yield 2
324      yield 3
325    end
326
327    def map(&block)
328      EnumerableMapping.new(self, block)
329    end
330  end
331
332  class Pattern
333    attr_reader :yielded
334
335    def initialize(&block)
336      @block = block
337      @yielded = []
338    end
339
340    def ===(*args)
341      @yielded << args
342      @block.call(*args)
343    end
344  end
345end # EnumerableSpecs utility classes
346