Skip to content
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
30 changes: 0 additions & 30 deletions core/src/test/java/fj/IOTest.java

This file was deleted.

69 changes: 63 additions & 6 deletions core/src/test/java/fj/data/IOFunctionsTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package fj.data;

import fj.Unit;
import org.hamcrest.core.Is;
import fj.*;
import org.junit.Assert;
import org.junit.Test;

import java.io.*;
import java.io.Reader;
import java.util.concurrent.atomic.AtomicBoolean;

import static fj.data.IOFunctions.*;
import static fj.data.Stream.cons;
import static fj.data.Stream.nil_;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;

public class IOFunctionsTest {
Expand All @@ -30,8 +33,8 @@ public void close() {
r -> () -> new BufferedReader(r).readLine()
);

Assert.assertThat(bracketed.run(), Is.is("Read OK"));
Assert.assertThat(closed.get(), Is.is(true));
Assert.assertThat(bracketed.run(), is("Read OK"));
Assert.assertThat(closed.get(), is(true));
}

@Test
Expand All @@ -56,9 +59,63 @@ public void close() {
bracketed.run();
fail("Exception expected");
} catch (IllegalArgumentException e) {
Assert.assertThat(e.getMessage(), Is.is("OoO"));
Assert.assertThat(e.getMessage(), is("OoO"));
}
Assert.assertThat(closed.get(), Is.is(true));
Assert.assertThat(closed.get(), is(true));
}

@Test
public void testTraverseIO() throws IOException {
String[] as = {"foo1", "bar2", "foobar3"};
Stream<String> stream = Stream.arrayStream(as);
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
PrintStream originalOut = System.out;
System.setOut(new PrintStream(outContent));
stream.traverseIO(IOFunctions::stdoutPrint).run();
System.setOut(originalOut);
assertThat(outContent.toString(), is("foobar3bar2foo1"));
}

@Test
public void testSequenceWhile() throws IOException {
BufferedReader r = new BufferedReader(new StringReader("foo1\nbar2\nfoobar3"));
Stream<IO<String>> s1 = Stream.repeat(() -> r.readLine());
IO<Stream<String>> io = sequenceWhile(s1, s -> !s.equals("foobar3"));
assertThat(io.run(), is(cons("foo1", () -> cons("bar2", () -> Stream.nil()))));
}

@Test
public void testForeach() throws IOException {
Stream<IO<String>> s1 = Stream.repeat(() -> "foo1");
IO<Stream<String>> io = sequence(s1.take(2));
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
PrintStream originalOut = System.out;
System.setOut(new PrintStream(outContent));
runSafe(io).foreach(s -> runSafe(stdoutPrint(s)));
System.setOut(originalOut);
assertThat(outContent.toString(), is("foo1foo1"));
}


@Test
public void testReplicateM() throws IOException {
final IO<String> is = () -> new BufferedReader(new StringReader("foo")).readLine();
assertThat(replicateM(is, 3).run(), is(List.list("foo", "foo", "foo")));
}


@Test
public void testLift() throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is lift tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just moved this code from the other Test.java I deleted

final IO<String> readName = () -> new BufferedReader(new StringReader("foo")).readLine();
final F<String, IO<String>> upperCaseAndPrint = F1Functions.<String, IO<String>, String>o(this::println).f(String::toUpperCase);
final IO<String> readAndPrintUpperCasedName = IOFunctions.bind(readName, upperCaseAndPrint);
assertThat(readAndPrintUpperCasedName.run(), is("FOO"));
}

private IO<String> println(final String s) {
return () -> {
return s;
};
}

}