From 4d8d92ed7df8cbf3639ed5c40d75b7a9ceb44158 Mon Sep 17 00:00:00 2001 From: magenoxx Date: Mon, 20 Dec 2010 16:38:20 +0300 Subject: [PATCH] Expect class for asserting thrown exception. Updated bdd classes. --- .../src/test/java/org/mage/test/LandTest.java | 13 ------- .../test/java/org/mage/test/base/Command.java | 10 +++++ .../test/java/org/mage/test/base/MageAPI.java | 29 -------------- .../org/mage/test/base/MageAPIExtended.java | 38 +++++++++++++++++++ .../base/exception/CardNotFoundException.java | 12 ++++++ .../test/java/org/mage/test/bdd/BDDTests.java | 32 ++++++++++++++++ .../test/java/org/mage/test/bdd/Expect.java | 23 +++++++++++ .../test/java/org/mage/test/bdd/and/And.java | 2 +- .../org/mage/test/bdd/and/Graveyards.java | 22 +++++++++-- .../test/java/org/mage/test/bdd/given/A.java | 5 ++- .../java/org/mage/test/bdd/then/Then.java | 4 ++ 11 files changed, 141 insertions(+), 49 deletions(-) create mode 100644 Mage.Tests/src/test/java/org/mage/test/base/Command.java create mode 100644 Mage.Tests/src/test/java/org/mage/test/base/MageAPIExtended.java create mode 100644 Mage.Tests/src/test/java/org/mage/test/base/exception/CardNotFoundException.java create mode 100644 Mage.Tests/src/test/java/org/mage/test/bdd/BDDTests.java create mode 100644 Mage.Tests/src/test/java/org/mage/test/bdd/Expect.java diff --git a/Mage.Tests/src/test/java/org/mage/test/LandTest.java b/Mage.Tests/src/test/java/org/mage/test/LandTest.java index 169afa0cc1..a2235509d0 100644 --- a/Mage.Tests/src/test/java/org/mage/test/LandTest.java +++ b/Mage.Tests/src/test/java/org/mage/test/LandTest.java @@ -19,17 +19,4 @@ public class LandTest extends MageAPI { Then.battlefield.has("Mountain"); 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(); - }*/ } diff --git a/Mage.Tests/src/test/java/org/mage/test/base/Command.java b/Mage.Tests/src/test/java/org/mage/test/base/Command.java new file mode 100644 index 0000000000..09deeb3b4b --- /dev/null +++ b/Mage.Tests/src/test/java/org/mage/test/base/Command.java @@ -0,0 +1,10 @@ +package org.mage.test.base; + +/** + * Command pattern. + * + * @author nantuko + */ +abstract public class Command { + abstract public void execute() throws Exception; +} diff --git a/Mage.Tests/src/test/java/org/mage/test/base/MageAPI.java b/Mage.Tests/src/test/java/org/mage/test/base/MageAPI.java index cbb0fcc2e7..ca3251e95f 100644 --- a/Mage.Tests/src/test/java/org/mage/test/base/MageAPI.java +++ b/Mage.Tests/src/test/java/org/mage/test/base/MageAPI.java @@ -22,35 +22,6 @@ public class MageAPI { 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. * If step is UNKNOWN, then use previous remember step, otherwise remember it as current. diff --git a/Mage.Tests/src/test/java/org/mage/test/base/MageAPIExtended.java b/Mage.Tests/src/test/java/org/mage/test/base/MageAPIExtended.java new file mode 100644 index 0000000000..52c4f5cadc --- /dev/null +++ b/Mage.Tests/src/test/java/org/mage/test/base/MageAPIExtended.java @@ -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(); + } +} diff --git a/Mage.Tests/src/test/java/org/mage/test/base/exception/CardNotFoundException.java b/Mage.Tests/src/test/java/org/mage/test/base/exception/CardNotFoundException.java new file mode 100644 index 0000000000..a911c36544 --- /dev/null +++ b/Mage.Tests/src/test/java/org/mage/test/base/exception/CardNotFoundException.java @@ -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); + } +} diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/BDDTests.java b/Mage.Tests/src/test/java/org/mage/test/bdd/BDDTests.java new file mode 100644 index 0000000000..f5a2d3d8a4 --- /dev/null +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/BDDTests.java @@ -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(); + } + }); + } +} diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/Expect.java b/Mage.Tests/src/test/java/org/mage/test/bdd/Expect.java new file mode 100644 index 0000000000..cf794b9124 --- /dev/null +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/Expect.java @@ -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 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()); + } +} diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/and/And.java b/Mage.Tests/src/test/java/org/mage/test/bdd/and/And.java index 4deb044354..21e57e3bff 100644 --- a/Mage.Tests/src/test/java/org/mage/test/bdd/and/And.java +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/and/And.java @@ -5,6 +5,6 @@ import org.mage.test.bdd.given.I; public class And { 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); } diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/and/Graveyards.java b/Mage.Tests/src/test/java/org/mage/test/bdd/and/Graveyards.java index 2411d87f7b..6942f1167c 100644 --- a/Mage.Tests/src/test/java/org/mage/test/bdd/and/Graveyards.java +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/and/Graveyards.java @@ -1,11 +1,25 @@ 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.bdd.StepState; + +import static org.hamcrest.core.Is.is; public class Graveyards { - public static boolean empty() throws Exception { - boolean empty = MageBase.getInstance().checkGraveyardsEmpty(); - System.out.println("empty: " + empty); - return empty; + private StepState step; + public Graveyards(StepState step) { + this.step = step; + } + 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."); + } } } diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/given/A.java b/Mage.Tests/src/test/java/org/mage/test/bdd/given/A.java index f9b5bbdfef..ca4b959809 100644 --- a/Mage.Tests/src/test/java/org/mage/test/bdd/given/A.java +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/given/A.java @@ -2,6 +2,7 @@ package org.mage.test.bdd.given; import org.mage.test.base.MageAPI; import org.mage.test.base.MageBase; +import org.mage.test.base.exception.CardNotFoundException; import org.mage.test.bdd.StepController; import org.mage.test.bdd.StepState; @@ -14,11 +15,11 @@ public class A { StepState current = MageAPI.defineStep(this.step); if (current.equals(StepState.GIVEN)) { 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)) { 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); } } } diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/then/Then.java b/Mage.Tests/src/test/java/org/mage/test/bdd/then/Then.java index 1dc9f269f3..897036c433 100644 --- a/Mage.Tests/src/test/java/org/mage/test/bdd/then/Then.java +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/then/Then.java @@ -1,5 +1,9 @@ package org.mage.test.bdd.then; +import org.mage.test.bdd.StepState; +import org.mage.test.bdd.and.Graveyards; + public class Then { public static Battlefield battlefield = new Battlefield(); + public static Graveyards graveyards = new Graveyards(StepState.THEN); }