Skip to content

Commit e9fd4ec

Browse files
gliptakgliptak-kemper
authored andcommitted
Add tests for Try, F, FW, Digit
Signed-off-by: Gábor Lipták <gliptak@gmail.com>
1 parent 5f75082 commit e9fd4ec

File tree

12 files changed

+226
-22
lines changed

12 files changed

+226
-22
lines changed

core/src/main/java/fj/F8Functions.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ private F8Functions() {
1111
/**
1212
* Partial application.
1313
*
14-
* @param a The <code>A</code> to which to apply this function.
1514
* @return The function partially applied to the given argument.
1615
*/
1716
public static <A, B, C, D, E, F$, G, H, I> F7<B, C, D, E, F$, G, H, I> f(final F8<A, B, C, D, E, F$, G, H, I> func, final A a) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package fj;
2+
3+
import fj.data.Option;
4+
import org.junit.Test;
5+
6+
import static fj.data.Array.range;
7+
import static org.hamcrest.core.Is.is;
8+
import static org.junit.Assert.assertThat;
9+
10+
public class DigitTest {
11+
@Test
12+
public void testInteger() {
13+
for (Integer i: range(0, 10)) {
14+
assertThat(Digit.fromLong(i).toLong(), is(i.longValue()));
15+
}
16+
}
17+
18+
@Test
19+
public void testChar() {
20+
for (Integer i: range(0, 10)) {
21+
Character c = Character.forDigit(i, 10);
22+
assertThat(Digit.fromChar(c).some().toChar(), is(c));
23+
}
24+
}
25+
26+
@Test
27+
public void testCharNone() {
28+
assertThat(Digit.fromChar('x'), is(Option.none()));
29+
}
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package fj;
2+
3+
import org.junit.Test;
4+
import static org.hamcrest.core.Is.is;
5+
import static org.junit.Assert.*;
6+
7+
public class FFunctionsTest {
8+
@Test
9+
public void testApply() {
10+
F8<Integer, Integer, Integer, Integer,
11+
Integer, Integer, Integer, Integer, Integer> f8 =
12+
(i1, i2, i3, i4, i5, i6, i7, i8) ->
13+
i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8;
14+
F7<Integer, Integer, Integer, Integer,
15+
Integer, Integer, Integer, Integer> f7 = F8Functions.f(f8, 8);
16+
F6<Integer, Integer, Integer, Integer, Integer, Integer, Integer> f6 =
17+
F7Functions.f(f7, 7);
18+
F5<Integer, Integer, Integer, Integer, Integer, Integer> f5 = F6Functions.f(f6, 6);
19+
F4<Integer, Integer, Integer, Integer, Integer> f4 = F5Functions.f(f5, 5);
20+
F3<Integer, Integer, Integer, Integer> f3 = F4Functions.f(f4, 4);
21+
F2<Integer, Integer, Integer> f2 = F3Functions.f(f3, 3);
22+
F<Integer, Integer> f1 = F2Functions.f(f2, 2);
23+
assertThat(F1Functions.f(f1, 1).f(), is(36));
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package fj;
2+
3+
import org.junit.Test;
4+
5+
import fj.F1W;
6+
import static org.hamcrest.core.Is.is;
7+
import static org.junit.Assert.assertThat;
8+
9+
public class FWFunctionsTest {
10+
@Test
11+
public void testLift1() {
12+
F<Integer, Integer> f = i -> i + 1;
13+
F1W f1w = F1W.lift(f);
14+
assertThat(f1w.f(1), is(2));
15+
}
16+
17+
@Test
18+
public void testLift2() {
19+
F2<Integer, Integer, Integer> f2 = (i, j) -> i + j;
20+
F2W f2w = F2W.lift(f2);
21+
assertThat(f2w.f(1, 2), is(3));
22+
}
23+
24+
}

core/src/test/java/fj/ShowTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,10 @@
1010
* Created by MarkPerry on 4/06/2015.
1111
*/
1212
public class ShowTest {
13-
14-
15-
1613
@Test
1714
public void arrayShow() {
1815
Array<Integer> a = array(3, 5, 7);
1916
String s = Show.arrayShow(Show.intShow).showS(a);
20-
System.out.println(s);
2117
assertTrue(s.equals("Array(3,5,7)"));
22-
2318
}
24-
2519
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package fj;
2+
3+
import fj.data.Validation;
4+
import fj.function.TryEffect0;
5+
import fj.function.TryEffect1;
6+
import org.junit.Test;
7+
import static org.hamcrest.core.Is.is;
8+
import static org.junit.Assert.*;
9+
10+
public class TryEffectTest {
11+
12+
@Test
13+
public void testTryEffect0Success() {
14+
F<TryEffect0, Validation<TryEffectException, Unit>> f =
15+
TryEffect.f(TryEffect0<TryEffectException>::f);
16+
Validation<TryEffectException, Unit> v = f.f(new AlwaysSucceed0());
17+
assertThat(v.isSuccess(), is(true));
18+
assertThat(v.success(), is(Unit.unit()));
19+
}
20+
21+
@Test
22+
public void testTryEffect0Fail() {
23+
F<TryEffect0, Validation<TryEffectException, Unit>> f =
24+
TryEffect.f(TryEffect0<TryEffectException>::f);
25+
Validation<TryEffectException, Unit> v = f.f(new AlwaysFail0());
26+
assertThat(v.isFail(), is(true));
27+
assertThat(v.fail(), is(new TryEffectException()));
28+
}
29+
30+
@Test
31+
public void testTryEffect1Success() {
32+
F2<TryEffect1<Integer, TryEffectException>, Integer, Validation<TryEffectException, Unit>> f =
33+
TryEffect.f(TryEffect1<Integer, TryEffectException>::f);
34+
Validation<TryEffectException, Unit> v = f.f(new AlwaysSucceed1(), 1);
35+
assertThat(v.isSuccess(), is(true));
36+
assertThat(v.success(), is(Unit.unit()));
37+
}
38+
39+
@Test
40+
public void testTryEffect1Fail() {
41+
F2<TryEffect1<Integer, TryEffectException>, Integer, Validation<TryEffectException, Unit>> f =
42+
TryEffect.f(TryEffect1<Integer, TryEffectException>::f);
43+
Validation<TryEffectException, Unit> v = f.f(new AlwaysFail1(), 1);
44+
assertThat(v.isFail(), is(true));
45+
assertThat(v.fail(), is(new TryEffectException()));
46+
}
47+
48+
class AlwaysSucceed0 implements TryEffect0<TryEffectException> {
49+
@Override
50+
public void f() throws TryEffectException {
51+
// SUCCESS
52+
}
53+
}
54+
55+
class AlwaysSucceed1 implements TryEffect1<Integer, TryEffectException> {
56+
@Override
57+
public void f(Integer i) throws TryEffectException {
58+
// SUCCESS;
59+
}
60+
}
61+
62+
class AlwaysFail0 implements TryEffect0<TryEffectException> {
63+
@Override
64+
public void f() throws TryEffectException {
65+
throw new TryEffectException();
66+
}
67+
}
68+
69+
class AlwaysFail1 implements TryEffect1<Integer, TryEffectException> {
70+
@Override
71+
public void f(Integer i) throws TryEffectException {
72+
throw new TryEffectException();
73+
}
74+
}
75+
76+
class TryEffectException extends Exception {
77+
@Override
78+
public boolean equals (Object obj) {
79+
return (obj instanceof TryEffectException);
80+
}
81+
}
82+
}

core/src/test/java/fj/TryTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package fj;
2+
3+
import fj.data.Validation;
4+
import fj.function.Try0;
5+
import org.junit.Test;
6+
7+
import static org.hamcrest.core.Is.is;
8+
import static org.junit.Assert.assertThat;
9+
10+
public class TryTest {
11+
12+
@Test
13+
public void testTrySuccess() {
14+
F<Try0<Integer, TryException>, Validation<TryException, Integer>> f =
15+
Try.f(Try0::f);
16+
Validation<TryException, Integer> v = f.f(new AlwaysSucceed());
17+
assertThat(v.isSuccess(), is(true));
18+
assertThat(v.success(), is(99));
19+
}
20+
21+
@Test
22+
public void testTryFail() {
23+
F<Try0<Integer, TryException>, Validation<TryException, Integer>> f =
24+
Try.f(Try0::f);
25+
Validation<TryException, Integer> v = f.f(new AlwaysFail());
26+
assertThat(v.isFail(), is(true));
27+
assertThat(v.fail(), is(new TryException()));
28+
}
29+
30+
class AlwaysSucceed implements Try0<Integer, TryException> {
31+
@Override
32+
public Integer f() throws TryException {
33+
return 99;
34+
}
35+
}
36+
37+
class AlwaysFail implements Try0<Integer, TryException> {
38+
@Override
39+
public Integer f() throws TryException {
40+
throw new TryException();
41+
}
42+
}
43+
44+
class TryException extends Exception {
45+
@Override
46+
public boolean equals (Object obj) {
47+
return (obj instanceof TryException);
48+
}
49+
}
50+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.util.EnumSet;
77

88
import static fj.Show.listShow;
9+
import static org.hamcrest.MatcherAssert.assertThat;
10+
import static org.hamcrest.core.Is.is;
911

1012
/**
1113
* Created by MarkPerry on 14/07/2014.
@@ -16,7 +18,7 @@ public class JavaTest {
1618
public void test1() {
1719
// #33: Fixes ClassCastException
1820
final List<Colors> colors = Java.<Colors>EnumSet_List().f(EnumSet.allOf(Colors.class));
19-
listShow(Show.<Colors>anyShow()).print(colors);
21+
assertThat(listShow(Show.<Colors>anyShow()).showS(colors), is("List(red,green,blue)"));
2022
}
2123

2224
enum Colors {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public void traverseList() {
2323
int max = 3;
2424
List<Option<Integer>> actual = some(max).traverseList(a -> List.range(1, a + 1));
2525
List<Option<Integer>> expected = List.range(1, max + 1).map(i -> some(i));
26-
System.out.println(String.format("actual: %s, expected: %s", actual.toString(), expected.toString()));
2726
assertTrue(actual.equals(expected));
2827
}
2928

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import fj.P2;
44
import org.junit.Test;
55

6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.hamcrest.core.Is.is;
68
import static org.junit.Assert.assertEquals;
79
import static org.junit.Assert.assertFalse;
810
import static org.junit.Assert.assertTrue;
@@ -40,7 +42,8 @@ public void convertToString() {
4042
@Test
4143
public void test() {
4244
P2<Seq<Integer>, Seq<Integer>> p2 = Seq.single(1).split(5);
43-
System.out.println(p2);
45+
assertThat(p2._1(), is(Seq.single(1)));
46+
assertThat(p2._2(), is(Seq.empty()));
4447
}
4548

4649
}

0 commit comments

Comments
 (0)