jashkenas--coffeescript/test/eval.coffee

31 lines
939 B
CoffeeScript
Raw Normal View History

2011-12-18 18:21:00 +00:00
if vm = require? 'vm'
2011-09-04 17:57:09 +00:00
2011-12-18 18:21:00 +00: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!'
2011-12-24 12:04:34 +00:00
2011-12-18 18:21:00 +00:00
test "CoffeeScript.eval can run in, and modify, a Script context sandbox", ->
2015-01-05 20:40:04 +00:00
createContext = vm.Script.createContext ? vm.createContext
sandbox = createContext()
2011-11-08 23:01:45 +00:00
sandbox.foo = 'bar'
code = '''
global.foo = 'not bar!'
'''
result = CoffeeScript.eval code, {sandbox}
eq result, 'not bar!'
eq sandbox.foo, 'not bar!'
2011-12-24 12:04:34 +00:00
2011-12-18 18:21:00 +00: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!'
2011-12-24 12:04:34 +00:00
eq sandbox.foo, 'bar'