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
35 changes: 35 additions & 0 deletions specs/add-a-new-product-to-gilded-rose/plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Implementation plan: Add Mature Cheese

## Technical context

The proposed implementation is C# on .NET 8, using xUnit and FluentAssertions. Product classification and maturation rules live in the Proposed Code domain; `Quality` already clamps values to 0–50. CI runs `dotnet test`.

## Approach

Add a `MatureCheese` item kind for the exact name `Mature Cheese`. Handle it in a dedicated policy method: choose a quality increase of 1 when the starting `sellIn` is positive, otherwise 2; let the existing `Quality` value object enforce the 0–50 range; then decrement `sellIn` once. Keep other policy branches unchanged. The service already returns updated item values without mutating the input item.

Use the defined name, increases, and cap as the implementation defaults. Add no behavior beyond the stated post-sell-by increase because no additional maximum-age rule is specified. Do not modify Original Code.

## Files

- `src/Core/ProposedCode/Domain/ItemKind.cs` — add exact-name classification for Mature Cheese.
- `src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs` — add the isolated Mature Cheese update rule.
- `tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs` — test exact classification and similar non-matching names.
- `tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs` — test before, on, and past sell-by behavior; sellIn countdown; quality boundaries and invalid imported values.
- `tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs` — test the returned updated values, multiple and consecutive updates in mixed inventories, unchanged input items, and existing product regressions.

## Risks

- The item kind and policy are shared dispatch points; an incorrect switch branch could alter existing product behavior. Preserve all existing branches and cover their current behavior with regression tests (FR-010).
- `Quality` clamps on state creation as well as when increased. Verify out-of-range values through the service path so tests reflect the application’s normalization policy (FR-008).
- The spec leaves additional maximum-age behavior undecided. Implement only the explicit rule for `sellIn <= 0`; do not add a separate age threshold (FR-014).
- Limit source changes to Proposed Code to preserve Original Code (FR-017).

## Test plan

Run `dotnet test` (the CI test command).

- `ItemKindTests`: verify exact-name classification and near-match exclusion (FR-001, FR-009).
- `ItemMaturingPolicyTests`: verify +1 for positive starting `sellIn`, +2 for zero or negative starting `sellIn`, decrement by one using the starting value, cap at 50, floor at 0, and repeated deterministic updates (FR-002–FR-008, FR-014–FR-016).
- `ItemMaturationServiceTests`: verify returned quality and `sellIn`, multiple matching items and consecutive updates in mixed inventories, and that the input item remains unchanged outside the update result (FR-011–FR-013, FR-015).
- Existing product cases in policy and service tests, plus the full test suite: verify established product behavior remains unchanged and Original Code is untouched (FR-010, FR-017–FR-019).
120 changes: 120 additions & 0 deletions specs/add-a-new-product-to-gilded-rose/spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Feature specification: Add a New Product to Gilded Rose
UI work: no
spec.md

## User scenarios

**Mature Cheese before its sell-by date**
Given a Mature Cheese item with `sellIn` greater than 0, when the inventory is updated, then its quality increases by 1 and its `sellIn` decreases by 1.

**Mature Cheese at or past its sell-by date**
Given a Mature Cheese item with `sellIn` equal to or less than 0 at the start of an update, when the inventory is updated, then its quality increases by 2 and its `sellIn` decreases by 1.

**Quality boundaries and legacy data**
Given Mature Cheese quality at a boundary or outside the valid range, when the inventory is updated, then its quality remains within 0–50.

**Mixed inventory and ordinary access**
Given an inventory containing Mature Cheese and other products, when a daily update runs, then only the exact Mature Cheese product follows these rules, and creating, reading, displaying, or searching for an item does not change it.

## Requirements

FR-001: The system shall recognize the exact product name `Mature Cheese` as an aging food product. [NEEDS CLARIFICATION: Is the official product name `Mature Cheese`, or should it be another exact name?]

FR-002: The system shall support Mature Cheese quality values from 0 through 50, inclusive.

FR-003: When Mature Cheese has `sellIn` greater than 0 at the start of an update, the system shall increase its quality by 1. [NEEDS CLARIFICATION: Should quality increase by 1 before sell-by and 2 afterwards?]

FR-004: When Mature Cheese has `sellIn` equal to or less than 0 at the start of an update, the system shall increase its quality by 2. [NEEDS CLARIFICATION: Should quality increase by 1 before sell-by and 2 afterwards?]

FR-005: At the end of each inventory update, the system shall decrease Mature Cheese `sellIn` by 1.

FR-006: The system shall determine the quality increase using `sellIn` before it is decreased.

FR-007: The system shall cap Mature Cheese quality at 50 after an increase. [NEEDS CLARIFICATION: Is 50 the correct maximum quality?]

FR-008: The system shall keep Mature Cheese quality between 0 and 50, inclusive, including when imported quality is outside that range; invalid values shall follow the proposed code’s existing quality normalization policy.

FR-009: The system shall apply Mature Cheese behavior only to the exact product name; similar but non-exact names shall not receive this behavior.

FR-010: The system shall leave the existing behavior of Aged Brie, Backstage passes, Sulfuras, Conjured items, and other products unchanged.

FR-011: The system shall change a Mature Cheese item’s quality or `sellIn` only during the daily inventory update, not during creation, reading, displaying, or searching.

FR-012: The system shall persist both updated quality and `sellIn` values after each daily inventory update.

FR-013: The system shall apply the rules consistently to multiple Mature Cheese items and across consecutive updates, including in mixed inventories.

FR-014: The system shall follow the approved policy for any additional behavior after a maximum age. [NEEDS CLARIFICATION: Should Mature Cheese have special behavior such as becoming unsafe or losing value after a maximum age?]

FR-015: The system shall produce the same result for the same starting item and update operation.

FR-016: The system shall keep the Mature Cheese behavior isolated and independently testable.

FR-017: The change shall modify only Proposed Code and shall not change Original Code.

FR-018: Automated unit tests shall cover the business rules and regression tests shall confirm existing product behavior is unchanged. Suggested test names are `increases_quality_by_one_before_sell_by`, `increases_quality_by_two_on_sell_by_date`, `increases_quality_by_two_after_sell_by`, `does_not_increase_quality_above_fifty`, `keeps_quality_at_fifty`, `decreases_sell_in_by_one_each_day`, and `does_not_change_existing_product_behaviour`.

FR-019: The change shall avoid unrelated refactoring or changes to unrelated product behavior.

## Success criteria

SC-001: One update produces `sellIn = 4`, `quality = 11` from `sellIn = 5`, `quality = 10`, and `sellIn = 0`, `quality = 11` from `sellIn = 1`, `quality = 10`.

SC-002: One update produces `sellIn = -1`, `quality = 12` from `sellIn = 0`, `quality = 10`, and `sellIn = -2`, `quality = 12` from `sellIn = -1`, `quality = 10`.

SC-003: Quality is 50 after an increase from 49, and remains 50 when it starts at 50; no update leaves quality outside 0–50.

SC-004: Existing product behavior and outputs remain unchanged, and accessing an item outside the daily update does not change its quality or `sellIn`.

## Acceptance criteria

AC-001 — When an item named exactly `Mature Cheese` is classified, the system shall recognize it as the new aging food product (FR-001).

AC-002 — When Mature Cheese is created or its quality is normalized, the system shall keep its quality within 0–50, inclusive (FR-002).

AC-003 — When one update starts with `sellIn = 5`, `quality = 10`, or `sellIn = 1`, `quality = 10`, the system shall increase quality by 1 (FR-003).

AC-004 — When one update starts with `sellIn = 0`, `quality = 10`, or `sellIn = -1`, `quality = 10`, the system shall increase quality by 2 (FR-004).

AC-005 — When a Mature Cheese item is updated once, the system shall decrease its `sellIn` by exactly 1 (FR-005).

AC-006 — When an update starts with `sellIn = 1`, the system shall apply the before-sell-by increase and end with `sellIn = 0` (FR-006).

AC-007 — When an increase would take quality above 50, the system shall store quality as 50 (FR-007).

AC-008 — When an update receives Mature Cheese quality below 0 or above 50, the system shall leave the updated quality within 0–50 (FR-008).

AC-009 — When an item’s name is similar to but not exactly `Mature Cheese`, the system shall not apply the Mature Cheese behavior (FR-009).

AC-010 — When existing products are updated, the system shall retain their previous behavior and outputs (FR-010).

AC-011 — When a Mature Cheese item is created, read, displayed, or searched for without a daily update, the system shall leave its quality and `sellIn` unchanged (FR-011).

AC-012 — When a daily update completes for Mature Cheese, the system shall provide the updated quality and `sellIn` values (FR-012).

AC-013 — When consecutive updates run on multiple Mature Cheese items in a mixed inventory, the system shall apply the Mature Cheese rules to each matching item on each update (FR-013).

AC-014 — When Mature Cheese reaches an age beyond its sell-by date, the system shall apply the approved maximum-age behavior (FR-014).

AC-015 — When the same starting item and update operation are repeated, the system shall produce the same result each time (FR-015).

AC-016 — When Mature Cheese rules are tested independently, the system shall allow those rules to be verified without changing other product behavior (FR-016).

AC-017 — When the feature is delivered, the system shall have changes only in Proposed Code, with Original Code unchanged (FR-017).

AC-018 — When automated tests run, the system shall have unit coverage for the business rules and regression coverage for existing products, using the suggested test names where applicable (FR-018).

AC-019 — When the feature is delivered, the system shall contain no unrelated refactoring or unrelated product behavior changes (FR-019).

## Out of scope

- Changing Original Code.
- Adding user-interface behavior.
- Introducing product behavior beyond the approved Mature Cheese rules.

## Open questions

- [NEEDS CLARIFICATION: Is the official product name `Mature Cheese`, or should it be another exact name?]
- [NEEDS CLARIFICATION: Should quality increase by 1 before sell-by and 2 afterwards?]
- [NEEDS CLARIFICATION: Is 50 the correct maximum quality?]
- [NEEDS CLARIFICATION: Should Mature Cheese have special behavior such as becoming unsafe or losing value after a maximum age?]
7 changes: 7 additions & 0 deletions specs/add-a-new-product-to-gilded-rose/tasks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Tasks: Add Mature Cheese

- [ ] T001 [P] Test exact `Mature Cheese` classification and similar-name exclusion in `tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs` (FR-001, FR-009, AC-001, AC-009)
- [ ] T002 [P] Test Mature Cheese policy behavior for positive, zero, and negative `sellIn`, countdown, quality boundaries, invalid imported values, and repeated updates in `tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs` (FR-002–FR-008, FR-014–FR-016, AC-002–AC-008, AC-014–AC-016)
- [ ] T003 [P] Test returned values, unchanged input items, multiple and consecutive updates in mixed inventories, and existing-product regressions in `tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs` (FR-010–FR-013, FR-015, FR-018, AC-010–AC-013, AC-015, AC-018)
- [ ] T004 [P] Add exact-name Mature Cheese classification in `src/Core/ProposedCode/Domain/ItemKind.cs` (FR-001, FR-009, FR-017, FR-019, AC-001, AC-009, AC-017, AC-019)
- [ ] T005 Add the isolated Mature Cheese maturation rule, using the starting `sellIn` and existing quality normalization, in `src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs` after T004 (FR-002–FR-008, FR-010, FR-012–FR-017, FR-019, AC-002–AC-008, AC-010, AC-012–AC-017, AC-019)
5 changes: 4 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,8 @@ public static ItemKind FromName(string itemName)
"Conjured Mana Cake" => new Conjured(),
"Conjured" => new Conjured(),
"Sulfuras, Hand of Ragnaros" => new Sulfuras(),
"Mature Cheese" => new MatureCheese(),
_ => new Standard(),
};
}
}
}
12 changes: 11 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 @@ public ItemState Mature(ItemKind kind, ItemState state)
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,13 @@ private ItemState MatureConjured(ItemState state)

return new(newQuality, newSellIn);
}
}

private ItemState MatureMatureCheese(ItemState state)
{
var increase = state.SellIn.Value > 0 ? 1 : 2;
var newQuality = state.Quality.Increase(increase);
var newSellIn = state.SellIn.NextDay();

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

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

[Theory]
[InlineData("mature cheese")]
[InlineData("Mature Cheese ")]
[InlineData("Mature Cheeses")]
public void FromName_MapsSimilarMatureCheeseNamesToStandard(string itemName)
{
var result = ItemKind.FromName(itemName);

result.Should().BeOfType<ItemKind.Standard>();
}
}
52 changes: 51 additions & 1 deletion tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,56 @@ public void Mature_WhenKindIsStandard_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, -1, 12, -2)]
[InlineData(0, 0, 2, -1)]
[InlineData(49, 5, 50, 4)]
[InlineData(49, 0, 50, -1)]
[InlineData(50, 5, 50, 4)]
[InlineData(50, 0, 50, -1)]
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.Quality.Value.Should().BeInRange(Quality.Minimum, Quality.Maximum);
}

[Theory]
[InlineData(-5, 0, 2)]
[InlineData(55, 5, 50)]
public void Mature_WhenKindIsMatureCheese_NormalizesInvalidImportedQuality(int importedQuality, int sellIn, int expectedQuality)
{
var state = ItemState.Create(new ItemKind.MatureCheese(), importedQuality, sellIn);

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

result.Quality.Value.Should().Be(expectedQuality);
result.Quality.Value.Should().BeInRange(Quality.Minimum, Quality.Maximum);
}

[Fact]
public void Mature_WhenKindIsMatureCheese_AppliesRulesOnRepeatedUpdates()
{
var state = ItemState.Create(new ItemKind.MatureCheese(), 10, 2);

var firstDay = this.policy.Mature(new ItemKind.MatureCheese(), state);
var secondDay = this.policy.Mature(new ItemKind.MatureCheese(), firstDay);
var thirdDay = this.policy.Mature(new ItemKind.MatureCheese(), secondDay);
var fourthDay = this.policy.Mature(new ItemKind.MatureCheese(), thirdDay);

firstDay.Should().Be(new ItemState(new Quality(11), new SellIn(1)));
secondDay.Should().Be(new ItemState(new Quality(12), new SellIn(0)));
thirdDay.Should().Be(new ItemState(new Quality(14), new SellIn(-1)));
fourthDay.Should().Be(new ItemState(new Quality(16), new SellIn(-2)));
}

[Theory]
[InlineData(20, 4, 21, 3)]
[InlineData(20, 0, 22, -1)]
Expand Down Expand Up @@ -89,4 +139,4 @@ public void Mature_WhenKindIsSulfuras_PreservesProvidedState()

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