diff options
author | jbion <joffrey.bion@amadeus.com> | 2016-12-23 01:30:08 +0100 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2016-12-23 01:30:08 +0100 |
commit | 0b8ca5503af1df6bf698ebfbf579201272a4de87 (patch) | |
tree | 6b3a66231dee727dd477db25c08926ba87b62b63 /src/test | |
parent | Suppress warnings for fields that are injected by Gson (diff) | |
download | seven-wonders-0b8ca5503af1df6bf698ebfbf579201272a4de87.tar.gz seven-wonders-0b8ca5503af1df6bf698ebfbf579201272a4de87.tar.bz2 seven-wonders-0b8ca5503af1df6bf698ebfbf579201272a4de87.zip |
Add tests for ScienceProgress
This also refactors a bit the science object creation for other science tests
Diffstat (limited to 'src/test')
4 files changed, 88 insertions, 51 deletions
diff --git a/src/test/java/org/luxons/sevenwonders/game/boards/ScienceTest.java b/src/test/java/org/luxons/sevenwonders/game/boards/ScienceTest.java index cd8cca8c..067a7eff 100644 --- a/src/test/java/org/luxons/sevenwonders/game/boards/ScienceTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/boards/ScienceTest.java @@ -5,6 +5,7 @@ import org.junit.experimental.theories.DataPoints; import org.junit.experimental.theories.Theories; import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.test.TestUtils; import static org.junit.Assert.assertEquals; @@ -36,18 +37,9 @@ public class ScienceTest { return new int[] {0, 1, 3, 5, 8}; } - private static Science science(int compasses, int wheels, int tablets, int jokers) { - Science science = new Science(); - science.add(ScienceType.COMPASS, compasses); - science.add(ScienceType.WHEEL, wheels); - science.add(ScienceType.TABLET, tablets); - science.addJoker(jokers); - return science; - } - @Test public void addAll_empty() { - Science initial = science(3, 4, 5, 1); + Science initial = TestUtils.createScience(3, 4, 5, 1); Science empty = new Science(); initial.addAll(empty); assertEquals(3, initial.getQuantity(ScienceType.COMPASS)); @@ -58,8 +50,8 @@ public class ScienceTest { @Test public void addAll_noJoker() { - Science initial = science(3, 4, 5, 1); - Science other = science(1, 2, 3, 0); + Science initial = TestUtils.createScience(3, 4, 5, 1); + Science other = TestUtils.createScience(1, 2, 3, 0); initial.addAll(other); assertEquals(4, initial.getQuantity(ScienceType.COMPASS)); assertEquals(6, initial.getQuantity(ScienceType.WHEEL)); @@ -69,8 +61,8 @@ public class ScienceTest { @Test public void addAll_withJokers() { - Science initial = science(3, 4, 5, 1); - Science other = science(0, 0, 0, 3); + Science initial = TestUtils.createScience(3, 4, 5, 1); + Science other = TestUtils.createScience(0, 0, 0, 3); initial.addAll(other); assertEquals(3, initial.getQuantity(ScienceType.COMPASS)); assertEquals(4, initial.getQuantity(ScienceType.WHEEL)); @@ -80,8 +72,8 @@ public class ScienceTest { @Test public void addAll_mixed() { - Science initial = science(3, 4, 5, 1); - Science other = science(1, 2, 3, 4); + Science initial = TestUtils.createScience(3, 4, 5, 1); + Science other = TestUtils.createScience(1, 2, 3, 4); initial.addAll(other); assertEquals(4, initial.getQuantity(ScienceType.COMPASS)); assertEquals(6, initial.getQuantity(ScienceType.WHEEL)); @@ -91,31 +83,31 @@ public class ScienceTest { @Theory public void computePoints_compassesOnly_noJoker(int compasses) { - Science science = science(compasses, 0, 0, 0); + Science science = TestUtils.createScience(compasses, 0, 0, 0); assertEquals(compasses * compasses, science.computePoints()); } @Theory public void computePoints_wheelsOnly_noJoker(int wheels) { - Science science = science(0, wheels, 0, 0); + Science science = TestUtils.createScience(0, wheels, 0, 0); assertEquals(wheels * wheels, science.computePoints()); } @Theory public void computePoints_tabletsOnly_noJoker(int tablets) { - Science science = science(0, 0, tablets, 0); + Science science = TestUtils.createScience(0, 0, tablets, 0); assertEquals(tablets * tablets, science.computePoints()); } @Theory public void computePoints_allSameNoJoker(int eachSymbol) { - Science science = science(eachSymbol, eachSymbol, eachSymbol, 0); + Science science = TestUtils.createScience(eachSymbol, eachSymbol, eachSymbol, 0); assertEquals(3 * eachSymbol * eachSymbol + 7 * eachSymbol, science.computePoints()); } @Theory public void computePoints_expectation(int[] expectation) { - Science science = science(expectation[0], expectation[1], expectation[2], expectation[3]); + Science science = TestUtils.createScience(expectation[0], expectation[1], expectation[2], expectation[3]); assertEquals(expectation[4], science.computePoints()); } }
\ No newline at end of file diff --git a/src/test/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.java b/src/test/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.java index 33126aee..40088fda 100644 --- a/src/test/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.java @@ -2,9 +2,9 @@ package org.luxons.sevenwonders.game.data.serializers; import org.junit.Before; import org.junit.Test; -import org.luxons.sevenwonders.game.boards.Science; import org.luxons.sevenwonders.game.boards.ScienceType; import org.luxons.sevenwonders.game.effects.ScienceProgress; +import org.luxons.sevenwonders.game.test.TestUtils; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -27,87 +27,68 @@ public class ScienceProgressSerializerTest { gson = new GsonBuilder().registerTypeAdapter(ScienceProgress.class, new ScienceProgressSerializer()).create(); } - private static ScienceProgress create(int compasses, int wheels, int tablets, int jokers) { - Science science = new Science(); - if (compasses > 0) { - science.add(ScienceType.COMPASS, compasses); - } - if (wheels > 0) { - science.add(ScienceType.WHEEL, wheels); - } - if (tablets > 0) { - science.add(ScienceType.TABLET, tablets); - } - if (jokers > 0) { - science.addJoker(jokers); - } - ScienceProgress progress = new ScienceProgress(); - progress.setScience(science); - return progress; - } - @Test public void serialize_emptyToNull() { - ScienceProgress progress = create(0,0,0,0); + ScienceProgress progress = TestUtils.createScienceProgress(0, 0, 0, 0); String json = gson.toJson(progress); assertEquals("null", json); } @Test public void serialize_oneCompass() { - ScienceProgress progress = create(1,0,0,0); + ScienceProgress progress = TestUtils.createScienceProgress(1, 0, 0, 0); String json = gson.toJson(progress); assertEquals(COMPASS_STR, json); } @Test public void serialize_oneWheel() { - ScienceProgress progress = create(0,1,0,0); + ScienceProgress progress = TestUtils.createScienceProgress(0, 1, 0, 0); String json = gson.toJson(progress); assertEquals(WHEEL_STR, json); } @Test public void serialize_oneTablet() { - ScienceProgress progress = create(0,0,1,0); + ScienceProgress progress = TestUtils.createScienceProgress(0, 0, 1, 0); String json = gson.toJson(progress); assertEquals(TABLET_STR, json); } @Test public void serialize_oneJoker() { - ScienceProgress progress = create(0,0,0,1); + ScienceProgress progress = TestUtils.createScienceProgress(0, 0, 0, 1); String json = gson.toJson(progress); assertEquals(JOKER_STR, json); } @Test(expected = UnsupportedOperationException.class) public void serialize_failOnMultipleCompasses() { - ScienceProgress progress = create(2,0,0,0); + ScienceProgress progress = TestUtils.createScienceProgress(2, 0, 0, 0); gson.toJson(progress); } @Test(expected = UnsupportedOperationException.class) public void serialize_failOnMultipleWheels() { - ScienceProgress progress = create(0,2,0,0); + ScienceProgress progress = TestUtils.createScienceProgress(0, 2, 0, 0); gson.toJson(progress); } @Test(expected = UnsupportedOperationException.class) public void serialize_failOnMultipleTablets() { - ScienceProgress progress = create(0,0,2,0); + ScienceProgress progress = TestUtils.createScienceProgress(0, 0, 2, 0); gson.toJson(progress); } @Test(expected = UnsupportedOperationException.class) public void serialize_failOnMultipleJokers() { - ScienceProgress progress = create(0,0,0,2); + ScienceProgress progress = TestUtils.createScienceProgress(0, 0, 0, 2); gson.toJson(progress); } @Test(expected = UnsupportedOperationException.class) public void serialize_failOnMixedElements() { - ScienceProgress progress = create(1,1,0,0); + ScienceProgress progress = TestUtils.createScienceProgress(1, 1, 0, 0); gson.toJson(progress); } diff --git a/src/test/java/org/luxons/sevenwonders/game/effects/ScienceProgressTest.java b/src/test/java/org/luxons/sevenwonders/game/effects/ScienceProgressTest.java new file mode 100644 index 00000000..56289654 --- /dev/null +++ b/src/test/java/org/luxons/sevenwonders/game/effects/ScienceProgressTest.java @@ -0,0 +1,38 @@ +package org.luxons.sevenwonders.game.effects; + +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.boards.Science; +import org.luxons.sevenwonders.game.boards.ScienceType; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.*; + +@RunWith(Theories.class) +public class ScienceProgressTest { + + @DataPoints + public static int[] elementsCount() { + return new int[] {0, 1, 2}; + } + + @Theory + public void apply_initContainsAddedScience(int initCompasses, int initWheels, int initTablets, int initJokers, + int compasses, int wheels, int tablets, int jokers) { + Board board = TestUtils.createBoard(ResourceType.ORE); + Science initialScience = TestUtils.createScience(initCompasses, initWheels, initTablets, initJokers); + board.getScience().addAll(initialScience); + + ScienceProgress effect = TestUtils.createScienceProgress(compasses, wheels, tablets, jokers); + effect.apply(board); + + assertEquals(initCompasses + compasses, board.getScience().getQuantity(ScienceType.COMPASS)); + assertEquals(initWheels + wheels, board.getScience().getQuantity(ScienceType.WHEEL)); + assertEquals(initTablets + tablets, board.getScience().getQuantity(ScienceType.TABLET)); + assertEquals(initJokers + jokers, board.getScience().getJokers()); + } +}
\ No newline at end of file diff --git a/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java b/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java index 3717d3ad..f92d4b99 100644 --- a/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java +++ b/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java @@ -7,9 +7,12 @@ import org.luxons.sevenwonders.game.Player; import org.luxons.sevenwonders.game.Settings; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.boards.Science; +import org.luxons.sevenwonders.game.boards.ScienceType; import org.luxons.sevenwonders.game.cards.Card; import org.luxons.sevenwonders.game.cards.Color; import org.luxons.sevenwonders.game.cards.Requirements; +import org.luxons.sevenwonders.game.effects.ScienceProgress; import org.luxons.sevenwonders.game.resources.BoughtResources; import org.luxons.sevenwonders.game.resources.Production; import org.luxons.sevenwonders.game.resources.Provider; @@ -102,4 +105,27 @@ public class TestUtils { } return sampleCards; } + + public static ScienceProgress createScienceProgress(int compasses, int wheels, int tablets, int jokers) { + ScienceProgress progress = new ScienceProgress(); + progress.setScience(TestUtils.createScience(compasses, wheels, tablets, jokers)); + return progress; + } + + public static Science createScience(int compasses, int wheels, int tablets, int jokers) { + Science science = new Science(); + if (compasses > 0) { + science.add(ScienceType.COMPASS, compasses); + } + if (wheels > 0) { + science.add(ScienceType.WHEEL, wheels); + } + if (tablets > 0) { + science.add(ScienceType.TABLET, tablets); + } + if (jokers > 0) { + science.addJoker(jokers); + } + return science; + } } |