Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use IsText for testing Text objects #1604

Merged
merged 4 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/org/cactoos/io/ResourceOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public ResourceOf(final Text res, final ClassLoader ldr) {
throw new IOException(
new FormattedText(
"The resource \"%s\" was not found in %s",
input.asString(),
input,
ldr
).asString()
);
Expand All @@ -174,7 +174,7 @@ public ResourceOf(final Text res, final ClassLoader ldr) {
* @param fbk Fallback
*/
public ResourceOf(final Text res, final Text fbk) {
this(res, input -> new InputOf(new BytesOf(fbk.asString())));
this(res, input -> new InputOf(fbk));
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/cactoos/io/TempFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ public TempFile(final String prefix, final String suffix) {
);
}

/**
* Ctor.
* <p>
* The temporary file will be created inside the filesystem's
* temporary folder (system property: {@code java.io.tmpdir}).
* @param prefix The temp filename's prefix
* @param suffix The temp filename's suffix
* @since 1.0
*/
public TempFile(final Text prefix, final Text suffix) {
this(
() -> Paths.get(System.getProperty("java.io.tmpdir")),
prefix,
suffix
);
}

/**
* Ctor.
* @param dir The directory in which to create the temp file
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/cactoos/text/SuffixOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.cactoos.text;

import org.cactoos.Text;
import org.cactoos.scalar.LengthOf;
import org.cactoos.scalar.ScalarOf;
import org.cactoos.scalar.Ternary;

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/cactoos/TextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.cactoos.text.TextOf;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.HasString;
import org.llorllale.cactoos.matchers.IsText;
import org.llorllale.cactoos.matchers.Throws;

/**
Expand Down Expand Up @@ -69,7 +69,7 @@ void okForNoNulls() {
new NoNulls(
new TextOf(message)
),
new HasString(message)
new IsText(message)
).affirm();
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/cactoos/io/GzipInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import java.util.zip.GZIPOutputStream;
import org.cactoos.scalar.LengthOf;
import org.cactoos.text.TextOf;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.IsText;

/**
* Test case for {@link org.cactoos.io.GzipInput}.
Expand Down Expand Up @@ -62,8 +62,8 @@ public void readFromGzipInput() throws Exception {
"Can't read from a gzip input",
new TextOf(
new GzipInput(new InputOf(bytes))
).asString(),
new IsEqual<>(content)
),
new IsText(content)
).affirm();
}

Expand Down
9 changes: 5 additions & 4 deletions src/test/java/org/cactoos/io/InputOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.llorllale.cactoos.matchers.EndsWith;
import org.llorllale.cactoos.matchers.HasContent;
import org.llorllale.cactoos.matchers.HasString;
import org.llorllale.cactoos.matchers.IsText;
import org.llorllale.cactoos.matchers.IsTrue;
import org.llorllale.cactoos.matchers.MatchesRegex;
import org.llorllale.cactoos.matchers.Satisfies;
Expand Down Expand Up @@ -272,8 +273,8 @@ void readsStringFromReader() throws Exception {
new InputOf(
new StringReader(source)
)
).asString(),
new IsEqual<>(source)
),
new IsText(source)
).affirm();
}

Expand All @@ -289,8 +290,8 @@ void readsEncodedStringFromReader() throws Exception {
StandardCharsets.UTF_8
)
)
).asString(),
new IsEqual<>(source)
),
new IsText(source)
).affirm();
}

Expand Down
64 changes: 32 additions & 32 deletions src/test/java/org/cactoos/io/InputStreamOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
import org.cactoos.bytes.BytesOf;
import org.cactoos.scalar.LengthOf;
import org.cactoos.text.TextOf;
import org.hamcrest.core.IsEqual;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.HasContent;
import org.llorllale.cactoos.matchers.IsText;
import org.llorllale.cactoos.matchers.Satisfies;

/**
Expand Down Expand Up @@ -107,18 +107,18 @@ public void readsFileContent() throws Exception {
).value();
new Assertion<>(
"Must read from file",
new TextOf(new InputStreamOf(file)).asString(),
new IsEqual<>(content)
new TextOf(new InputStreamOf(file)),
new IsText(content)
).affirm();
}

@Test
public void readsBytes() throws Exception {
public void readsBytes() {
final String content = "Bytes content";
new Assertion<>(
"Must read from bytes",
new TextOf(new InputStreamOf(new BytesOf(content))).asString(),
new IsEqual<>(content)
new TextOf(new InputStreamOf(new BytesOf(content))),
new IsText(content)
).affirm();
}

Expand All @@ -128,18 +128,18 @@ public void readsBytesArray() throws Exception {
final byte[] bytes = new BytesOf(content).asBytes();
new Assertion<>(
"Must read from byte array",
new TextOf(new InputStreamOf(bytes)).asString(),
new IsEqual<>(content)
new TextOf(new InputStreamOf(bytes)),
new IsText(content)
).affirm();
}

@Test
public void readsText() throws Exception {
public void readsText() {
final String content = "Text content";
new Assertion<>(
"Must read from text",
new TextOf(new InputStreamOf(new TextOf(content))).asString(),
new IsEqual<>(content)
new TextOf(new InputStreamOf(new TextOf(content))),
new IsText(content)
).affirm();
}

Expand All @@ -152,8 +152,8 @@ public void readsFromUri() throws Exception {
).value();
new Assertion<>(
"Must read from URI",
new TextOf(new InputStreamOf(file.toURI())).asString(),
new IsEqual<>(content)
new TextOf(new InputStreamOf(file.toURI())),
new IsText(content)
).affirm();
}

Expand All @@ -166,13 +166,13 @@ public void readsFromUrl() throws Exception {
).value();
new Assertion<>(
"Must read from URL",
new TextOf(new InputStreamOf(file.toURI().toURL())).asString(),
new IsEqual<>(content)
new TextOf(new InputStreamOf(file.toURI().toURL())),
new IsText(content)
).affirm();
}

@Test
public void readsFromReaderWithMax() throws Exception {
public void readsFromReaderWithMax() {
final String content = "Reading with charset name and buffer size";
final int max = 3;
new Assertion<>(
Expand All @@ -183,13 +183,13 @@ public void readsFromReaderWithMax() throws Exception {
StandardCharsets.UTF_8.name(),
max
)
).asString(),
new IsEqual<>(content)
),
new IsText(content)
).affirm();
}

@Test
public void readsFromReaderWithCharsetWithMax() throws Exception {
public void readsFromReaderWithCharsetWithMax() {
final String content = "Reading with charset and buffer size";
new Assertion<>(
"Must read from reader with charset and buffer size",
Expand All @@ -199,13 +199,13 @@ public void readsFromReaderWithCharsetWithMax() throws Exception {
StandardCharsets.UTF_8,
1
)
).asString(),
new IsEqual<>(content)
),
new IsText(content)
).affirm();
}

@Test
public void readsFromReaderWithCharset() throws Exception {
public void readsFromReaderWithCharset() {
final String content = "Content for reading with charset";
new Assertion<>(
"Must read from reader with charset name",
Expand All @@ -214,8 +214,8 @@ public void readsFromReaderWithCharset() throws Exception {
new StringReader(content),
StandardCharsets.UTF_8.name()
)
).asString(),
new IsEqual<>(content)
),
new IsText(content)
).affirm();
}

Expand All @@ -233,13 +233,13 @@ public void readsFromTextWithCharset() throws Exception {
new TextOf(file),
StandardCharsets.UTF_8.name()
)
).asString(),
new IsEqual<>(content)
),
new IsText(content)
).affirm();
}

@Test
public void readsFromCharSequenceWithCharsetName() throws Exception {
public void readsFromCharSequenceWithCharsetName() {
final String content = "Simple content";
new Assertion<>(
"Must read from char sequence with charset name",
Expand All @@ -248,13 +248,13 @@ public void readsFromCharSequenceWithCharsetName() throws Exception {
content,
StandardCharsets.UTF_8.name()
)
).asString(),
new IsEqual<>(content)
),
new IsText(content)
).affirm();
}

@Test
public void readsFromCharSequenceWithCharset() throws Exception {
public void readsFromCharSequenceWithCharset() {
final String content = "Another simple content";
new Assertion<>(
"Must read from char sequence with charset",
Expand All @@ -263,8 +263,8 @@ public void readsFromCharSequenceWithCharset() throws Exception {
content,
StandardCharsets.UTF_8
)
).asString(),
new IsEqual<>(content)
),
new IsText(content)
).affirm();
}
}
13 changes: 7 additions & 6 deletions src/test/java/org/cactoos/io/TempFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.cactoos.Text;
import org.cactoos.text.FormattedText;
import org.cactoos.text.TextOf;
import org.hamcrest.core.AllOf;
Expand Down Expand Up @@ -87,11 +88,11 @@ void deleteFile() throws Exception {

@Test
void createFileWithPrefix() throws Exception {
final String prefix = new FormattedText(
final Text prefix = new FormattedText(
"randomPrefix%s",
System.currentTimeMillis()
).asString();
try (TempFile file = new TempFile(prefix, "")) {
);
try (TempFile file = new TempFile(prefix, new TextOf(""))) {
new Assertion<>(
"File must be created with the given prefix",
new TextOf(file.value().getFileName().toString()),
Expand All @@ -102,10 +103,10 @@ void createFileWithPrefix() throws Exception {

@Test
void createFileWithSuffix() throws Exception {
final String suffix = new FormattedText(
final Text suffix = new FormattedText(
"randomSuffix%s", System.currentTimeMillis()
).asString();
try (TempFile file = new TempFile("", suffix)) {
);
try (TempFile file = new TempFile(new TextOf(""), suffix)) {
new Assertion<>(
"File must be created with the given suffix",
new TextOf(file.value().getFileName().toString()),
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/cactoos/iterable/MappedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.IsText;

/**
* Test case for {@link Mapped}.
Expand All @@ -50,8 +51,8 @@ void transformsList() throws Exception {
new IterableOf<>(
"hello", "world", "друг"
)
).iterator().next().asString(),
new IsEqual<>("HELLO")
).iterator().next(),
new IsText("HELLO")
).affirm();
}

Expand Down
9 changes: 4 additions & 5 deletions src/test/java/org/cactoos/text/TextEnvelopeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.cactoos.Scalar;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.IsText;

/**
* Tests for {@link TextEnvelope}.
Expand All @@ -38,15 +38,14 @@ final class TextEnvelopeTest {
/**
* Test for {@link TextEnvelope#asString()} method. Must assert that
* the envelope asString value is equal to its string value.
* @throws Exception Throws from asString.
*/
@Test
void testAsString() throws Exception {
void testAsString() {
final String text = "asString";
new Assertion<>(
"Envelope value does not match String value",
new TextEnvelopeDummy(text).asString(),
new IsEqual<>(text)
new TextEnvelopeDummy(text),
new IsText(text)
).affirm();
}

Expand Down
Loading