Skip to content

Instantly share code, notes, and snippets.

@ataraxie
Last active March 7, 2020 17:26
Show Gist options
  • Save ataraxie/3741c3f5eaa0853dfbd6e98860726c30 to your computer and use it in GitHub Desktop.
Save ataraxie/3741c3f5eaa0853dfbd6e98860726c30 to your computer and use it in GitHub Desktop.
package ca.ubc.cpsc210.grocery;
import ca.ubc.cpsc210.grocery.model.GroceryBill;
import ca.ubc.cpsc210.grocery.model.GroceryItem;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class GroceryBillTest {
private GroceryBill testBill;
private GroceryItem milk;
private GroceryItem cereal;
private GroceryItem tea;
@BeforeEach
void runBefore() {
testBill = new GroceryBill();
initGroceryItems();
}
@Test
void testConstructor() {
assertEquals(0, testBill.getNumLineItems());
assertEquals(0, testBill.getTotalNumberOfItemsPurchased());
}
@Test
void testAddOneItem() {
testBill.addPurchase(milk, 1);
assertEquals(1, testBill.getNumLineItems());
assertEquals(1, testBill.getTotalNumberOfItemsPurchased());
}
@Test
void testAddMultipleItems() {
testBill.addPurchase(milk, 1);
testBill.addPurchase(tea, 1);
assertEquals(2, testBill.getNumLineItems());
assertEquals(2, testBill.getTotalNumberOfItemsPurchased());
}
@Test
void testAddMoreThanOneOfSingleItem() {
testBill.addPurchase(milk, 2);
assertEquals(1, testBill.getNumLineItems());
assertEquals(2, testBill.getTotalNumberOfItemsPurchased());
}
@Test
void testGetQuantityAddedItemNotPurchased() {
testBill.addPurchase(milk, 2);
assertEquals(0, testBill.getTotalQuantityOfItemPurchased(tea));
}
@Test
void testGetQuantityAddedSingleItemPurchased() {
testBill.addPurchase(milk, 2);
assertEquals(2, testBill.getTotalQuantityOfItemPurchased(milk));
}
@Test
void testAddSameItemMoreThanOnce() {
testBill.addPurchase(milk, 1);
testBill.addPurchase(tea, 1);
testBill.addPurchase(milk, 2);
assertEquals(2, testBill.getNumLineItems());
assertEquals(4, testBill.getTotalNumberOfItemsPurchased());
}
@Test
void testGetQuantityAddedMultipleItemsPurchased() {
testBill.addPurchase(milk, 1);
testBill.addPurchase(tea, 1);
testBill.addPurchase(milk, 2);
assertEquals(3, testBill.getTotalQuantityOfItemPurchased(milk));
assertEquals(1, testBill.getTotalQuantityOfItemPurchased(tea));
}
@Test
void testToStringSingleLineItem() {
testBill.addPurchase(cereal, 2);
assertEquals("2x Cereal @ $3.59\n", testBill.toString());
}
@Test
void testToStringMultipleLineItems() {
testBill.addPurchase(cereal, 2);
testBill.addPurchase(milk, 1);
testBill.addPurchase(tea, 1);
testBill.addPurchase(milk, 2);
assertEquals("2x Cereal @ $3.59\n3x Milk @ $2.99\n1x Tea @ $8.75\n", testBill.toString());
}
private void initGroceryItems() {
milk = new GroceryItem("Milk", 299);
cereal = new GroceryItem("Cereal", 359);
tea = new GroceryItem("Tea", 875);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment