refactor: simplify updateItem and add param tests

This commit is contained in:
Kadir Sirimsi 2025-02-10 16:16:13 +01:00
parent 8879189e92
commit 541c817175
No known key found for this signature in database
GPG Key ID: A21C0144C2D2A134
2 changed files with 97 additions and 24 deletions

View File

@ -1,5 +1,7 @@
package com.gildedrose;
import java.util.Objects;
import static com.gildedrose.ItemType.fromName;
public class Item {
@ -24,17 +26,26 @@ public class Item {
}
public void updateItem() {
boolean isAgedBrie = name.equals("Aged Brie");
boolean isSulfuras = name.equals("Sulfuras, Hand of Ragnaros");
boolean isBackStagePass = name.equals("Backstage passes to a TAFKAL80ETC concert");
switch (type) {
case AgedBrie -> {
if (!isAgedBrie && !isBackStagePass) {
if (quality > 0 && !isSulfuras) {
deductOneFromQuality();
if (quality < 50) {
addOneToQuality();
}
deductSellIn();
if (sellIn < 0 && quality < 50) {
addOneToQuality();
}
}
} else if(quality < 50) {
addOneToQuality();
if (isBackStagePass) {
case BackstagePass -> {
if (quality < 50) {
addOneToQuality();
}
if (sellIn < 11 && quality < 50) {
addOneToQuality();
}
@ -42,24 +53,26 @@ public class Item {
if (sellIn < 6 && quality < 50) {
addOneToQuality();
}
}
}
deductSellIn();
if (!isSulfuras) {
deductSellIn();
}
if (sellIn < 0) {
if (!isAgedBrie) {
if (!isBackStagePass) {
if (quality > 0 && !isSulfuras) {
deductOneFromQuality();
}
} else {
setQualityToZero();
if (sellIn < 0) {
quality = 0;
}
}
case Sulfuras -> {}
case Unknown -> {
deductSellIn();
if (quality > 0) {
deductOneFromQuality();
}
if (sellIn < 0 && quality > 0) {
deductOneFromQuality();
}
} else if ((quality < 50)) {
addOneToQuality();
}
}
}

View File

@ -0,0 +1,60 @@
package com.gildedrose;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
import static com.gildedrose.ItemType.AgedBrie;
import static com.gildedrose.ItemType.BackstagePass;
import static com.gildedrose.ItemType.Sulfuras;
import static com.gildedrose.ItemType.Unknown;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.params.provider.Arguments.of;
class ItemTest {
@ParameterizedTest
@MethodSource("provideAgedBrieOptions")
void givenAgedBrie_whenUpdateItem_thenCorrect(Item input, Item expected, int days) {
for (int i = 0; i < days; i++) {
input.updateItem();
}
assertEquals(expected, input);
}
private static Stream<Arguments> provideAgedBrieOptions() {
return Stream.of(
// Aged Brie
of(new Item(AgedBrie.getName(), -1, 0), new Item(AgedBrie.getName(), -2, 2), 1),
of(new Item(AgedBrie.getName(), 100, 0), new Item(AgedBrie.getName(), 0, 50), 100),
of(new Item(AgedBrie.getName(), 50, 0), new Item(AgedBrie.getName(), 40, 10), 10),
of(new Item(AgedBrie.getName(), 50, 0), new Item(AgedBrie.getName(), 20, 30), 30),
// Sulfuras -- no changes with sulfuras
of(new Item(Sulfuras.getName(), -1, 0), new Item(Sulfuras.getName(), -1, 0), 1),
of(new Item(Sulfuras.getName(), 10, 0), new Item(Sulfuras.getName(), 10, 0), 1),
of(new Item(Sulfuras.getName(), 100, 0), new Item(Sulfuras.getName(), 100, 0), 10),
of(new Item(Sulfuras.getName(), 10, 20), new Item(Sulfuras.getName(), 10, 20), 10),
of(new Item(Sulfuras.getName(), 4, 20), new Item(Sulfuras.getName(), 4, 20), 10),
// Backstagepass
of(new Item(BackstagePass.getName(), 8, 40), new Item(BackstagePass.getName(), 7, 42), 1),
of(new Item(BackstagePass.getName(), 8, 40), new Item(BackstagePass.getName(), -2, 0), 10),
of(new Item(BackstagePass.getName(), 4, 40), new Item(BackstagePass.getName(), -6, 0), 10),
of(new Item(BackstagePass.getName(), 0, 40), new Item(BackstagePass.getName(), -10, 0), 10),
of(new Item(BackstagePass.getName(), -1, 40), new Item(BackstagePass.getName(), -11, 0), 10),
// Other
of(new Item(Unknown.getName(), 0, 40), new Item(Unknown.getName(), -10, 20), 10),
of(new Item(Unknown.getName(), -1, 40), new Item(Unknown.getName(), -11, 20), 10),
of(new Item(Unknown.getName(), -5, 80), new Item(Unknown.getName(), -25, 40), 20)
);
}
}