2011-12-18 13:21:00 -05:00
|
|
|
if vm = require? 'vm'
|
2011-09-04 13:57:09 -04:00
|
|
|
|
2011-12-18 13:21:00 -05:00
|
|
|
test "CoffeeScript.eval runs in the global context by default", ->
|
|
|
|
global.punctuation = '!'
|
|
|
|
code = '''
|
|
|
|
global.fhqwhgads = "global superpower#{global.punctuation}"
|
|
|
|
'''
|
|
|
|
result = CoffeeScript.eval code
|
|
|
|
eq result, 'global superpower!'
|
|
|
|
eq fhqwhgads, 'global superpower!'
|
|
|
|
|
|
|
|
test "CoffeeScript.eval can run in, and modify, a Script context sandbox", ->
|
2011-11-08 18:01:45 -05:00
|
|
|
sandbox = vm.Script.createContext()
|
|
|
|
sandbox.foo = 'bar'
|
|
|
|
code = '''
|
|
|
|
global.foo = 'not bar!'
|
|
|
|
'''
|
|
|
|
result = CoffeeScript.eval code, {sandbox}
|
|
|
|
eq result, 'not bar!'
|
|
|
|
eq sandbox.foo, 'not bar!'
|
2011-12-18 13:21:00 -05:00
|
|
|
|
|
|
|
test "CoffeeScript.eval can run in, but cannot modify, an ordinary object sandbox", ->
|
|
|
|
sandbox = {foo: 'bar'}
|
|
|
|
code = '''
|
|
|
|
global.foo = 'not bar!'
|
|
|
|
'''
|
|
|
|
result = CoffeeScript.eval code, {sandbox}
|
|
|
|
eq result, 'not bar!'
|
|
|
|
eq sandbox.foo, 'bar'
|