@Test public void testPrependToProtectedList() throws SyntaxException, InvalidTermException { Context context = new CompileTimeContext(); // Create a protected resource. ListResource list = new ListResource(); list = (ListResource) list.protect(); Element value = StringProperty.getInstance("OK"); Operation dml = Prepend.getInstance(null, list, value); Element result = context.executeDmlBlock(dml); // Check that the list argument has NOT been updated. assertTrue(list.size() == 0); // Verify that a copy has been made. assertTrue(result != list); // It must also be a list. assertTrue(result instanceof ListResource); // Verify that the new list has the correct value. Element element = ((ListResource) result).get(TermFactory.create(0)); assertTrue(value == element); }
@Test public void testNoIterations() throws SyntaxException { // Create the loop. Operation op = new Foreach( null, new SetValue(null, "k", new Operation[] {}), new SetValue(null, "v", new Operation[] {}), new HashResource(), BooleanProperty.TRUE); // Run the operation. Context context = new CompileTimeContext(); Element r1 = op.execute(context); // Check the value. assertTrue(r1 instanceof Undef); // Check side-effects of the loop. Element k = context.getLocalVariable("k"); Element v = context.getLocalVariable("v"); assertTrue(k instanceof Undef); assertTrue(v instanceof Undef); }
@Test public void testPrependToList() throws SyntaxException, InvalidTermException { Context context = new CompileTimeContext(); ListResource list = new ListResource(); Element value = StringProperty.getInstance("OK"); Operation dml = Prepend.getInstance(null, list, value); Element result = context.executeDmlBlock(dml); // Check that the list argument has been updated. assertTrue(list.size() == 1); Element element = list.get(TermFactory.create(0)); assertTrue(value == element); // Verify that the same list is given as the result. assertTrue(result == list); }
private static Element runDefaultDml(Operation dml) throws SyntaxException { // If the argument is null, return null as the value immediately. if (dml == null) { return null; } // Create a nearly empty execution context. There are no global // variables by default (including no 'self' variable). Only the // standard built-in functions are accessible. Context context = new CompileTimeContext(); Element value = null; // IF this is an AbstractOperation, pull out the source location. SourceRange sourceRange = null; if (dml instanceof AbstractOperation) { AbstractOperation op = (AbstractOperation) dml; sourceRange = op.getSourceRange(); } // Execute the DML block. The block must evaluate to an Element. Any // error is fatal for the compilation. try { value = context.executeDmlBlock(dml); } catch (EvaluationException ee) { SyntaxException se = SyntaxException.create(sourceRange, MSG_DEF_VALUE_NOT_CONSTANT); se.initCause(ee); throw se; } // The default value cannot be either undef or null. Throw a syntax // error if that is the case. if (value instanceof Undef) { throw SyntaxException.create(sourceRange, MSG_DEF_VALUE_CANNOT_BE_UNDEF); } else if (value instanceof Null) { throw SyntaxException.create(sourceRange, MSG_DEF_VALUE_CANNOT_BE_UNDEF); } // Looks Ok; return the value. return value; }
@Override public Element execute(Context context) { // Look up the variable. Element result = context.getVariable(identifier); // Return an error if the variable doesn't exist. if (result == null && !lookupOnly) { throw new EvaluationException( MessageUtils.format(MSG_UNDEFINED_VAR, identifier), sourceRange, context); } return result; }