Skip to content
28 changes: 20 additions & 8 deletions src/Core/ProposedCode/Domain/ItemKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@

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.

Check failure on line 19 in src/Core/ProposedCode/Domain/ItemKind.cs

View workflow job for this annotation

GitHub Actions / build / build

Convert comment to documentation comment (https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1181)

Check failure on line 19 in src/Core/ProposedCode/Domain/ItemKind.cs

View workflow job for this annotation

GitHub Actions / build / build

Convert comment to documentation comment (https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1181)
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;
Expand All @@ -22,14 +37,11 @@
{
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(),
};
}
}
}
60 changes: 15 additions & 45 deletions src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemState, Quality> dailyChange,
Func<Quality, Quality> 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);
}
}
}
10 changes: 8 additions & 2 deletions src/Core/ProposedCode/Domain/ItemState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}
}
21 changes: 9 additions & 12 deletions src/Core/ProposedCode/Domain/Quality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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));
Expand All @@ -43,17 +35,22 @@ 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
? throw new ArgumentOutOfRangeException(nameof(amount), "Amount must be non-negative.")
: 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;
}
}
30 changes: 29 additions & 1 deletion tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,32 @@ public void FromName_MapsNamesToExpectedKinds(string itemName, Type expectedType

result.Should().BeOfType(expectedType);
}
}

[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<ItemKind.Standard>();
}

[Fact]
public void FromName_NullName_MapsToStandardWithoutThrowing()
{
ItemKind result = null;

var act = () => result = ItemKind.FromName(null);

act.Should().NotThrow();
result.Should().BeOfType<ItemKind.Standard>();
}
}
18 changes: 17 additions & 1 deletion tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand Down Expand Up @@ -89,4 +94,15 @@ public void Mature_WhenKindIsSulfuras_PreservesProvidedState()

result.Should().Be(state);
}
}

[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);
}
}
45 changes: 45 additions & 0 deletions tests/Core.Tests/ProposedCode/Domain/ItemStateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//-----------------------------------------------------------------------
// <copyright file="ItemStateTests.cs" company="KsiProgramming">
// Copyright (c) KsiProgramming. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

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);
}
}
27 changes: 20 additions & 7 deletions tests/Core.Tests/ProposedCode/Domain/QualityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace GildedRoseKata.ProposedCode.Tests;

using System;
using FluentAssertions;

public class QualityTests
Expand Down Expand Up @@ -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<ArgumentOutOfRangeException>();
}

[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<ArgumentOutOfRangeException>();
}

result.Value.Should().Be(Quality.Legendary);
[Fact]
public void Reset_ReturnsZero()
{
var result = new Quality(10).Reset();

result.Value.Should().Be(0);
}
}
}
Loading
Loading