Ejemplo n.º 1
0
 @Test
 public void test() {
   assertEquals(1, eval(read("(car '(1 2))"), env));
   assertEquals(1, eval(read("((lambda (x) (car x)) '(1 2))"), env));
   eval(read("(define kar (lambda (x) (car x)))"), env);
   assertEquals(1, eval(read("(kar '(1 2))"), env));
   eval(read("(define (qar x) (car x))"), env);
   assertEquals(1, eval(read("(qar '(1 2))"), env));
 }
Ejemplo n.º 2
0
 @Test
 public void testNamedFunction() {
   eval("var handlers = {}");
   eval("var registerHandler = function(name, f) {", "handlers[name] = f;", "}");
   eval(
       "var unregisterHandler = function(name, f) {",
       "if (handlers[name] === f) {",
       "  handlers[name] = null;",
       "}" + "}");
   eval(
       "registerHandler('myhandler', function MyHandler() {",
       "unregisterHandler('myhandler', MyHandler);",
       "return 'foobar';",
       "});");
   assertThat(eval("handlers['myhandler']()")).isEqualTo("foobar");
   assertThat(eval("handlers['myhandler'] == null")).isEqualTo(true);
 }
Ejemplo n.º 3
0
  @Test
  public void testInvalidFunction() {
    try {
      eval("function (){};");
      fail("Invalid functions should be invalid, dammit.");
    } catch (Exception e) {

    }
  }
Ejemplo n.º 4
0
  @Test
  public void testFunctionDeclaration() {
    eval("function foo() { return 42.0 };");
    Reference foo = getContext().resolve("foo");
    assertThat(foo).isNotNull();
    assertThat(foo.isUnresolvableReference()).isFalse();
    JSFunction fn = (JSFunction) foo.getValue(getContext());
    assertThat(fn).isNotNull();

    Object result = fn.call(getContext());
    assertThat(result).isEqualTo(42L);
  }
Ejemplo n.º 5
0
 @Test
 public void testFunctionPropertyNamedFoo() {
   eval("var o = new Object();");
   eval("o.foo = function() { return 'foo' }");
   assertThat(eval("o.foo()")).isEqualTo("foo");
 }
Ejemplo n.º 6
0
 @Test
 public void testFunctionPropertyNamedPrint() {
   eval("var o = new Object();");
   eval("o.print = function() { return 'printed' }");
   assertThat(eval("o.print()")).isEqualTo("printed");
 }
Ejemplo n.º 7
0
 @Test
 public void testValidFunctionExpr() {
   eval("(function (){});");
 }
Ejemplo n.º 8
0
 @Test
 public void testPrintf() {
   eval(read("(define (format f . args) (String (format f (vargs args))))"), env);
   assertEquals("a0123", eval(read("(format \"%s%04d\" \"a\" 123)"), env));
   assertEquals("01230004", eval(read("(format \"%04d%04d\" 123 4)"), env));
 }
Ejemplo n.º 9
0
 @Test
 public void testVarargsLambda() {
   eval(read("(define list (lambda x x))"), env);
   assertEquals(list(1, 2, 3), eval(read("(list 1 2 3)"), env));
 }
Ejemplo n.º 10
0
 @Test
 public void testVarargs() {
   eval(read("(define (list . b) b)"), env);
   assertEquals(list(1, 2, 3), eval(read("(list 1 2 3)"), env));
 }