Expect class for asserting thrown exception. Updated bdd classes.

This commit is contained in:
magenoxx 2010-12-20 16:38:20 +03:00
parent 2e27a676ed
commit 4d8d92ed7d
11 changed files with 141 additions and 49 deletions

View file

@ -19,17 +19,4 @@ public class LandTest extends MageAPI {
Then.battlefield.has("Mountain"); Then.battlefield.has("Mountain");
And.graveyards.empty(); And.graveyards.empty();
} }
/*@Test
public void testLightningHelix() throws Exception {
Given.I.have.a.card("Lightning Helix");
And.battlefield.has("Mountain","Plains");
And.phase.is("End of Turn", ai);
And.lifes(20,20);
When.I.play("Lightning Helix");
Then.my.life(23);
And.ai.life(17);
And.my.graveyard.has("Lightning Helix");
And.ai.graveyard.empty();
}*/
} }

View file

@ -0,0 +1,10 @@
package org.mage.test.base;
/**
* Command pattern.
*
* @author nantuko
*/
abstract public class Command {
abstract public void execute() throws Exception;
}

View file

@ -22,35 +22,6 @@ public class MageAPI {
MageBase.getInstance().start(); MageBase.getInstance().start();
} }
public void giveme(String card) throws Exception {
MageBase.getInstance().giveme(card);
}
public boolean checkIhave(String card) throws Exception {
return MageBase.getInstance().checkIhave(card);
}
public void goToPhase(String phase, Owner owner) {
if ("Precombat Main".equals(phase) && (owner.equals(Owner.mine) || owner.equals(Owner.me))) {
MageBase.getInstance().goToPhase("Precombat Main - play spells and sorceries.");
return;
}
System.err.println("waitForPhase not implemented for phase="+phase+", owner="+owner.name());
throw new NotImplementedException();
}
public void playCard(String cardName) throws Exception {
MageBase.getInstance().playCard(cardName);
}
public boolean checkBattlefield(String cardName) throws Exception {
return MageBase.getInstance().checkBattlefield(cardName);
}
public boolean checkGraveyardsEmpty() throws Exception {
return MageBase.getInstance().checkGraveyardsEmpty();
}
/** /**
* Defined step depending on input parameter. * Defined step depending on input parameter.
* If step is UNKNOWN, then use previous remember step, otherwise remember it as current. * If step is UNKNOWN, then use previous remember step, otherwise remember it as current.

View file

@ -0,0 +1,38 @@
package org.mage.test.base;
import mage.Constants;
import org.junit.BeforeClass;
import org.mage.test.bdd.StepController;
import org.mage.test.bdd.StepState;
import org.mage.test.bdd.and.And;
import org.mage.test.bdd.given.Given;
import org.mage.test.bdd.then.Then;
import org.mage.test.bdd.when.When;
import static org.mage.test.base.MageAPI.Owner.mine;
/**
* Contains wrappers for bdd calls.
*/
public class MageAPIExtended extends MageAPI {
public void addCard(String cardName, Constants.Zone zone) throws Exception {
Given.I.have.a.card("Mountain");
}
public void setPhase(String phase, Owner owner) throws Exception {
And.phase.is("Precombat Main", mine);
}
public void play(String cardName) throws Exception {
When.I.play("Mountain");
}
public void assertBattlefield(String cardName) throws Exception {
Then.battlefield.has("Mountain");
}
public void assertGraveyardsCount(int count) throws Exception {
And.graveyards.empty();
}
}

View file

@ -0,0 +1,12 @@
package org.mage.test.base.exception;
/**
* Thrown when server couldn't create card with given name.
*
* @author nantuko
*/
public class CardNotFoundException extends RuntimeException {
public CardNotFoundException(String s) {
super(s);
}
}

View file

@ -0,0 +1,32 @@
package org.mage.test.bdd;
import org.junit.Test;
import org.mage.test.base.Command;
import org.mage.test.base.MageAPI;
import org.mage.test.base.exception.CardNotFoundException;
import org.mage.test.bdd.and.And;
import org.mage.test.bdd.given.Given;
import org.mage.test.bdd.then.Then;
import org.mage.test.bdd.when.When;
import static org.mage.test.base.MageAPI.Owner.mine;
/**
* Tests BDD classes.
*/
public class BDDTests extends MageAPI {
@Test
public void testNonExistingCard() throws Exception {
Expect.expect(CardNotFoundException.class, new Command() {
@Override
public void execute() throws Exception {
Given.I.have.a.card("Super Puper Card");
And.phase.is("Precombat Main", mine);
When.I.play("Super Puper Card");
Then.battlefield.has("Mountain");
And.graveyards.empty();
}
});
}
}

View file

@ -0,0 +1,23 @@
package org.mage.test.bdd;
import org.junit.Assert;
import org.mage.test.base.Command;
import static org.hamcrest.CoreMatchers.is;
/**
* Asserts expecting exception.
*
* @author nantuko
*/
public class Expect {
public static void expect(Class<? extends RuntimeException> t, Command command) {
try {
command.execute();
} catch (Throwable e) {
Assert.assertThat(t.getName(), is(e.getClass().getName()));
return;
}
throw new AssertionError("Expected exception wasn't thrown: " + t.getName());
}
}

View file

@ -5,6 +5,6 @@ import org.mage.test.bdd.given.I;
public class And { public class And {
public static Phase phase = new Phase(StepState.UNKNOWN); public static Phase phase = new Phase(StepState.UNKNOWN);
public static Graveyards graveyards = new Graveyards(); public static Graveyards graveyards = new Graveyards(StepState.UNKNOWN);
public static I I = new I(StepState.UNKNOWN); public static I I = new I(StepState.UNKNOWN);
} }

View file

@ -1,11 +1,25 @@
package org.mage.test.bdd.and; package org.mage.test.bdd.and;
import org.junit.Assert;
import org.mage.test.base.MageAPI;
import org.mage.test.base.MageBase; import org.mage.test.base.MageBase;
import org.mage.test.bdd.StepState;
import static org.hamcrest.core.Is.is;
public class Graveyards { public class Graveyards {
public static boolean empty() throws Exception { private StepState step;
boolean empty = MageBase.getInstance().checkGraveyardsEmpty(); public Graveyards(StepState step) {
System.out.println("empty: " + empty); this.step = step;
return empty; }
public boolean empty() throws Exception {
StepState current = MageAPI.defineStep(this.step);
if (current.equals(StepState.THEN)) {
boolean empty = MageBase.getInstance().checkGraveyardsEmpty();
Assert.assertThat(empty, is(true));
return empty;
} else {
throw new AssertionError("Graveyards are not empty.");
}
} }
} }

View file

@ -2,6 +2,7 @@ package org.mage.test.bdd.given;
import org.mage.test.base.MageAPI; import org.mage.test.base.MageAPI;
import org.mage.test.base.MageBase; import org.mage.test.base.MageBase;
import org.mage.test.base.exception.CardNotFoundException;
import org.mage.test.bdd.StepController; import org.mage.test.bdd.StepController;
import org.mage.test.bdd.StepState; import org.mage.test.bdd.StepState;
@ -14,11 +15,11 @@ public class A {
StepState current = MageAPI.defineStep(this.step); StepState current = MageAPI.defineStep(this.step);
if (current.equals(StepState.GIVEN)) { if (current.equals(StepState.GIVEN)) {
if (!MageBase.getInstance().giveme(cardName)) { if (!MageBase.getInstance().giveme(cardName)) {
throw new IllegalStateException("Couldn't create card: " + cardName); throw new CardNotFoundException("Couldn't create card: " + cardName);
} }
} else if (current.equals(StepState.THEN)) { } else if (current.equals(StepState.THEN)) {
if (!MageBase.getInstance().checkIhave(cardName)) { if (!MageBase.getInstance().checkIhave(cardName)) {
throw new IllegalStateException("Couldn't find requested card in hand: " + cardName); throw new CardNotFoundException("Couldn't find requested card in hand: " + cardName);
} }
} }
} }

View file

@ -1,5 +1,9 @@
package org.mage.test.bdd.then; package org.mage.test.bdd.then;
import org.mage.test.bdd.StepState;
import org.mage.test.bdd.and.Graveyards;
public class Then { public class Then {
public static Battlefield battlefield = new Battlefield(); public static Battlefield battlefield = new Battlefield();
public static Graveyards graveyards = new Graveyards(StepState.THEN);
} }