package no.ssb.vtl.script;

/*
 * ========================LICENSE_START=================================
 * Java VTL
 * %%
 * Copyright (C) 2016 - 2017 Hadrien Kohl
 * %%
 * Licensed 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.
 * =========================LICENSE_END==================================
 */

import no.ssb.vtl.script.error.VTLCompileException;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class VTLErrorsTest {

    @Test
    public void testErrorRecovery() {

        // @formatter:off
        String script = "variable := nvl(bla\"error\") // normal syntax error\n" +
                        "variable := nvl(\"string\", \"\")   // type error\n" +
                        "variable := round(0)             // missing argument\n";
        // @formatter:on

        VTLScriptEngine engine = new VTLScriptEngine();
        assertThatThrownBy(() -> {
            engine.eval(script);
        })
                .as("engine exception")
                .isInstanceOf(VTLCompileException.class)
                .hasMessageContaining("undefined variable bla")
                .hasMessageContaining("missing ','")
                .hasMessageContaining("missing argument(s): decimals");
    }
}

@@ -1738,11 +1738,541 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testUndefinedFunction() %7B%0A%0A        // @formatter:off%0A        String script = %22variable := notReallyAFunction(1,2,3)%22;%0A        // @formatter:on%0A%0A        VTLScriptEngine engine = new VTLScriptEngine();%0A        assertThatThrownBy(() -%3E %7B%0A            engine.eval(script);%0A        %7D)%0A                .as(%22engine exception%22)%0A                .isInstanceOf(VTLCompileException.class)%0A                .hasMessageContaining(%22undefined function%22)%0A                .hasMessageContaining(%22notReallyAFunction%22);%0A    %7D
 %0A%7D%0A

Add test expecting undefined functions