diff --git a/src/Core/ProposedCode/Domain/ItemKind.cs b/src/Core/ProposedCode/Domain/ItemKind.cs index 199f351..aa00e3a 100644 --- a/src/Core/ProposedCode/Domain/ItemKind.cs +++ b/src/Core/ProposedCode/Domain/ItemKind.cs @@ -8,6 +8,21 @@ namespace GildedRoseKata.ProposedCode; public abstract record ItemKind { + public const string AgedBrieName = "Aged Brie"; + + public const string BackstageName = "Backstage passes to a TAFKAL80ETC concert"; + + public const string ConjuredName = "Conjured Mana Cake"; + + public const string SulfurasName = "Sulfuras, Hand of Ragnaros"; + + // Short aliases, accepted in addition to the canonical names above. + public const string AgedBrieAlias = "AgedBrie"; + + public const string BackstageAlias = "Backstage"; + + public const string ConjuredAlias = "Conjured"; + public sealed record Standard : ItemKind; public sealed record AgedBrie : ItemKind; @@ -22,14 +37,11 @@ public static ItemKind FromName(string itemName) { return itemName switch { - "Aged Brie" => new AgedBrie(), - "AgedBrie" => new AgedBrie(), - "Backstage passes to a TAFKAL80ETC concert" => new Backstage(), - "Backstage" => new Backstage(), - "Conjured Mana Cake" => new Conjured(), - "Conjured" => new Conjured(), - "Sulfuras, Hand of Ragnaros" => new Sulfuras(), + AgedBrieName or AgedBrieAlias => new AgedBrie(), + BackstageName or BackstageAlias => new Backstage(), + ConjuredName or ConjuredAlias => new Conjured(), + SulfurasName => new Sulfuras(), _ => new Standard(), }; } -} \ No newline at end of file +} diff --git a/src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs b/src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs index 0221621..26a4625 100644 --- a/src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs +++ b/src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs @@ -6,75 +6,45 @@ namespace GildedRoseKata.ProposedCode; +using System; + public class ItemMaturingPolicy : IMaturingPolicy { public ItemState Mature(ItemKind kind, ItemState state) { return kind switch { - ItemKind.AgedBrie => this.MatureAgedBrie(state), - ItemKind.Backstage => this.MatureBackstage(state), - ItemKind.Conjured => this.MatureConjured(state), + ItemKind.AgedBrie => Age(state, s => s.Quality.Increase(), q => q.Increase()), + ItemKind.Backstage => Age(state, BackstageDailyChange, q => q.Reset()), + ItemKind.Conjured => Age(state, s => s.Quality.Decrease(2), q => q.Decrease(2)), ItemKind.Sulfuras => state, - _ => this.MatureStandard(state), + _ => Age(state, s => s.Quality.Decrease(), q => q.Decrease()), }; } - private ItemState MatureStandard(ItemState state) + private static ItemState Age( + ItemState state, + Func dailyChange, + Func expiredChange) { - var newQuality = state.Quality.Decrease(); + var newQuality = dailyChange(state); var newSellIn = state.SellIn.NextDay(); if (newSellIn.IsExpired) { - newQuality = newQuality.Decrease(); + newQuality = expiredChange(newQuality); } return new(newQuality, newSellIn); } - private ItemState MatureAgedBrie(ItemState state) + private static Quality BackstageDailyChange(ItemState state) { - var newQuality = state.Quality.Increase(); - var newSellIn = state.SellIn.NextDay(); - - if (newSellIn.IsExpired) - { - newQuality = newQuality.Increase(); - } - - return new(newQuality, newSellIn); - } - - private ItemState MatureBackstage(ItemState state) - { - var newQuality = state.SellIn switch + return state.SellIn switch { _ when state.SellIn.IsBelow(6) => state.Quality.Increase(3), _ when state.SellIn.IsBelow(11) => state.Quality.Increase(2), _ => state.Quality.Increase(), }; - - var newSellIn = state.SellIn.NextDay(); - - if (newSellIn.IsExpired) - { - newQuality = newQuality.Reset(); - } - - return new(newQuality, newSellIn); - } - - private ItemState MatureConjured(ItemState state) - { - var newQuality = state.Quality.Decrease(2); - var newSellIn = state.SellIn.NextDay(); - - if (newSellIn.IsExpired) - { - newQuality = newQuality.Decrease(2); - } - - return new(newQuality, newSellIn); } -} \ No newline at end of file +} diff --git a/src/Core/ProposedCode/Domain/ItemState.cs b/src/Core/ProposedCode/Domain/ItemState.cs index 9979b8c..ec5b5c0 100644 --- a/src/Core/ProposedCode/Domain/ItemState.cs +++ b/src/Core/ProposedCode/Domain/ItemState.cs @@ -8,6 +8,8 @@ namespace GildedRoseKata.ProposedCode; public readonly record struct ItemState { + public const int LegendaryQuality = 80; + public ItemState(Quality quality, SellIn sellIn) { this.Quality = quality; @@ -20,6 +22,10 @@ public ItemState(Quality quality, SellIn sellIn) public static ItemState Create(ItemKind kind, int quality, int sellIn) { - return new ItemState(Quality.FromItemKind(kind, quality), new SellIn(sellIn)); + var initialQuality = kind is ItemKind.Sulfuras + ? Quality.Unclamped(LegendaryQuality) + : new Quality(quality); + + return new ItemState(initialQuality, new SellIn(sellIn)); } -} \ No newline at end of file +} diff --git a/src/Core/ProposedCode/Domain/Quality.cs b/src/Core/ProposedCode/Domain/Quality.cs index 7cc9395..410e311 100644 --- a/src/Core/ProposedCode/Domain/Quality.cs +++ b/src/Core/ProposedCode/Domain/Quality.cs @@ -12,7 +12,6 @@ public readonly record struct Quality { public const int Minimum = 0; public const int Maximum = 50; - public const int Legendary = 80; public Quality(int value) { @@ -21,13 +20,6 @@ public Quality(int value) public int Value { get; } - public static Quality FromItemKind(ItemKind kind, int value) - { - return kind is ItemKind.Sulfuras - ? new Quality(Legendary, allowLegendary: true) - : new Quality(value); - } - public Quality Increase(int amount = 1) { return new(this.Value + ValidateAmount(amount)); @@ -43,6 +35,11 @@ public Quality Reset() return new(0); } + internal static Quality Unclamped(int value) + { + return new Quality(value, clamp: false); + } + private static int ValidateAmount(int amount) { return amount < 0 @@ -50,10 +47,10 @@ private static int ValidateAmount(int amount) : amount; } - private Quality(int value, bool allowLegendary) + private Quality(int value, bool clamp) { - this.Value = allowLegendary - ? Legendary - : Math.Clamp(value, Minimum, Maximum); + this.Value = clamp + ? Math.Clamp(value, Minimum, Maximum) + : value; } } diff --git a/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs b/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs index 040e12d..a75620a 100644 --- a/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs +++ b/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs @@ -25,4 +25,32 @@ public void FromName_MapsNamesToExpectedKinds(string itemName, Type expectedType result.Should().BeOfType(expectedType); } -} \ No newline at end of file + + [Theory] + [InlineData("aged brie")] + [InlineData("AGED BRIE")] + [InlineData("sulfuras, hand of ragnaros")] + [InlineData("backstage")] + [InlineData(" Aged Brie")] + [InlineData("Aged Brie ")] + [InlineData("\tSulfuras, Hand of Ragnaros\n")] + [InlineData("")] + [InlineData(" ")] + public void FromName_NamesDifferingInCaseOrWhitespaceOrEmpty_MapToStandard(string itemName) + { + var result = ItemKind.FromName(itemName); + + result.Should().BeOfType(); + } + + [Fact] + public void FromName_NullName_MapsToStandardWithoutThrowing() + { + ItemKind result = null; + + var act = () => result = ItemKind.FromName(null); + + act.Should().NotThrow(); + result.Should().BeOfType(); + } +} diff --git a/tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs b/tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs index 55d645c..bf3e8ff 100644 --- a/tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs +++ b/tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs @@ -17,6 +17,7 @@ public class ItemMaturingPolicyTests [InlineData(10, 5, 9, 4)] [InlineData(2, 0, 0, -1)] [InlineData(0, 0, 0, -1)] + [InlineData(10, -1, 8, -2)] public void Mature_WhenKindIsStandard_FollowsExpectedRules(int quality, int sellIn, int expectedQuality, int expectedSellIn) { var state = new ItemState(new Quality(quality), new SellIn(sellIn)); @@ -33,6 +34,7 @@ public void Mature_WhenKindIsStandard_FollowsExpectedRules(int quality, int sell [InlineData(49, 0, 50, -1)] [InlineData(50, 4, 50, 3)] [InlineData(50, 0, 50, -1)] + [InlineData(20, -1, 22, -2)] public void Mature_WhenKindIsAgedBrie_FollowsExpectedRules(int quality, int sellIn, int expectedQuality, int expectedSellIn) { var state = new ItemState(new Quality(quality), new SellIn(sellIn)); @@ -55,6 +57,9 @@ public void Mature_WhenKindIsAgedBrie_FollowsExpectedRules(int quality, int sell [InlineData(49, 10, 50, 9)] [InlineData(49, 15, 50, 14)] [InlineData(50, 5, 50, 4)] + [InlineData(48, 5, 50, 4)] + [InlineData(50, 0, 0, -1)] + [InlineData(20, -1, 0, -2)] public void Mature_WhenKindIsBackstage_FollowsExpectedRules(int quality, int sellIn, int expectedQuality, int expectedSellIn) { var state = new ItemState(new Quality(quality), new SellIn(sellIn)); @@ -89,4 +94,15 @@ public void Mature_WhenKindIsSulfuras_PreservesProvidedState() result.Should().Be(state); } -} \ No newline at end of file + + [Fact] + public void Mature_WhenKindIsSulfurasWithNonZeroSellIn_PreservesProvidedState() + { + var state = ItemState.Create(new ItemKind.Sulfuras(), 10, 5); + + var result = this.policy.Mature(new ItemKind.Sulfuras(), state); + + result.Should().Be(state); + result.SellIn.Value.Should().Be(5); + } +} diff --git a/tests/Core.Tests/ProposedCode/Domain/ItemStateTests.cs b/tests/Core.Tests/ProposedCode/Domain/ItemStateTests.cs new file mode 100644 index 0000000..51d4e87 --- /dev/null +++ b/tests/Core.Tests/ProposedCode/Domain/ItemStateTests.cs @@ -0,0 +1,45 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) KsiProgramming. All rights reserved. +// +//----------------------------------------------------------------------- + +namespace GildedRoseKata.ProposedCode.Tests; + +using FluentAssertions; + +public class ItemStateTests +{ + [Theory] + [InlineData(12)] + [InlineData(80)] + public void Create_WhenKindIsSulfuras_UsesLegendaryQuality(int quality) + { + var state = ItemState.Create(new ItemKind.Sulfuras(), quality, 0); + + state.Quality.Value.Should().Be(ItemState.LegendaryQuality); + state.Quality.Value.Should().Be(80); + } + + [Theory] + [InlineData(99, 50)] + [InlineData(-5, 0)] + [InlineData(20, 20)] + public void Create_WhenKindIsNotLegendary_ClampsQuality(int quality, int expected) + { + var state = ItemState.Create(new ItemKind.Standard(), quality, 5); + + state.Quality.Value.Should().Be(expected); + } + + [Theory] + [InlineData(10)] + [InlineData(0)] + [InlineData(-3)] + public void Create_PassesSellInThrough(int sellIn) + { + var state = ItemState.Create(new ItemKind.Standard(), 10, sellIn); + + state.SellIn.Value.Should().Be(sellIn); + } +} diff --git a/tests/Core.Tests/ProposedCode/Domain/QualityTests.cs b/tests/Core.Tests/ProposedCode/Domain/QualityTests.cs index 9c5322a..20c0d6e 100644 --- a/tests/Core.Tests/ProposedCode/Domain/QualityTests.cs +++ b/tests/Core.Tests/ProposedCode/Domain/QualityTests.cs @@ -6,6 +6,7 @@ namespace GildedRoseKata.ProposedCode.Tests; +using System; using FluentAssertions; public class QualityTests @@ -50,18 +51,30 @@ public void Decrease_RespectsMinimum(int initialValue, int amount, int expected) } [Fact] - public void Reset_ReturnsZero() + public void Increase_WhenAmountIsNegative_Throws() { - var result = new Quality(10).Reset(); + var quality = new Quality(10); - result.Value.Should().Be(0); + var act = () => quality.Increase(-1); + + act.Should().Throw(); } [Fact] - public void FromItemKind_WhenKindIsSulfuras_UsesLegendaryQuality() + public void Decrease_WhenAmountIsNegative_Throws() { - var result = Quality.FromItemKind(new ItemKind.Sulfuras(), 12); + var quality = new Quality(10); + + var act = () => quality.Decrease(-1); + + act.Should().Throw(); + } - result.Value.Should().Be(Quality.Legendary); + [Fact] + public void Reset_ReturnsZero() + { + var result = new Quality(10).Reset(); + + result.Value.Should().Be(0); } -} \ No newline at end of file +} diff --git a/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs b/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs index 4690ec6..20c1d0a 100644 --- a/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs +++ b/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs @@ -20,12 +20,19 @@ public class ItemMaturationServiceTests [InlineData("Conjured Mana Cake", 10, 5, 8, 4)] [InlineData("Conjured", 10, 0, 6, -1)] [InlineData("Sulfuras, Hand of Ragnaros", 80, 0, 80, 0)] - public void Mature_UsesExpectedRulesForResolvedItemKinds(string name, int quality, int sellIn, int expectedQuality, int expectedSellIn) + [InlineData("Sulfuras, Hand of Ragnaros", 10, 3, 80, 3)] + [InlineData("Backstage", 20, -1, 0, -2)] + [InlineData("Elixir of the Mongoose", 10, -1, 8, -2)] + [InlineData("Elixir of the Mongoose", 60, 5, 49, 4)] + [InlineData("Elixir of the Mongoose", -5, 5, 0, 4)] + [InlineData("", 10, 5, 9, 4)] + [InlineData(null, 10, 5, 9, 4)] + public void Mature_UsesExpectedRulesForResolvedItemKinds(string? name, int quality, int sellIn, int expectedQuality, int expectedSellIn) { var service = new ItemMaturationService(); var item = new Item { - Name = name, + Name = name!, Quality = quality, SellIn = sellIn, }; @@ -35,4 +42,89 @@ public void Mature_UsesExpectedRulesForResolvedItemKinds(string name, int qualit result.Quality.Should().Be(expectedQuality); result.SellIn.Should().Be(expectedSellIn); } -} \ No newline at end of file + + [Theory] + [InlineData("Elixir of the Mongoose")] + [InlineData("Aged Brie")] + [InlineData("Sulfuras, Hand of Ragnaros")] + [InlineData("A custom item name")] + [InlineData("")] + [InlineData(null)] + public void Mature_PreservesName(string? name) + { + var service = new ItemMaturationService(); + var item = new Item { Name = name!, Quality = 10, SellIn = 5 }; + + var result = service.Mature(item); + + result.Name.Should().Be(name); + } + + [Fact] + public void Mature_DoesNotMutateInputItem() + { + var service = new ItemMaturationService(); + var item = new Item { Name = "Aged Brie", Quality = 10, SellIn = 2 }; + + var result = service.Mature(item); + + result.Should().NotBeSameAs(item); + item.Name.Should().Be("Aged Brie"); + item.Quality.Should().Be(10); + item.SellIn.Should().Be(2); + } + + [Fact] + public void Mature_NullItem_ThrowsNullReferenceException() + { + var service = new ItemMaturationService(); + + var act = () => service.Mature(null!); + + act.Should().Throw(); + } + + [Fact] + public void Mature_WithInjectedPolicy_PassesResolvedKindAndStateAndUsesReturnedState() + { + var returnedState = new ItemState(new Quality(33), new SellIn(7)); + var policy = new StubMaturingPolicy(returnedState); + var service = new ItemMaturationService(policy); + var item = new Item { Name = "Aged Brie", Quality = 10, SellIn = 2 }; + + var result = service.Mature(item); + + policy.CallCount.Should().Be(1); + policy.ReceivedKind.Should().BeOfType(); + policy.ReceivedState.Quality.Value.Should().Be(10); + policy.ReceivedState.SellIn.Value.Should().Be(2); + result.Name.Should().Be("Aged Brie"); + result.Quality.Should().Be(33); + result.SellIn.Should().Be(7); + } + + private sealed class StubMaturingPolicy : IMaturingPolicy + { + private readonly ItemState stateToReturn; + + public StubMaturingPolicy(ItemState stateToReturn) + { + this.stateToReturn = stateToReturn; + } + + public int CallCount { get; private set; } + + public ItemKind? ReceivedKind { get; private set; } + + public ItemState ReceivedState { get; private set; } + + public ItemState Mature(ItemKind kind, ItemState state) + { + this.CallCount++; + this.ReceivedKind = kind; + this.ReceivedState = state; + + return this.stateToReturn; + } + } +}