Scope implements ValueExpression { @Override public ImmutableList<Value> eval(final ParseState parseState, final Encoding encoding) { return scopeSize.evalSingle(parseState, encoding) .filter(sizeValue -> !sizeValue.equals(NOT_A_VALUE) && sizeValue.asNumeric().compareTo(ZERO) >= 0) .map(sizeValue -> scopedValueExpression.eval(parseState.withOrder(calculateScope(parseState.order, sizeValue.asNumeric().intValueExact())), encoding)) .orElseThrow(() -> new IllegalArgumentException("Argument scopeSize must evaluate to a positive, countable value.")); } Scope(final ValueExpression scopedValueExpression, final SingleValueExpression scopeSize); @Override ImmutableList<Value> eval(final ParseState parseState, final Encoding encoding); @Override String toString(); @Override boolean equals(final Object obj); @Override int hashCode(); final ValueExpression scopedValueExpression; final SingleValueExpression scopeSize; }
@Test public void notAValueScopeSize() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Argument scopeSize must evaluate to a positive, countable value."); new Scope(con(0), EMPTY_SVE).eval(EMPTY_PARSE_STATE, enc()); }
@Test public void negativeScopeSize() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Argument scopeSize must evaluate to a positive, countable value."); new Scope(con(0), con(-1, signed())).eval(EMPTY_PARSE_STATE, enc()); }