Updated MSpec source to a54f23d0.
[rbx.git] / spec / frozen / 1.8 / language / for_spec.rb
blob40feb4ae3981d9acc7566b70511b5ce99d4667b7
1 require File.dirname(__FILE__) + '/../spec_helper'
3 # for name[, name]... in expr [do]
4 #   body
5 # end
6 describe "The for expression" do
7   it "iterates over an Enumerable passing each element to the block" do
8     j = 0
9     for i in 1..3
10       j += i
11     end
12     j.should == 6
13   end
15   it "iterates over an Hash passing each key-value pair to the block" do
16     k = 0
17     l = 0
18     
19     for i, j in { 1 => 10, 2 => 20 }
20       k += i
21       l += j
22     end
23     
24     k.should == 3
25     l.should == 30
26   end
27   
28   it "iterates over any object responding to 'each'" do
29     class XYZ
30       def each
31         (0..10).each { |i| yield i }
32       end
33     end
34     
35     j = 0
36     for i in XYZ.new
37       j += i
38     end
39     j.should == 55
40   end
42   it "allows an instance variable as an iterator name" do
43     m = [1,2,3]
44     n = 0
45     for @var in m
46       n += 1
47     end
48     @var.should == 3
49     n.should == 3
50   end
51   
52   # TODO: commented out due to a compiler error
53   #it "allows a class variable as an iterator name" do
54   #  m = [1,2,3]
55   #  n = 0
56   #  for @@var in m
57   #    n += 1
58   #  end
59   #  @@var.should == 3
60   #  n.should == 3
61   #end
63   it "splats multiple arguments together if there are fewer arguments than values" do
64     class OFor
65       def each
66         [[1,2,3], [4,5,6]].each do |a|
67           yield(a[0],a[1],a[2])
68         end
69       end
70     end
71     o = OFor.new
72     qs = []
73     for q in o
74       qs << q
75     end
76     qs.should == [[1,2,3], [4,5,6]]
77     q.should == [4,5,6]
78   end
79   
80   it "optionally takes a 'do' after the expression" do
81     j = 0
82     for i in 1..3 do
83       j += i
84     end
85     j.should == 6
86   end
87   
88   it "allows body begin on the same line if do is used" do
89     j = 0
90     for i in 1..3 do j += i
91     end
92     j.should == 6
93   end
94   
95   it "executes code in containing variable scope" do
96     for i in 1..2
97       a = 123
98     end
99     
100     a.should == 123
101   end
102   
103   it "executes code in containing variable scope with 'do'" do
104     for i in 1..2 do
105       a = 123
106     end
107     
108     a.should == 123
109   end
111   it "returns expr" do
112     for i in 1..3; end.should == (1..3)
113     for i,j in { 1 => 10, 2 => 20 }; end.should == { 1 => 10, 2 => 20 }
114   end
116   it "breaks out of a loop upon 'break', returning nil" do
117     j = 0
118     for i in 1..3
119       j += i
121       break if i == 2
122     end.should == nil
123     
124     j.should == 3
125   end
126   
127   it "allows 'break' to have an argument which becomes the value of the for expression" do
128     for i in 1..3
129       break 10 if i == 2
130     end.should == 10
131   end
133   it "starts the next iteration with 'next'" do
134     j = 0
135     for i in 1..5
136       next if i == 2
138       j += i
139     end
140     
141     j.should == 13
142   end
143   
144   it "repeats current iteration with 'redo'" do
145     j = 0
146     for i in 1..3
147       j += i
148       
149       redo if i == 2 && j < 4
150     end
151     
152     j.should == 8
153   end
154   
155   it "repeats the loop from the beginning with 'retry'" do
156     j = 0
157     for i in 1..5
158       j += i
159       
160       retry if i == 3 && j < 7
161     end
162     
163     j.should == 21
164   end