mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
Adding tests for CoffeeScript.eval
This commit is contained in:
29
test/eval.coffee
Normal file
29
test/eval.coffee
Normal file
@@ -0,0 +1,29 @@
|
||||
{Script} = require 'vm'
|
||||
|
||||
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", ->
|
||||
sandbox = Script.createContext()
|
||||
sandbox.foo = 'bar'
|
||||
code = '''
|
||||
global.foo = 'not bar!'
|
||||
'''
|
||||
result = CoffeeScript.eval code, {sandbox}
|
||||
eq result, 'not bar!'
|
||||
eq sandbox.foo, 'not bar!'
|
||||
|
||||
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'
|
||||
Reference in New Issue
Block a user