Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored and mikearnaldi committed Oct 17, 2022
1 parent be638f4 commit e1fd35e
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 26 deletions.
15 changes: 15 additions & 0 deletions test/Semigroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,24 @@ describe("Semigroup", () => {
it("reverse", () => {
const S = _.reverse(string.Semigroup)
U.deepStrictEqual(pipe("a", S.combine("b")), "ba")
U.deepStrictEqual(pipe("a", S.combineMany([])), "a")
U.deepStrictEqual(pipe("a", S.combineMany(["b"])), "ba")
})

it("constant", () => {
const S = _.constant("c")
U.deepStrictEqual(pipe("a", S.combine("b")), "c")
U.deepStrictEqual(pipe("a", S.combineMany([])), "c")
U.deepStrictEqual(pipe("a", S.combineMany(["b"])), "c")
})

it("intercalate", () => {
const S = pipe(string.Semigroup, _.intercalate("|"))
U.deepStrictEqual(pipe("a", S.combine("b")), "a|b")
U.deepStrictEqual(pipe("a", S.combineMany([])), "a")
U.deepStrictEqual(pipe("a", S.combineMany(["b"])), "a|b")
})

describe("min", () => {
it("should return the minimum", () => {
const S = _.min(number.Sortable)
Expand Down
19 changes: 11 additions & 8 deletions test/Semigroupal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import * as O from "./data/Option"
import * as U from "./util"

describe("Semigroupal", () => {
it("zipWithComposition", () => {
const sum = (a: number, b: number): number => a + b
const zipWith = _.zipWithComposition(O.Semigroupal, O.Semigroupal)
U.deepStrictEqual(pipe(O.none, zipWith(O.none, sum)), O.none)
U.deepStrictEqual(pipe(O.some(O.none), zipWith(O.none, sum)), O.none)
U.deepStrictEqual(pipe(O.some(O.some(1)), zipWith(O.none, sum)), O.none)
U.deepStrictEqual(pipe(O.some(O.some(1)), zipWith(O.some(O.none), sum)), O.some(O.none))
U.deepStrictEqual(pipe(O.some(O.none), zipWith(O.some(O.some(2)), sum)), O.some(O.none))
U.deepStrictEqual(pipe(O.some(O.some(1)), zipWith(O.some(O.some(2)), sum)), O.some(O.some(3)))
})

it("zipManyComposition", () => {
const zipMany = _.zipManyComposition(O.Semigroupal, O.Semigroupal)
U.deepStrictEqual(pipe(O.none, zipMany([O.none])), O.none)
Expand Down Expand Up @@ -32,14 +43,6 @@ describe("Semigroupal", () => {
U.deepStrictEqual(pipe(O.some(1), zip(O.some("a"))), O.some([1, "a"] as const))
})

it("zipWith", () => {
const zipWith = _.zipWith(O.Semigroupal)
const sum = (a: number, b: number) => a + b
U.deepStrictEqual(pipe(O.none, zipWith(O.none, sum)), O.none)
U.deepStrictEqual(pipe(O.some(1), zipWith(O.none, sum)), O.none)
U.deepStrictEqual(pipe(O.some(1), zipWith(O.some(2), sum)), O.some(3))
})

it("lift2", () => {
const sum = _.lift2(O.Semigroupal)((a: number, b: number) => a + b)
U.deepStrictEqual(sum(O.none, O.none), O.none)
Expand Down
72 changes: 64 additions & 8 deletions test/Sortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,54 @@ import * as U from "./util"

describe("Sortable", () => {
it("tuple", () => {
const O = _.tuple(string.Sortable, number.Sortable, boolean.Sortable)
U.deepStrictEqual(pipe(["a", 1, true], O.compare(["b", 2, true])), -1)
U.deepStrictEqual(pipe(["a", 1, true], O.compare(["a", 2, true])), -1)
U.deepStrictEqual(pipe(["a", 1, true], O.compare(["a", 1, false])), 1)
const S = _.tuple(string.Sortable, number.Sortable, boolean.Sortable)
U.deepStrictEqual(pipe(["a", 1, true], S.compare(["b", 2, true])), -1)
U.deepStrictEqual(pipe(["a", 1, true], S.compare(["a", 2, true])), -1)
U.deepStrictEqual(pipe(["a", 1, true], S.compare(["a", 1, false])), 1)
})

it("Contravariant", () => {
const S = pipe(number.Sortable, _.Contravariant.contramap((s: string) => s.length))
U.deepStrictEqual(pipe("a", S.compare("b")), 0)
U.deepStrictEqual(pipe("a", S.compare("bb")), -1)
U.deepStrictEqual(pipe("aa", S.compare("b")), 1)
})

it("getSemigroup", () => {
type T = readonly [number, string]
const tuples: ReadonlyArray<T> = [
[2, "c"],
[1, "b"],
[2, "a"],
[1, "c"]
]
const S = _.getSemigroup<T>()
const sortByFst = pipe(
number.Sortable,
_.contramap((x: T) => x[0])
)
const sortBySnd = pipe(
string.Sortable,
_.contramap((x: T) => x[1])
)
U.deepStrictEqual(sort(pipe(sortByFst, S.combine(sortBySnd)))(tuples), [
[1, "b"],
[1, "c"],
[2, "a"],
[2, "c"]
])
U.deepStrictEqual(sort(pipe(sortBySnd, S.combine(sortByFst)))(tuples), [
[2, "a"],
[1, "b"],
[1, "c"],
[2, "c"]
])
U.deepStrictEqual(sort(pipe(sortBySnd, S.combineMany([])))(tuples), [
[2, "a"],
[1, "b"],
[2, "c"],
[1, "c"]
])
})

it("getMonoid", () => {
Expand All @@ -31,15 +75,13 @@ describe("Sortable", () => {
string.Sortable,
_.contramap((x: T) => x[1])
)
const O1 = pipe(M.empty, M.combineMany([sortByFst, sortBySnd]))
U.deepStrictEqual(sort(O1)(tuples), [
U.deepStrictEqual(sort(pipe(M.empty, M.combineMany([sortByFst, sortBySnd])))(tuples), [
[1, "b"],
[1, "c"],
[2, "a"],
[2, "c"]
])
const O2 = pipe(sortBySnd, M.combineMany([sortByFst, M.empty]))
U.deepStrictEqual(sort(O2)(tuples), [
U.deepStrictEqual(sort(pipe(sortBySnd, M.combineMany([sortByFst, M.empty])))(tuples), [
[2, "a"],
[1, "b"],
[1, "c"],
Expand Down Expand Up @@ -72,13 +114,27 @@ describe("Sortable", () => {
U.deepStrictEqual(pipe(2, Compare.compare(2)), 0)
})

it("lt", () => {
const lt = _.lt(number.Sortable)
U.deepStrictEqual(pipe(0, lt(1)), true)
U.deepStrictEqual(pipe(1, lt(1)), false)
U.deepStrictEqual(pipe(2, lt(1)), false)
})

it("leq", () => {
const leq = _.leq(number.Sortable)
U.deepStrictEqual(pipe(0, leq(1)), true)
U.deepStrictEqual(pipe(1, leq(1)), true)
U.deepStrictEqual(pipe(2, leq(1)), false)
})

it("gt", () => {
const gt = _.gt(number.Sortable)
U.deepStrictEqual(pipe(0, gt(1)), false)
U.deepStrictEqual(pipe(1, gt(1)), false)
U.deepStrictEqual(pipe(2, gt(1)), true)
})

it("geq", () => {
const geq = _.geq(number.Sortable)
U.deepStrictEqual(pipe(0, geq(1)), false)
Expand Down
5 changes: 0 additions & 5 deletions test/data/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,6 @@ export const zip: <B, A>(
that: Option<B>
) => (self: Option<A>) => Option<readonly [A, B]> = semigroupal.zip(Semigroupal)

export const zipWith: <B, A, C>(
that: Option<B>,
f: (a: A, b: B) => C
) => (self: Option<A>) => Option<C> = semigroupal.zipWith(Semigroupal)

export const ap: <A>(fa: Option<A>) => <B>(fab: Option<(a: A) => B>) => Option<B> = semigroupal
.ap(
Semigroupal
Expand Down
5 changes: 0 additions & 5 deletions test/data/Result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,3 @@ export const Semigroupal: semigroupal.Semigroupal<ResultTypeLambda> = {
export const zip: <E2, B, A>(
that: Result<E2, B>
) => <E1>(self: Result<E1, A>) => Result<E2 | E1, readonly [A, B]> = semigroupal.zip(Semigroupal)

export const zipWith: <E2, B, A, C>(
that: Result<E2, B>,
f: (a: A, b: B) => C
) => <E1>(self: Result<E1, A>) => Result<E2 | E1, C> = semigroupal.zipWith(Semigroupal)

0 comments on commit e1fd35e

Please sign in to comment.