mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
refactor and unit tests from pair-programming exercise
This commit is contained in:
parent
c6040ed788
commit
8d081db79b
@ -5,6 +5,7 @@ namespace GildedRoseKata
|
||||
public class GildedRose
|
||||
{
|
||||
IList<Item> Items;
|
||||
|
||||
public GildedRose(IList<Item> Items)
|
||||
{
|
||||
this.Items = Items;
|
||||
@ -14,72 +15,76 @@ namespace GildedRoseKata
|
||||
{
|
||||
for (var i = 0; i < Items.Count; i++)
|
||||
{
|
||||
if (Items[i].Name != "Aged Brie" && Items[i].Name != "Backstage passes to a TAFKAL80ETC concert")
|
||||
int qualityModifier = Items[i].Name.StartsWith("Conjured") ? 2 : 1;
|
||||
{
|
||||
if (Items[i].Quality > 0)
|
||||
if (Items[i].Name != "Aged Brie" && Items[i].Name != "Backstage passes to a TAFKAL80ETC concert")
|
||||
{
|
||||
if (Items[i].Name != "Sulfuras, Hand of Ragnaros")
|
||||
if (Items[i].Quality > 0)
|
||||
{
|
||||
Items[i].Quality = Items[i].Quality - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Items[i].Quality < 50)
|
||||
{
|
||||
Items[i].Quality = Items[i].Quality + 1;
|
||||
|
||||
if (Items[i].Name == "Backstage passes to a TAFKAL80ETC concert")
|
||||
{
|
||||
if (Items[i].SellIn < 11)
|
||||
if (Items[i].Name != "Sulfuras, Hand of Ragnaros")
|
||||
{
|
||||
if (Items[i].Quality < 50)
|
||||
{
|
||||
Items[i].Quality = Items[i].Quality + 1;
|
||||
}
|
||||
Items[i].Quality -= qualityModifier;
|
||||
}
|
||||
|
||||
if (Items[i].SellIn < 6)
|
||||
{
|
||||
if (Items[i].Quality < 50)
|
||||
{
|
||||
Items[i].Quality = Items[i].Quality + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Items[i].Name != "Sulfuras, Hand of Ragnaros")
|
||||
{
|
||||
Items[i].SellIn = Items[i].SellIn - 1;
|
||||
}
|
||||
|
||||
if (Items[i].SellIn < 0)
|
||||
{
|
||||
if (Items[i].Name != "Aged Brie")
|
||||
{
|
||||
if (Items[i].Name != "Backstage passes to a TAFKAL80ETC concert")
|
||||
{
|
||||
if (Items[i].Quality > 0)
|
||||
{
|
||||
if (Items[i].Name != "Sulfuras, Hand of Ragnaros")
|
||||
{
|
||||
Items[i].Quality = Items[i].Quality - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Items[i].Quality = Items[i].Quality - Items[i].Quality;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Items[i].Quality < 50)
|
||||
{
|
||||
Items[i].Quality = Items[i].Quality + 1;
|
||||
Items[i].Quality += 1;
|
||||
|
||||
if (Items[i].Name == "Backstage passes to a TAFKAL80ETC concert")
|
||||
{
|
||||
if (Items[i].SellIn < 11)
|
||||
{
|
||||
if (Items[i].Quality < 50)
|
||||
{
|
||||
Items[i].Quality += qualityModifier;
|
||||
}
|
||||
}
|
||||
|
||||
if (Items[i].SellIn < 6)
|
||||
{
|
||||
if (Items[i].Quality < 50)
|
||||
{
|
||||
Items[i].Quality += qualityModifier;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Items[i].Name != "Sulfuras, Hand of Ragnaros")
|
||||
{
|
||||
Items[i].SellIn -= 1;
|
||||
}
|
||||
|
||||
if (Items[i].SellIn < 0)
|
||||
{
|
||||
if (Items[i].Name != "Aged Brie")
|
||||
{
|
||||
if (Items[i].Name != "Backstage passes to a TAFKAL80ETC concert")
|
||||
{
|
||||
if (Items[i].Quality > 0)
|
||||
{
|
||||
if (Items[i].Name != "Sulfuras, Hand of Ragnaros")
|
||||
{
|
||||
Items[i].Quality -= qualityModifier;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Items[i].Quality = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Items[i].Quality < 50)
|
||||
{
|
||||
Items[i].Quality += qualityModifier;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -87,3 +92,4 @@ namespace GildedRoseKata
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,18 +1,148 @@
|
||||
using Xunit;
|
||||
using NUnit;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using GildedRoseKata;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace GildedRoseTests
|
||||
{
|
||||
public class GildedRoseTest
|
||||
[TestFixture]
|
||||
public class GildedRoseUpdateQuality
|
||||
{
|
||||
[Fact]
|
||||
public void foo()
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
IList<Item> Items = new List<Item> { new Item { Name = "foo", SellIn = 0, Quality = 0 } };
|
||||
GildedRose app = new GildedRose(Items);
|
||||
app.UpdateQuality();
|
||||
Assert.Equal("fixme", Items[0].Name);
|
||||
}
|
||||
|
||||
private List<Item> ResetItems()
|
||||
{
|
||||
return new List<Item>
|
||||
{
|
||||
new Item { Name = "+5 Dexterity Vest", SellIn = 10, Quality = 20 },
|
||||
new Item { Name = "Aged Brie", SellIn = 2, Quality = 0 },
|
||||
new Item { Name = "Elixir of the Mongoose", SellIn = 5, Quality = 7 },
|
||||
new Item { Name = "Sulfuras, Hand of Ragnaros", SellIn = 0, Quality = 80 },
|
||||
new Item { Name = "Sulfuras, Hand of Ragnaros", SellIn = -1, Quality = 80 },
|
||||
new Item
|
||||
{
|
||||
Name = "Backstage passes to a TAFKAL80ETC concert",
|
||||
SellIn = 15,
|
||||
Quality = 20
|
||||
},
|
||||
new Item
|
||||
{
|
||||
Name = "Backstage passes to a TAFKAL80ETC concert",
|
||||
SellIn = 10,
|
||||
Quality = 49
|
||||
},
|
||||
new Item
|
||||
{
|
||||
Name = "Backstage passes to a TAFKAL80ETC concert",
|
||||
SellIn = 5,
|
||||
Quality = 49
|
||||
},
|
||||
// this conjured item does not work properly yet
|
||||
new Item { Name = "Conjured Mana Cake", SellIn = 3, Quality = 6 }
|
||||
};
|
||||
}
|
||||
|
||||
[TestCase("Sulfuras, Hand of Ragnaros", 10, 80, 80)]
|
||||
[TestCase("Elixir of the Mongoose", 10, 7, 6)]
|
||||
[TestCase("Aged Brie", 10, 0, 1)]
|
||||
[TestCase("Backstage passes to a TAFKAL80ETC concert",15,10,11)]
|
||||
[TestCase("Backstage passes to a TAFKAL80ETC concert",10,10,12)]
|
||||
[TestCase("Backstage passes to a TAFKAL80ETC concert",5,10,13)]
|
||||
[TestCase("Backstage passes to a TAFKAL80ETC concert",0,10,0)]
|
||||
[TestCase("Conjured Mana Cake", 10,10,8)]
|
||||
public void ShouldUpdateQuality_BeforeSellByDate(string name, int sellInDays, int startQuality, int endQuality)
|
||||
{
|
||||
//Arrange
|
||||
var item = new Item
|
||||
{
|
||||
Name = name,
|
||||
Quality = startQuality,
|
||||
SellIn = sellInDays
|
||||
};
|
||||
var items = new List<Item>() { item };
|
||||
var app = new GildedRose(items);
|
||||
//Act
|
||||
app.UpdateQuality();
|
||||
//Assert
|
||||
var updatedItem = items.FirstOrDefault(x => x.Name == name);
|
||||
Assert.NotNull(updatedItem);
|
||||
Assert.AreEqual(endQuality, updatedItem.Quality);
|
||||
}
|
||||
|
||||
[TestCase("Sulfuras, Hand of Ragnaros", 0, 80, 80)]
|
||||
[TestCase("Elixir of the Mongoose", 0, 7, 5)]
|
||||
[TestCase("Aged Brie", 0, 0, 2)]
|
||||
[TestCase("Conjured Mana Cake", 0, 10, 6)]
|
||||
public void ShouldUpdateQualityDifferently_AfterSellByDate(string name, int sellInDays, int startQuality, int endQuality)
|
||||
{
|
||||
//Arrange
|
||||
var item = new Item
|
||||
{
|
||||
Name = name,
|
||||
Quality = startQuality,
|
||||
SellIn = sellInDays
|
||||
};
|
||||
var items = new List<Item>() { item };
|
||||
var app = new GildedRose(items);
|
||||
//Act
|
||||
app.UpdateQuality();
|
||||
//Assert
|
||||
var updatedItem = items.FirstOrDefault();
|
||||
Assert.NotNull(updatedItem);
|
||||
Assert.AreEqual(endQuality, updatedItem.Quality);
|
||||
}
|
||||
|
||||
[TestCase("Sulfuras, Hand of Ragnaros", 80)]
|
||||
[TestCase("Elixir of the Mongoose", 0)]
|
||||
[TestCase("Aged Brie", 0)]
|
||||
[TestCase("Conjured Mana Cake", 0)]
|
||||
public void ShouldNotAllowNegativeQuality(string name, int startQuality)
|
||||
{
|
||||
//Arrange
|
||||
var item = new Item
|
||||
{
|
||||
Name = name,
|
||||
Quality = startQuality,
|
||||
SellIn = 10
|
||||
};
|
||||
var items = new List<Item>() { item };
|
||||
var app = new GildedRose(items);
|
||||
//Act
|
||||
app.UpdateQuality();
|
||||
|
||||
//Assert
|
||||
var updatedItem = items.FirstOrDefault();
|
||||
Assert.GreaterOrEqual(item.Quality, 0);
|
||||
}
|
||||
[TestCase("Sulfuras, Hand of Ragnaros", 80)]
|
||||
[TestCase("Elixir of the Mongoose")]
|
||||
[TestCase("Aged Brie")]
|
||||
[TestCase("Conjured Mana Cake")]
|
||||
|
||||
public void ShouldNotExceedMaximumQuality(string name, int maxQuality=50)
|
||||
{
|
||||
//Arrange
|
||||
var item = new Item
|
||||
{
|
||||
Name = name,
|
||||
Quality = maxQuality,
|
||||
SellIn = 10
|
||||
};
|
||||
var items = new List<Item>() { item };
|
||||
var app = new GildedRose(items);
|
||||
//Act
|
||||
app.UpdateQuality();
|
||||
|
||||
//Assert
|
||||
var updatedItem = items.FirstOrDefault();
|
||||
Assert.LessOrEqual(item.Quality, maxQuality);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
|
||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.0" />
|
||||
|
||||
Loading…
Reference in New Issue
Block a user