Add tests for FlashcardProvider
This commit is contained in:
+15
-17
@@ -1,6 +1,5 @@
|
||||
package it1901.groups2021.gr2141.core.domainlogic;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.prefs.Preferences;
|
||||
@@ -8,9 +7,9 @@ import java.util.prefs.Preferences;
|
||||
import it1901.groups2021.gr2141.core.models.CardContent;
|
||||
import it1901.groups2021.gr2141.core.models.CardDeck;
|
||||
|
||||
public class FlashCardProvider {
|
||||
public class FlashcardProvider {
|
||||
|
||||
private static final Preferences prefs = Preferences.userRoot().node(FlashCardProvider.class.getName());
|
||||
private static final Preferences prefs = Preferences.userRoot().node(FlashcardProvider.class.getName());
|
||||
|
||||
private CardDeck deck;
|
||||
private int cardIndex;
|
||||
@@ -20,14 +19,13 @@ public class FlashCardProvider {
|
||||
private boolean cardOrderIsRandomMode;
|
||||
private boolean firstLastWrapAroundMode;
|
||||
|
||||
public FlashCardProvider() {
|
||||
public FlashcardProvider() {
|
||||
this.cardIndex = 0;
|
||||
this.cardShowsFront = true;
|
||||
|
||||
this.showOrderIsFlippedMode = prefs.getBoolean("showOrderIsFlippedMode", false);
|
||||
this.cardOrderIsRandomMode = prefs.getBoolean("cardOrderIsRandomMode",false);
|
||||
this.firstLastWrapAroundMode = prefs.getBoolean("firstLastWrapAroundMode", true);
|
||||
|
||||
}
|
||||
|
||||
public CardDeck getCardDeck() {
|
||||
@@ -35,6 +33,9 @@ public class FlashCardProvider {
|
||||
}
|
||||
|
||||
public void setCardDeck(CardDeck deck) {
|
||||
if (deck == null)
|
||||
throw new IllegalArgumentException("deck can not be null.");
|
||||
|
||||
this.deck = deck;
|
||||
}
|
||||
|
||||
@@ -61,8 +62,7 @@ public class FlashCardProvider {
|
||||
public void turnToCard(int cardIndex) throws IllegalArgumentException {
|
||||
if (!(firstLastWrapAroundMode || cardOrderIsRandomMode)
|
||||
&& (cardIndex < 0 || this.deck.getNumberOfCards() <= cardIndex)) {
|
||||
System.err.println("This card does not exist: " + cardIndex);
|
||||
return;
|
||||
throw new IllegalArgumentException("This card does not exist: " + cardIndex);
|
||||
}
|
||||
|
||||
if (cardOrderIsRandomMode) {
|
||||
@@ -72,11 +72,9 @@ public class FlashCardProvider {
|
||||
this.cardIndex = cardIndex;
|
||||
|
||||
if (firstLastWrapAroundMode)
|
||||
this.cardIndex %= this.deck.getNumberOfCards();
|
||||
this.cardIndex = Math.floorMod(this.cardIndex, this.deck.getNumberOfCards());
|
||||
|
||||
cardShowsFront = true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
// used for later iterations
|
||||
@@ -88,11 +86,11 @@ public class FlashCardProvider {
|
||||
// turnToCard(this.deck.getNumberOfCards() - 1);
|
||||
// }
|
||||
|
||||
public void turnToNextCard(int cardIndex) throws IllegalArgumentException {
|
||||
public void turnToNextCard() throws IllegalArgumentException {
|
||||
turnToCard(this.cardIndex + 1);
|
||||
}
|
||||
|
||||
public void turnToPreviousCard(int cardIndex) throws IllegalArgumentException {
|
||||
public void turnToPreviousCard() throws IllegalArgumentException {
|
||||
turnToCard(this.cardIndex - 1);
|
||||
}
|
||||
|
||||
@@ -101,7 +99,7 @@ public class FlashCardProvider {
|
||||
*
|
||||
* @return cardIndex
|
||||
*/
|
||||
public int generateRandomCardIndex() {
|
||||
private int generateRandomCardIndex() {
|
||||
if (deck.getNumberOfCards() <= 1) {
|
||||
return 0;
|
||||
}
|
||||
@@ -116,15 +114,15 @@ public class FlashCardProvider {
|
||||
this.cardShowsFront = !this.cardShowsFront;
|
||||
}
|
||||
|
||||
public boolean getShowOrderIsFlippedMode() {
|
||||
public boolean isShowOrderIsFlippedMode() {
|
||||
return this.showOrderIsFlippedMode;
|
||||
}
|
||||
|
||||
public boolean getCardOrderIsRandomMode() {
|
||||
public boolean isCardOrderIsRandomMode() {
|
||||
return this.cardOrderIsRandomMode;
|
||||
}
|
||||
|
||||
public boolean getFirstLastWrapAroundMode() {
|
||||
public boolean isFirstLastWrapAroundMode() {
|
||||
return this.firstLastWrapAroundMode;
|
||||
}
|
||||
|
||||
@@ -143,7 +141,7 @@ public class FlashCardProvider {
|
||||
prefs.putBoolean("firstLastWrapAroundMode", this.firstLastWrapAroundMode);
|
||||
}
|
||||
|
||||
public void clearPreferences() {
|
||||
public static void clearPreferences() {
|
||||
List.of(
|
||||
"showOrderIsFlippedMode",
|
||||
"cardOrderIsRandomMode",
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
package it1901.groups2021.gr2141.core.domainlogic;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import it1901.groups2021.gr2141.core.models.CardContent;
|
||||
import it1901.groups2021.gr2141.core.models.CardDeck;
|
||||
import it1901.groups2021.gr2141.core.models.Flashcard;
|
||||
|
||||
public class FlashcardProviderTest {
|
||||
|
||||
private FlashcardProvider exampleFlashCardProvider;
|
||||
private CardDeck exampleDeck1;
|
||||
private CardDeck exampleDeck2;
|
||||
|
||||
@BeforeEach
|
||||
private void setupExampleObjects() {
|
||||
exampleDeck1 = new CardDeck(
|
||||
List.of(
|
||||
Flashcard.of("asdf", "content"),
|
||||
Flashcard.of("no bueno", "the test failed"),
|
||||
Flashcard.of("0", "1\n2")
|
||||
)
|
||||
);
|
||||
|
||||
exampleDeck2 = new CardDeck(
|
||||
List.of(
|
||||
Flashcard.of("another", "flashcard")
|
||||
)
|
||||
);
|
||||
|
||||
FlashcardProvider.clearPreferences();
|
||||
|
||||
exampleFlashCardProvider = new FlashcardProvider();
|
||||
exampleFlashCardProvider.toggleFirstLastWrapAroundMode();
|
||||
exampleFlashCardProvider.setCardDeck(exampleDeck1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor() {
|
||||
var instance = new FlashcardProvider();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCardDeckSetGetters() {
|
||||
assertEquals(exampleDeck1, exampleFlashCardProvider.getCardDeck());
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> exampleFlashCardProvider.setCardDeck(null));
|
||||
|
||||
exampleFlashCardProvider.setCardDeck(exampleDeck2);
|
||||
assertEquals(exampleDeck2, exampleFlashCardProvider.getCardDeck());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTurnToCard() {
|
||||
assertThrows(IllegalArgumentException.class, () -> exampleFlashCardProvider.turnToCard(-1));
|
||||
assertThrows(IllegalArgumentException.class, () -> exampleFlashCardProvider.turnToCard(exampleDeck1.getNumberOfCards() + 1));
|
||||
|
||||
exampleFlashCardProvider.turnToCard(1);
|
||||
assertEquals(exampleDeck1.getFlashcards().get(1).getFront(), exampleFlashCardProvider.getCard());
|
||||
|
||||
exampleFlashCardProvider.flipCard();
|
||||
exampleFlashCardProvider.turnToCard(0);
|
||||
assertEquals(exampleDeck1.getFlashcards().get(0).getFront(), exampleFlashCardProvider.getCard());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTurnToNextPreviousCard() {
|
||||
exampleFlashCardProvider.turnToNextCard();
|
||||
assertEquals(1, exampleFlashCardProvider.getCardIndex());
|
||||
|
||||
exampleFlashCardProvider.turnToPreviousCard();
|
||||
assertEquals(0, exampleFlashCardProvider.getCardIndex());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTurnToCardWithRandomMode () {
|
||||
exampleFlashCardProvider.toggleCardOrderIsRandomMode();
|
||||
boolean hasTurnedToAnotherCard = false;
|
||||
|
||||
for (int i=0; i<9999; i++) {
|
||||
exampleFlashCardProvider.turnToCard(0);
|
||||
if (!hasTurnedToAnotherCard && exampleFlashCardProvider.getCardIndex() != 0)
|
||||
hasTurnedToAnotherCard = true;
|
||||
}
|
||||
|
||||
assertTrue(hasTurnedToAnotherCard);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTurnToCardWithRandomModeWithDeckSize1 () {
|
||||
exampleFlashCardProvider.toggleCardOrderIsRandomMode();
|
||||
exampleFlashCardProvider.setCardDeck(exampleDeck2);
|
||||
boolean hasTurnedToAnotherCard = false;
|
||||
|
||||
for (int i=0; i<9999; i++) {
|
||||
exampleFlashCardProvider.turnToCard(0);
|
||||
if (!hasTurnedToAnotherCard && exampleFlashCardProvider.getCardIndex() != 0)
|
||||
hasTurnedToAnotherCard = true;
|
||||
}
|
||||
|
||||
assertFalse(hasTurnedToAnotherCard);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTurnToCardWithWrapAroundMode() {
|
||||
exampleFlashCardProvider.toggleFirstLastWrapAroundMode();
|
||||
int maxIndex = exampleDeck1.getNumberOfCards() - 1;
|
||||
int minIndex = 0;
|
||||
|
||||
exampleFlashCardProvider.turnToCard(maxIndex + 1);
|
||||
assertEquals(minIndex, exampleFlashCardProvider.getCardIndex());
|
||||
|
||||
exampleFlashCardProvider.turnToCard(minIndex - 1);
|
||||
assertEquals(maxIndex, exampleFlashCardProvider.getCardIndex());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlipCard(){
|
||||
CardContent front = exampleFlashCardProvider.getCard();
|
||||
exampleFlashCardProvider.flipCard();
|
||||
CardContent back = exampleFlashCardProvider.getCard();
|
||||
exampleFlashCardProvider.flipCard();
|
||||
CardContent front2 = exampleFlashCardProvider.getCard();
|
||||
|
||||
assertNotEquals(front, back);
|
||||
assertEquals(front, front2);
|
||||
}
|
||||
|
||||
private void toggleAllModes() {
|
||||
exampleFlashCardProvider.toggleShowOrderIsFlippedMode();
|
||||
exampleFlashCardProvider.toggleCardOrderIsRandomMode();
|
||||
exampleFlashCardProvider.toggleFirstLastWrapAroundMode();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testModeTogglers() {
|
||||
List<BooleanSupplier> modes = List.of(
|
||||
() -> exampleFlashCardProvider.isShowOrderIsFlippedMode(),
|
||||
() -> exampleFlashCardProvider.isCardOrderIsRandomMode(),
|
||||
() -> exampleFlashCardProvider.isFirstLastWrapAroundMode()
|
||||
);
|
||||
|
||||
modes.forEach((b) -> assertFalse(b));
|
||||
toggleAllModes();
|
||||
modes.forEach((b) -> assertTrue(b));
|
||||
toggleAllModes();
|
||||
modes.forEach((b) -> assertFalse(b));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClearPreferences() {
|
||||
toggleAllModes();
|
||||
var anotherFlashcardProvider = new FlashcardProvider();
|
||||
|
||||
toggleAllModes();
|
||||
FlashcardProvider.clearPreferences();
|
||||
var yetAnotherFlashcardProvider = new FlashcardProvider();
|
||||
|
||||
assertFalse(yetAnotherFlashcardProvider.isShowOrderIsFlippedMode());
|
||||
assertFalse(yetAnotherFlashcardProvider.isCardOrderIsRandomMode());
|
||||
assertTrue(yetAnotherFlashcardProvider.isFirstLastWrapAroundMode());
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import java.util.ResourceBundle;
|
||||
import it1901.groups2021.gr2141.core.models.CardDeck;
|
||||
import it1901.groups2021.gr2141.core.models.Flashcard;
|
||||
import it1901.groups2021.gr2141.core.storage.CardDeckStorage;
|
||||
import it1901.groups2021.gr2141.core.domainlogic.FlashCardProvider;
|
||||
import it1901.groups2021.gr2141.core.domainlogic.FlashcardProvider;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
@@ -39,7 +39,7 @@ public class FlashyAppController implements Initializable {
|
||||
@FXML
|
||||
private ToggleButton btnToggleShowOtherSide;
|
||||
|
||||
private FlashCardProvider flashCardProvider = new FlashCardProvider();
|
||||
private FlashcardProvider flashCardProvider = new FlashcardProvider();
|
||||
|
||||
/**
|
||||
* Initializes the card deck.
|
||||
@@ -60,9 +60,9 @@ public class FlashyAppController implements Initializable {
|
||||
}
|
||||
|
||||
private void setToggleButtonValues() {
|
||||
btnToggleRandomMode.setSelected(flashCardProvider.getCardOrderIsRandomMode());
|
||||
btnToggleShowOtherSide.setSelected(flashCardProvider.getShowOrderIsFlippedMode());
|
||||
btnToggleFirstLastWrapAroundMode.setSelected(flashCardProvider.getFirstLastWrapAroundMode());
|
||||
btnToggleRandomMode.setSelected(flashCardProvider.isCardOrderIsRandomMode());
|
||||
btnToggleShowOtherSide.setSelected(flashCardProvider.isShowOrderIsFlippedMode());
|
||||
btnToggleFirstLastWrapAroundMode.setSelected(flashCardProvider.isFirstLastWrapAroundMode());
|
||||
}
|
||||
|
||||
public void showCurrentCardContent() {
|
||||
@@ -94,14 +94,18 @@ public class FlashyAppController implements Initializable {
|
||||
|
||||
@FXML
|
||||
public void handleTurnToNextCard(ActionEvent event) {
|
||||
flashCardProvider.turnToNextCard(flashCardProvider.getCardIndex());
|
||||
showCurrentCardContent();
|
||||
try {
|
||||
flashCardProvider.turnToNextCard();
|
||||
showCurrentCardContent();
|
||||
} finally {}
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void handleTurnToPreviousCard(ActionEvent event) {
|
||||
flashCardProvider.turnToPreviousCard(flashCardProvider.getCardIndex());
|
||||
showCurrentCardContent();
|
||||
try {
|
||||
flashCardProvider.turnToPreviousCard();
|
||||
showCurrentCardContent();
|
||||
} finally {}
|
||||
}
|
||||
|
||||
@FXML
|
||||
|
||||
Reference in New Issue
Block a user