source: EcnlProtoTool/trunk/mruby-1.3.0/test/t/syntax.rb@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 7.8 KB
Line 
1assert('__FILE__') do
2 file = __FILE__[-9, 9]
3 assert_equal 'syntax.rb', file
4end
5
6assert('__LINE__') do
7 assert_equal 7, __LINE__
8end
9
10assert('super', '11.3.4') do
11 assert_raise NoMethodError do
12 super
13 end
14
15 class SuperFoo
16 def foo
17 true
18 end
19 def bar(*a)
20 a
21 end
22 end
23 class SuperBar < SuperFoo
24 def foo
25 super
26 end
27 def bar(*a)
28 super(*a)
29 end
30 end
31 bar = SuperBar.new
32
33 assert_true bar.foo
34 assert_equal [1,2,3], bar.bar(1,2,3)
35end
36
37assert('yield', '11.3.5') do
38 assert_raise LocalJumpError do
39 yield
40 end
41 assert_raise LocalJumpError do
42 o = Object.new
43 def o.foo
44 yield
45 end
46 o.foo
47 end
48end
49
50assert('redo in a for loop (#3275)') do
51 sum = 0
52 for i in 1..10
53 sum += i
54 i -= 1
55 if i > 0
56 redo
57 end
58 end
59
60 assert_equal 220, sum
61end
62
63assert('Abbreviated variable assignment', '11.4.2.3.2') do
64 a ||= 1
65 b &&= 1
66 c = 1
67 c += 2
68
69 assert_equal 1, a
70 assert_nil b
71 assert_equal 3, c
72end
73
74assert('case expression', '11.5.2.2.4') do
75 # case-expression-with-expression, one when-clause
76 x = 0
77 case "a"
78 when "a"
79 x = 1
80 end
81 assert_equal 1, x
82
83 # case-expression-with-expression, multiple when-clauses
84 x = 0
85 case "b"
86 when "a"
87 x = 1
88 when "b"
89 x = 2
90 end
91 assert_equal 2, x
92
93 # no matching when-clause
94 x = 0
95 case "c"
96 when "a"
97 x = 1
98 when "b"
99 x = 2
100 end
101 assert_equal 0, x
102
103 # case-expression-with-expression, one when-clause and one else-clause
104 a = 0
105 case "c"
106 when "a"
107 x = 1
108 else
109 x = 3
110 end
111 assert_equal 3, x
112
113 # case-expression-without-expression, one when-clause
114 x = 0
115 case
116 when true
117 x = 1
118 end
119 assert_equal 1, x
120
121 # case-expression-without-expression, multiple when-clauses
122 x = 0
123 case
124 when 0 == 1
125 x = 1
126 when 1 == 1
127 x = 2
128 end
129 assert_equal 2, x
130
131 # case-expression-without-expression, one when-clause and one else-clause
132 x = 0
133 case
134 when 0 == 1
135 x = 1
136 else
137 x = 3
138 end
139 assert_equal 3, x
140
141 # multiple when-arguments
142 x = 0
143 case 4
144 when 1, 3, 5
145 x = 1
146 when 2, 4, 6
147 x = 2
148 end
149 assert_equal 2, x
150
151 # when-argument with splatting argument
152 x = :integer
153 odds = [ 1, 3, 5, 7, 9 ]
154 evens = [ 2, 4, 6, 8 ]
155 case 5
156 when *odds
157 x = :odd
158 when *evens
159 x = :even
160 end
161 assert_equal :odd, x
162
163 true
164end
165
166assert('Nested const reference') do
167 module Syntax4Const
168 CONST1 = "hello world"
169 class Const2
170 def const1
171 CONST1
172 end
173 end
174 end
175 assert_equal "hello world", Syntax4Const::CONST1
176 assert_equal "hello world", Syntax4Const::Const2.new.const1
177end
178
179assert('Abbreviated variable assignment as returns') do
180 module Syntax4AbbrVarAsgnAsReturns
181 class A
182 def b
183 @c ||= 1
184 end
185 end
186 end
187 assert_equal 1, Syntax4AbbrVarAsgnAsReturns::A.new.b
188end
189
190assert('Splat and multiple assignment') do
191 *a = *[1,2,3]
192 b, *c = *[7,8,9]
193
194 assert_equal [1,2,3], a
195 assert_equal 7, b
196 assert_equal [8,9], c
197
198 (a, b), c = [1,2],3
199 assert_equal [1,2,3], [a,b,c]
200 (a, b), c = 1,2,3
201 assert_equal [1,nil,2], [a,b,c]
202end
203
204assert('Splat and multiple assignment from variable') do
205 a = [1, 2, 3]
206 b, *c = a
207
208 assert_equal 1, b
209 assert_equal [2, 3], c
210end
211
212assert('Splat and multiple assignment from variables') do
213 a = [1, 2, 3]
214 b = [4, 5, 6, 7]
215 c, d, *e, f, g = *a, *b
216
217 assert_equal 1, c
218 assert_equal 2, d
219 assert_equal [3, 4, 5], e
220 assert_equal 6, f
221 assert_equal 7, g
222end
223
224assert('Splat and multiple assignment in for') do
225 a = [1, 2, 3, 4, 5, 6, 7]
226 for b, c, *d, e, f in [a] do
227 end
228
229 assert_equal 1, b
230 assert_equal 2, c
231 assert_equal [3, 4, 5], d
232 assert_equal 6, e
233 assert_equal 7, f
234end
235
236assert('Splat without assignment') do
237 * = [0]
238 a, * = [1, 2]
239 assert_equal 1, a
240end
241
242assert('multiple assignment (rest)') do
243 *a = 0
244 assert_equal [0], a
245end
246
247assert('multiple assignment (rest+post)') do
248 *a, b = 0, 1, 2
249 *c, d = 3
250
251 assert_equal [0, 1], a
252 assert_equal 2, b
253 assert_equal [], c
254 assert_equal 3, d
255end
256
257assert('multiple assignment (nosplat array rhs)') do
258 a, *b = []
259 *c, d = [0]
260 e, *f, g = [1, 2]
261
262 assert_nil a
263 assert_equal [], b
264 assert_equal [], c
265 assert_equal 0, d
266 assert_equal 1, e
267 assert_equal [], f
268 assert_equal 2, g
269end
270
271assert('multiple assignment (empty array rhs #3236, #3239)') do
272 a,b,*c = []; assert_equal [nil, nil, []], [a, b, c]
273 a,b,*c = [1]; assert_equal [1, nil, []], [a, b, c]
274 a,b,*c = [nil]; assert_equal [nil,nil, []], [a, b, c]
275 a,b,*c = [[]]; assert_equal [[], nil, []], [a, b, c]
276end
277
278assert('Return values of case statements') do
279 a = [] << case 1
280 when 3 then 2
281 when 2 then 2
282 when 1 then 2
283 end
284
285 b = [] << case 1
286 when 2 then 2
287 else
288 end
289
290 def fb
291 n = 0
292 Proc.new do
293 n += 1
294 case
295 when n % 15 == 0
296 else n
297 end
298 end
299 end
300
301 assert_equal [2], a
302 assert_equal [nil], b
303 assert_equal 1, fb.call
304end
305
306assert('Return values of if and case statements') do
307 true_clause_value =
308 if true
309 1
310 else
311 case 2
312 when 3
313 end
314 4
315 end
316
317 assert_equal 1, true_clause_value
318end
319
320assert('Return values of no expression case statement') do
321 when_value =
322 case
323 when true
324 1
325 end
326
327 assert_equal 1, when_value
328end
329
330assert('splat object in assignment') do
331 o = Object.new
332 def o.to_a
333 nil
334 end
335 assert_equal [o], (a = *o)
336
337 def o.to_a
338 1
339 end
340 assert_raise(TypeError) { a = *o }
341
342 def o.to_a
343 [2]
344 end
345 assert_equal [2], (a = *o)
346end
347
348assert('splat object in case statement') do
349 o = Object.new
350 def o.to_a
351 nil
352 end
353 a = case o
354 when *o
355 1
356 end
357 assert_equal 1, a
358end
359
360assert('splat in case statement') do
361 values = [3,5,1,7,8]
362 testa = [1,2,7]
363 testb = [5,6]
364 resulta = []
365 resultb = []
366 resultc = []
367 values.each do |value|
368 case value
369 when *testa
370 resulta << value
371 when *testb
372 resultb << value
373 else
374 resultc << value
375 end
376 end
377
378 assert_equal [1,7], resulta
379 assert_equal [5], resultb
380 assert_equal [3,8], resultc
381end
382
383assert('External command execution.') do
384 module Kernel
385 sym = '`'.to_sym
386 alias_method :old_cmd, sym
387
388 results = []
389 define_method(sym) do |str|
390 results.push str
391 str
392 end
393
394 `test` # NOVAL NODE_XSTR
395 `test dynamic #{sym}` # NOVAL NODE_DXSTR
396 assert_equal ['test', 'test dynamic `'], results
397
398 t = `test` # VAL NODE_XSTR
399 assert_equal 'test', t
400 assert_equal ['test', 'test dynamic `', 'test'], results
401
402 t = `test dynamic #{sym}` # VAL NODE_DXSTR
403 assert_equal 'test dynamic `', t
404 assert_equal ['test', 'test dynamic `', 'test', 'test dynamic `'], results
405
406 alias_method sym, :old_cmd
407 end
408 true
409end
410
411assert('parenthesed do-block in cmdarg') do
412 class ParenDoBlockCmdArg
413 def test(block)
414 block.call
415 end
416 end
417 x = ParenDoBlockCmdArg.new
418 result = x.test (Proc.new do :ok; end)
419 assert_equal :ok, result
420end
421
422assert('method definition in cmdarg') do
423 if false
424 bar def foo; self.each do end end
425 end
426 true
427end
428
429assert('optional argument in the rhs default expressions') do
430 class OptArgInRHS
431 def foo
432 "method called"
433 end
434 def t(foo = foo)
435 foo
436 end
437 def t2(foo = foo())
438 foo
439 end
440 end
441 o = OptArgInRHS.new
442 assert_nil(o.t)
443 assert_equal("method called", o.t2)
444end
445
446assert('optional block argument in the rhs default expressions') do
447 assert_nil(Proc.new {|foo = foo| foo}.call)
448end
449
450assert('multiline comments work correctly') do
451=begin
452this is a comment with nothing after begin and end
453=end
454=begin this is a comment
455this is a comment with extra after =begin
456=end
457=begin
458this is a comment that has =end with spaces after it
459=end
460=begin this is a comment
461this is a comment that has extra after =begin and =end with spaces after it
462=end
463 line = __LINE__
464=begin this is a comment
465this is a comment that has extra after =begin and =end with tabs after it
466=end xxxxxxxxxxxxxxxxxxxxxxxxxx
467 assert_equal(line + 4, __LINE__)
468end
Note: See TracBrowser for help on using the repository browser.