@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)); }
@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); }
@Test public void testInvalidFunction() { try { eval("function (){};"); fail("Invalid functions should be invalid, dammit."); } catch (Exception e) { } }
@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); }
@Test public void testFunctionPropertyNamedFoo() { eval("var o = new Object();"); eval("o.foo = function() { return 'foo' }"); assertThat(eval("o.foo()")).isEqualTo("foo"); }
@Test public void testFunctionPropertyNamedPrint() { eval("var o = new Object();"); eval("o.print = function() { return 'printed' }"); assertThat(eval("o.print()")).isEqualTo("printed"); }
@Test public void testValidFunctionExpr() { eval("(function (){});"); }
@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)); }
@Test public void testVarargsLambda() { eval(read("(define list (lambda x x))"), env); assertEquals(list(1, 2, 3), eval(read("(list 1 2 3)"), env)); }
@Test public void testVarargs() { eval(read("(define (list . b) b)"), env); assertEquals(list(1, 2, 3), eval(read("(list 1 2 3)"), env)); }