File: while_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (238 lines) | stat: -rw-r--r-- 4,380 bytes parent folder | download
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
require File.expand_path('../../spec_helper', __FILE__)

# while bool-expr [do]
#   body
# end
#
# begin
#   body
# end while bool-expr
#
# expr while bool-expr
describe "The while expression" do
  it "runs while the expression is true" do
    i = 0
    while i < 3
      i += 1
    end
    i.should == 3
  end

  it "optionally takes a 'do' after the expression" do
    i = 0
    while i < 3 do
      i += 1
    end

    i.should == 3
  end

  it "allows body begin on the same line if do is used" do
    i = 0
    while i < 3 do i += 1
    end

    i.should == 3
  end

  it "executes code in containing variable scope" do
    i = 0
    while i != 1
      a = 123
      i = 1
    end

    a.should == 123
  end

  it "executes code in containing variable scope with 'do'" do
    i = 0
    while i != 1 do
      a = 123
      i = 1
    end

    a.should == 123
  end

  it "returns nil if ended when condition became false" do
    i = 0
    while i < 3
      i += 1
    end.should == nil
  end

  it "does not evaluate the body if expression is empty" do
    a = []
    while ()
      a << :body_evaluated
    end
    a.should == []
  end

  it "stops running body if interrupted by break" do
    i = 0
    while i < 10
      i += 1
      break if i > 5
    end
    i.should == 6
  end

  it "returns value passed to break if interrupted by break" do
    while true
      break 123
    end.should == 123
  end

  it "returns nil if interrupted by break with no arguments" do
    while true
      break
    end.should == nil
  end

  it "skips to end of body with next" do
    a = []
    i = 0
    while (i+=1)<5
      next if i==3
      a << i
    end
    a.should == [1, 2, 4]
  end

  it "restarts the current iteration without reevaluating condition with redo" do
    a = []
    i = 0
    j = 0
    while (i+=1)<3
      a << i
      j+=1
      redo if j<3
    end
    a.should == [1, 1, 1, 2]
  end
end

describe "The while modifier" do
  it "runs preceding statement while the condition is true" do
    i = 0
    i += 1 while i < 3
    i.should == 3
  end

  it "evaluates condition before statement execution" do
    a = []
    i = 0
    a << i while (i+=1) < 3
    a.should == [1, 2]
  end

  it "does not run preceding statement if the condition is false" do
    i = 0
    i += 1 while false
    i.should == 0
  end

  it "does not run preceding statement if the condition is empty" do
    i = 0
    i += 1 while ()
    i.should == 0
  end

  it "returns nil if ended when condition became false" do
    i = 0
    (i += 1 while i<10).should == nil
  end

  it "returns value passed to break if interrupted by break" do
    (break 123 while true).should == 123
  end

  it "returns nil if interrupted by break with no arguments" do
    (break while true).should == nil
  end

  it "skips to end of body with next" do
    i = 0
    j = 0
    ((i+=1) == 3 ? next : j+=i) while i <= 10
    j.should == 63
  end

  it "restarts the current iteration without reevaluating condition with redo" do
    i = 0
    j = 0
    (i+=1) == 4 ? redo : j+=i while (i+=1) <= 10
    j.should == 34
  end
end

describe "The while modifier with begin .. end block" do
  it "runs block while the expression is true" do
    i = 0
    begin
      i += 1
    end while i < 3

    i.should == 3
  end

  it "stops running block if interrupted by break" do
    i = 0
    begin
      i += 1
      break if i > 5
    end while i < 10

    i.should == 6
  end

  it "returns value passed to break if interrupted by break" do
    (begin; break 123; end while true).should == 123
  end

  it "returns nil if interrupted by break with no arguments" do
    (begin; break; end while true).should == nil
  end

  it "runs block at least once (even if the expression is false)" do
    i = 0
    begin
      i += 1
    end while false

    i.should == 1
  end

  it "evaluates condition after block execution" do
    a = []
    i = 0
    begin
      a << i
    end while (i+=1)<5
    a.should == [0, 1, 2, 3, 4]
  end

  it "skips to end of body with next" do
    a = []
    i = 0
    begin
      next if i==3
      a << i
    end while (i+=1)<5
    a.should == [0, 1, 2, 4]
  end

  it "restarts the current iteration without reevaluting condition with redo" do
    a = []
    i = 0
    j = 0
    begin
      a << i
      j+=1
      redo if j<3
    end while (i+=1)<3
    a.should == [0, 0, 0, 1, 2]
  end
end