PageRenderTime 22ms CodeModel.GetById 10ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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