Search in sources :

Example 1 with Text

use of org.cactoos.Text in project cactoos by yegor256.

the class BytesOfTest method closesInputStream.

@Test
public void closesInputStream() throws IOException {
    final AtomicBoolean closed = new AtomicBoolean();
    final InputStream input = new ByteArrayInputStream("how are you?".getBytes());
    MatcherAssert.assertThat("Can't close InputStream correctly", new TextOf(new InputOf(new InputStream() {

        @Override
        public int read() throws IOException {
            return input.read();
        }

        @Override
        public void close() throws IOException {
            input.close();
            closed.set(true);
        }
    }), StandardCharsets.UTF_8).asString(), new MatcherOf<>(text -> {
        return closed.get();
    }));
}
Also used : Text(org.cactoos.Text) JoinedText(org.cactoos.text.JoinedText) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) Limited(org.cactoos.iterable.Limited) StandardCharsets(java.nio.charset.StandardCharsets) MatcherOf(org.cactoos.matchers.MatcherOf) TextHasString(org.cactoos.matchers.TextHasString) Endless(org.cactoos.iterable.Endless) MatcherAssert(org.hamcrest.MatcherAssert) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) TextOf(org.cactoos.text.TextOf) InputStream(java.io.InputStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TextOf(org.cactoos.text.TextOf) Test(org.junit.Test)

Example 2 with Text

use of org.cactoos.Text in project cactoos by yegor256.

the class BytesOfTest method asBytes.

@Test
public void asBytes() throws IOException {
    final Text text = new TextOf("Hello!");
    MatcherAssert.assertThat("Can't convert text into bytes", new BytesOf(new InputOf(text)).asBytes(), Matchers.equalTo(new BytesOf(text.asString()).asBytes()));
}
Also used : Text(org.cactoos.Text) JoinedText(org.cactoos.text.JoinedText) TextOf(org.cactoos.text.TextOf) Test(org.junit.Test)

Example 3 with Text

use of org.cactoos.Text in project cactoos by yegor256.

the class MappedWithIndexTest method failsIfIteratorExhausted.

@Test
void failsIfIteratorExhausted() {
    final Iterator<Text> iterator = new MappedWithIndex<>((item, index) -> new FormattedText("%1$s X %2$s", index, item), new IterableOf<>("1").iterator());
    iterator.next();
    new Assertion<>("must throw NSEE", iterator::next, new Throws<>(NoSuchElementException.class)).affirm();
}
Also used : IterableOf(org.cactoos.iterable.IterableOf) Text(org.cactoos.Text) FormattedText(org.cactoos.text.FormattedText) FormattedText(org.cactoos.text.FormattedText) Throws(org.llorllale.cactoos.matchers.Throws) Test(org.junit.jupiter.api.Test)

Example 4 with Text

use of org.cactoos.Text in project cactoos by yegor256.

the class MappedWithIndexTest method removingElementsFromIterator.

@Test
void removingElementsFromIterator() {
    final Iterator<Text> iterator = new MappedWithIndex<>((item, index) -> new FormattedText("%1$s : %2$s", index, item), new ArrayList<>(Arrays.asList("1", "2", "3")).iterator());
    iterator.next();
    iterator.remove();
    new Assertion<>("must map values of changed iterator", new IterableOf<>(iterator), new HasValues<>(new TextOf("1 : 2"), new TextOf("2 : 3"))).affirm();
}
Also used : HasValues(org.llorllale.cactoos.matchers.HasValues) IterableOf(org.cactoos.iterable.IterableOf) ArrayList(java.util.ArrayList) Text(org.cactoos.Text) FormattedText(org.cactoos.text.FormattedText) TextOf(org.cactoos.text.TextOf) FormattedText(org.cactoos.text.FormattedText) Test(org.junit.jupiter.api.Test)

Example 5 with Text

use of org.cactoos.Text in project cactoos by yegor256.

the class UncheckedBytesTest method worksNormallyWhenNoExceptionIsThrown.

@Test
void worksNormallyWhenNoExceptionIsThrown() throws Exception {
    final Text source = new TextOf("hello, cactoos!");
    new Assertion<>("Must work normally when no exception is thrown", new UncheckedBytes(new BytesOf(source)).asBytes(), new IsEqual<>(new BytesOf(source).asBytes())).affirm();
}
Also used : Text(org.cactoos.Text) TextOf(org.cactoos.text.TextOf) IsEqual(org.hamcrest.core.IsEqual) Test(org.junit.jupiter.api.Test)

Aggregations

Text (org.cactoos.Text)20 Test (org.junit.jupiter.api.Test)15 IsText (org.llorllale.cactoos.matchers.IsText)11 TextOf (org.cactoos.text.TextOf)9 IsEqual (org.hamcrest.core.IsEqual)9 Test (org.junit.Test)5 AllOf (org.hamcrest.core.AllOf)3 IterableOf (org.cactoos.iterable.IterableOf)2 FormattedText (org.cactoos.text.FormattedText)2 JoinedText (org.cactoos.text.JoinedText)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Logger (java.util.logging.Logger)1 InputOf (org.cactoos.io.InputOf)1