|
| 1 | +package com.garciat.typeclasses; |
| 2 | + |
| 3 | +import static com.garciat.typeclasses.TypeClasses.witness; |
| 4 | +import static org.junit.jupiter.api.Assertions.*; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | +import java.util.Optional; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +/** Tests demonstrating the library's public API usage. */ |
| 11 | +final class LibraryUsageTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + void canResolveShowForBasicTypes() { |
| 15 | + Show<Integer> showInt = witness(new Ty<>() {}); |
| 16 | + assertEquals("42", showInt.show(42)); |
| 17 | + |
| 18 | + Show<String> showString = witness(new Ty<>() {}); |
| 19 | + assertEquals("\"hello\"", showString.show("hello")); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + void canResolveShowForComplexTypes() { |
| 24 | + Show<List<Integer>> showListInt = witness(new Ty<>() {}); |
| 25 | + String result = showListInt.show(List.of(1, 2, 3)); |
| 26 | + assertEquals("[1, 2, 3]", result); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void canResolveShowForNestedTypes() { |
| 31 | + Show<Optional<List<String>>> showOptListStr = witness(new Ty<>() {}); |
| 32 | + String result = showOptListStr.show(Optional.of(List.of("a", "b"))); |
| 33 | + assertEquals("Some([\"a\", \"b\"])", result); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void canResolveEqForBasicTypes() { |
| 38 | + Eq<Integer> eqInt = witness(new Ty<>() {}); |
| 39 | + assertTrue(eqInt.eq(42, 42)); |
| 40 | + assertFalse(eqInt.eq(42, 43)); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void canResolveEqForComplexTypes() { |
| 45 | + Eq<List<String>> eqListStr = witness(new Ty<>() {}); |
| 46 | + assertTrue(eqListStr.eq(List.of("a", "b"), List.of("a", "b"))); |
| 47 | + assertFalse(eqListStr.eq(List.of("a", "b"), List.of("a", "c"))); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void canResolveOrdForBasicTypes() { |
| 52 | + Ord<Integer> ordInt = witness(new Ty<>() {}); |
| 53 | + assertEquals(Ordering.LT, ordInt.compare(1, 2)); |
| 54 | + assertEquals(Ordering.EQ, ordInt.compare(2, 2)); |
| 55 | + assertEquals(Ordering.GT, ordInt.compare(3, 2)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void canResolveMonoidForString() { |
| 60 | + Monoid<String> monoidString = witness(new Ty<>() {}); |
| 61 | + assertEquals("", monoidString.identity()); |
| 62 | + assertEquals("ab", monoidString.combine("a", "b")); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void canUseMonoidCombineAll() { |
| 67 | + List<String> strings = List.of("Hello", " ", "World"); |
| 68 | + String result = Monoid.combineAll(witness(new Ty<>() {}), strings); |
| 69 | + assertEquals("Hello World", result); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void canUseFunctorMap() { |
| 74 | + Functor<Maybe.Tag> functorMaybe = witness(new Ty<>() {}); |
| 75 | + TApp<Maybe.Tag, Integer> maybeInt = Maybe.just(42); |
| 76 | + TApp<Maybe.Tag, String> maybeStr = functorMaybe.map(Object::toString, maybeInt); |
| 77 | + assertEquals("42", Maybe.unwrap(maybeStr).fold(() -> null, x -> x)); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void canUseApplicativePure() { |
| 82 | + Applicative<Maybe.Tag> appMaybe = witness(new Ty<>() {}); |
| 83 | + TApp<Maybe.Tag, Integer> maybeInt = appMaybe.pure(42); |
| 84 | + Integer unwrapped = Maybe.unwrap(maybeInt).fold(() -> null, x -> x); |
| 85 | + assertEquals(42, unwrapped.intValue()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void canUseMonadFlatMap() { |
| 90 | + Monad<Maybe.Tag> monadMaybe = witness(new Ty<>() {}); |
| 91 | + TApp<Maybe.Tag, Integer> maybeInt = Maybe.just(42); |
| 92 | + TApp<Maybe.Tag, Integer> result = monadMaybe.flatMap(x -> Maybe.just(x * 2), maybeInt); |
| 93 | + Integer unwrapped = Maybe.unwrap(result).fold(() -> null, x -> x); |
| 94 | + assertEquals(84, unwrapped.intValue()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void canUseFoldableLength() { |
| 99 | + Foldable<FwdList.Tag> foldableFwdList = witness(new Ty<>() {}); |
| 100 | + FwdList<Integer> list = FwdList.of(1, 2, 3, 4, 5); |
| 101 | + assertEquals(5, (int) foldableFwdList.length(list)); |
| 102 | + } |
| 103 | +} |
0 commit comments