Skip to content

Commit 3093d93

Browse files
committed
Replacing unsafe calls in tests with IOMatcher interactions
1 parent 1b49bc6 commit 3093d93

File tree

6 files changed

+62
-56
lines changed

6 files changed

+62
-56
lines changed

src/test/java/com/jnape/palatable/lambda/functions/EffectTest.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
import static com.jnape.palatable.lambda.functions.Effect.effect;
1313
import static com.jnape.palatable.lambda.functions.Effect.fromConsumer;
1414
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
15+
import static com.jnape.palatable.lambda.functions.builtin.fn2.Alter.alter;
16+
import static com.jnape.palatable.lambda.functions.builtin.fn2.Sequence.sequence;
1517
import static com.jnape.palatable.lambda.functions.specialized.SideEffect.sideEffect;
1618
import static com.jnape.palatable.lambda.io.IO.io;
1719
import static java.util.Arrays.asList;
1820
import static java.util.Collections.singletonList;
21+
import static org.hamcrest.CoreMatchers.equalTo;
1922
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertThat;
24+
import static testsupport.matchers.IOMatcher.yieldsValue;
2025

2126
public class EffectTest {
2227

@@ -25,38 +30,39 @@ public void covariantReturns() {
2530
List<Object> results = new ArrayList<>();
2631

2732
Effect<String> effect = fromConsumer(results::add);
28-
Effect<Object> diMapL = effect.diMapL(Object::toString);
29-
Effect<Object> contraMap = effect.contraMap(Object::toString);
33+
Effect<String> diMapL = effect.diMapL(Object::toString);
34+
Effect<String> contraMap = effect.contraMap(Object::toString);
3035
Effect<String> stringEffect = effect.discardR(constantly("1"));
3136

32-
effect.apply("1").unsafePerformIO();
33-
diMapL.apply("2").unsafePerformIO();
34-
contraMap.apply("3").unsafePerformIO();
35-
stringEffect.apply("4").unsafePerformIO();
36-
37-
assertEquals(asList("1", "2", "3", "4"), results);
37+
assertThat(sequence(asList(effect.apply("1"),
38+
diMapL.apply("2"),
39+
contraMap.apply("3"),
40+
stringEffect.apply("4")),
41+
IO::io)
42+
.fmap(constantly(results)),
43+
yieldsValue(equalTo(asList("1", "2", "3", "4"))));
3844
}
3945

4046
@Test
4147
public void andThen() {
4248
AtomicInteger counter = new AtomicInteger();
4349
Effect<AtomicInteger> inc = c -> io(sideEffect(c::incrementAndGet));
4450

45-
inc.andThen(inc).apply(counter).unsafePerformIO();
46-
assertEquals(2, counter.get());
51+
assertThat(alter(inc.andThen(inc), counter).fmap(AtomicInteger::get),
52+
yieldsValue(equalTo(2)));
4753
}
4854

4955
@Test
5056
public void staticFactoryMethods() {
5157
AtomicInteger counter = new AtomicInteger();
5258

5359
Effect<String> sideEffect = effect(counter::incrementAndGet);
54-
sideEffect.apply("foo").unsafePerformIO();
55-
assertEquals(1, counter.get());
60+
assertThat(sideEffect.apply("foo").flatMap(constantly(io(counter::get))),
61+
yieldsValue(equalTo(1)));
5662

5763
Effect<AtomicInteger> fnEffect = Effect.fromConsumer(AtomicInteger::incrementAndGet);
58-
fnEffect.apply(counter).unsafePerformIO();
59-
assertEquals(2, counter.get());
64+
assertThat(fnEffect.apply(counter).flatMap(constantly(io(counter::get))),
65+
yieldsValue(equalTo(2)));
6066
}
6167

6268
@Test

src/test/java/com/jnape/palatable/lambda/functions/builtin/fn2/AlterTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77
import static com.jnape.palatable.lambda.functions.Effect.fromConsumer;
88
import static com.jnape.palatable.lambda.functions.builtin.fn2.Alter.alter;
99
import static java.util.Collections.singletonList;
10-
import static org.junit.Assert.assertEquals;
11-
import static org.junit.Assert.assertSame;
10+
import static org.hamcrest.CoreMatchers.allOf;
11+
import static org.hamcrest.CoreMatchers.equalTo;
12+
import static org.hamcrest.CoreMatchers.sameInstance;
13+
import static org.junit.Assert.assertThat;
14+
import static testsupport.matchers.IOMatcher.yieldsValue;
1215

1316
public class AlterTest {
1417

1518
@Test
1619
public void altersInput() {
1720
ArrayList<String> input = new ArrayList<>();
18-
assertSame(input, alter(fromConsumer(xs -> xs.add("foo")), input).unsafePerformIO());
19-
assertEquals(singletonList("foo"), input);
21+
assertThat(alter(fromConsumer(xs -> xs.add("foo")), input),
22+
yieldsValue(allOf(sameInstance(input),
23+
equalTo(singletonList("foo")))));
2024
}
2125
}

src/test/java/com/jnape/palatable/lambda/functions/builtin/fn2/UntilTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@
77
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
88
import static com.jnape.palatable.lambda.functions.builtin.fn2.Until.until;
99
import static com.jnape.palatable.lambda.io.IO.io;
10-
import static org.junit.Assert.assertEquals;
10+
import static org.hamcrest.CoreMatchers.equalTo;
11+
import static org.junit.Assert.assertThat;
12+
import static testsupport.matchers.IOMatcher.yieldsValue;
1113

1214
public class UntilTest {
1315

1416
@Test
1517
public void repeatedlyExecutesUntilPredicateMatches() {
1618
AtomicInteger counter = new AtomicInteger(0);
17-
assertEquals((Integer) 10, until(x -> x == 10, io(counter::getAndIncrement)).unsafePerformIO());
19+
assertThat(until(x -> x == 10, io(counter::getAndIncrement)),
20+
yieldsValue(equalTo(10)));
1821
}
1922

2023
@Test
2124
public void predicateThatImmediatelyMatchesDoesNotChangeIO() {
22-
assertEquals((Integer) 0, until(constantly(true), io(0)).unsafePerformIO());
25+
assertThat(until(constantly(true), io(0)), yieldsValue(equalTo(0)));
2326
}
2427
}

src/test/java/com/jnape/palatable/lambda/functions/builtin/fn3/BracketTest.java

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66

77
import java.util.concurrent.atomic.AtomicInteger;
88

9+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
910
import static com.jnape.palatable.lambda.functions.builtin.fn3.Bracket.bracket;
1011
import static com.jnape.palatable.lambda.io.IO.io;
12+
import static org.hamcrest.CoreMatchers.equalTo;
1113
import static org.junit.Assert.assertArrayEquals;
1214
import static org.junit.Assert.assertEquals;
13-
import static org.junit.Assert.fail;
15+
import static org.junit.Assert.assertThat;
16+
import static testsupport.matchers.IOMatcher.throwsException;
17+
import static testsupport.matchers.IOMatcher.yieldsValue;
1418

1519
public class BracketTest {
1620

@@ -26,7 +30,7 @@ public void cleanupHappyPath() {
2630
IO<Integer> hashIO = bracket(io(() -> count), c -> io(c::incrementAndGet), c -> io(c::hashCode));
2731

2832
assertEquals(0, count.get());
29-
assertEquals((Integer) count.hashCode(), hashIO.unsafePerformIO());
33+
assertThat(hashIO, yieldsValue(equalTo(count.hashCode())));
3034
assertEquals(1, count.get());
3135
}
3236

@@ -35,43 +39,30 @@ public void cleanupSadPath() {
3539
IllegalStateException thrown = new IllegalStateException("kaboom");
3640
IO<Integer> hashIO = bracket(io(count), c -> io(c::incrementAndGet), c -> io(() -> {throw thrown;}));
3741

38-
try {
39-
hashIO.unsafePerformIO();
40-
fail("Expected exception to be raised");
41-
} catch (IllegalStateException actual) {
42-
assertEquals(thrown, actual);
43-
assertEquals(1, count.get());
44-
}
42+
assertThat(hashIO, throwsException(equalTo(thrown)));
43+
assertEquals(1, count.get());
4544
}
4645

4746
@Test
4847
public void cleanupOnlyRunsIfInitialIORuns() {
4948
IllegalStateException thrown = new IllegalStateException("kaboom");
5049
IO<Integer> hashIO = bracket(io(() -> {throw thrown;}),
51-
__ -> io(count::incrementAndGet),
52-
__ -> io(count::incrementAndGet));
53-
try {
54-
hashIO.unsafePerformIO();
55-
fail("Expected exception to be raised");
56-
} catch (IllegalStateException actual) {
57-
assertEquals(thrown, actual);
58-
assertEquals(0, count.get());
59-
}
50+
constantly(io(count::incrementAndGet)),
51+
constantly(io(count::incrementAndGet)));
52+
53+
assertThat(hashIO, throwsException(equalTo(thrown)));
54+
assertEquals(0, count.get());
6055
}
6156

6257
@Test
6358
public void errorsInCleanupAreAddedToBodyErrors() {
6459
IllegalStateException bodyError = new IllegalStateException("kaboom");
6560
IllegalStateException cleanupError = new IllegalStateException("KABOOM");
6661
IO<Integer> hashIO = bracket(io(count),
67-
c -> io(() -> {throw cleanupError;}),
68-
c -> io(() -> {throw bodyError;}));
69-
try {
70-
hashIO.unsafePerformIO();
71-
fail("Expected exception to be raised");
72-
} catch (IllegalStateException actual) {
73-
assertEquals(bodyError, actual);
74-
assertArrayEquals(new Throwable[]{cleanupError}, actual.getSuppressed());
75-
}
62+
constantly(io(() -> {throw cleanupError;})),
63+
constantly(io(() -> {throw bodyError;})));
64+
65+
assertThat(hashIO, throwsException(equalTo(bodyError)));
66+
assertArrayEquals(new Throwable[]{cleanupError}, bodyError.getSuppressed());
7667
}
7768
}

src/test/java/com/jnape/palatable/lambda/monoid/builtin/RunAllTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55

66
import static com.jnape.palatable.lambda.io.IO.io;
77
import static com.jnape.palatable.lambda.monoid.builtin.RunAll.runAll;
8-
import static org.junit.Assert.assertEquals;
8+
import static org.hamcrest.CoreMatchers.equalTo;
9+
import static org.junit.Assert.assertThat;
10+
import static testsupport.matchers.IOMatcher.yieldsValue;
911

1012
public class RunAllTest {
1113

1214
@Test
1315
public void monoid() {
14-
Monoid<Integer> add = Monoid.monoid((x, y) -> x + y, 0);
15-
assertEquals((Integer) 3, runAll(add).apply(io(1), io(2)).unsafePerformIO());
16-
assertEquals((Integer) 0, runAll(add).identity().unsafePerformIO());
16+
Monoid<Integer> add = Monoid.monoid(Integer::sum, 0);
17+
assertThat(runAll(add).apply(io(1), io(2)), yieldsValue(equalTo(3)));
18+
assertThat(runAll(add).identity(), yieldsValue(equalTo(0)));
1719
}
1820
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package com.jnape.palatable.lambda.semigroup.builtin;
22

3-
import com.jnape.palatable.lambda.semigroup.Semigroup;
43
import org.junit.Test;
54

65
import static com.jnape.palatable.lambda.io.IO.io;
76
import static com.jnape.palatable.lambda.semigroup.builtin.RunAll.runAll;
8-
import static org.junit.Assert.assertEquals;
7+
import static org.hamcrest.CoreMatchers.equalTo;
8+
import static org.junit.Assert.assertThat;
9+
import static testsupport.matchers.IOMatcher.yieldsValue;
910

1011
public class RunAllTest {
1112

1213
@Test
1314
public void semigroup() {
14-
Semigroup<Integer> add = (x, y) -> x + y;
15-
assertEquals((Integer) 3, runAll(add).apply(io(1), io(2)).unsafePerformIO());
15+
assertThat(runAll(Integer::sum).apply(io(1), io(2)), yieldsValue(equalTo(3)));
1616
}
1717
}

0 commit comments

Comments
 (0)