Skip to content
Permalink
Browse files
fix(core): an Actor will now complain if given the same ability more …
…than once
  • Loading branch information
jan-molak committed Apr 28, 2020
1 parent e938d21 commit d34f4d58ed44c764fd9b516a007330c01cb7cbf5
Showing 2 changed files with 14 additions and 1 deletion.
@@ -107,6 +107,14 @@ describe('Actor', () => {
PlayAChord.of(Chords.AMajor),
)).to.be.eventually.rejectedWith(ConfigurationError, `Ben can't PlayAGuitar yet. Did you give them the ability to do so?`));

/** @test {Actor} */
it('complains if given the same ability twice', () => {

expect(() =>
actor('Ben').whoCan(PlayAGuitar.suchAs(guitar), PlayAGuitar.suchAs(guitar))
).to.throw(ConfigurationError, `Ben already has an ability to PlayAGuitar, so you don't need to give it to them again.`);
});

/** @test {Actor} */
it('can be instantiated without explicitly specifying the Stage', () => {

@@ -1,5 +1,5 @@
import { ActivityRelatedArtifactGenerated } from '../../events';
import { Ability, AbilityType, Answerable, Cast, ConfigurationError, serenity } from '../../index';
import { Ability, AbilityType, Answerable, Cast, ConfigurationError, LogicError, serenity } from '../../index';
import { Artifact, Name } from '../../model';
import { Stage } from '../../stage';
import { TrackedActivity } from '../activities';
@@ -61,6 +61,11 @@ export class Actor implements PerformsActivities, UsesAbilities, CanHaveAbilitie

whoCan(...abilities: Ability[]): Actor {
abilities.forEach(ability => {
const abilityType = ability.constructor as AbilityType<Ability>;
if (this.abilities.has(abilityType)) {
throw new ConfigurationError(`${ this.name } already has an ability to ${ abilityType.name }, so you don't need to give it to them again.`);
}

this.abilities.set(ability.constructor as AbilityType<Ability>, ability);
});

0 comments on commit d34f4d5

Please sign in to comment.