Skip to content

Commit b280c53

Browse files
committed
Add IOFunctions tests
Signed-off-by: Gábor Lipták <gliptak@gmail.com>
1 parent 49f52e9 commit b280c53

File tree

2 files changed

+63
-36
lines changed

2 files changed

+63
-36
lines changed

core/src/test/java/fj/IOTest.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

core/src/test/java/fj/data/IOFunctionsTest.java

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package fj.data;
22

3-
import fj.Unit;
4-
import org.hamcrest.core.Is;
3+
import fj.*;
54
import org.junit.Assert;
65
import org.junit.Test;
76

87
import java.io.*;
98
import java.io.Reader;
109
import java.util.concurrent.atomic.AtomicBoolean;
1110

11+
import static fj.data.IOFunctions.*;
12+
import static fj.data.Stream.cons;
13+
import static fj.data.Stream.nil_;
14+
import static org.hamcrest.CoreMatchers.is;
1215
import static org.junit.Assert.*;
1316

1417
public class IOFunctionsTest {
@@ -30,8 +33,8 @@ public void close() {
3033
r -> () -> new BufferedReader(r).readLine()
3134
);
3235

33-
Assert.assertThat(bracketed.run(), Is.is("Read OK"));
34-
Assert.assertThat(closed.get(), Is.is(true));
36+
Assert.assertThat(bracketed.run(), is("Read OK"));
37+
Assert.assertThat(closed.get(), is(true));
3538
}
3639

3740
@Test
@@ -56,9 +59,63 @@ public void close() {
5659
bracketed.run();
5760
fail("Exception expected");
5861
} catch (IllegalArgumentException e) {
59-
Assert.assertThat(e.getMessage(), Is.is("OoO"));
62+
Assert.assertThat(e.getMessage(), is("OoO"));
6063
}
61-
Assert.assertThat(closed.get(), Is.is(true));
64+
Assert.assertThat(closed.get(), is(true));
65+
}
66+
67+
@Test
68+
public void testTraverseIO() throws IOException {
69+
String[] as = {"foo1", "bar2", "foobar3"};
70+
Stream<String> stream = Stream.arrayStream(as);
71+
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
72+
PrintStream originalOut = System.out;
73+
System.setOut(new PrintStream(outContent));
74+
stream.traverseIO(IOFunctions::stdoutPrint).run();
75+
System.setOut(originalOut);
76+
assertThat(outContent.toString(), is("foobar3bar2foo1"));
77+
}
78+
79+
@Test
80+
public void testSequenceWhile() throws IOException {
81+
BufferedReader r = new BufferedReader(new StringReader("foo1\nbar2\nfoobar3"));
82+
Stream<IO<String>> s1 = Stream.repeat(() -> r.readLine());
83+
IO<Stream<String>> io = sequenceWhile(s1, s -> !s.equals("foobar3"));
84+
assertThat(io.run(), is(cons("foo1", () -> cons("bar2", () -> Stream.nil()))));
85+
}
86+
87+
@Test
88+
public void testForeach() throws IOException {
89+
Stream<IO<String>> s1 = Stream.repeat(() -> "foo1");
90+
IO<Stream<String>> io = sequence(s1.take(2));
91+
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
92+
PrintStream originalOut = System.out;
93+
System.setOut(new PrintStream(outContent));
94+
runSafe(io).foreach(s -> runSafe(stdoutPrint(s)));
95+
System.setOut(originalOut);
96+
assertThat(outContent.toString(), is("foo1foo1"));
97+
}
98+
99+
100+
@Test
101+
public void testReplicateM() throws IOException {
102+
final IO<String> is = () -> new BufferedReader(new StringReader("foo")).readLine();
103+
assertThat(replicateM(is, 3).run(), is(List.list("foo", "foo", "foo")));
104+
}
105+
106+
107+
@Test
108+
public void testLift() throws IOException {
109+
final IO<String> readName = () -> new BufferedReader(new StringReader("foo")).readLine();
110+
final F<String, IO<String>> upperCaseAndPrint = F1Functions.<String, IO<String>, String>o(this::println).f(String::toUpperCase);
111+
final IO<String> readAndPrintUpperCasedName = IOFunctions.bind(readName, upperCaseAndPrint);
112+
assertThat(readAndPrintUpperCasedName.run(), is("FOO"));
113+
}
114+
115+
private IO<String> println(final String s) {
116+
return () -> {
117+
return s;
118+
};
62119
}
63120

64121
}

0 commit comments

Comments
 (0)