Skip to content

Commit

Permalink
Doing tests little by little
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneWWolf committed Jan 8, 2024
1 parent c4d769c commit 0ee8d8e
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 9 deletions.
2 changes: 1 addition & 1 deletion rpgsaga/saga/src/characterGenerators/archerGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CharacterClass } from '../characterClasses';
import { Character } from '../character';
import { randomIntFromInterval } from '../randomMath';
import { FireArrow } from '../spell_system/spell/fireArrow';
import { FireArrow } from '../spell_system/activeEffects/fireArrow';
import { Spell } from '../spell_system/spell/spell';
import { FireArrowEffect } from '../spell_system/statusEffect/fireArrowEffect';

Expand Down
2 changes: 1 addition & 1 deletion rpgsaga/saga/src/characterGenerators/knightGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CharacterClass } from '../characterClasses';
import { Character } from '../character';
import { randomIntFromInterval } from '../randomMath';
import { KnightAttack } from '../spell_system/spell/knightAttack';
import { KnightAttack } from '../spell_system/activeEffects/knightAttack';
import { Spell } from '../spell_system/spell/spell';

import { CharacterGeneratorHelper } from './characterGeneratorHelper';
Expand Down
2 changes: 1 addition & 1 deletion rpgsaga/saga/src/characterGenerators/mageGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CharacterClass } from '../characterClasses';
import { Character } from '../character';
import { randomIntFromInterval } from '../randomMath';
import { Stun } from '../spell_system/spell/stun';
import { Stun } from '../spell_system/activeEffects/stun';
import { Spell } from '../spell_system/spell/spell';
import { StunEffect } from '../spell_system/statusEffect/stunEffect';

Expand Down
6 changes: 6 additions & 0 deletions rpgsaga/saga/src/doublyLinkedList/doublyLinkedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export class DoublyLinkedList {
return this._tail;
}

get size() {
return this._size;
}

constructor() {
this._head = null;
this._tail = null;
Expand Down Expand Up @@ -68,6 +72,8 @@ export class DoublyLinkedList {

node.next = tmp;

this._tail = node.next;

tmp.prev = node;

this._size += 1;
Expand Down
4 changes: 2 additions & 2 deletions rpgsaga/saga/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export class Message {
attackerInfo: string,
target: Character,
targetInfo: string,
damagePoints?: number,
spell?: ISpell,
damagePoints: number,
spell: ISpell,
) {
this._attacker = attacker;
this._attackerInfo = attackerInfo;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Character } from '../../character';

import { IActiveEffect } from './IActiveEffect';
import { CastHelper } from './castHelper';
import { IActiveEffect } from './IActiveEffect';

export class FireArrow implements IActiveEffect {
private _castsRemaining: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Character } from '../../character';

import { IActiveEffect } from './IActiveEffect';
import { CastHelper } from './castHelper';
import { IActiveEffect } from './IActiveEffect';

export class KnightAttack implements IActiveEffect {
private _castsRemaining: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Character } from '../../character';

import { IActiveEffect } from './IActiveEffect';
import { CastHelper } from './castHelper';
import { IActiveEffect } from './IActiveEffect';

export class Stun implements IActiveEffect {
private _castsRemaining: number;
Expand Down
6 changes: 5 additions & 1 deletion rpgsaga/saga/src/spell_system/spell/spell.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Character } from '../../character';
import { IStatusEffect } from '../statusEffect/IStatusEffect';
import { IActiveEffect } from '../activeEffects/IActiveEffect';

import { IActiveEffect } from './IActiveEffect';
import { ISpell } from './ISpell';

export class Spell implements ISpell {
Expand All @@ -21,6 +21,10 @@ export class Spell implements ISpell {
return this._statusEffect;
}

get activeEffect() {
return this._activeEffect;
}

constructor(name: string, cast: IActiveEffect, statusEffect?: IStatusEffect) {
this._name = name;
this._activeEffect = cast;
Expand Down
55 changes: 55 additions & 0 deletions rpgsaga/saga/tests/character.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Character } from '../src/character';
import { CharacterClass } from '../src/characterClasses';
import { Spell } from '../src/spell_system/spell/spell';
import { Stun } from '../src/spell_system/activeEffects/stun';
import { StunEffect } from '../src/spell_system/statusEffect/stunEffect';

describe('Testing Character class', () => {
it('Constructor tests', () => {
const character = new Character(
'Linda',
CharacterClass.mage,
105,
new Spell('Freeze', new Stun(2, 0), new StunEffect('Stun', 1)),
);

expect(character.name).toBe('Linda');
expect(character.class).toBe(CharacterClass.mage);
expect(character.healthPoints).toBe(105);
expect(character.spell).toBeInstanceOf(Spell);
});
it('Testing receiveDamage method', () => {
const character = new Character(
'Linda',
CharacterClass.mage,
105,
new Spell('Freeze', new Stun(2, 0), new StunEffect('Stun', 1)),
);

character.receiveDamage(5);

expect(character.healthPoints).toBe(100);
});
it('Testing getAttackPoints method', () => {
const character = new Character(
'Linda',
CharacterClass.mage,
105,
new Spell('Freeze', new Stun(2, 0), new StunEffect('Stun', 1)),
);

character.strengthModificator = 5.0;

expect(character.getAttackPoints()).toBe(25);
});
it('Testing toString method', () => {
const character = new Character(
'Linda',
CharacterClass.mage,
105,
new Spell('Freeze', new Stun(2, 0), new StunEffect('Stun', 1)),
);

expect(character.toString()).toBe('Linda (mage) [105/105]');
});
});
87 changes: 87 additions & 0 deletions rpgsaga/saga/tests/doublyLinkedList.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { DoublyLinkedList } from '../src/doublyLinkedList/doublyLinkedList';
import { FireArrowEffect } from '../src/spell_system/statusEffect/fireArrowEffect';
import { StunEffect } from '../src/spell_system/statusEffect/stunEffect';
import { IStatusEffect } from '../src/spell_system/statusEffect/IStatusEffect';

describe('Testing DoublyLinkedList constructor', () => {
it('Should initialise properly', () => {
const linkedList = new DoublyLinkedList();
expect(linkedList.head).toBe(null);
expect(linkedList.tail).toBe(null);
expect(linkedList.size).toBe(0);
});
});

describe('Testing addLast method', () => {
it('Should add an element into the empty doubly linked list', () => {
const linkedList = new DoublyLinkedList();
linkedList.addLast(new FireArrowEffect('Burning', 3));

expect(linkedList.head.value).toBeInstanceOf(FireArrowEffect);
expect(linkedList.tail).toBe(null);
expect(linkedList.size).toBe(1);
});
it('Should add an element into the non-empty doubly linked list', () => {
const linkedList = new DoublyLinkedList();
linkedList.addLast(new FireArrowEffect('Burning', 3));
linkedList.addLast(new FireArrowEffect('Blue Fire', 1));
linkedList.addLast(new StunEffect('Stun', 1));

expect(linkedList.head.value).toBeInstanceOf(FireArrowEffect);
expect(linkedList.head.next.value).toBeInstanceOf(FireArrowEffect);
expect(linkedList.head.next.next.value).toBeInstanceOf(StunEffect);
expect(linkedList.tail.value).toBeInstanceOf(StunEffect);
expect(linkedList.size).toBe(3);
});
});

describe('Testing contains method', () => {
it('Should result in true if element is in the linked list', () => {
const linkedList = new DoublyLinkedList();
const statusEffect: IStatusEffect = new FireArrowEffect('Burning', 3);

linkedList.addLast(statusEffect);

expect(linkedList.contains(statusEffect)).toBe(true);
});
it('Should result in false if linked list is empty', () => {
const linkedList = new DoublyLinkedList();
const statusEffect: IStatusEffect = new FireArrowEffect('Burning', 3);

expect(linkedList.contains(statusEffect)).toBe(false);
});
it("Should result in false if element isn't in the linked list", () => {
const linkedList = new DoublyLinkedList();
const statusEffect: IStatusEffect = new FireArrowEffect('Burning', 3);
const anotherStatusEffect: IStatusEffect = new FireArrowEffect('Blue Fire', 1);

linkedList.addLast(statusEffect);

expect(linkedList.contains(anotherStatusEffect)).toBe(false);
});
});

describe('Testing remove method', () => {
it('Should work without errors if linked list is empty', () => {
const linkedList = new DoublyLinkedList();
const statusEffect: IStatusEffect = new FireArrowEffect('Blue Fire', 1);

expect(() => {
linkedList.remove(statusEffect);
}).not.toThrow();
});

it('Should remove an element from non-empty linked list', () => {
const linkedList = new DoublyLinkedList();
const statusEffect: IStatusEffect = new FireArrowEffect('Blue Fire', 1);
const anotherStatusEffect: IStatusEffect = new FireArrowEffect('Burning', 3);

linkedList.addLast(statusEffect);
linkedList.addLast(anotherStatusEffect);

linkedList.remove(statusEffect);

expect(linkedList.size).toBe(1);
expect(linkedList.head.value).toBe(anotherStatusEffect);
});
});
32 changes: 32 additions & 0 deletions rpgsaga/saga/tests/message.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Character } from '../src/character';
import { CharacterClass } from '../src/characterClasses';
import { Message } from '../src/message';
import { KnightAttack } from '../src/spell_system/activeEffects/knightAttack';
import { Spell } from '../src/spell_system/spell/spell';

describe('Testing Message class', () => {
it('Testing Message constructor', () => {
const dummyCharacter = new Character(
'Korsika',
CharacterClass.knight,
150,
new Spell('Mighty Slash', new KnightAttack(2, 5), null),
);

const message = new Message(
dummyCharacter,
dummyCharacter.toString(),
dummyCharacter,
dummyCharacter.toString(),
dummyCharacter.getAttackPoints(),
dummyCharacter.spell,
);

expect(message.attacker).toBe(dummyCharacter);
expect(message.attackerInfo).toBe(dummyCharacter.toString());
expect(message.target).toBe(dummyCharacter);
expect(message.targetInfo).toBe(dummyCharacter.toString());
expect(message.damagePoints).toBe(dummyCharacter.getAttackPoints());
expect(message.spell).toBe(dummyCharacter.spell);
});
});
46 changes: 46 additions & 0 deletions rpgsaga/saga/tests/testSpellSystem/spell.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Character } from '../../src/character';
import { CharacterClass } from '../../src/characterClasses';
import { FireArrow } from '../../src/spell_system/activeEffects/fireArrow';
import { Spell } from '../../src/spell_system/spell/spell';
import { FireArrowEffect } from '../../src/spell_system/statusEffect/fireArrowEffect';

describe('Testing Spell class constructor', () => {
it('Should result in correct values', () => {
const spell = new Spell('Fire arrow', new FireArrow(2, 7), new FireArrowEffect('Burning', 3));

expect(spell.name).toBe('Fire arrow');
expect(spell.activeEffect).toBeInstanceOf(FireArrow);
expect(spell.statusEffect).toBeInstanceOf(FireArrowEffect);
});
it("Status effect in constructor should result to null if it's not passed as a parameter", () => {
const spell = new Spell('Fire arrow', new FireArrow(2, 7));

expect(spell.name).toBe('Fire arrow');
expect(spell.activeEffect).toBeInstanceOf(FireArrow);
expect(spell.statusEffect).toBeFalsy();
});
});

describe('Testing canExecute method', () => {
it('Should result in true if spell can be casted', () => {
const spell = new Spell('Fire arrow', new FireArrow(2, 7), new FireArrowEffect('Burning', 3));
expect(spell.canExecute()).toBe(true);
});
it('Should result in false if spell cannot be casted', () => {
const spell = new Spell('Fire arrow', new FireArrow(0, 7), new FireArrowEffect('Burning', 3));
expect(spell.canExecute()).toBe(false);
});
});

describe('Testing execute method', () => {
it('Should result in true if spell casted successfully', () => {
const spell = new Spell('Fire arrow', new FireArrow(1, 7), new FireArrowEffect('Burning', 3));
const dummyTarget = new Character('Lilian', CharacterClass.mage, 100, spell);
expect(spell.execute(dummyTarget)).toBe(true);
});
it('Should result in true if spell has not been casted', () => {
const spell = new Spell('Fire arrow', new FireArrow(0, 7), new FireArrowEffect('Burning', 3));
const dummyTarget = new Character('Lilian', CharacterClass.mage, 100, spell);
expect(spell.execute(dummyTarget)).toBe(false);
});
});

0 comments on commit 0ee8d8e

Please sign in to comment.