Skip to content

Instantly share code, notes, and snippets.

@xpmatteo
Created March 26, 2013 07:44
Show Gist options
  • Save xpmatteo/5243745 to your computer and use it in GitHub Desktop.
Save xpmatteo/5243745 to your computer and use it in GitHub Desktop.
This is IMHO an improvement over Bobby Johnson test cases for the Gilded Rose Kata (https://github.com/NotMyself/GildedRose/blob/first_refactor/src/GildedRose.Tests/UpdateItemsTests.cs) Chief improvements: a) the duplication is factored in small nice methods. If the GildedRose API changes, we will have to change the test in one place, not hundre…
import static org.junit.Assert.*;
import org.junit.Test;
public class GildedRoseTest {
private GildedRose app;
@Test
public void at_the_end_of_each_day_our_system_lowers_both_values_for_every_item() {
givenItemWithSellinAndQuality("foo", 10, 10);
whenWeUpdateTheQuality();
thenItemBecomes("foo", 9, 9);
}
@Test
public void the_Quality_of_an_item_is_never_negative() throws Exception {
givenItemWithSellinAndQuality("foo", 10, 0);
whenWeUpdateTheQuality();
thenItemBecomes("foo", 9, 0);
}
@Test
public void once_the_sell_by_date_has_passed_Quality_degrades_twice_as_fast() throws Exception {
givenItemWithSellinAndQuality("foo", 0, 10);
whenWeUpdateTheQuality();
thenItemBecomes("foo", -1, 8);
}
@Test
public void once_the_sell_by_date_has_passed_Quality_degrades_twice_as_fast_2() throws Exception {
givenItemWithSellinAndQuality("foo", 0, 1);
whenWeUpdateTheQuality();
thenItemBecomes("foo", -1, 0);
}
@Test
public void Aged_Brie__actually_increases_in_Quality_the_older_it_gets() throws Exception {
givenItemWithSellinAndQuality("Aged Brie", 10, 10);
whenWeUpdateTheQuality();
thenItemBecomes("Aged Brie", 9, 11);
}
@Test
public void the_Quality_of_an_item_is_never_more_than_50() throws Exception {
givenItemWithSellinAndQuality("Aged Brie", 10, 50);
whenWeUpdateTheQuality();
thenItemBecomes("Aged Brie", 9, 50);
}
@Test
public void Sulfuras_being_a_legendary_item_never_has_to_be_sold_or_decreases_in_Quality() throws Exception {
String sulfuras = "Sulfuras, Hand of Ragnaros";
givenItemWithSellinAndQuality(sulfuras, 10, 80);
whenWeUpdateTheQuality();
thenItemBecomes(sulfuras, 10, 80);
}
@Test
public void backstage_passes_increases_in_Quality_as_its_SellIn_value_approaches() throws Exception {
backstagePassQuality(20, 10, 11);
backstagePassQuality(11, 10, 11);
backstagePassQuality(10, 10, 12);
backstagePassQuality(9, 10, 12);
backstagePassQuality(6, 10, 12);
backstagePassQuality(5, 10, 13);
backstagePassQuality(4, 10, 13);
backstagePassQuality(1, 10, 13);
backstagePassQuality(0, 10, 0);
backstagePassQuality(-1, 10, 0);
backstagePassQuality(20, 50, 50);
backstagePassQuality(9, 50, 50);
backstagePassQuality(3, 50, 50);
}
private void backstagePassQuality(int sellIn, int startQuality, int expectedQuality) {
String backstagePass = "Backstage passes to a TAFKAL80ETC concert";
givenItemWithSellinAndQuality(backstagePass, sellIn, startQuality );
whenWeUpdateTheQuality();
assertEquals("quality for sellIn " + sellIn, expectedQuality, app.items(0).quality);
assertEquals("new sellIn for old sellIn " + sellIn, sellIn - 1, app.items(0).sellIn);
}
private void whenWeUpdateTheQuality() {
app.updateQuality();
}
private void givenItemWithSellinAndQuality(String name, int sellIn, int quality) {
Item[] items = new Item[] { new Item(name, sellIn, quality) };
app = new GildedRose(items);
}
private void thenItemBecomes(String name, int sellIn, int quality) {
assertEquals(name, app.items(0).name);
assertEquals("sellIn", sellIn, app.items(0).sellIn);
assertEquals("quality", quality, app.items(0).quality);
}
}
@emilybache
Copy link

Interesting, thanks for sharing! Most of the tests are simulating Cucumber, and the backstage pass test is simulating Fitnesse. There's a discussion to be had about whether that is worthwhile in a unit testing framework, but I'll leave that for another day. A couple of things that occurred to me

  • It's sometimes hard to see which argument is referring to quality and which refers to sell in. I think this is partly a problem with Java - it doesn't allow named arguments - but you get around this neatly with the method "givenItemWithSellInAndQuality" - clearly sellin comes first, then quality. With the method "thenItemBecomes" it's less clear.
  • with the backstage pass test, I think you could make it more readable by adding an extra argument to explain what rule each line illustrates,

ie instead of:

    backstagePassQuality(9, 10, 12); 

I'd rather see

   Backstage_Pass_Update_With_SellIn_And_Initial_Quality(9, 10, 12, "with between 5 and 10 days left before the concert, quality increases by two each day")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment