syntax.rb 7.83 KB
Newer Older
1
assert('__FILE__') do
mimaki's avatar
mimaki committed
2 3
  file = __FILE__[-9, 9]
  assert_equal 'syntax.rb', file
4 5 6
end

assert('__LINE__') do
7
  assert_equal 7, __LINE__
8 9
end

10
assert('super', '11.3.4') do
Daniel Bovensiepen's avatar
Daniel Bovensiepen committed
11
  assert_raise NoMethodError do
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    super
  end

  class SuperFoo
    def foo
      true
    end
    def bar(*a)
      a
    end
  end
  class SuperBar < SuperFoo
    def foo
      super
    end
    def bar(*a)
      super(*a)
    end
  end
  bar = SuperBar.new
Daniel Bovensiepen's avatar
Daniel Bovensiepen committed
32 33

  assert_true bar.foo
34
  assert_equal [1,2,3], bar.bar(1,2,3)
35 36 37
end

assert('yield', '11.3.5') do
Daniel Bovensiepen's avatar
Daniel Bovensiepen committed
38
  assert_raise LocalJumpError do
39 40
    yield
  end
41 42 43 44 45 46 47
  assert_raise LocalJumpError do
    o = Object.new
    def o.foo
      yield
    end
    o.foo
  end
48 49
end

50 51 52 53 54 55 56 57 58 59 60 61 62
assert('redo in a for loop (#3275)') do
  sum = 0
  for i in 1..10
    sum += i
    i -= 1
    if i > 0
      redo
    end
  end

  assert_equal 220, sum
end

63 64 65 66 67
assert('Abbreviated variable assignment', '11.4.2.3.2') do
  a ||= 1
  b &&= 1
  c = 1
  c += 2
Daniel Bovensiepen's avatar
Daniel Bovensiepen committed
68

69
  assert_equal 1, a
Daniel Bovensiepen's avatar
Daniel Bovensiepen committed
70
  assert_nil b
71
  assert_equal 3, c
72
end
Yukihiro Matsumoto's avatar
Yukihiro Matsumoto committed
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
assert('case expression', '11.5.2.2.4') do
  # case-expression-with-expression, one when-clause
  x = 0
  case "a"
  when "a"
    x = 1
  end
  assert_equal 1, x

  # case-expression-with-expression, multiple when-clauses
  x = 0
  case "b"
  when "a"
    x = 1
  when "b"
    x = 2
  end
  assert_equal 2, x

  # no matching when-clause
  x = 0
  case "c"
  when "a"
    x = 1
  when "b"
    x = 2
  end
  assert_equal 0, x

  # case-expression-with-expression, one when-clause and one else-clause
  a = 0
  case "c"
  when "a"
    x = 1
  else
    x = 3
  end
  assert_equal 3, x

  # case-expression-without-expression, one when-clause
  x = 0
  case
  when true
    x = 1
  end
  assert_equal 1, x

  # case-expression-without-expression, multiple when-clauses
  x = 0
  case
  when 0 == 1
    x = 1
  when 1 == 1
    x = 2
  end
  assert_equal 2, x

  # case-expression-without-expression, one when-clause and one else-clause
  x = 0
  case
  when 0 == 1
    x = 1
  else
    x = 3
  end
  assert_equal 3, x

  # multiple when-arguments
  x = 0
  case 4
  when 1, 3, 5
    x = 1
  when 2, 4, 6
    x = 2
  end
  assert_equal 2, x

  # when-argument with splatting argument
  x = :integer
  odds  = [ 1, 3, 5, 7, 9 ]
  evens = [ 2, 4, 6, 8 ]
  case 5
  when *odds
    x = :odd
  when *evens
    x = :even
  end
  assert_equal :odd, x

  true
end

Yukihiro Matsumoto's avatar
Yukihiro Matsumoto committed
166 167 168 169 170 171 172 173 174
assert('Nested const reference') do
  module Syntax4Const
    CONST1 = "hello world"
    class Const2
      def const1
        CONST1
      end
    end
  end
175 176
  assert_equal "hello world", Syntax4Const::CONST1
  assert_equal "hello world", Syntax4Const::Const2.new.const1
Yukihiro Matsumoto's avatar
Yukihiro Matsumoto committed
177
end
178 179 180 181 182 183 184 185 186

assert('Abbreviated variable assignment as returns') do
  module Syntax4AbbrVarAsgnAsReturns
    class A
      def b
        @c ||= 1
      end
    end
  end
187
  assert_equal 1, Syntax4AbbrVarAsgnAsReturns::A.new.b
188
end
Carson McDonald's avatar
Carson McDonald committed
189

190
assert('Splat and multiple assignment') do
Carson McDonald's avatar
Carson McDonald committed
191 192 193 194 195 196
  *a = *[1,2,3]
  b, *c = *[7,8,9]

  assert_equal [1,2,3], a
  assert_equal 7, b
  assert_equal [8,9], c
197 198 199 200 201

  (a, b), c = [1,2],3
  assert_equal [1,2,3], [a,b,c]
  (a, b), c = 1,2,3
  assert_equal [1,nil,2], [a,b,c]
Carson McDonald's avatar
Carson McDonald committed
202
end
h2so5's avatar
h2so5 committed
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
assert('Splat and multiple assignment from variable') do
  a = [1, 2, 3]
  b, *c = a

  assert_equal 1, b
  assert_equal [2, 3], c
end

assert('Splat and multiple assignment from variables') do
  a = [1, 2, 3]
  b = [4, 5, 6, 7]
  c, d, *e, f, g = *a, *b

  assert_equal 1, c
  assert_equal 2, d
  assert_equal [3, 4, 5], e
  assert_equal 6, f
  assert_equal 7, g
end

assert('Splat and multiple assignment in for') do
  a = [1, 2, 3, 4, 5, 6, 7]
  for b, c, *d, e, f in [a] do
  end

  assert_equal 1, b
  assert_equal 2, c
  assert_equal [3, 4, 5], d
  assert_equal 6, e
  assert_equal 7, f
end

236 237 238 239 240 241
assert('Splat without assignment') do
  * = [0]
  a, * = [1, 2]
  assert_equal 1, a
end

242 243 244 245 246 247 248 249 250 251
assert('multiple assignment (rest)') do
  *a = 0
  assert_equal [0], a
end

assert('multiple assignment (rest+post)') do
  *a, b = 0, 1, 2
  *c, d = 3

  assert_equal [0, 1], a
252
  assert_equal 2, b
253
  assert_equal [], c
254
  assert_equal 3, d
255 256
end

cremno's avatar
cremno committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270
assert('multiple assignment (nosplat array rhs)') do
  a, *b = []
  *c, d = [0]
  e, *f, g = [1, 2]

  assert_nil a
  assert_equal [], b
  assert_equal [], c
  assert_equal 0, d
  assert_equal 1, e
  assert_equal [], f
  assert_equal 2, g
end

271 272 273 274 275 276 277
assert('multiple assignment (empty array rhs #3236, #3239)') do
  a,b,*c = []; assert_equal [nil, nil, []], [a, b, c]
  a,b,*c = [1]; assert_equal [1, nil, []], [a, b, c]
  a,b,*c = [nil]; assert_equal [nil,nil, []], [a, b, c]
  a,b,*c = [[]]; assert_equal [[], nil, []], [a, b, c]
end

h2so5's avatar
h2so5 committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
assert('Return values of case statements') do
  a = [] << case 1
  when 3 then 2
  when 2 then 2
  when 1 then 2
  end

  b = [] << case 1
  when 2 then 2
  else
  end

  def fb
    n = 0
    Proc.new do
      n += 1
      case
      when n % 15 == 0
      else n
      end
    end
  end

  assert_equal [2], a
  assert_equal [nil], b
  assert_equal 1, fb.call
end
305

306 307 308 309 310 311 312 313 314 315 316 317 318 319
assert('Return values of if and case statements') do
  true_clause_value =
    if true
      1
    else
      case 2
      when 3
      end
      4
    end

  assert_equal 1, true_clause_value
end

320 321 322 323 324 325 326 327 328 329
assert('Return values of no expression case statement') do
  when_value =
    case
    when true
      1
    end

  assert_equal 1, when_value
end

ksss's avatar
ksss committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
assert('splat object in assignment') do
  o = Object.new
  def o.to_a
    nil
  end
  assert_equal [o], (a = *o)

  def o.to_a
    1
  end
  assert_raise(TypeError) { a = *o }

  def o.to_a
    [2]
  end
  assert_equal [2], (a = *o)
end

assert('splat object in case statement') do
  o = Object.new
  def o.to_a
    nil
  end
  a = case o
  when *o
    1
  end
  assert_equal 1, a
end

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
assert('splat in case statement') do
  values = [3,5,1,7,8]
  testa = [1,2,7]
  testb = [5,6]
  resulta = []
  resultb = []
  resultc = []
  values.each do |value|
    case value
    when *testa
      resulta << value
    when *testb
      resultb << value
    else
      resultc << value
    end
  end

  assert_equal [1,7], resulta
  assert_equal [5], resultb
  assert_equal [3,8], resultc
end
take_cheeze's avatar
take_cheeze committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

assert('External command execution.') do
  class << Kernel
    sym = '`'.to_sym
    alias_method :old_cmd, sym

    results = []
    define_method(sym) do |str|
      results.push str
      str
    end

    `test` # NOVAL NODE_XSTR
    `test dynamic #{sym}` # NOVAL NODE_DXSTR
    assert_equal ['test', 'test dynamic `'], results

    t = `test` # VAL NODE_XSTR
    assert_equal 'test', t
    assert_equal ['test', 'test dynamic `', 'test'], results

    t = `test dynamic #{sym}` # VAL NODE_DXSTR
    assert_equal 'test dynamic `', t
    assert_equal ['test', 'test dynamic `', 'test', 'test dynamic `'], results

    alias_method sym, :old_cmd
  end
  true
end
410 411 412 413 414 415 416 417

assert('parenthesed do-block in cmdarg') do
  class ParenDoBlockCmdArg
    def test(block)
      block.call
    end
  end
  x = ParenDoBlockCmdArg.new
418
  result = x.test (Proc.new do :ok; end)
419 420
  assert_equal :ok, result
end
421 422 423 424 425 426 427

assert('method definition in cmdarg') do
  if false
    bar def foo; self.each do end end
  end
  true
end
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

assert('optional argument in the rhs default expressions') do
  class OptArgInRHS
    def foo
      "method called"
    end
    def t(foo = foo)
      foo
    end
    def t2(foo = foo())
      foo
    end
  end
  o = OptArgInRHS.new
  assert_nil(o.t)
  assert_equal("method called", o.t2)
end
445 446

assert('optional block argument in the rhs default expressions') do
447
  assert_nil(Proc.new {|foo = foo| foo}.call)
448
end
449 450 451 452 453 454 455 456 457 458 459 460 461 462

assert('multiline comments work correctly') do
=begin
this is a comment with nothing after begin and end
=end
=begin  this is a comment 
this is a comment with extra after =begin
=end
=begin
this is a comment that has =end with spaces after it
=end  
=begin this is a comment
this is a comment that has extra after =begin and =end with spaces after it
=end  
463
  line = __LINE__
Nobuyoshi Nakada's avatar
Nobuyoshi Nakada committed
464 465
=begin	this is a comment
this is a comment that has extra after =begin and =end with tabs after it
466
=end	xxxxxxxxxxxxxxxxxxxxxxxxxx
467
  assert_equal(line + 4, __LINE__)
468
end