| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.tests; |
| 6 |
|
| 7 |
import org.mozilla.javascript.*; |
| 8 |
import junit.framework.TestCase; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public class DefineFunctionPropertiesTest extends TestCase { |
| 16 |
ScriptableObject global; |
| 17 |
static Object key = "DefineFunctionPropertiesTest"; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
@Override |
| 24 |
public void setUp() { |
| 25 |
Context cx = Context.enter(); |
| 26 |
try { |
| 27 |
global = cx.initStandardObjects(); |
| 28 |
String[] names = { "f", "g" }; |
| 29 |
global.defineFunctionProperties(names, |
| 30 |
DefineFunctionPropertiesTest.class, |
| 31 |
ScriptableObject.DONTENUM); |
| 32 |
} finally { |
| 33 |
Context.exit(); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public static int f(int a) { |
| 41 |
return a * 2; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public void testSimpleFunction() { |
| 48 |
Context cx = Context.enter(); |
| 49 |
try { |
| 50 |
Object result = cx.evaluateString(global, "f(7) + 1", |
| 51 |
"test source", 1, null); |
| 52 |
assertEquals(15.0, result); |
| 53 |
} finally { |
| 54 |
Context.exit(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public static Object g(Context cx, Scriptable thisObj, Object[] args, |
| 66 |
Function funObj) |
| 67 |
{ |
| 68 |
Object arg = args.length > 0 ? args[0] : Undefined.instance; |
| 69 |
Object privateValue = Undefined.instance; |
| 70 |
if (thisObj instanceof ScriptableObject) { |
| 71 |
privateValue = |
| 72 |
((ScriptableObject) thisObj).getAssociatedValue(key); |
| 73 |
} |
| 74 |
return arg.toString() + privateValue; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public void testPrivateData() { |
| 82 |
Context cx = Context.enter(); |
| 83 |
try { |
| 84 |
global.associateValue(key, "bar"); |
| 85 |
Object result = cx.evaluateString(global, "g('foo');", |
| 86 |
"test source", 1, null); |
| 87 |
assertEquals("foobar", result); |
| 88 |
} finally { |
| 89 |
Context.exit(); |
| 90 |
} |
| 91 |
} |
| 92 |
} |