/* --------------------------------------------------------- * * __________ D E L T A S C R I P T * * (_________() * * / === / - A fast, dynamic scripting language * * | == | - Version 4.13.11.0 * * / === / - Developed by Adam R. Nelson * * | = = | - 2011-2013 * * / === / - Distributed under GNU LGPL v3 * * (________() - http://github.com/ar-nelson/deltascript * * * * --------------------------------------------------------- */ package com.sector91.delta.script.test; import static org.junit.Assert.*; import org.junit.Test; import com.sector91.delta.script.*; import com.sector91.delta.script.parser.DScriptParserException; import com.sector91.delta.script.test.util.DeltaScriptTest; public class ErrorsTest extends DeltaScriptTest { @Test public void testErrorFunc() { assertEvalThrowsError("fakeVar; \"No errors!\""); assertEval("error = -> \"Error!\"; fakeVar; \"No errors!\"", "Error!"); } @Test public void testErrorFuncInScope() { assertEval("scope, error = -> \"Error!\", fakeVar, xs", "Error!"); assertEval("error = -> \"Error!\"; scope, fakeVar, xs", "Error!"); assertEval("def error = -> \"Outer Error\";" + "scope, def error = -> \"Inner Error\", fakeVar, xs", "Inner Error"); } @Test public void testErrorFuncInFunc() { assertEval("func f, error = -> \"Error!\", fakeVar, xf; f()", "Error!"); assertEval("error=->\"Error!\"; f=->fakeVar; f()", "Error!"); assertEval("def error = -> \"Outer Error\";" + "func f, def error = -> \"Inner Error\", fakeVar, xf; f()", "Inner Error"); } @Test public void testErrorFuncClosure() { assertEval("def error = -> \"Outer Error\";" + "scope s," + " def error = -> \"Inner Error\"," + " func f(), fakeVar, xf," + "xs;" + "s.f()", "Inner Error"); } @Test public void testErrorFuncWithTags() { assertEval("func error(msg, tags), return 'Undefined in tags, xf;" + "fakeVar; \"No errors!\"", true); assertEval("func error(msg, tags), return 'FakeTag in tags, xf;" + "fakeVar; \"No errors!\"", false); } @Test public void testThrowingErrors() { assertEvalThrowsError("..error(\"Catch me!\")"); assertEval("error = -> _; ..error(\"Catch me!\")", "Catch me!"); } @Test public void testThrowingErrorsWithTags() { assertEval("func error(msg, tags), return 'tag1 in tags, xf;" + "..error(\"err\", 'tag1, 'tag2); \"No errors!\"", true); } @Test public void testRetaggingErrors() { } @Test(timeout=5000) public void testErrorsWithinErrors() { assertEvalThrowsError("error = -> ..error(_); fakeVar"); assertEval("error = -> 91;" + " func f, def error = -> ..error(_), fakeVar, xf; f()", 91); } /*@Test public void testLineNumbers() { assertErrorAtLine("..error(\"Line 1\")", 1); assertErrorAtLine("\n\n..error(\"Line 3\")", 3); assertErrorAtLine("func f\n..error(\"Line 2\")\nxf\nf()", 2); assertErrorAtLine("[\n2,\n3,\n4,\n..error(\"5\"),\n6\n]", 5); } private final void assertErrorAtLine(String toEval, int line) { boolean thrown = false; try {evalAndThrow(toEval);} catch (DScriptErr ex) { thrown = true; if (line != ex.getLineNumber()) { fail("Expected error to be thrown at line " + line + ", but" + " was thrown at line " + ex.getLineNumber() + ". The" + " error message was \"" + ex.getMessage() + "\""); } } catch (DScriptParserException ex) {throw new RuntimeException(ex);} if (!thrown) fail("Did not throw an error."); }*/ }