X Tutup
Skip to content

Commit 245a209

Browse files
committed
Adding BifunctorLaws to assist with testing properties
1 parent 4ee9daf commit 245a209

File tree

13 files changed

+78
-79
lines changed

13 files changed

+78
-79
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
1010
### Changed
1111
- `Functor`, `Bifunctor`, and `Profunctor` (as well as all instances) get a unification parameter
1212
- `Identity` supports value equality
13+
- `Const` supports value equality
1314

1415
## [1.5.6] - 2017-02-11
1516
### Added

src/main/java/com/jnape/palatable/lambda/functor/builtin/Const.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.jnape.palatable.lambda.functor.Applicative;
44
import com.jnape.palatable.lambda.functor.Bifunctor;
55

6+
import java.util.Objects;
67
import java.util.function.Function;
78

89
/**
@@ -31,6 +32,16 @@ public A runConst() {
3132
return a;
3233
}
3334

35+
@Override
36+
public boolean equals(Object other) {
37+
return other instanceof Const && Objects.equals(a, ((Const) other).a);
38+
}
39+
40+
@Override
41+
public int hashCode() {
42+
return Objects.hash(a);
43+
}
44+
3445
/**
3546
* Map over the right parameter. Note that because <code>B</code> is never actually known quantity outside of a type
3647
* signature, this is effectively a no-op that serves only to alter <code>Const's</code> type signature.

src/test/java/com/jnape/palatable/lambda/adt/EitherTest.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.junit.rules.ExpectedException;
99
import org.junit.runner.RunWith;
1010
import testsupport.traits.ApplicativeLaws;
11+
import testsupport.traits.BifunctorLaws;
1112
import testsupport.traits.FunctorLaws;
1213

1314
import java.util.Optional;
@@ -29,7 +30,7 @@ public class EitherTest {
2930
@Rule
3031
public ExpectedException thrown = ExpectedException.none();
3132

32-
@TestTraits({FunctorLaws.class, ApplicativeLaws.class})
33+
@TestTraits({FunctorLaws.class, ApplicativeLaws.class, BifunctorLaws.class})
3334
public Subjects<Either<String, Integer>> testSubjects() {
3435
return subjects(left("foo"), right(1));
3536
}
@@ -163,15 +164,6 @@ public void fromOptionalDoesNotEvaluateLeftFnForRight() {
163164
assertThat(atomicInteger.get(), is(0));
164165
}
165166

166-
@Test
167-
public void bifunctorProperties() {
168-
Either<String, Integer> left = left("foo");
169-
Either<String, Integer> right = right(1);
170-
171-
assertThat(left.biMap(l -> l + "bar", r -> r + 1), is(left("foobar")));
172-
assertThat(right.biMap(l -> l + "bar", r -> r + 1), is(right(2)));
173-
}
174-
175167
@Test
176168
public void monadicTryingLiftsCheckedSupplier() {
177169
assertEquals(right(1), Either.trying(() -> 1));

src/test/java/com/jnape/palatable/lambda/adt/choice/Choice2Test.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.Test;
88
import org.junit.runner.RunWith;
99
import testsupport.traits.ApplicativeLaws;
10+
import testsupport.traits.BifunctorLaws;
1011
import testsupport.traits.FunctorLaws;
1112

1213
import static com.jnape.palatable.lambda.adt.choice.Choice2.a;
@@ -26,7 +27,7 @@ public void setUp() {
2627
b = b(true);
2728
}
2829

29-
@TestTraits({FunctorLaws.class, ApplicativeLaws.class})
30+
@TestTraits({FunctorLaws.class, ApplicativeLaws.class, BifunctorLaws.class})
3031
public Subjects<Choice2<String, Integer>> testSubjects() {
3132
return subjects(a("foo"), b(1));
3233
}
@@ -36,10 +37,4 @@ public void divergeStaysInChoice() {
3637
assertEquals(Choice3.a(1), a.diverge());
3738
assertEquals(Choice3.b(true), b.diverge());
3839
}
39-
40-
@Test
41-
public void bifunctorProperties() {
42-
assertEquals(a(-1), a.biMap(i -> i * -1, bool -> !bool));
43-
assertEquals(b(false), b.biMap(i -> i * -1, bool -> !bool));
44-
}
4540
}

src/test/java/com/jnape/palatable/lambda/adt/choice/Choice3Test.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.Test;
88
import org.junit.runner.RunWith;
99
import testsupport.traits.ApplicativeLaws;
10+
import testsupport.traits.BifunctorLaws;
1011
import testsupport.traits.FunctorLaws;
1112

1213
import static com.jnape.palatable.lambda.adt.choice.Choice3.a;
@@ -29,7 +30,7 @@ public void setUp() {
2930
c = Choice3.c(true);
3031
}
3132

32-
@TestTraits({FunctorLaws.class, ApplicativeLaws.class})
33+
@TestTraits({FunctorLaws.class, ApplicativeLaws.class, BifunctorLaws.class})
3334
public Subjects<Choice3<String, Integer, Boolean>> testSubjects() {
3435
return subjects(a("foo"), b(1), c(true));
3536
}
@@ -47,11 +48,4 @@ public void divergeStaysInChoice() {
4748
assertEquals(Choice4.b("two"), b.diverge());
4849
assertEquals(Choice4.c(true), c.diverge());
4950
}
50-
51-
@Test
52-
public void bifunctorProperties() {
53-
assertEquals(a, a.biMap(String::toUpperCase, bool -> !bool));
54-
assertEquals(b("TWO"), b.biMap(String::toUpperCase, bool -> !bool));
55-
assertEquals(c(false), c.biMap(String::toUpperCase, bool -> !bool));
56-
}
5751
}

src/test/java/com/jnape/palatable/lambda/adt/choice/Choice4Test.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.Test;
88
import org.junit.runner.RunWith;
99
import testsupport.traits.ApplicativeLaws;
10+
import testsupport.traits.BifunctorLaws;
1011
import testsupport.traits.FunctorLaws;
1112

1213
import static com.jnape.palatable.lambda.adt.choice.Choice4.a;
@@ -32,7 +33,7 @@ public void setUp() {
3233
d = d(4D);
3334
}
3435

35-
@TestTraits({FunctorLaws.class, ApplicativeLaws.class})
36+
@TestTraits({FunctorLaws.class, ApplicativeLaws.class, BifunctorLaws.class})
3637
public Subjects<Choice4<String, Integer, Boolean, Character>> testSubjects() {
3738
return subjects(a("foo"), b(1), c(true), d('a'));
3839
}
@@ -52,12 +53,4 @@ public void divergeStaysInChoice() {
5253
assertEquals(Choice5.c(true), c.diverge());
5354
assertEquals(Choice5.d(4D), d.diverge());
5455
}
55-
56-
@Test
57-
public void bifunctorProperties() {
58-
assertEquals(a, a.biMap(c -> !c, d -> -d));
59-
assertEquals(b, b.biMap(c -> !c, d -> -d));
60-
assertEquals(c(false), c.biMap(c -> !c, d -> -d));
61-
assertEquals(d(-4D), d.biMap(c -> !c, d -> -d));
62-
}
6356
}

src/test/java/com/jnape/palatable/lambda/adt/choice/Choice5Test.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.Test;
88
import org.junit.runner.RunWith;
99
import testsupport.traits.ApplicativeLaws;
10+
import testsupport.traits.BifunctorLaws;
1011
import testsupport.traits.FunctorLaws;
1112

1213
import static com.jnape.palatable.lambda.adt.choice.Choice5.a;
@@ -35,7 +36,7 @@ public void setUp() {
3536
e = e('z');
3637
}
3738

38-
@TestTraits({FunctorLaws.class, ApplicativeLaws.class})
39+
@TestTraits({FunctorLaws.class, ApplicativeLaws.class, BifunctorLaws.class})
3940
public Subjects<Choice5<String, Integer, Boolean, Character, Double>> testSubjects() {
4041
return subjects(Choice5.a("foo"), Choice5.b(1), Choice5.c(true), Choice5.d('a'), Choice5.e(2d));
4142
}
@@ -48,13 +49,4 @@ public void convergeStaysInChoice() {
4849
assertEquals(Choice4.d(4d), d.converge(e -> Choice4.b(e.toString())));
4950
assertEquals(Choice4.b("z"), e.converge(e -> Choice4.b(e.toString())));
5051
}
51-
52-
@Test
53-
public void bifunctorProperties() {
54-
assertEquals(a, a.biMap(d -> -d, Character::toUpperCase));
55-
assertEquals(b, b.biMap(d -> -d, Character::toUpperCase));
56-
assertEquals(c, c.biMap(d -> -d, Character::toUpperCase));
57-
assertEquals(d(-4D), d.biMap(d -> -d, Character::toUpperCase));
58-
assertEquals(e('Z'), e.biMap(d -> -d, Character::toUpperCase));
59-
}
6052
}

src/test/java/com/jnape/palatable/lambda/adt/hlist/Tuple2Test.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.Test;
77
import org.junit.runner.RunWith;
88
import testsupport.traits.ApplicativeLaws;
9+
import testsupport.traits.BifunctorLaws;
910
import testsupport.traits.FunctorLaws;
1011

1112
import java.util.HashMap;
@@ -29,7 +30,7 @@ public void setUp() throws Exception {
2930
tuple2 = new Tuple2<>(1, new SingletonHList<>(2));
3031
}
3132

32-
@TestTraits({FunctorLaws.class, ApplicativeLaws.class})
33+
@TestTraits({FunctorLaws.class, ApplicativeLaws.class, BifunctorLaws.class})
3334
public Tuple2 testSubject() {
3435
return tuple("one", 2);
3536
}
@@ -77,11 +78,6 @@ public void fill() {
7778
assertEquals(tuple("foo", "foo"), Tuple2.fill("foo"));
7879
}
7980

80-
@Test
81-
public void bifunctorProperties() {
82-
assertEquals(new Tuple2<>("1", new SingletonHList<>("2")), tuple2.biMap(Object::toString, Object::toString));
83-
}
84-
8581
@Test
8682
public void mapEntryProperties() {
8783
assertEquals((Integer) 1, tuple2.getKey());

src/test/java/com/jnape/palatable/lambda/adt/hlist/Tuple3Test.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.Test;
77
import org.junit.runner.RunWith;
88
import testsupport.traits.ApplicativeLaws;
9+
import testsupport.traits.BifunctorLaws;
910
import testsupport.traits.FunctorLaws;
1011

1112
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
@@ -25,7 +26,7 @@ public void setUp() {
2526
tuple3 = new Tuple3<>(1, new Tuple2<>("2", new SingletonHList<>('3')));
2627
}
2728

28-
@TestTraits({FunctorLaws.class, ApplicativeLaws.class})
29+
@TestTraits({FunctorLaws.class, ApplicativeLaws.class, BifunctorLaws.class})
2930
public Tuple3 testSubject() {
3031
return tuple("one", 2, 3d);
3132
}
@@ -75,9 +76,4 @@ public void into() {
7576
public void fill() {
7677
assertEquals(tuple("foo", "foo", "foo"), Tuple3.fill("foo"));
7778
}
78-
79-
@Test
80-
public void bifunctorProperties() {
81-
assertEquals(new Tuple3<>(1, new Tuple2<>(2, new SingletonHList<>("3"))), tuple3.biMap(Integer::parseInt, Object::toString));
82-
}
8379
}

src/test/java/com/jnape/palatable/lambda/adt/hlist/Tuple4Test.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.Test;
77
import org.junit.runner.RunWith;
88
import testsupport.traits.ApplicativeLaws;
9+
import testsupport.traits.BifunctorLaws;
910
import testsupport.traits.FunctorLaws;
1011

1112
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
@@ -25,7 +26,7 @@ public void setUp() {
2526
tuple4 = new Tuple4<>(1, new Tuple3<>("2", new Tuple2<>('3', new SingletonHList<>(false))));
2627
}
2728

28-
@TestTraits({FunctorLaws.class, ApplicativeLaws.class})
29+
@TestTraits({FunctorLaws.class, ApplicativeLaws.class, BifunctorLaws.class})
2930
public Tuple4 testSubject() {
3031
return tuple("one", 2, 3d, 4f);
3132
}
@@ -78,10 +79,4 @@ public void into() {
7879
public void fill() {
7980
assertEquals(tuple("foo", "foo", "foo", "foo"), Tuple4.fill("foo"));
8081
}
81-
82-
@Test
83-
public void bifunctorProperties() {
84-
assertEquals(new Tuple4<>(1, new Tuple3<>("2", new Tuple2<>("3", new SingletonHList<>(true)))),
85-
tuple4.biMap(Object::toString, x -> !x));
86-
}
8782
}

0 commit comments

Comments
 (0)
X Tutup