Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions specs/KAN-7-mature-a-new-product/plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Implementation plan: Mature Cheese, a new product with its own maturing rule (KAN-7)

## Technical context
- **Language and runtime:** C# (`LangVersion` latest, nullable enabled, implicit usings). Target framework is `net8.0` from `Directory.Build.props`. CI uses the .NET 9.0.x SDK.
- **Projects:** `src/Core/GildedRoseKata.Core.csproj` holds the code. `tests/Core.Tests/GildedRoseKata.Core.Tests.csproj` holds the tests.
- **Test tools:** xUnit 2.9.3 and FluentAssertions 8.10.0. The tests use `[Theory]` with `[InlineData]`.
- **Analyzers:** Roslynator and SonarAnalyzer run in Debug. `TreatWarningsAsErrors` is false.
- **CI:** `.github/workflows/build.yml` runs `dotnet restore`, `dotnet build --no-restore`, then `dotnet test --no-build`.
- **Code that handles maturing:**
- `ItemKind` is an abstract record with one sealed nested record per kind. Its `FromName(string)` maps names to kinds.
- `ItemMaturingPolicy.Mature(ItemKind, ItemState)` is a `switch` that picks one private method per kind.
- `ItemMaturationService` ties these together.
- `Quality` is clamped to 0–50 by its constructor. Only Sulfuras bypasses this, through `Quality.FromItemKind`.
- `SellIn.NextDay()` and `SellIn.IsExpired` (value < 0) are used after the day has passed.
- `OriginalCode/Item.cs` and `OriginalCode/UpdateQualityService.cs` are out of scope and stay untouched.

## Approach
Add Mature Cheese as a new kind that follows the existing pattern. It needs a new record in `ItemKind`, a name mapping and a policy method.

1. **Kind (FR-001):** add `public sealed record MatureCheese : ItemKind;` to `ItemKind`.
2. **Recognition (FR-002):** add two cases to the `FromName` switch, `"Mature Cheese"` and `"MatureCheese"`. Both return `new MatureCheese()`. They go before the `_ => Standard` default. The match is exact and case-sensitive, like the other names. No other aliases are added.
3. **Rule (FR-003, FR-004, FR-005):**
- Add the case `ItemKind.MatureCheese => this.MatureMatureCheese(state)` to the `ItemMaturingPolicy.Mature` switch.
- Add a private method `MatureMatureCheese` that mirrors `MatureAgedBrie`:
- `newQuality = state.Quality.Increase()`.
- `newSellIn = state.SellIn.NextDay()`.
- If `newSellIn.IsExpired`, apply `newQuality.Increase()` again.
- Return `new(newQuality, newSellIn)`.
- `IsExpired` on the decremented value is true exactly when the old SellIn is 0 or below. That gives +1 per day while SellIn is above 0 and +2 per day when it is 0 or below, which is what the spec asks for.
- `Quality.Increase` clamps to 50. Quality 49 with expired SellIn gives 50, and Quality 50 stays 50. The quality never goes down.
- The method is a separate, own-named rule and doesn't call the Aged Brie method, so the two kinds can change independently (FR-003).
4. **Name unchanged (FR-006):** `ItemMaturationService.Mature` already copies `item.Name`. No change is needed.
5. **Other kinds unchanged (FR-007):** the existing switch arms and methods are not edited. The new arm is added before the `_` default, and no existing name maps differently.
6. `Quality.FromItemKind`, `ItemState`, `SellIn` and `IMaturingPolicy` need no change. Mature Cheese is not a special case like Sulfuras, so it goes through the normal clamped `Quality`.

## Files
- `src/Core/ProposedCode/Domain/ItemKind.cs` (change): add the `MatureCheese` record and the two name mappings.
- `src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs` (change): add the switch arm and the private `MatureMatureCheese` method.
- `tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs` (change): add `InlineData` rows for `"Mature Cheese"` and `"MatureCheese"`. Both map to `ItemKind.MatureCheese`.
- `tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs` (change): add a theory `Mature_WhenKindIsMatureCheese_FollowsExpectedRules`.
- `tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs` (change): add rows for both names, plus a name-preserved check.

No other files are added. Existing test rows are not modified (SC-003).

## Risks
- **Shared switch and name mapping:** `FromName` and `Mature` are used by all kinds. The new arms come before the default, and the existing arms stay as they are. The existing tests would catch a regression.
- **Public interface:** the public surface only grows. `ItemKind` gets a new nested type, and `IMaturingPolicy` and the `Item` class don't change. Any outside code that switches over `ItemKind` exhaustively would fall into its default arm. The search found none in the repo besides `ItemMaturingPolicy` and `Quality.FromItemKind`, and both have defaults.
- **Boundary semantics:** the "SellIn 0 or below" rule relies on `IsExpired` being tested after `NextDay()`. The tests cover SellIn 1, 0 and -1 to guard this.
- **Naming:** the method name `MatureMatureCheese` reads awkwardly. It follows the existing `Mature<Kind>` convention, so keep it for consistency.
- **Migrations and data:** none.

## Test plan
Run `dotnet test` from the repo root. It restores and builds first. CI uses `dotnet restore`, `dotnet build --no-restore` and `dotnet test --no-build`.

| Requirement | Test | Cases |
|---|---|---|
| FR-001, FR-002 | `ItemKindTests.FromName_MapsNamesToExpectedKinds` | `"Mature Cheese"` and `"MatureCheese"` map to `ItemKind.MatureCheese` |
| FR-003 | `ItemMaturingPolicyTests.Mature_WhenKindIsMatureCheese_FollowsExpectedRules` (quality, sellIn → expected quality, expected sellIn) | (10,5→11,4), (10,1→11,0), (10,0→12,-1), (10,-3→12,-4) |
| FR-004 | Same theory, SellIn assertion | Every row asserts SellIn decreases by 1 |
| FR-005 | Same theory, limit rows | (49,5→50,4), (49,0→50,-1), (50,5→50,4), (50,0→50,-1), (0,5→1,4) |
| FR-003 (never decreases) | Same theory, assertions | Result quality ≥ input quality in all rows |
| FR-006 | `ItemMaturationServiceTests` | Both names come back unchanged; the result name equals the input name |
| FR-007, SC-003 | Existing tests in `ItemMaturingPolicyTests`, `ItemKindTests` and `ItemMaturationServiceTests` | All existing rows run unmodified and pass |
| SC-001 | `ItemMaturationServiceTests.Mature_UsesExpectedRulesForResolvedItemKinds` | `"Mature Cheese"` and `"MatureCheese"` with Quality 10 and SellIn 5 give 11 and 4. The existing `"Elixir of the Mongoose"` row gives 9 and 4. |
| SC-002 | The Mature Cheese policy theory | Covers SellIn above 0, SellIn 0, SellIn below 0 and the Quality limit of 50 |
56 changes: 56 additions & 0 deletions specs/KAN-7-mature-a-new-product/spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Feature specification: Mature Cheese, a new product with its own maturing rule

UI work: no

Issue: KAN-7

## User scenarios

### Scenario 1: The inventory recognises Mature Cheese
A shop keeper adds Mature Cheese to the inventory. They want the system to recognise it by name, so it isn't handled like a standard item.
- Given an item named "Mature Cheese", When the system matures the item for one day, Then the system applies the Mature Cheese rule and not the standard rule.
- Given an item named "MatureCheese", When the system matures the item for one day, Then the system applies the Mature Cheese rule and not the standard rule.

### Scenario 2: Mature Cheese improves as it ages
A shop keeper runs the end-of-day update. They want Mature Cheese to gain Quality each day, and to gain it faster once the sell-by date has passed.
- Given a Mature Cheese with SellIn above 0 and Quality below 50, When one day passes, Then SellIn decreases by 1 and Quality increases by 1.
- Given a Mature Cheese with SellIn of 0 or below and Quality below 50, When one day passes, Then SellIn decreases by 1 and Quality increases by 2, up to a maximum of 50.
- Given a Mature Cheese with Quality of 50, When one day passes, Then Quality stays at 50.
- Given a Mature Cheese with Quality of 49 and SellIn of 0 or below, When one day passes, Then Quality becomes 50 and not 51.
- Given a Mature Cheese, When one day passes, Then its Quality never decreases.

### Scenario 3: Existing products are unchanged
A shop keeper keeps selling the existing products. They want nothing about them to change.
- Given a Standard, Aged Brie, Backstage pass, Conjured or Sulfuras item, When one day passes, Then its Quality and SellIn change exactly as they did before this change.

## Requirements

- FR-001: The system shall support a new product kind, Mature Cheese, in addition to Standard, Aged Brie, Backstage, Conjured and Sulfuras.
- FR-002: The system shall recognise an item as Mature Cheese when its name is "Mature Cheese" or "MatureCheese".
- FR-003: The system shall mature Mature Cheese by its own rule, separate from the rules of the other kinds. Quality increases by 1 per day while SellIn is above 0, and by 2 per day when SellIn is 0 or below. Quality never decreases.
- FR-004: When Mature Cheese matures for one day, the system shall decrease its SellIn by 1.
- FR-005: The system shall keep the Quality of Mature Cheese between 0 and 50 after maturing. Quality stops increasing at 50, and Mature Cheese is not a special case like Sulfuras.
- FR-006: The system shall keep the name of Mature Cheese unchanged when it matures.
- FR-007: The system shall mature Standard, Aged Brie, Backstage, Conjured and Sulfuras items exactly as it did before this change.

## Success criteria

- SC-001: A "Mature Cheese" item and a "MatureCheese" item each mature differently from a Standard item with the same Quality and SellIn. For example, with Quality 10 and SellIn 5, Mature Cheese goes to Quality 11 and a Standard item goes to Quality 9.
- SC-002: Automated tests for the Mature Cheese rule pass. They cover SellIn above 0, SellIn of 0, SellIn below 0, and the Quality limit of 50.
- SC-003: All existing tests for the other kinds pass without changes.

## Acceptance criteria

- AC-001: When the name of an item is "Mature Cheese" or "MatureCheese", the system shall treat the item as the Mature Cheese kind (FR-001, FR-002).
- AC-002: When a Mature Cheese with SellIn above 0 and Quality below 50 matures for one day, the system shall return a Quality that is 1 higher than before. When its SellIn is 0 or below and its Quality is below 49, the system shall return a Quality that is 2 higher than before. In both cases the system shall not apply the rule of any other kind (FR-003).
- AC-003: When a Mature Cheese matures for one day, the system shall return a SellIn that is 1 lower than before (FR-004).
- AC-004: When a Mature Cheese matures, the system shall return a Quality of at least 0 and at most 50, including when its Quality is 49 or 50 before maturing (FR-005).
- AC-005: When a Mature Cheese matures, the system shall return an item with the same name (FR-006).
- AC-006: When a Standard, Aged Brie, Backstage, Conjured or Sulfuras item matures, the system shall return the same Quality and SellIn as before this change (FR-007).

## Out of scope

- Changes to the rules of Standard, Aged Brie, Backstage, Conjured and Sulfuras items.
- Changes to the `Item` class or to the original `UpdateQualityService`.
- A user interface, a product catalogue, or a way to define rules at run time.
- Name aliases for Mature Cheese other than "Mature Cheese" and "MatureCheese".
14 changes: 14 additions & 0 deletions specs/KAN-7-mature-a-new-product/tasks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Tasks: Mature Cheese, a new product with its own maturing rule (KAN-7)

- [ ] T001 [P] Add `InlineData` rows `"Mature Cheese"` and `"MatureCheese"`, both expecting `ItemKind.MatureCheese`, to `FromName_MapsNamesToExpectedKinds`. Leave the existing rows unchanged. Edit `tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs` (FR-001, FR-002, AC-001)
- [ ] T002 [P] Add the theory `Mature_WhenKindIsMatureCheese_FollowsExpectedRules` (quality, sellIn → expected quality, expected sellIn) in `tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs`. Leave the existing tests unchanged. (FR-003, FR-004, FR-005, AC-002, AC-003, AC-004)
- Rows: (10,5→11,4), (10,1→11,0), (10,0→12,-1), (10,-3→12,-4), (49,5→50,4), (49,0→50,-1), (50,5→50,4), (50,0→50,-1), (0,5→1,4).
- Every row asserts that SellIn drops by 1 and that the resulting Quality is at least the input Quality.
- [ ] T003 [P] In `tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs`, add `"Mature Cheese"` and `"MatureCheese"` rows (Quality 10, SellIn 5 → Quality 11, SellIn 4) to `Mature_UsesExpectedRulesForResolvedItemKinds`. Also add a test that the returned item keeps the input name for both names. Leave the existing rows unchanged. (FR-002, FR-006, SC-001, AC-001, AC-005)
- [ ] T004 [P] Add `public sealed record MatureCheese : ItemKind;` to `ItemKind`. Add the `"Mature Cheese"` and `"MatureCheese"` cases to the `FromName` switch, both returning `new MatureCheese()` and placed before the `_ => new Standard()` default. Leave the existing arms unchanged. Edit `src/Core/ProposedCode/Domain/ItemKind.cs` (FR-001, FR-002, FR-007, AC-001, AC-006)
- [ ] T005 Add the arm `ItemKind.MatureCheese => this.MatureMatureCheese(state)` before the default in `ItemMaturingPolicy.Mature`. Add the private method `MatureMatureCheese` after T004, in `src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs`. (FR-003, FR-004, FR-005, FR-007, AC-002, AC-003, AC-004, AC-006)
- The method calls `state.Quality.Increase()` and `state.SellIn.NextDay()`.
- If the new SellIn `IsExpired`, it calls `Increase()` once more.
- It returns `new(newQuality, newSellIn)`.
- It does not call `MatureAgedBrie`, and it leaves the existing arms and methods unchanged.
- [ ] T006 Run `dotnet restore`, `dotnet build --no-restore` and `dotnet test --no-build` from the repo root, after T001–T005. Confirm that the new Mature Cheese tests pass and that all existing tests pass unmodified. No files change. (FR-007, SC-002, SC-003, AC-006)
6 changes: 5 additions & 1 deletion src/Core/ProposedCode/Domain/ItemKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public sealed record Conjured : ItemKind;

public sealed record Sulfuras : ItemKind;

public sealed record MatureCheese : ItemKind;

public static ItemKind FromName(string itemName)
{
return itemName switch
Expand All @@ -29,7 +31,9 @@ public static ItemKind FromName(string itemName)
"Conjured Mana Cake" => new Conjured(),
"Conjured" => new Conjured(),
"Sulfuras, Hand of Ragnaros" => new Sulfuras(),
"Mature Cheese" => new MatureCheese(),
"MatureCheese" => new MatureCheese(),
_ => new Standard(),
};
}
}
}
16 changes: 15 additions & 1 deletion src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ItemKind.Backstage => this.MatureBackstage(state),
ItemKind.Conjured => this.MatureConjured(state),
ItemKind.Sulfuras => state,
ItemKind.MatureCheese => this.MatureMatureCheese(state),
_ => this.MatureStandard(state),
};
}
Expand Down Expand Up @@ -77,4 +78,17 @@

return new(newQuality, newSellIn);
}
}

private ItemState MatureMatureCheese(ItemState state)

Check failure on line 82 in src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs

View workflow job for this annotation

GitHub Actions / build / build

Update this method so that its implementation is not identical to 'MatureAgedBrie'.

Check failure on line 82 in src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs

View workflow job for this annotation

GitHub Actions / build / build

Update this method so that its implementation is not identical to 'MatureAgedBrie'.
{
var newQuality = state.Quality.Increase();
var newSellIn = state.SellIn.NextDay();

if (newSellIn.IsExpired)
{
newQuality = newQuality.Increase();
}

return new(newQuality, newSellIn);
}
}
4 changes: 3 additions & 1 deletion tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ public class ItemKindTests
[InlineData("Conjured", typeof(ItemKind.Conjured))]
[InlineData("Sulfuras, Hand of Ragnaros", typeof(ItemKind.Sulfuras))]
[InlineData("Elixir of the Mongoose", typeof(ItemKind.Standard))]
[InlineData("Mature Cheese", typeof(ItemKind.MatureCheese))]
[InlineData("MatureCheese", typeof(ItemKind.MatureCheese))]
public void FromName_MapsNamesToExpectedKinds(string itemName, Type expectedType)
{
var result = ItemKind.FromName(itemName);

result.Should().BeOfType(expectedType);
}
}
}
24 changes: 23 additions & 1 deletion tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ public void Mature_WhenKindIsConjured_FollowsExpectedRules(int quality, int sell
result.SellIn.Value.Should().Be(expectedSellIn);
}

[Theory]
[InlineData(10, 5, 11, 4)]
[InlineData(10, 1, 11, 0)]
[InlineData(10, 0, 12, -1)]
[InlineData(10, -3, 12, -4)]
[InlineData(49, 5, 50, 4)]
[InlineData(49, 0, 50, -1)]
[InlineData(50, 5, 50, 4)]
[InlineData(50, 0, 50, -1)]
[InlineData(0, 5, 1, 4)]
public void Mature_WhenKindIsMatureCheese_FollowsExpectedRules(int quality, int sellIn, int expectedQuality, int expectedSellIn)
{
var state = new ItemState(new Quality(quality), new SellIn(sellIn));

var result = this.policy.Mature(new ItemKind.MatureCheese(), state);

result.Quality.Value.Should().Be(expectedQuality);
result.SellIn.Value.Should().Be(expectedSellIn);
result.SellIn.Value.Should().Be(sellIn - 1);
result.Quality.Value.Should().BeGreaterThanOrEqualTo(quality);
}

[Fact]
public void Mature_WhenKindIsSulfuras_PreservesProvidedState()
{
Expand All @@ -89,4 +111,4 @@ public void Mature_WhenKindIsSulfuras_PreservesProvidedState()

result.Should().Be(state);
}
}
}
22 changes: 21 additions & 1 deletion tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ 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)]
[InlineData("Mature Cheese", 10, 5, 11, 4)]
[InlineData("MatureCheese", 10, 5, 11, 4)]
public void Mature_UsesExpectedRulesForResolvedItemKinds(string name, int quality, int sellIn, int expectedQuality, int expectedSellIn)
{
var service = new ItemMaturationService();
Expand All @@ -35,4 +37,22 @@ public void Mature_UsesExpectedRulesForResolvedItemKinds(string name, int qualit
result.Quality.Should().Be(expectedQuality);
result.SellIn.Should().Be(expectedSellIn);
}
}

[Theory]
[InlineData("Mature Cheese")]
[InlineData("MatureCheese")]
public void Mature_WhenItemIsMatureCheese_KeepsInputName(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);
}
}
Loading