Expand implements ValueExpression { @Override public ImmutableList<Value> eval(final ParseState parseState, final Encoding encoding) { final ImmutableList<Value> baseList = bases.eval(parseState, encoding); if (baseList.isEmpty()) { return baseList; } return count.evalSingle(parseState, encoding) .filter(countValue -> !countValue.equals(NOT_A_VALUE)) .map(countValue -> expand(baseList, countValue.asNumeric().intValueExact(), new ImmutableList<>()).computeResult()) .orElseThrow(() -> new IllegalArgumentException("Count must evaluate to a non-empty countable value.")); } Expand(final ValueExpression bases, final SingleValueExpression count); @Override ImmutableList<Value> eval(final ParseState parseState, final Encoding encoding); @Override String toString(); @Override boolean equals(final Object obj); @Override int hashCode(); final ValueExpression bases; final SingleValueExpression count; }
@Test public void expandEmpty() { final ImmutableList<Value> result = exp(ref("a"), con(5)).eval(EMPTY_PARSE_STATE, enc()); assertTrue(result.isEmpty()); }
@Test public void expandNotAValueTimes() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Count must evaluate to a non-empty countable value."); exp(con(1), last(div(con(1), con(0)))).eval(EMPTY_PARSE_STATE, enc()); }
@Test public void expandEmptyTimes() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Count must evaluate to a non-empty countable value."); exp(con(1), last(ref("a"))).eval(EMPTY_PARSE_STATE, enc()); }
@Test public void expandZeroTimes() { final ImmutableList<Value> result = exp(con(1), con(0)).eval(EMPTY_PARSE_STATE, enc()); assertTrue(result.isEmpty()); }
@Test public void expandValue() { ImmutableList<Value> result = exp(con(VALUE_1), con(SIZE)).eval(EMPTY_PARSE_STATE, enc()); assertEquals(SIZE, result.size); for (int i = 0; i < SIZE; i++) { assertEquals(VALUE_1, result.head.asNumeric().intValueExact()); result = result.tail; } }
@Test public void expandList() { ImmutableList<Value> result = exp(ref("a"), con(SIZE)).eval(createFromByteStream(DUMMY_STREAM).add(PARSEVALUE_2).add(PARSEVALUE_1), enc()); assertEquals(2 * SIZE, result.size); for (int i = 0; i < SIZE; i++) { assertEquals(VALUE_1, result.head.asNumeric().intValueExact()); result = result.tail; assertEquals(VALUE_2, result.head.asNumeric().intValueExact()); result = result.tail; } }