This repository has been archived on 2023-05-29. You can view files and clone it, but cannot push or open issues or pull requests.
fp-ts-training/src/exo2/exo2.test.ts

231 lines
6.6 KiB
TypeScript
Raw Normal View History

import { either, option } from 'fp-ts';
2020-07-06 16:18:55 +03:00
import {
Warrior,
Wizard,
Archer,
Damage,
noTargetFailure,
invalidTargetFailure,
checkTargetAndSmash,
checkTargetAndBurn,
checkTargetAndShoot,
smashOption,
burnOption,
shootOption,
attack,
} from './exo2';
describe('exo2', () => {
describe('checkTargetAndSmash', () => {
it('should return a NoTarget error if no unit is selected', () => {
const result = checkTargetAndSmash(option.none);
const expected = either.left(
2020-07-06 16:18:55 +03:00
noTargetFailure('No unit currently selected'),
);
expect(result).toStrictEqual(expected);
});
it('should return an InvalidTarget error if the wrong unit is selected', () => {
const archer = new Archer();
const resultArcher = checkTargetAndSmash(option.some(archer));
const expectedArcher = either.left(
2020-07-06 16:18:55 +03:00
invalidTargetFailure('Archer cannot perform smash'),
);
const wizard = new Wizard();
const resultWizard = checkTargetAndSmash(option.some(wizard));
const expectedWizard = either.left(
2020-07-06 16:18:55 +03:00
invalidTargetFailure('Wizard cannot perform smash'),
);
expect(resultArcher).toStrictEqual(expectedArcher);
expect(resultWizard).toStrictEqual(expectedWizard);
});
it('should return the proper type of damage', () => {
const warrior = new Warrior();
const result = checkTargetAndSmash(option.some(warrior));
const expected = either.right(Damage.Physical);
2020-07-06 16:18:55 +03:00
expect(result).toStrictEqual(expected);
});
});
describe('checkTargetAndBurn', () => {
it('should return a NoTarget error if no unit is selected', () => {
const result = checkTargetAndBurn(option.none);
const expected = either.left(
2020-07-06 16:18:55 +03:00
noTargetFailure('No unit currently selected'),
);
expect(result).toStrictEqual(expected);
});
it('should return an InvalidTarget error if the wrong unit is selected', () => {
const warrior = new Warrior();
const resultWarrior = checkTargetAndBurn(option.some(warrior));
const expectedWarrior = either.left(
2020-07-06 16:18:55 +03:00
invalidTargetFailure('Warrior cannot perform burn'),
);
const archer = new Archer();
const resultArcher = checkTargetAndBurn(option.some(archer));
const expectedArcher = either.left(
2020-07-06 16:18:55 +03:00
invalidTargetFailure('Archer cannot perform burn'),
);
expect(resultWarrior).toStrictEqual(expectedWarrior);
expect(resultArcher).toStrictEqual(expectedArcher);
});
it('should return the proper type of damage', () => {
const wizard = new Wizard();
const result = checkTargetAndBurn(option.some(wizard));
const expected = either.right(Damage.Magical);
2020-07-06 16:18:55 +03:00
expect(result).toStrictEqual(expected);
});
});
describe('checkTargetAndShoot', () => {
it('should return a NoTarget error if no unit is selected', () => {
const result = checkTargetAndShoot(option.none);
const expected = either.left(
2020-07-06 16:18:55 +03:00
noTargetFailure('No unit currently selected'),
);
expect(result).toStrictEqual(expected);
});
it('should return an InvalidTarget error if the wrong unit is selected', () => {
const warrior = new Warrior();
const resultWarrior = checkTargetAndShoot(option.some(warrior));
const expectedWarrior = either.left(
2020-07-06 16:18:55 +03:00
invalidTargetFailure('Warrior cannot perform shoot'),
);
const wizard = new Wizard();
const resultWizard = checkTargetAndShoot(option.some(wizard));
const expectedWizard = either.left(
2020-07-06 16:18:55 +03:00
invalidTargetFailure('Wizard cannot perform shoot'),
);
expect(resultWarrior).toStrictEqual(expectedWarrior);
expect(resultWizard).toStrictEqual(expectedWizard);
});
it('should return the proper type of damage', () => {
const archer = new Archer();
const result = checkTargetAndShoot(option.some(archer));
const expected = either.right(Damage.Ranged);
2020-07-06 16:18:55 +03:00
expect(result).toStrictEqual(expected);
});
});
describe('smashOption', () => {
it('should return option.none if the character is of the wrong type', () => {
2020-07-06 16:18:55 +03:00
const wizard = new Wizard();
const archer = new Archer();
const resultWizard = smashOption(wizard);
const resultArcher = smashOption(archer);
const expected = option.none;
2020-07-06 16:18:55 +03:00
expect(resultWizard).toStrictEqual(expected);
expect(resultArcher).toStrictEqual(expected);
});
it('should return option.some(Damage.Physical) if the character is a warrior', () => {
2020-07-06 16:18:55 +03:00
const warrior = new Warrior();
const result = smashOption(warrior);
const expected = option.some(Damage.Physical);
2020-07-06 16:18:55 +03:00
expect(result).toStrictEqual(expected);
});
});
describe('burnOption', () => {
it('should return option.none if the character is of the wrong type', () => {
2020-07-06 16:18:55 +03:00
const warrior = new Warrior();
const archer = new Archer();
const resultWarrior = burnOption(warrior);
const resultArcher = burnOption(archer);
const expected = option.none;
2020-07-06 16:18:55 +03:00
expect(resultWarrior).toStrictEqual(expected);
expect(resultArcher).toStrictEqual(expected);
});
it('should return option.some(Damage.Magical) if the character is a wizard', () => {
2020-07-06 16:18:55 +03:00
const wizard = new Wizard();
const result = burnOption(wizard);
const expected = option.some(Damage.Magical);
2020-07-06 16:18:55 +03:00
expect(result).toStrictEqual(expected);
});
});
describe('shootOption', () => {
it('should return option.none if the character is of the wrong type', () => {
2020-07-06 16:18:55 +03:00
const warrior = new Warrior();
const wizard = new Wizard();
const resultWizard = shootOption(wizard);
const resultWarrior = shootOption(warrior);
const expected = option.none;
2020-07-06 16:18:55 +03:00
expect(resultWarrior).toStrictEqual(expected);
expect(resultWizard).toStrictEqual(expected);
});
it('should return option.some(Damage.Ranged) if the character is an archer', () => {
2020-07-06 16:18:55 +03:00
const archer = new Archer();
const result = shootOption(archer);
const expected = option.some(Damage.Ranged);
2020-07-06 16:18:55 +03:00
expect(result).toStrictEqual(expected);
});
});
describe('attack', () => {
it('should return the correct number of each type of attacks', () => {
const warrior = new Warrior();
const wizard = new Wizard();
const archer = new Archer();
const army = [
warrior,
wizard,
archer,
wizard,
wizard,
archer,
warrior,
wizard,
archer,
];
const result = attack(army);
const expected = {
[Damage.Physical]: 2,
[Damage.Magical]: 4,
[Damage.Ranged]: 3,
};
expect(result).toStrictEqual(expected);
});
});
});