Skip to content

Commit 7deb02f

Browse files
committed
#74: Std.print should support Text subtypes
1 parent ae33ebe commit 7deb02f

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

src/main/java/io/github/dgroup/term4j/Std.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,5 @@ public interface Std {
6565
* @throws UncheckedStdOutputException In the case of STD output printing
6666
* failure.
6767
*/
68-
void print(Iterable<Text> msgs);
69-
68+
void print(Iterable<? extends Text> msgs);
7069
}

src/main/java/io/github/dgroup/term4j/std/StdEnvelope.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ public class StdEnvelope implements Std {
4040
/**
4141
* The procedure to print the text to the standart output.
4242
*/
43-
private final Proc<Iterable<Text>> prc;
43+
private final Proc<Iterable<? extends Text>> prc;
4444

4545
/**
4646
* Ctor.
4747
* @param prc The procedure to print the text to the standart output.
4848
*/
49-
public StdEnvelope(final Proc<Iterable<Text>> prc) {
49+
public StdEnvelope(final Proc<Iterable<? extends Text>> prc) {
5050
this.prc = prc;
5151
}
5252

@@ -67,7 +67,7 @@ public final void print(final Text... msgs) {
6767

6868
@Override
6969
@SuppressWarnings("PMD.AvoidCatchingGenericException")
70-
public final void print(final Iterable<Text> msgs) {
70+
public final void print(final Iterable<? extends Text> msgs) {
7171
try {
7272
this.prc.exec(msgs);
7373
// @checkstyle IllegalCatchCheck (3 lines)

src/main/java/io/github/dgroup/term4j/std/StdOf.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.io.Writer;
2828
import org.cactoos.Proc;
2929
import org.cactoos.Text;
30-
import org.cactoos.scalar.And;
3130

3231
/**
3332
* Default implementation of std output.
@@ -101,6 +100,10 @@ public StdOf(final Writer std, final String idnt) {
101100
* @param std The procedure to handle each line of the output.
102101
*/
103102
public StdOf(final Proc<Text> std) {
104-
super(msgs -> new And(std, msgs).value());
103+
super(lines -> {
104+
for (final Text line : lines) {
105+
std.exec(line);
106+
}
107+
});
105108
}
106109
}

src/test/java/io/github/dgroup/term4j/std/StdOfTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@
2424

2525
package io.github.dgroup.term4j.std;
2626

27+
import io.github.dgroup.term4j.Highlighted;
2728
import io.github.dgroup.term4j.Std;
29+
import io.github.dgroup.term4j.highlighted.Green;
30+
import io.github.dgroup.term4j.highlighted.Red;
2831
import java.io.ByteArrayOutputStream;
2932
import java.io.PrintStream;
3033
import java.io.StringWriter;
3134
import java.io.UnsupportedEncodingException;
3235
import java.nio.charset.StandardCharsets;
36+
import java.util.Collection;
37+
import org.cactoos.collection.CollectionOf;
3338
import org.junit.Test;
3439
import org.llorllale.cactoos.matchers.Assertion;
3540
import org.llorllale.cactoos.matchers.HasLines;
@@ -39,6 +44,7 @@
3944
*
4045
* @since 0.1.0
4146
* @checkstyle MagicNumberCheck (200 lines)
47+
* @checkstyle ClassDataAbstractionCouplingCheck (200 lines)
4248
*/
4349
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
4450
public final class StdOfTest {
@@ -77,4 +83,22 @@ public void printToWriter() {
7783
new HasLines("line1", "line2", "line3", "line4")
7884
).affirm();
7985
}
86+
87+
/**
88+
* Ensure that subtypes of {@link org.cactoos.Text} can be printed through the {@link Std}.
89+
*/
90+
@Test
91+
public void inheritance() {
92+
final Collection<Highlighted> colours = new CollectionOf<>(
93+
new Green(" one "), new Red(" two ")
94+
);
95+
final StringWriter swter = new StringWriter();
96+
final Std std = new StdOf(swter);
97+
std.print(colours);
98+
new Assertion<>(
99+
"subtypes of text has been printed",
100+
swter.toString(),
101+
new HasLines("\u001B[92m one \u001B[m", "\u001B[91m two \u001B[m")
102+
).affirm();
103+
}
80104
}

0 commit comments

Comments
 (0)