/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.tinkerpop.gremlin.util.function;

import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;

/**
 * @author Stephen Mallette (http://stephen.genoprime.com)
 */
public class ScriptEngineLambdaTest {

    @Test
    public void shouldCallAsFunction() {
        final ScriptEngineLambda lambda = new ScriptEngineLambda("nashorn", "1+a");
        assertEquals(11.0, lambda.apply(10));
    }

    @Test
    public void shouldCallAsSupplier() {
        final ScriptEngineLambda lambda = new ScriptEngineLambda("nashorn", "11");
        assertEquals(11, lambda.get());
    }

    @Test
    public void shouldCallAsPredicate() {
        final ScriptEngineLambda lambda = new ScriptEngineLambda("nashorn", "a > 10");
        assertThat(lambda.test(100), is(true));
    }

    @Test
    public void shouldCallAsConsumer() {
        final ScriptEngineLambda lambda = new ScriptEngineLambda("nashorn", "a.setData('test')");
        final Junk junk = new Junk();
        lambda.accept(junk);
        assertEquals("test", junk.getData());
    }

    @Test
    public void shouldCallAsBiConsumer() {
        final ScriptEngineLambda lambda = new ScriptEngineLambda("nashorn", "a.setData('testa');b.setData('testb')");
        final Junk junkA = new Junk();
        final Junk junkB = new Junk();
        lambda.accept(junkA, junkB);

        assertEquals("testa", junkA.getData());
        assertEquals("testb", junkB.getData());
    }

    @Test
    public void shouldCallAsTriConsumer() {
        final ScriptEngineLambda lambda = new ScriptEngineLambda("nashorn", "a.setData('testa');b.setData('testb');c.setData('testc')");
        final Junk junkA = new Junk();
        final Junk junkB = new Junk();
        final Junk junkC = new Junk();
        lambda.accept(junkA, junkB, junkC);

        assertEquals("testa", junkA.getData());
        assertEquals("testb", junkB.getData());
        assertEquals("testc", junkC.getData());
    }

    public static class Junk {
        private String data = "";

        public String getData() {
            return data;
        }

        public void setData(final String x) {
            data = x;
        }
    }
}

@@ -1326,170 +1326,525 @@
 Test
-%0A    public void shouldCallAsSupplier() %7B%0A        final ScriptEngineLambda lambda = new ScriptEngineLambda(%22nashorn%22, %2211%22);%0A        assertEquals(11, lambda.get()
+(expected = IllegalArgumentException.class)%0A    public void shouldThrowOnBadScriptAsFunction() %7B%0A        new ScriptEngineLambda(%22nashorn%22, %221432423)a%22).apply(%22a%22);%0A    %7D%0A%0A    @Test%0A    public void shouldCallAsSupplier() %7B%0A        final ScriptEngineLambda lambda = new ScriptEngineLambda(%22nashorn%22, %2211%22);%0A        assertEquals(11, lambda.get());%0A    %7D%0A%0A    @Test(expected = IllegalArgumentException.class)%0A    public void shouldThrowOnBadScriptAsSupplier() %7B%0A        new ScriptEngineLambda(%22nashorn%22, %221432423)a%22).get(
 );%0A 
@@ -2035,32 +2035,210 @@
 (true));%0A    %7D%0A%0A
+    @Test(expected = IllegalArgumentException.class)%0A    public void shouldThrowOnBadScriptAsPredicate() %7B%0A        new ScriptEngineLambda(%22nashorn%22, %221432423)a%22).test(1);%0A    %7D%0A%0A
     @Test%0A    pu
@@ -2482,32 +2482,213 @@
 Data());%0A    %7D%0A%0A
+    @Test(expected = IllegalArgumentException.class)%0A    public void shouldThrowOnBadScriptAsConsumer() %7B%0A        new ScriptEngineLambda(%22nashorn%22, %221432423)a%22).accept(%221%22);%0A    %7D%0A%0A
     @Test%0A    pu
@@ -3053,32 +3053,220 @@
 Data());%0A    %7D%0A%0A
+    @Test(expected = IllegalArgumentException.class)%0A    public void shouldThrowOnBadScriptAsBiConsumer() %7B%0A        new ScriptEngineLambda(%22nashorn%22, %221432423)a%22).accept(%221%22, %222%22);%0A    %7D%0A%0A
     @Test%0A    pu
@@ -3749,24 +3749,218 @@
 ());%0A    %7D%0A%0A
+    @Test(expected = IllegalArgumentException.class)%0A    public void shouldThrowOnBadScriptAsTriConsumer() %7B%0A        new ScriptEngineLambda(%22nashorn%22, %221432423)a%22).accept(%221%22, %222%22, %223%22);%0A    %7D%0A%0A
     public s

Enforce exception types for ScriptEngineLambda.