2020-07-03 19:01:45 +03:00
|
|
|
import * as Either from 'fp-ts/lib/Either';
|
|
|
|
import * as Option from 'fp-ts/lib/Option';
|
|
|
|
|
|
|
|
import {
|
|
|
|
divide,
|
|
|
|
DivisionByZero,
|
|
|
|
safeDivide,
|
|
|
|
safeDivideWithError,
|
|
|
|
asyncDivide,
|
|
|
|
asyncSafeDivideWithError,
|
|
|
|
} from './exo1';
|
|
|
|
|
|
|
|
describe('exo1', () => {
|
|
|
|
describe('divide', () => {
|
|
|
|
it('should return the result of dividing two numbers', () => {
|
|
|
|
expect(divide(25, 5)).toEqual(5);
|
|
|
|
});
|
|
|
|
|
2020-12-08 18:52:02 +03:00
|
|
|
it('should return Infinity or -Infinity if the denominator is zero', () => {
|
2020-07-03 19:01:45 +03:00
|
|
|
expect(divide(25, 0)).toBe(Infinity);
|
2020-12-08 18:52:02 +03:00
|
|
|
expect(divide(-25, 0)).toBe(-Infinity);
|
2020-07-03 19:01:45 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('safeDivide', () => {
|
|
|
|
it('should return the result of dividing two numbers', () => {
|
|
|
|
expect(safeDivide(25, 5)).toStrictEqual(Option.some(5));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return Option.none if the denominator is zero', () => {
|
|
|
|
expect(safeDivide(25, 0)).toStrictEqual(Option.none);
|
2020-12-08 18:52:02 +03:00
|
|
|
expect(safeDivide(-25, 0)).toStrictEqual(Option.none);
|
2020-07-03 19:01:45 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('safeDivideWithError', () => {
|
|
|
|
it('should return the result of dividing two numbers', () => {
|
|
|
|
expect(safeDivideWithError(25, 5)).toStrictEqual(Either.right(5));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return Either.left(DivisionByZero) if the denominator is zero', () => {
|
|
|
|
expect(safeDivideWithError(25, 0)).toStrictEqual(
|
|
|
|
Either.left(DivisionByZero),
|
|
|
|
);
|
2020-12-08 18:52:02 +03:00
|
|
|
expect(safeDivideWithError(-25, 0)).toStrictEqual(
|
|
|
|
Either.left(DivisionByZero),
|
|
|
|
);
|
2020-07-03 19:01:45 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('asyncDivide', () => {
|
|
|
|
it('should eventually return the result of dividing two numbers', async () => {
|
|
|
|
const result = await asyncDivide(25, 5);
|
|
|
|
|
|
|
|
expect(result).toEqual(5);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should eventually return Infinity if the denominator is zero', async () => {
|
|
|
|
await expect(asyncDivide(25, 0)).rejects.toThrow();
|
2020-12-08 18:52:02 +03:00
|
|
|
await expect(asyncDivide(-25, 0)).rejects.toThrow();
|
2020-07-03 19:01:45 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('asyncSafeDivideWithError', () => {
|
|
|
|
it('should eventually return the result of dividing two numbers', async () => {
|
|
|
|
const result = await asyncSafeDivideWithError(25, 5)();
|
|
|
|
|
|
|
|
expect(result).toStrictEqual(Either.right(5));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should eventually return Either.left(DivisionByZero) if the denominator is zero', async () => {
|
2020-12-08 18:52:02 +03:00
|
|
|
const resultA = await asyncSafeDivideWithError(25, 0)();
|
|
|
|
const resultB = await asyncSafeDivideWithError(-25, 0)();
|
2020-07-03 19:01:45 +03:00
|
|
|
|
2020-12-08 18:52:02 +03:00
|
|
|
expect(resultA).toStrictEqual(Either.left(DivisionByZero));
|
|
|
|
expect(resultB).toStrictEqual(Either.left(DivisionByZero));
|
2020-07-03 19:01:45 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|