Skip to content

Commit c2392a1

Browse files
committed
Add Reader/Writer tests
Signed-off-by: Gábor Lipták <gliptak@gmail.com>
1 parent 21a73c4 commit c2392a1

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

props-core/src/test/java/fj/data/ReaderTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import static fj.test.Cogen.cogenInteger;
1212
import static fj.test.Property.prop;
1313
import static fj.test.Property.property;
14+
import static org.hamcrest.core.Is.is;
15+
import static org.junit.Assert.assertThat;
1416
import static org.junit.Assert.assertTrue;
1517

1618
/**
@@ -24,7 +26,6 @@ public void testMap() {
2426
// example taken from http://learnyouahaskell.com/for-a-few-monads-more
2527
int x = Reader.unit((Integer i) -> i + 3).map(i -> i * 5).f(8);
2628
assertTrue(x == 55);
27-
// System.out.println(x); // 55
2829
}
2930

3031
@Test
@@ -108,6 +109,19 @@ public void testAssociativity() {
108109
PropertyAssert.assertResult(p);
109110
}
110111

112+
@Test
113+
public void testAndThen() {
114+
final int y = Reader.unit((Integer i) -> i * 2).andThen(i -> i + 10).f(10);
115+
assertThat(y, is(30));
116+
}
117+
118+
@Test
119+
public void testBind() {
120+
final int y = Reader.unit((Integer i) -> i * 2)
121+
.bind(a -> Reader.unit(i -> a + i + 11)).f(10);
122+
assertThat(y, is(41));
123+
}
124+
111125
public Gen<Reader<Integer, Integer>> arbReader() {
112126
return Arbitrary.arbReader(cogenInteger, arbInteger);
113127
}

props-core/src/test/java/fj/data/WriterTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import fj.Equal;
44
import fj.F;
5-
import fj.data.test.PropertyAssert;
6-
import fj.test.Arbitrary;
5+
import fj.P;
76
import fj.test.Gen;
87
import fj.test.Property;
98
import org.junit.Assert;
@@ -14,7 +13,8 @@
1413
import static fj.test.Cogen.cogenInteger;
1514
import static fj.test.Property.prop;
1615
import static fj.test.Property.property;
17-
import static org.junit.Assert.assertTrue;
16+
import static org.hamcrest.MatcherAssert.assertThat;
17+
import static org.hamcrest.core.Is.is;
1818

1919
/**
2020
* Created by MarkPerry on 17/12/2014.
@@ -107,7 +107,13 @@ public void testAssociativity() {
107107
assertResult(p);
108108
}
109109

110-
110+
@Test
111+
public void testUnit() {
112+
Writer<String, String> w = Writer.unit("+").tell("foo").tell("bar");
113+
assertThat(w.run(), is(P.p("foobar", "+")));
114+
assertThat(w.log(), is("foobar"));
115+
assertThat(w.value(), is("+"));
116+
}
111117

112118

113119
}

0 commit comments

Comments
 (0)