org.cactoos.iterable.IterableOf Java Examples

The following examples show how to use org.cactoos.iterable.IterableOf. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ScalarWithFallbackTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void usesFallbackOfInterruptedException() throws Exception {
    final String message = "Fallback from InterruptedException";
    new Assertion<>(
        "Must use a fallback from Interrupted in case of exception",
        new ScalarWithFallback<>(
            () -> {
                throw new InterruptedException(
                    "Failure with InterruptedException"
                );
            },
            new IterableOf<>(
                new FallbackFrom<>(
                    new IterableOf<>(InterruptedException.class),
                    exp -> message
                )
            )
        ),
        new ScalarHasValue<>(message)
    ).affirm();
}
 
Example #2
Source File: InputAsBytesTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void readsInputIntoBytesWithSmallBuffer() throws Exception {
    new Assertion<>(
        "must read bytes from Input with a small reading buffer",
        new TextOf(
            new InputAsBytes(
                new InputOf(
                    new BytesOf(
                        new TextOf("Hello, товарищ!")
                    )
                ),
                2
            ),
            StandardCharsets.UTF_8
        ),
        new AllOf<>(
            new IterableOf<>(
                new StartsWith("Hello,"),
                new EndsWith("товарищ!")
            )
        )
    ).affirm();
}
 
Example #3
Source File: SlicedTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void sliceTheWholeTail() {
    new Assertion<>(
        "Must return the iterator of tail elements",
        new IterableOf<>(
            new Sliced<>(
                5,
                new IteratorOf<>(
                    1, 2, 3, 4, 5, 6, 7, 8, 9, 0
                )
            )
        ),
        new IsEqual<>(
            new IterableOf<>(
                6, 7, 8, 9, 0
            )
        )
    ).affirm();
}
 
Example #4
Source File: FirstOfTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void throwsFallbackIfNothingMatches() {
    new Assertion<>(
        "Fallback was not thrown",
        new FirstOf<>(
            num -> num.equals(0),
            // @checkstyle MagicNumber (10 lines)
            new IterableOf<>(
                1, 2, 3, 4, 5
            ),
            () -> {
                throw new IllegalArgumentException(
                    String.format("Unable to found element with id %d", 0)
                );
            }
        ),
        new Throws<>(IllegalArgumentException.class)
    ).affirm();
}
 
Example #5
Source File: AndInThreadsTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void iteratesEmptyList() {
    final List<String> list = new Synced<>(
        new ArrayList<>(2)
    );
    new Assertion<>(
        "Must iterate an empty list",
        new AndInThreads(
            new Mapped<String, Scalar<Boolean>>(
                new FuncOf<>(list::add, () -> true), new IterableOf<>()
            )
        ),
        new ScalarHasValue<>(
            Matchers.allOf(
                Matchers.equalTo(true),
                new MatcherOf<>(
                    value -> {
                        return list.isEmpty();
                    }
                )
            )
        )
    ).affirm();
}
 
Example #6
Source File: InputAsBytesTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void readsInputIntoBytes() throws Exception {
    new Assertion<>(
        "must read bytes from Input",
        new TextOf(
            new InputAsBytes(
                new InputOf(
                    new BytesOf(
                        new TextOf("Hello, друг!")
                    )
                )
            ),
            StandardCharsets.UTF_8
        ),
        new AllOf<>(
            new IterableOf<>(
                new StartsWith("Hello, "),
                new EndsWith("друг!")
            )
        )
    ).affirm();
}
 
Example #7
Source File: SkippedTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public void skipIterator() {
    new Assertion<>(
        "Must skip elements in iterator",
        new IterableOf<>(
            new Skipped<>(
                2,
                new IteratorOf<>(
                    "one", "two", "three", "four"
                )
            )
        ),
        new HasValues<>(
            "three",
            "four"
        )
    ).affirm();
}
 
Example #8
Source File: SetOfTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void behaveAsSetWithOriginalDuplicationsOfCharsInTheMiddle() {
    new Assertion<>(
        "Must keep unique characters",
        new SetOf<>('a', 'b', 'b', 'c', 'a', 'b', 'd'),
        new AllOf<>(
            new IterableOf<Matcher<? super SetOf<Character>>>(
                new HasSize(4),
                new IsCollectionContaining<>(new IsEqual<>('a')),
                new IsCollectionContaining<>(new IsEqual<>('b')),
                new IsCollectionContaining<>(new IsEqual<>('c')),
                new IsCollectionContaining<>(new IsEqual<>('d'))
            )
        )
    ).affirm();
}
 
Example #9
Source File: FuncWithFallbackTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void usesTheClosestFallback() throws Exception {
    final String expected = "Fallback from IllegalFormatException";
    MatcherAssert.assertThat(
        "Can't find the closest fallback",
        new FuncWithFallback<>(
            input -> {
                throw new IllegalFormatWidthException(1);
            },
            new IterableOf<>(
                new FallbackFrom<>(
                    IllegalArgumentException.class,
                    exp -> "Fallback from IllegalArgumentException"
                ),
                new FallbackFrom<>(
                    IllegalFormatException.class,
                    exp -> expected
                )
            )
        ),
        new FuncApplies<>(1, expected)
    );
}
 
Example #10
Source File: ReducedTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void firstAtIterable() throws Exception {
    final String one = "Apple";
    final String two = "Banana";
    final String three = "Orange";
    new Assertion<>(
        "Must find the first",
        new Reduced<>(
            (first, last) -> first,
            new IterableOf<Scalar<String>>(
                () -> one,
                () -> two,
                () -> three
            )
        ).value(),
        Matchers.equalTo(one)
    ).affirm();
}
 
Example #11
Source File: ForEachTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void testProcIterable() throws Exception {
    final List<Integer> list = new LinkedList<>();
    new ForEach<Integer>(
        list::add
    ).exec(
        new IterableOf<>(
            1, 1
        )
    );
    new Assertion<>(
        "List does not contain mapped Iterable elements",
        list,
        new IsEqual<>(
            new ListOf<>(
                1, 1
            )
        )
    ).affirm();
}
 
Example #12
Source File: InputOfTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void readsStringBuffer() throws Exception {
    final String starts = "The future ";
    final String ends = "is now!";
    new Assertion<>(
        "must receive a string buffer",
        new TextOf(
            new BytesOf(
                new InputOf(
                    new StringBuffer(starts)
                        .append(ends)
                )
            )
        ),
        new AllOf<>(
            new IterableOf<>(
                new StartsWith(starts),
                new EndsWith(ends)
            )
        )
    ).affirm();
}
 
Example #13
Source File: ScalarWithFallbackTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void usesTheClosestFallback() throws Exception {
    final String expected = "Fallback from IllegalFormatException";
    new Assertion<>(
        "Must find the closest fallback",
        new ScalarWithFallback<>(
            () -> {
                throw new IllegalFormatWidthException(1);
            },
            new IterableOf<>(
                new FallbackFrom<>(
                    new IterableOf<>(IllegalArgumentException.class),
                    exp -> "Fallback from IllegalArgumentException"
                ),
                new FallbackFrom<>(
                    new IterableOf<>(IllegalFormatException.class),
                    exp -> expected
                )
            )
        ),
        new ScalarHasValue<>(expected)
    ).affirm();
}
 
Example #14
Source File: InputOfTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void readsArrayOfChars() throws Exception {
    new Assertion<>(
        "must read array of chars.",
        new TextOf(
            new BytesOf(
                new InputOf(
                    'H', 'o', 'l', 'd', ' ',
                    'i', 'n', 'f', 'i', 'n', 'i', 't', 'y'
                )
            )
        ),
        new AllOf<>(
            new IterableOf<>(
                new StartsWith("Hold "),
                new EndsWith("infinity")
            )
        )
    ).affirm();
}
 
Example #15
Source File: ReducedTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void lastAtIterable() throws Exception {
    final Character one = 'A';
    final Character two = 'B';
    final Character three = 'O';
    new Assertion<>(
        "Must find the last",
        new Reduced<>(
            (first, last) -> last,
            new IterableOf<Scalar<Character>>(
                () -> one,
                () -> two,
                () -> three
            )
        ).value(),
        Matchers.equalTo(three)
    ).affirm();
}
 
Example #16
Source File: CycledTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void repeatIteratorTest() throws Exception {
    final String expected = "two";
    new Assertion<>(
        "must repeat iterator",
        new ItemAt<>(
            // @checkstyle MagicNumberCheck (1 line)
            7,
            new IterableOf<>(
                new Cycled<>(
                    new NoNulls<>(
                        new IterableOf<>(
                            "one", expected, "three"
                        )
                    )
                )
            )
        ),
        new ScalarHasValue<>(
            expected
        )
    ).affirm();
}
 
Example #17
Source File: FuturesTest.java    From jpeek with MIT License 6 votes vote down vote up
@Test
public void testSimpleScenario() throws Exception {
    new Assertion<>(
        "Futures returns Response",
        new Futures(
            (artifact, group) -> input -> new RsPage(
                new RqFake(),
                "wait",
                () -> new IterableOf<>(
                    new XeAppend("group", group),
                    new XeAppend("artifact", artifact)
                )
            )
        ).apply("a", "g").get().apply("test"),
        new IsNot<>(new IsEqual<>(null))
    ).affirm();
}
 
Example #18
Source File: ScalarWithFallback.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param origin Original scalar
 * @param exception Supported exception type
 * @param fallback Function that converts the given exception into fallback value
 */
public ScalarWithFallback(
    final Scalar<T> origin,
    final Class<? extends Throwable> exception,
    final Func<Throwable, T> fallback
) {
    this(origin, new IterableOf<Class<? extends Throwable>>(exception), fallback);
}
 
Example #19
Source File: ScalarWithFallback.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param origin Original scalar
 * @param exceptions Supported exceptions types
 * @param fallback Function that converts the given exception into fallback value
 */
public ScalarWithFallback(
    final Scalar<T> origin,
    final Iterable<Class<? extends Throwable>> exceptions,
    final Func<Throwable, T> fallback
) {
    this(
        origin,
        new IterableOf<FallbackFrom<T>>(
            new FallbackFrom<>(
                exceptions, fallback
            )
        )
    );
}
 
Example #20
Source File: SumOfScalar.java    From cactoos with MIT License 5 votes vote down vote up
@Override
public SumOf value() {
    return new SumOf(
        new IterableOf<>(
            new ListOf<>(this.scalars)
                .stream()
                .map(
                    scalar -> new Unchecked<>(scalar).value()
                ).collect(Collectors.toList())
        )
    );
}
 
Example #21
Source File: MultiplicationOfTest.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ensures that multiplication of float numbers iterable
 * return proper value.
 */
@Test
public void withIterableFloat() {
    new Assertion<>(
        "Multiplication floating numbers must be iterable",
        new MultiplicationOf(
            new IterableOf<>(0.5f, 2.0f, 10.0f)
        ).floatValue(),
        new IsEqual<>(10.0f)
    ).affirm();
}
 
Example #22
Source File: AvgOf.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param src Numbers
 */
public AvgOf(final Long... src) {
    this(
        new Mapped<>(
            number -> () -> number,
            new IterableOf<>(src)
        )
    );
}
 
Example #23
Source File: SplitTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void splitTextWithTextRegex() throws Exception {
    new Assertion<>(
        "Must split text with text regex",
        new Split(new TextOf("Split#OOP"), new TextOf("#")),
        new IsEqual<>(new IterableOf<>(new TextOf("Split"), new TextOf("OOP")))
    ).affirm();
}
 
Example #24
Source File: SplitTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void splitTextWithTextRegexAndLimit() throws Exception {
    final Text txt = new TextOf("Split!# #OOP");
    new Assertion<>(
        "Must split text with text regex and limit",
        new Split(txt, "\\W+", 1),
        new IsEqual<>(new IterableOf<>(txt))
    ).affirm();
}
 
Example #25
Source File: AvgOfTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void withIterableOfScalars() {
    new Assertion<>(
        "Average of iterable scalars must be long value",
        new AvgOf(
            new IterableOf<Scalar<Number>>(() -> 1L, () -> 2L, () -> 10L)
        ).longValue(),
        Matchers.equalTo(4L)
    ).affirm();
}
 
Example #26
Source File: Split.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param text The text
 * @param rgx The regex
 * @param lmt The limit
 * @see String#split(String, int)
 */
public Split(final UncheckedText text, final UncheckedText rgx, final int lmt) {
    super(
        new Mapped<String, Text>(
            TextOf::new,
            new IterableOf<>(
                text.asString().split(rgx.asString(), lmt)
            )
        )
    );
}
 
Example #27
Source File: ItemAtTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void sameValueTest() throws Exception {
    final ItemAt<Integer> item = new ItemAt<>(
        1,
        // @checkstyle MagicNumberCheck (1 lines)
        new IterableOf<>(1, 2, 3)
    );
    new Assertion<>(
        "Not the same value",
        item,
        new ScalarHasValue<>(item.value())
    ).affirm();
}
 
Example #28
Source File: FuncWithFallbackTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test(expected = Exception.class)
public void noFallbackIsProvided() throws Exception {
    new FuncWithFallback<>(
        input -> {
            throw new IllegalFormatWidthException(1);
        },
        new IterableOf<>()
    ).apply(1);
}
 
Example #29
Source File: RepeatedTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void emptyTest() throws Exception {
    new Assertion<>(
        "Must generate an empty iterator",
        new IterableOf<>(new Repeated<>(0, 0)),
        new HasSize(0)
    ).affirm();
}
 
Example #30
Source File: OrTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void emptyIterator() throws Exception {
    MatcherAssert.assertThat(
        new Or(new IterableOf<Scalar<Boolean>>()),
        new ScalarHasValue<>(false)
    );
}