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