mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-19 08:21:37 +00:00
✨ move design back to contract
This commit is contained in:
parent
7a609b11f1
commit
5f64e3fb2e
@ -1,22 +1,16 @@
|
|||||||
package com.gildedrose.main;
|
package com.gildedrose.main;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static com.gildedrose.item_helpers.ItemFactory.getItemType;
|
import static com.gildedrose.item_helpers.ItemFactory.getItemType;
|
||||||
import static java.util.Collections.singletonList;
|
import static java.util.Arrays.stream;
|
||||||
|
|
||||||
public class GildedRose {
|
public class GildedRose {
|
||||||
private final List<Item> items;
|
Item[] items;
|
||||||
|
|
||||||
public GildedRose(List<Item> items) {
|
public GildedRose(Item[] items) {
|
||||||
this.items = items;
|
this.items = items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GildedRose(Item items) {
|
|
||||||
this.items = singletonList(items);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateQuality() {
|
public void updateQuality() {
|
||||||
items.forEach(item -> getItemType(item).updateQuality());
|
stream(items).forEach(item -> getItemType(item).updateQuality());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,8 +10,10 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||||||
public class TestHelper {
|
public class TestHelper {
|
||||||
|
|
||||||
public static void testItem(Item item, int daysToPass, int expectedSellIn, int expectedQuality) {
|
public static void testItem(Item item, int daysToPass, int expectedSellIn, int expectedQuality) {
|
||||||
|
Item[] items = new Item[1];
|
||||||
|
items[0] = item;
|
||||||
// given
|
// given
|
||||||
GildedRose app = new GildedRose(item);
|
GildedRose app = new GildedRose(items);
|
||||||
// when
|
// when
|
||||||
for (int i = 0; i < daysToPass; i++) {
|
for (int i = 0; i < daysToPass; i++) {
|
||||||
app.updateQuality();
|
app.updateQuality();
|
||||||
@ -22,14 +24,18 @@ public class TestHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void testItemException(Item item) {
|
public static void testItemException(Item item) {
|
||||||
GildedRose gildedRose = new GildedRose(item);
|
Item[] items = new Item[1];
|
||||||
|
items[0] = item;
|
||||||
|
GildedRose gildedRose = new GildedRose(items);
|
||||||
Exception exception = assertThrows(IllegalArgumentException.class, gildedRose::updateQuality);
|
Exception exception = assertThrows(IllegalArgumentException.class, gildedRose::updateQuality);
|
||||||
String actualMessage = exception.getMessage();
|
String actualMessage = exception.getMessage();
|
||||||
assertTrue(actualMessage.contains(QUALITY_ERROR_MESSAGE));
|
assertTrue(actualMessage.contains(QUALITY_ERROR_MESSAGE));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void testItemQualityAboveLimitException(Item item) {
|
public static void testItemQualityAboveLimitException(Item item) {
|
||||||
GildedRose gildedRose = new GildedRose(item);
|
Item[] items = new Item[1];
|
||||||
|
items[0] = item;
|
||||||
|
GildedRose gildedRose = new GildedRose(items);
|
||||||
Exception exception = assertThrows(IllegalArgumentException.class, gildedRose::updateQuality);
|
Exception exception = assertThrows(IllegalArgumentException.class, gildedRose::updateQuality);
|
||||||
String actualMessage = exception.getMessage();
|
String actualMessage = exception.getMessage();
|
||||||
assertTrue(actualMessage.contains(OUT_OF_BOUND_QUALITY_MESSAGE));
|
assertTrue(actualMessage.contains(OUT_OF_BOUND_QUALITY_MESSAGE));
|
||||||
|
|||||||
@ -35,7 +35,7 @@ class LegendaryItemTest {
|
|||||||
@Test
|
@Test
|
||||||
@Order(3)
|
@Order(3)
|
||||||
void testFakeLegendaryItemExceptionFail() {
|
void testFakeLegendaryItemExceptionFail() {
|
||||||
GildedRose gildedRose = new GildedRose(fakeLegendaryItem);
|
GildedRose gildedRose = new GildedRose(new Item[]{fakeLegendaryItem});
|
||||||
Exception exception = assertThrows(IllegalArgumentException.class, gildedRose::updateQuality);
|
Exception exception = assertThrows(IllegalArgumentException.class, gildedRose::updateQuality);
|
||||||
String actualMessage = exception.getMessage();
|
String actualMessage = exception.getMessage();
|
||||||
assertTrue(actualMessage.contains(NOT_LEGENDARY_ITEM_ERROR_MESSAGE));
|
assertTrue(actualMessage.contains(NOT_LEGENDARY_ITEM_ERROR_MESSAGE));
|
||||||
|
|||||||
17
src/test/java/com/gildedrose/main/GildedRoseTest.java
Normal file
17
src/test/java/com/gildedrose/main/GildedRoseTest.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package com.gildedrose.main;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
class GildedRoseTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void foo() {
|
||||||
|
Item[] items = new Item[]{new Item("foo", 0, 0)};
|
||||||
|
GildedRose app = new GildedRose(items);
|
||||||
|
app.updateQuality();
|
||||||
|
assertEquals("foo", app.items[0].name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
37
src/test/java/com/gildedrose/main/TexttestFixture.java
Normal file
37
src/test/java/com/gildedrose/main/TexttestFixture.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package com.gildedrose.main;
|
||||||
|
|
||||||
|
public class TexttestFixture {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("OMGHAI!");
|
||||||
|
|
||||||
|
Item[] items = new Item[]{
|
||||||
|
new Item("+5 Dexterity Vest", 10, 20), //
|
||||||
|
new Item("Aged Brie", 2, 0), //
|
||||||
|
new Item("Elixir of the Mongoose", 5, 7), //
|
||||||
|
new Item("Sulfuras, Hand of Ragnaros", 0, 80), //
|
||||||
|
new Item("Sulfuras, Hand of Ragnaros", -1, 80),
|
||||||
|
new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
|
||||||
|
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
|
||||||
|
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49),
|
||||||
|
// this conjured item does not work properly yet
|
||||||
|
new Item("Conjured Mana Cake", 3, 6)};
|
||||||
|
|
||||||
|
GildedRose app = new GildedRose(items);
|
||||||
|
|
||||||
|
int days = 2;
|
||||||
|
if (args.length > 0) {
|
||||||
|
days = Integer.parseInt(args[0]) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < days; i++) {
|
||||||
|
System.out.println("-------- day " + i + " --------");
|
||||||
|
System.out.println("name, sellIn, quality");
|
||||||
|
for (Item item : items) {
|
||||||
|
System.out.println(item);
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
app.updateQuality();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -8,9 +8,6 @@ import org.junit.jupiter.api.Order;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.TestMethodOrder;
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
@TestMethodOrder(OrderAnnotation.class)
|
@TestMethodOrder(OrderAnnotation.class)
|
||||||
@ -29,16 +26,16 @@ class MultipleItemsTest {
|
|||||||
public static final Item backStagePassItem3 = new Item(BACKSTAGE_PASS, 5, 49);
|
public static final Item backStagePassItem3 = new Item(BACKSTAGE_PASS, 5, 49);
|
||||||
public static final Item conjuredItem = new Item("Conjured Mana Cake", 3, 6);
|
public static final Item conjuredItem = new Item("Conjured Mana Cake", 3, 6);
|
||||||
|
|
||||||
public static List<Item> getMultipleItems() {
|
public static Item[] getMultipleItems() {
|
||||||
return asList(normalItem1, agedBrieItem, normalItem2,
|
return new Item[]{normalItem1, agedBrieItem, normalItem2,
|
||||||
legendaryItem1, legendaryItem2,
|
legendaryItem1, legendaryItem2,
|
||||||
backStagePassItem1, backStagePassItem2, backStagePassItem3,
|
backStagePassItem1, backStagePassItem2, backStagePassItem3,
|
||||||
conjuredItem);
|
conjuredItem};
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
static void updateItemsQualityFor20Days() {
|
static void updateItemsQualityFor20Days() {
|
||||||
List<Item> items = getMultipleItems();
|
Item[] items = getMultipleItems();
|
||||||
GildedRose gildedRose = new GildedRose(items);
|
GildedRose gildedRose = new GildedRose(items);
|
||||||
int days = 20;
|
int days = 20;
|
||||||
for (int i = 0; i < days; i++) {
|
for (int i = 0; i < days; i++) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user