/test/scope.coffee

http://github.com/jashkenas/coffee-script · CoffeeScript · 128 lines · 101 code · 20 blank · 7 comment · 3 complexity · 677ea02d6bfa8a3fa24d5b2183f2c1b3 MD5 · raw file

  1. # Scope
  2. # -----
  3. # * Variable Safety
  4. # * Variable Shadowing
  5. # * Auto-closure (`do`)
  6. # * Global Scope Leaks
  7. test "reference `arguments` inside of functions", ->
  8. sumOfArgs = ->
  9. sum = (a,b) -> a + b
  10. sum = 0
  11. sum += num for num in arguments
  12. sum
  13. eq 10, sumOfArgs(0, 1, 2, 3, 4)
  14. test "assignment to an Object.prototype-named variable should not leak to outer scope", ->
  15. # FIXME: fails on IE
  16. (->
  17. constructor = 'word'
  18. )()
  19. ok constructor isnt 'word'
  20. test "siblings of splat parameters shouldn't leak to surrounding scope", ->
  21. x = 10
  22. oops = (x, args...) ->
  23. oops(20, 1, 2, 3)
  24. eq x, 10
  25. test "catch statements should introduce their argument to scope", ->
  26. try throw ''
  27. catch e
  28. do -> e = 5
  29. eq 5, e
  30. test "loop variable should be accessible after for-of loop", ->
  31. d = (x for x of {1:'a',2:'b'})
  32. ok x in ['1','2']
  33. test "loop variable should be accessible after for-in loop", ->
  34. d = (x for x in [1,2])
  35. eq x, 2
  36. test "loop variable should be accessible after for-from loop", ->
  37. d = (x for x from [1,2])
  38. eq x, 2
  39. class Array then slice: fail # needs to be global
  40. class Object then hasOwnProperty: fail
  41. test "#1973: redefining Array/Object constructors shouldn't confuse __X helpers", ->
  42. arr = [1..4]
  43. arrayEq [3, 4], arr[2..]
  44. obj = {arr}
  45. for own k of obj
  46. eq arr, obj[k]
  47. test "#2255: global leak with splatted @-params", ->
  48. ok not x?
  49. arrayEq [0], ((@x...) -> @x).call {}, 0
  50. ok not x?
  51. test "#1183: super + fat arrows", ->
  52. dolater = (cb) -> cb()
  53. class A
  54. constructor: ->
  55. @_i = 0
  56. foo : (cb) ->
  57. dolater =>
  58. @_i += 1
  59. cb()
  60. class B extends A
  61. constructor : ->
  62. super()
  63. foo : (cb) ->
  64. dolater =>
  65. dolater =>
  66. @_i += 2
  67. super cb
  68. b = new B
  69. b.foo => eq b._i, 3
  70. test "#1183: super + wrap", ->
  71. class A
  72. m : -> 10
  73. class B extends A
  74. constructor : -> super()
  75. m: -> r = try super()
  76. m: -> r = super()
  77. eq (new B).m(), 10
  78. test "#1183: super + closures", ->
  79. class A
  80. constructor: ->
  81. @i = 10
  82. foo : -> @i
  83. class B extends A
  84. foo : ->
  85. ret = switch 1
  86. when 0 then 0
  87. when 1 then super()
  88. ret
  89. eq (new B).foo(), 10
  90. test "#2331: bound super regression", ->
  91. class A
  92. @value = 'A'
  93. method: -> @constructor.value
  94. class B extends A
  95. method: => super()
  96. eq (new B).method(), 'A'
  97. test "#3259: leak with @-params within destructured parameters", ->
  98. fn = ({@foo}, [@bar], [{@baz}]) ->
  99. foo = bar = baz = false
  100. fn.call {}, {foo: 'foo'}, ['bar'], [{baz: 'baz'}]
  101. eq 'undefined', typeof foo
  102. eq 'undefined', typeof bar
  103. eq 'undefined', typeof baz