Skip to content

Commit

Permalink
Merge pull request serenity-bdd#1389 from miles-mason/master
Browse files Browse the repository at this point in the history
fix: Treat subclasses of an Ability as the superclass Ability
  • Loading branch information
wakaleo authored Oct 3, 2018
2 parents 7bab324 + 5d8cc20 commit f92fff9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,20 @@ public <T extends Ability> Actor whoCan(T doSomething) {

@SuppressWarnings("unchecked")
public <T extends Ability> T abilityTo(Class<? extends T> doSomething) {
return (T) abilities.get(doSomething);
T ability = (T) abilities.get(doSomething);

if (ability == null) {
// See if any ability is a subclass of doSomething
for (Map.Entry<Class, Ability> entry: abilities.entrySet()) {
// Return the first subclass we find
if (doSomething.isAssignableFrom(entry.getKey())) {
ability = (T) entry.getValue();
break;
}
}
}

return ability;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class WhenActorsHaveAbilities extends Specification{
}
}

class ReachEnlightenment extends Meditate {}

def "actors can be associated with Abilities"() {
given:
def actor = Actor.named("Bruce")
Expand All @@ -38,4 +40,15 @@ class WhenActorsHaveAbilities extends Specification{

}

def "actors associate extensions of Abilities with the superclass too"() {
given:
def actor = Actor.named("Bruce")
def reachEnlightenment = new ReachEnlightenment()
when:
actor.can(reachEnlightenment)
then:
actor.abilityTo(Meditate) instanceof ReachEnlightenment

}

}

0 comments on commit f92fff9

Please sign in to comment.