Skip to content

Instantly share code, notes, and snippets.

@ataraxie
Created March 7, 2020 17:27
Show Gist options
  • Save ataraxie/c65eaab4cf4747923f8dd4ddfe375a85 to your computer and use it in GitHub Desktop.
Save ataraxie/c65eaab4cf4747923f8dd4ddfe375a85 to your computer and use it in GitHub Desktop.
package faculty;
import ca.ubc.cpsc210.grocery.model.GroceryBill;
import ca.ubc.cpsc210.grocery.model.GroceryItem;
import ca.ubc.cs.autotest.AutoTest;
import ca.ubc.cs.autotest.Grader;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@AutoTest
class GroceryBillReferenceTest {
private GroceryBill testBill;
private GroceryItem milk;
private GroceryItem cereal;
private GroceryItem tea;
@BeforeEach
void runBefore() {
testBill = new GroceryBill();
initGroceryItems();
}
@Test
@Grader(rank=1)
@DisplayName("GroceryBill: a test of the constructor has failed")
void testConstructor() {
assertEquals(0, testBill.getNumLineItems());
assertEquals(0, testBill.getTotalNumberOfItemsPurchased());
}
@Test
@Grader(rank=2)
@DisplayName("GroceryBill.addPurchase: a test that adds a single item to the bill has failed")
void testAddOneItem() {
testBill.addPurchase(milk, 1);
assertEquals(1, testBill.getNumLineItems());
assertEquals(1, testBill.getTotalNumberOfItemsPurchased());
}
@Test
@Grader(rank=3)
@DisplayName("GroceryBill.addPurchase: a test that adds multiple items to the bill has failed")
void testAddMultipleItems() {
testBill.addPurchase(milk, 1);
testBill.addPurchase(tea, 1);
assertEquals(2, testBill.getNumLineItems());
assertEquals(2, testBill.getTotalNumberOfItemsPurchased());
}
@Test
@Grader(rank=4)
@DisplayName("GroceryBill.addPurchase: a test that adds a single item with quantity greater than one has failed")
void testAddMoreThanOneOfSingleItem() {
testBill.addPurchase(milk, 2);
assertEquals(1, testBill.getNumLineItems());
assertEquals(2, testBill.getTotalNumberOfItemsPurchased());
}
@Test
@Grader(rank=5)
@DisplayName("GroceryBill.getTotalQuantityOfItemPurchased: a test where the item has not been purchased has failed")
void testGetQuantityAddedItemNotPurchased() {
testBill.addPurchase(milk, 2);
assertEquals(0, testBill.getTotalQuantityOfItemPurchased(tea));
}
@Test
@Grader(rank=6)
@DisplayName("GroceryBill.getTotalQuantityOfItemPurchased: a test where the item has been purchased has failed")
void testGetQuantityAddedSingleItemPurchased() {
testBill.addPurchase(milk, 2);
assertEquals(2, testBill.getTotalQuantityOfItemPurchased(milk));
}
@Test
@Grader(rank=7, weight=2)
@DisplayName("GroceryBill.addPurchase: a test where the same item is purchased more than once, in separate calls to addPurchase, has failed")
void testAddSameItemMoreThanOnce() {
testBill.addPurchase(milk, 1);
testBill.addPurchase(tea, 1);
testBill.addPurchase(milk, 2);
assertEquals(2, testBill.getNumLineItems());
assertEquals(4, testBill.getTotalNumberOfItemsPurchased());
}
@Test
@Grader(rank=8, weight=2)
@DisplayName("GroceryBill.getTotalQuantityOfItemPurchased: a test where the same item is purchased more than once, in separate calls to addPurchase, has failed")
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
@Grader(rank=9)
@DisplayName("GroceryBill.toString: a test where a single item was purchased has failed")
void testToStringSingleLineItem() {
testBill.addPurchase(cereal, 2);
assertEquals("2x Cereal @ $3.59\n", testBill.toString());
}
@Test
@Grader(rank=10, weight=2)
@DisplayName("GroceryBill.toString: a test where multiple items were purchased has failed")
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());
}
// EFFECTS: construct a number of grocery items for testing
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