From 71ebf90e865995c55a4b8243a1767216d615fde6 Mon Sep 17 00:00:00 2001 From: Nicklas Lundsteen Jensen Date: Tue, 11 Oct 2022 13:32:54 +0200 Subject: [PATCH 1/5] tests --- GildedRose.Tests/ProgramTests.cs | 167 ++++++++++++++++++++++++++++++- GildedRose.Tests/Usings.cs | 1 + GildedRose.sln | 2 +- GildedRose/Program.cs | 6 +- 4 files changed, 170 insertions(+), 6 deletions(-) diff --git a/GildedRose.Tests/ProgramTests.cs b/GildedRose.Tests/ProgramTests.cs index 1dd936f..fc8f4b8 100644 --- a/GildedRose.Tests/ProgramTests.cs +++ b/GildedRose.Tests/ProgramTests.cs @@ -3,8 +3,171 @@ namespace GildedRose.Tests; public class ProgramTests { [Fact] - public void TestTheTruth() + public void Day_0_Has_Correct_Values() { - true.Should().BeTrue(); + using var writer = new StringWriter(); + Console.SetOut(writer); + + var program = Assembly.Load(nameof(GildedRose)); + program.EntryPoint?.Invoke(null, new[] { Array.Empty() }); + + var start = 1; + var end = 12; + var outputString = ""; + + var output = writer.GetStringBuilder().ToString().TrimEnd().Split(Environment.NewLine); + + for (var i = start; i < end; i++) { + outputString = outputString + output[i]; + } + + outputString.Should().Be("-------- day 0 --------name, sellIn, quality+5 Dexterity Vest, 10, 20Aged Brie, 2, 0Elixir of the Mongoose, 5, 7Sulfuras, Hand of Ragnaros, 0, 80Sulfuras, Hand of Ragnaros, -1, 80Backstage passes to a TAFKAL80ETC concert, 15, 20Backstage passes to a TAFKAL80ETC concert, 10, 49Backstage passes to a TAFKAL80ETC concert, 5, 49Conjured Mana Cake, 3, 6"); + } + + [Fact] + public void Day_1_Has_Correct_Values() + { + using var writer = new StringWriter(); + Console.SetOut(writer); + + var program = Assembly.Load(nameof(GildedRose)); + program.EntryPoint?.Invoke(null, new[] { Array.Empty() }); + + var start = 13; + var end = 24; + var outputString = ""; + + var output = writer.GetStringBuilder().ToString().TrimEnd().Split(Environment.NewLine); + + for (var i = start; i < end; i++) { + outputString = outputString + output[i]; + } + + + outputString.Should().Be("-------- day 3 --------name, sellIn, quality+5 Dexterity Vest, 7, 17Aged Brie, -1, 3Elixir of the Mongoose, 2, 4Sulfuras, Hand of Ragnaros, -3, 20Sulfuras, Hand of Ragnaros, -4, 20Backstage passes to a TAFKAL80ETC concert, 12, 23Backstage passes to a TAFKAL80ETC concert, 7, 55Backstage passes to a TAFKAL80ETC concert, 2, 58Conjured Mana Cake, 0, 3"); + + } + + [Fact] + public void Day_3_Has_Correct_Values() + { + using var writer = new StringWriter(); + Console.SetOut(writer); + + var program = Assembly.Load(nameof(GildedRose)); + program.EntryPoint?.Invoke(null, new[] { Array.Empty() }); + + var start = 37; + var end = 48; + var outputString = ""; + + var output = writer.GetStringBuilder().ToString().TrimEnd().Split(Environment.NewLine); + + for (var i = start; i < end; i++) { + outputString = outputString + output[i]; + } + + + outputString.Should().Be("-------- day 3 --------name, sellIn, quality+5 Dexterity Vest, 7, 17Aged Brie, -1, 3Elixir of the Mongoose, 2, 4Sulfuras, Hand of Ragnaros, -3, 20Sulfuras, Hand of Ragnaros, -4, 20Backstage passes to a TAFKAL80ETC concert, 12, 23Backstage passes to a TAFKAL80ETC concert, 7, 55Backstage passes to a TAFKAL80ETC concert, 2, 58Conjured Mana Cake, 0, 3"); + + } + + [Fact] + public void Given_Items_Should_Update_Quality_And_SellIn_3_Times(){ + + var app = new GildedRose.Program() + { + Items = new List + { + 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 } + } + + }; + + app.UpdateQuality(); + app.UpdateQuality(); + app.UpdateQuality(); + + + var itemsResult = new List(); + itemsResult.AddRange(new Item[] { + new Item { Name = "+5 Dexterity Vest", SellIn = 7, Quality = 17 }, + new Item { Name = "Aged Brie", SellIn = -1, Quality = 4 }, + new Item { Name = "Elixir of the Mongoose", SellIn = 2, Quality = 4 }, + 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 = 12, + Quality = 23 + }, + new Item + { + Name = "Backstage passes to a TAFKAL80ETC concert", + SellIn = 7, + Quality = 50 + }, + new Item + { + Name = "Backstage passes to a TAFKAL80ETC concert", + SellIn = 2, + Quality = 50 + }, + // this conjured item does not work properly yet + new Item { Name = "Conjured Mana Cake", SellIn = 0, Quality = 3 }}); + + + + + app.Items.Should().BeEquivalentTo(itemsResult); + + } + + /* + [Fact] + public void Given_Items_If_Correct_Quaility_Return_True(){ + IList Items; + Items = new List + { + new Item { Name = "+5 Dexterity Vest", SellIn = 12, Quality = 15 }, + new Item { Name = "Aged Brie", SellIn = 5, Quality = 2 }, + new Item { Name = "Elixir of the Mongoose", SellIn = 7, Quality = 7 }, + new Item { Name = "Sulfuras, Hand of Ragnaros", SellIn = 1, Quality = 80 }, + new Item + { + Name = "Backstage passes to a TAFKAL80ETC concert", + SellIn = 10, + Quality = 30 + } + }; + } + */ + + } \ No newline at end of file diff --git a/GildedRose.Tests/Usings.cs b/GildedRose.Tests/Usings.cs index 7b25ddd..aee9c8c 100644 --- a/GildedRose.Tests/Usings.cs +++ b/GildedRose.Tests/Usings.cs @@ -1,2 +1,3 @@ global using FluentAssertions; global using Xunit; +global using System.Reflection; diff --git a/GildedRose.sln b/GildedRose.sln index 43bab5b..1e6ad39 100644 --- a/GildedRose.sln +++ b/GildedRose.sln @@ -1,4 +1,4 @@ - + Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.30114.105 diff --git a/GildedRose/Program.cs b/GildedRose/Program.cs index 52b511b..b480299 100644 --- a/GildedRose/Program.cs +++ b/GildedRose/Program.cs @@ -1,11 +1,11 @@ -using System; +using System; using System.Collections.Generic; namespace GildedRose { - class Program + public class Program { - IList Items; + public IList Items; static void Main(string[] args) { System.Console.WriteLine("OMGHAI!"); From c0eb7302262f76d000a2555cc6d95d1651a245b7 Mon Sep 17 00:00:00 2001 From: adamjhr Date: Tue, 11 Oct 2022 14:37:11 +0200 Subject: [PATCH 2/5] Finalized tests --- GildedRose.Tests/ProgramTests.cs | 198 ++++++++++++------------------- GildedRose.Tests/output.txt | Bin 0 -> 23414 bytes 2 files changed, 73 insertions(+), 125 deletions(-) create mode 100644 GildedRose.Tests/output.txt diff --git a/GildedRose.Tests/ProgramTests.cs b/GildedRose.Tests/ProgramTests.cs index fc8f4b8..41c6c19 100644 --- a/GildedRose.Tests/ProgramTests.cs +++ b/GildedRose.Tests/ProgramTests.cs @@ -2,172 +2,120 @@ namespace GildedRose.Tests; public class ProgramTests { + + private List items; + public ProgramTests() { + + var app = new GildedRose.Program(); + app.Items = new List { new Item { Name = "Vest", SellIn = 10, Quality = 20 }, + new Item { Name = "Jacket", SellIn = 0, Quality = 10}, + new Item { Name = "Poor Jacket", SellIn = 0, Quality = 1}, + new Item { Name = "Aged Brie", SellIn = 5, Quality = 47}, + new Item { Name = "Sulfuras, Hand of Ragnaros", SellIn = 0, Quality = 80}, + new Item { Name = "Backstage passes to a TAFKAL80ETC concert", SellIn = 15, Quality = 30}, + new Item { Name = "Backstage passes to a TAFKAL80ETC concert", SellIn = 10, Quality = 30}, + new Item { Name = "Backstage passes to a TAFKAL80ETC concert", SellIn = 5, Quality = 30}, + new Item { Name = "Backstage passes to a TAFKAL80ETC concert", SellIn = 1, Quality = 30} }; + app.UpdateQuality(); + app.UpdateQuality(); + app.UpdateQuality(); + app.UpdateQuality(); + app.UpdateQuality(); + items = app.Items.ToList(); + + } + + [Fact] - public void Day_0_Has_Correct_Values() - { + public void Program_Output_Is_Correct_Output() { + using var writer = new StringWriter(); Console.SetOut(writer); var program = Assembly.Load(nameof(GildedRose)); program.EntryPoint?.Invoke(null, new[] { Array.Empty() }); - var start = 1; - var end = 12; - var outputString = ""; + var output = writer.GetStringBuilder().ToString().TrimEnd(); + var outputFile = File.ReadAllText(@"../../../output.txt"); - var output = writer.GetStringBuilder().ToString().TrimEnd().Split(Environment.NewLine); + output.Should().Be(outputFile); + } + - for (var i = start; i < end; i++) { - outputString = outputString + output[i]; - } + [Fact] + public void Vest_Quality_Is_15_After_5_Days() { - outputString.Should().Be("-------- day 0 --------name, sellIn, quality+5 Dexterity Vest, 10, 20Aged Brie, 2, 0Elixir of the Mongoose, 5, 7Sulfuras, Hand of Ragnaros, 0, 80Sulfuras, Hand of Ragnaros, -1, 80Backstage passes to a TAFKAL80ETC concert, 15, 20Backstage passes to a TAFKAL80ETC concert, 10, 49Backstage passes to a TAFKAL80ETC concert, 5, 49Conjured Mana Cake, 3, 6"); + items[0].Quality.Should().Be(15); } [Fact] - public void Day_1_Has_Correct_Values() - { - using var writer = new StringWriter(); - Console.SetOut(writer); + public void Vest_SellIn_Is_5_After_5_Days() { - var program = Assembly.Load(nameof(GildedRose)); - program.EntryPoint?.Invoke(null, new[] { Array.Empty() }); + items[0].SellIn.Should().Be(5); - var start = 13; - var end = 24; - var outputString = ""; + } - var output = writer.GetStringBuilder().ToString().TrimEnd().Split(Environment.NewLine); + [Fact] + public void Jacket_Quality_Is_0_After_5_Days() { - for (var i = start; i < end; i++) { - outputString = outputString + output[i]; - } + + items[1].Quality.Should().Be(0); + } + + [Fact] + public void Poor_Jacket_Quality_Is_0_After_5_Days() { - outputString.Should().Be("-------- day 3 --------name, sellIn, quality+5 Dexterity Vest, 7, 17Aged Brie, -1, 3Elixir of the Mongoose, 2, 4Sulfuras, Hand of Ragnaros, -3, 20Sulfuras, Hand of Ragnaros, -4, 20Backstage passes to a TAFKAL80ETC concert, 12, 23Backstage passes to a TAFKAL80ETC concert, 7, 55Backstage passes to a TAFKAL80ETC concert, 2, 58Conjured Mana Cake, 0, 3"); + + items[2].Quality.Should().Be(0); } [Fact] - public void Day_3_Has_Correct_Values() - { - using var writer = new StringWriter(); - Console.SetOut(writer); + public void Aged_Brie_Quality_Is_50_After_5_Days() { - var program = Assembly.Load(nameof(GildedRose)); - program.EntryPoint?.Invoke(null, new[] { Array.Empty() }); + + items[3].Quality.Should().Be(50); - var start = 37; - var end = 48; - var outputString = ""; + } - var output = writer.GetStringBuilder().ToString().TrimEnd().Split(Environment.NewLine); + [Fact] + public void Sulfuras_Doesnt_Change() { - for (var i = start; i < end; i++) { - outputString = outputString + output[i]; - } + + items[4].Quality.Should().Be(80); + } - outputString.Should().Be("-------- day 3 --------name, sellIn, quality+5 Dexterity Vest, 7, 17Aged Brie, -1, 3Elixir of the Mongoose, 2, 4Sulfuras, Hand of Ragnaros, -3, 20Sulfuras, Hand of Ragnaros, -4, 20Backstage passes to a TAFKAL80ETC concert, 12, 23Backstage passes to a TAFKAL80ETC concert, 7, 55Backstage passes to a TAFKAL80ETC concert, 2, 58Conjured Mana Cake, 0, 3"); + [Fact] + public void Backstage_Pass_Increase_To_35() { + + + items[5].Quality.Should().Be(35); } [Fact] - public void Given_Items_Should_Update_Quality_And_SellIn_3_Times(){ - - var app = new GildedRose.Program() - { - Items = new List - { - 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 } - } - - }; + public void Backstage_Pass_Increase_To_40() { - app.UpdateQuality(); - app.UpdateQuality(); - app.UpdateQuality(); + items[6].Quality.Should().Be(40); + } - var itemsResult = new List(); - itemsResult.AddRange(new Item[] { - new Item { Name = "+5 Dexterity Vest", SellIn = 7, Quality = 17 }, - new Item { Name = "Aged Brie", SellIn = -1, Quality = 4 }, - new Item { Name = "Elixir of the Mongoose", SellIn = 2, Quality = 4 }, - 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 = 12, - Quality = 23 - }, - new Item - { - Name = "Backstage passes to a TAFKAL80ETC concert", - SellIn = 7, - Quality = 50 - }, - new Item - { - Name = "Backstage passes to a TAFKAL80ETC concert", - SellIn = 2, - Quality = 50 - }, - // this conjured item does not work properly yet - new Item { Name = "Conjured Mana Cake", SellIn = 0, Quality = 3 }}); - - - - - app.Items.Should().BeEquivalentTo(itemsResult); + [Fact] + public void Backstage_Pass_Increase_To_45() { + + + items[7].Quality.Should().Be(45); } - /* [Fact] - public void Given_Items_If_Correct_Quaility_Return_True(){ - IList Items; - Items = new List - { - new Item { Name = "+5 Dexterity Vest", SellIn = 12, Quality = 15 }, - new Item { Name = "Aged Brie", SellIn = 5, Quality = 2 }, - new Item { Name = "Elixir of the Mongoose", SellIn = 7, Quality = 7 }, - new Item { Name = "Sulfuras, Hand of Ragnaros", SellIn = 1, Quality = 80 }, - new Item - { - Name = "Backstage passes to a TAFKAL80ETC concert", - SellIn = 10, - Quality = 30 - } - }; + public void Backstage_Pass_Quality_Becomes_0() { + + items[8].Quality.Should().Be(0); + } - */ - } \ No newline at end of file diff --git a/GildedRose.Tests/output.txt b/GildedRose.Tests/output.txt new file mode 100644 index 0000000000000000000000000000000000000000..2203c8f6d2cb095470e9ced602365af8f73dae4f GIT binary patch literal 23414 zcmd^{TW=jT5Xb$D#CJG6!%FNWx5u^=T0wNPM?YN3}Z6A## zWJNvc*wh@6TQ2q zV?1soY5aSl-#qGxyP~si>Bv(*0@5ab4X$|ZrF@{f-O*LI?q_;^aKF%~1L=56^SPqe zTR%g-^NAmO=I{Ok&GMQ?yr;33bOoP(LI1n)5^=BB^#020dgt%L*ID>d(;6O(X?nhdguidxw`kf#DoQ^!B<8SDS8+zsS zl@*1?vZ7W}uqKPU@GEO01)7MAXI|Iey!F7yD_R?-@<3~GNh=9jX1~g(eqP(8k2JCs z=@))hV_2`pIR_u3pLr?gi_*H5(Ek_b(0C%RVk_Ga2&<%o$V8I(^A zDL)UCpZj?|PPs&UwpuCW@NH<_2j$Rsq9>#s<*=F%XhXSJ4o~r1$`KKl49e$*l%EC4 zkySiSxjgML_fpQ`-5A>k=g@d^vmxiOn=R)Om&-UuOoZ;+Im^#U~_(nVvesSW9{n^0&Y1A2&ze9{pY@f_GD{j?$Js5ZtBe^R8&RdstV;^i3K1MR32 zCx?vChQqi{w2={8E}wNo#vInq8nTXAjUlH@u`aP66;^3nAE+|J9wGa953HkFoJ@EA z;%d=~b#1wP*5QXate-bz9lDHRpCHA$Wb}1a=|G;;7wf1PXVX1KSWfZ_E7rB;@>xf0 z%wheaA?v6;#$c5c>#|EzSD6l!A@#*NV#92@0~1$`R;+8w<+Bd`b6DrDWxLuG^#{w` zs!ija_wp=8dwLI94D_8t#cxAmTshjv7(#~|bmY^H=$J=)%RZ@8AX^=zfpSld&C=s8 zr(OGc540m9&h*&vU4gGB5@vz|JYO7W4@jVca2(g&%xQp#b2kZK)vh; z*hXP(Jv-{P?MkTEyZ3vszGa7XpyzK6^|ISmTfcH&Yn9Un^;%;qBDM9bWW=^Bp5vi?bC4)b8vNBRaz3#;9QQxxbD!cA@ z4yUO{Oqr`IS^jL=x^jI`uQj$JQrDSQGGg16P_Of^ZR)w})3Tb4Yn3$jh$_-!sfqU5 z^ZVdlYi!3P>So*ef%jO}s8vF~&cXJ`Z{3O2^-7w2M3yCdLZOCy?frd_uQj$KU)#@4 ziq@rse4U5wk>9o}8`mr;^7%=`KEH%dUDlAVy}u9gwZ?W#wy2)RX!oOpe4UH!k>9pI z8`mvq@)232<5)w!_WnM|*BaZAuj@`bDO#6&@+D#-7uzGhZJ#!-UDD(uvMfi`x7z#r zAYW^2$7G8tn!mxNCeP>ZDOtzuk>9po8`myr@)22<>W*#f{e6%h?;hB(ud7chDY5-Z zqQ1_?_SkRRwH>qiw*2?{?7i|=I_3FJ?Ek&6uVbDSm9Yh_q{Q|sVPEHCd+fLE-NyAx zI_@L39K*i$|6bVFI$Kd0ThK}hfBV#$yOgl6GqOGQ+jelrtR60Z9`o3^ul>Il_O;Gd zRK^yxlEU9eRmZ;0$oAN8+sBP-m~`C7PTetaU;BSA>}#E^sEjRWB}KAc?ADaTecch* oW`Azn&5diAH2c`!I);7i|GluUb+)6jML&0{?C0`#0O5J@ANq Date: Tue, 11 Oct 2022 21:10:09 +0200 Subject: [PATCH 3/5] Working on refactor --- GildedRose/Program.cs | 146 +++++++++++++++++++++++------------------- 1 file changed, 80 insertions(+), 66 deletions(-) diff --git a/GildedRose/Program.cs b/GildedRose/Program.cs index 52b511b..b6a255b 100644 --- a/GildedRose/Program.cs +++ b/GildedRose/Program.cs @@ -37,7 +37,7 @@ static void Main(string[] args) SellIn = 5, Quality = 49 }, - // this conjured item does not work properly yet + new Item { Name = "Conjured Mana Cake", SellIn = 3, Quality = 6 } } @@ -54,90 +54,104 @@ static void Main(string[] args) Console.WriteLine(""); app.UpdateQuality(); } + } + + public int increase(int start, int add = 1){ + return Math.Min(50, start+add); + } + public int decrease(int start, int remove = 1) { + return Math.Max(0, start-remove); } + public void UpdateQuality() { - for (var i = 0; i < Items.Count; i++) + foreach (Item item in Items) { - if (Items[i].Name != "Aged Brie" && 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; - } - } + if (item.Name == "Sulfuras, Hand of Ragnaros") { + continue; } - 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].Quality < 50) - { - Items[i].Quality = Items[i].Quality + 1; - } - } - - if (Items[i].SellIn < 6) - { - if (Items[i].Quality < 50) - { - Items[i].Quality = Items[i].Quality + 1; - } - } - } - } + + if (item.Name == "Aged Brie") { + updateBrie(item); + condition(item); + continue; } - if (Items[i].Name != "Sulfuras, Hand of Ragnaros") - { - Items[i].SellIn = Items[i].SellIn - 1; + if (item.Name == "Backstage passes to a TAFKAL80ETC concert") { + updateBackstage(item); + condition(item); + continue; } - 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; - } - } + if (!isSulfuras && !isBrie && !isBackstage && !isConjured) { + updateItem(item); + condition(item); + continue; + } + + if (item.Name!.Contains("Conjured", StringComparison.OrdinalIgnoreCase)) { + updateItem(item, 2); + condition(item); + continue; } } } + + + public void updateItem(Item item, int number = 1) { + item.Quality = decrease(item.Quality, number); + item.SellIn--; + if(expired(item.SellIn)){ + item.Quality = decrease(item.Quality, number); + } + } + + public void updateBrie(Item item) { + item.Quality = increase(item.Quality); + item.SellIn--; + if(expired(item.SellIn)) + { + item.Quality = increase(item.Quality); + } + } + + public void updateBackstage(Item item) { + if(item.SellIn >= 11) + { + item.Quality = increase(item.Quality, 1); + } + if(item.SellIn is >= 6 and <= 10) + { + item.Quality = increase(item.Quality, 2); + } + if(item.SellIn is >= 0 and <= 5) + { + item.Quality = increase(item.Quality, 3); + } + + item.SellIn--; + + if(expired(item.SellIn)) + { + item.Quality = 0; + } + } + + public void condition(Item item) { + item.Quality = increase(item.Quality, 0); + item.Quality = decrease(item.Quality, 0); + } + public bool expired(Int sellIn) { + return sellIn < 0; } +} public class Item { - public string Name { get; set; } + public string? Name { get; set; } public int SellIn { get; set; } From 7d62146b0e2ac7cbb78dca7fef93bde93e18604b Mon Sep 17 00:00:00 2001 From: Nicklas Lundsteen Jensen Date: Wed, 12 Oct 2022 17:09:16 +0200 Subject: [PATCH 4/5] Completed refactor --- GildedRose/Program.cs | 182 ++++++++++++++++++------------------------ 1 file changed, 76 insertions(+), 106 deletions(-) diff --git a/GildedRose/Program.cs b/GildedRose/Program.cs index b6a255b..d49a61d 100644 --- a/GildedRose/Program.cs +++ b/GildedRose/Program.cs @@ -1,106 +1,85 @@ using System; using System.Collections.Generic; -namespace GildedRose -{ - class Program - { - IList Items; - static void Main(string[] args) - { - System.Console.WriteLine("OMGHAI!"); - - var app = new Program() - { - Items = new List - { +namespace GildedRose; + +public class Program { + public IList? Items; + public static void Main(string[] args){ + System.Console.WriteLine("OMGHAI!"); + + var app = new Program(){ + Items = new List{ 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 - }, - + 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}, new Item { Name = "Conjured Mana Cake", SellIn = 3, Quality = 6 } - } - - }; - - for (var i = 0; i < 31; i++) - { - Console.WriteLine("-------- day " + i + " --------"); - Console.WriteLine("name, sellIn, quality"); - for (var j = 0; j < app.Items.Count; j++) - { - Console.WriteLine(app.Items[j].Name + ", " + app.Items[j].SellIn + ", " + app.Items[j].Quality); - } - Console.WriteLine(""); - app.UpdateQuality(); } - } - public int increase(int start, int add = 1){ - return Math.Min(50, start+add); - } + }; - public int decrease(int start, int remove = 1) { - return Math.Max(0, start-remove); + for (var i = 0; i < 31; i++) { + Console.WriteLine("-------- day " + i + " --------"); + Console.WriteLine("name, sellIn, quality"); + for (var j = 0; j < app.Items.Count; j++) { + Console.WriteLine(app.Items[j].Name + ", " + app.Items[j].SellIn + ", " + app.Items[j].Quality); + } + Console.WriteLine(""); + app.UpdateQuality(); } + } + + internal int increase(int start, int add = 1){ + return Math.Min(50, start+add); + } + + internal int decrease(int start, int remove = 1) { + return Math.Max(0, start-remove); + } - public void UpdateQuality() - { - foreach (Item item in Items) - { - if (item.Name == "Sulfuras, Hand of Ragnaros") { - continue; - } + public void UpdateQuality(){ + foreach (Item item in Items!) { + if (item.Name == "Sulfuras, Hand of Ragnaros") { + continue; + } - if (item.Name == "Aged Brie") { - updateBrie(item); - condition(item); - continue; - } - - if (item.Name == "Backstage passes to a TAFKAL80ETC concert") { - updateBackstage(item); - condition(item); - continue; - } - - if (!isSulfuras && !isBrie && !isBackstage && !isConjured) { - updateItem(item); - condition(item); - continue; - } - - if (item.Name!.Contains("Conjured", StringComparison.OrdinalIgnoreCase)) { - updateItem(item, 2); - condition(item); - continue; - } + if (item.Name == "Aged Brie") { + updateBrie(item); + condition(item); + continue; + } + + if (item.Name == "Backstage passes to a TAFKAL80ETC concert") { + updateBackstage(item); + condition(item); + continue; + } + + if (!(item.Name == "Sulfuras, Hand of Ragnaros") + && !(item.Name == "Backstage passes to a TAFKAL80ETC concert") + && !(item.Name == "Aged Brie") + && !(item.Name!.Contains("Conjured", StringComparison.OrdinalIgnoreCase))){ + updateItem(item); + condition(item); + continue; + } + + if (item.Name!.Contains("Conjured", StringComparison.OrdinalIgnoreCase)) { + updateItem(item, 2); + condition(item); + continue; } } + } - public void updateItem(Item item, int number = 1) { + internal void updateItem(Item item, int number = 1) { item.Quality = decrease(item.Quality, number); item.SellIn--; if(expired(item.SellIn)){ @@ -108,54 +87,45 @@ public void updateItem(Item item, int number = 1) { } } - public void updateBrie(Item item) { + internal void updateBrie(Item item) { item.Quality = increase(item.Quality); item.SellIn--; - if(expired(item.SellIn)) - { + if(expired(item.SellIn)){ item.Quality = increase(item.Quality); } } - public void updateBackstage(Item item) { - if(item.SellIn >= 11) - { + internal void updateBackstage(Item item) { + if(item.SellIn >= 11){ item.Quality = increase(item.Quality, 1); } - if(item.SellIn is >= 6 and <= 10) - { + if(item.SellIn is >= 6 and <= 10){ item.Quality = increase(item.Quality, 2); } - if(item.SellIn is >= 0 and <= 5) - { + if(item.SellIn is >= 0 and <= 5){ item.Quality = increase(item.Quality, 3); } item.SellIn--; - if(expired(item.SellIn)) - { + if(expired(item.SellIn)) { item.Quality = 0; } } - public void condition(Item item) { + internal void condition(Item item) { item.Quality = increase(item.Quality, 0); item.Quality = decrease(item.Quality, 0); } - public bool expired(Int sellIn) { + internal bool expired(int sellIn) { return sellIn < 0; } } +public class Item { + public string? Name { get; set; } - public class Item - { - public string? Name { get; set; } - - public int SellIn { get; set; } + public int SellIn { get; set; } - public int Quality { get; set; } - } - -} \ No newline at end of file + public int Quality { get; set; } +} From 8dc92ca474611e6cc6c0321b2f2e1781d253b30e Mon Sep 17 00:00:00 2001 From: adamjhr Date: Wed, 12 Oct 2022 21:40:55 +0200 Subject: [PATCH 5/5] Modified tests to test for conjured case --- GildedRose.Tests/ProgramTests.cs | 11 ++++++++++- GildedRose.Tests/output.txt | Bin 23414 -> 23414 bytes 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/GildedRose.Tests/ProgramTests.cs b/GildedRose.Tests/ProgramTests.cs index 41c6c19..80d9427 100644 --- a/GildedRose.Tests/ProgramTests.cs +++ b/GildedRose.Tests/ProgramTests.cs @@ -15,7 +15,9 @@ public ProgramTests() { new Item { Name = "Backstage passes to a TAFKAL80ETC concert", SellIn = 15, Quality = 30}, new Item { Name = "Backstage passes to a TAFKAL80ETC concert", SellIn = 10, Quality = 30}, new Item { Name = "Backstage passes to a TAFKAL80ETC concert", SellIn = 5, Quality = 30}, - new Item { Name = "Backstage passes to a TAFKAL80ETC concert", SellIn = 1, Quality = 30} }; + new Item { Name = "Backstage passes to a TAFKAL80ETC concert", SellIn = 1, Quality = 30}, + new Item { Name = "Conjured", SellIn = 10, Quality = 30} }; + app.UpdateQuality(); app.UpdateQuality(); app.UpdateQuality(); @@ -118,4 +120,11 @@ public void Backstage_Pass_Quality_Becomes_0() { } + [Fact] + public void Conjured_Quality_Becomes_20() { + + items[9].Quality.Should().Be(20); + + } + } \ No newline at end of file diff --git a/GildedRose.Tests/output.txt b/GildedRose.Tests/output.txt index 2203c8f6d2cb095470e9ced602365af8f73dae4f..7e99fec2d9f3ecb9b675ed4178b8fb3e7e4b9d82 100644 GIT binary patch delta 48 zcmV-00MGySwgL9G0kGNy0W_0=QYEqmzybj>v%v`G0+Uo1B(uy5#sZUWA_kLM7ap^= G4!}(6>=OO} delta 48 zcmeyijq%$y#tpYv8BHfQ2B~ghJHW(fviTs#b0$XP$p<-8j{W^Us<-~#~W CFcSCx