diff --git a/specs/KAN-8-add-a-new-product-to-gilded-rose/plan.md b/specs/KAN-8-add-a-new-product-to-gilded-rose/plan.md new file mode 100644 index 0000000..ee26e90 --- /dev/null +++ b/specs/KAN-8-add-a-new-product-to-gilded-rose/plan.md @@ -0,0 +1,86 @@ +# Implementation plan: Add Mature Cheese product (KAN-8) + +## Technical context +- Language and runtime: C# on .NET 9 (CI uses `dotnet-version: 9.0.x`). The solution is `GildedRoseKata.sln`. +- Source project: `src/Core/GildedRoseKata.Core.csproj`. + - Legacy code is in `src/Core/OriginalCode` (`Item`, `UpdateQualityService`). **It must not be touched** (FR-013). + - The new code is in `src/Core/ProposedCode`. +- Existing design in the proposed code: + - `ItemMaturationService.Mature(Item)` calls `ItemKind.FromName(name)`. + - It then calls `ItemState.Create(kind, quality, sellIn)`, which builds `Quality` and `SellIn`. + - It then calls `IMaturingPolicy.Mature(kind, state)`. The implementation is `ItemMaturingPolicy`, which has one private method per kind. + - It returns a new `Item`. +- `Quality` is a value type whose constructor clamps to 0–50. This is the existing policy for invalid legacy data. `Increase(n)` and `Decrease(n)` clamp too. Sulfuras is the only kind pinned to 80. +- `SellIn` has `NextDay()` and `IsExpired` (`Value < 0`). +- `ItemKind` is an abstract record with one nested sealed record per kind. `FromName` is an exact, case-sensitive `switch` on the name, and unknown names map to `Standard`. +- Tests: xUnit 2.9.3, FluentAssertions 8.10, coverlet. The project is `tests/Core.Tests`. + - Test files mirror the source layout. + - They use `[Theory]`/`[InlineData]` and `Method_WhenCondition_Expected` names. + - The namespace is `GildedRoseKata.ProposedCode.Tests`, and files carry the copyright header. +- CI (`.github/workflows/build.yml`) runs `dotnet restore`, `dotnet build --no-restore` and `dotnet test --no-build`. + +## Approach +Add Mature Cheese as a sixth `ItemKind`, with its own isolated method in `ItemMaturingPolicy`, following the existing Aged Brie pattern. No other rule is touched. + +1. **`ItemKind`** + - Add `public sealed record MatureCheese : ItemKind;`. + - Add the arm `"Mature Cheese" => new MatureCheese()` to `FromName`. + - Matching stays exact and case-sensitive, so `mature cheese` and `Mature Cheese Deluxe` fall through to `Standard` (FR-001, FR-009). +2. **`ItemMaturingPolicy`** + - Add the case `ItemKind.MatureCheese => this.MatureMatureCheese(state)` to the switch. The method name can be `MatureCheese`, if that avoids the awkward repetition. + - The rule uses the sellIn from before the decrement (FR-005): + - `increase = state.SellIn.Value > 0 ? 1 : 2` + - `newQuality = state.Quality.Increase(increase)`, which clamps to 50 (FR-006, FR-007) + - `newSellIn = state.SellIn.NextDay()` (FR-002) + - Return `new(newQuality, newSellIn)`. + - Alternative: mirror Aged Brie with `Increase()` and then a second `Increase()` if `newSellIn.IsExpired`. This is equivalent, because `sellIn <= 0` before the update is the same as `newSellIn < 0`. The explicit "before decrement" form is clearer against the spec. To avoid adding a `SellIn` member, read `state.SellIn.Value` or use `!state.SellIn.IsBelow(1)`. +3. **Negative or invalid input quality (FR-008)** + - `ItemState.Create` already builds `Quality` through the clamping constructor. A loaded quality of -5 becomes 0 before the rule runs, so the result is 1 or 2. + - No change is needed in `Quality`, `SellIn`, `ItemState`, `IMaturingPolicy` or `ItemMaturationService`. +4. **FR-011 and FR-012 (no mutation outside update, determinism)** + - `Mature` returns a new `Item` and does not modify its input. The policy is pure. + - These requirements are satisfied by the existing design, and tests will confirm it. + +## Files +- `src/Core/ProposedCode/Domain/ItemKind.cs`: add the `MatureCheese` record and the `"Mature Cheese"` mapping. +- `src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs`: add the switch case and the private Mature Cheese method. +- `tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs`: add the `"Mature Cheese"` → `MatureCheese` case, plus `"mature cheese"`, `"Mature Cheese Deluxe"` and `"MatureCheese"` → `Standard`. +- `tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs`: add a `Mature_WhenKindIsMatureCheese_FollowsExpectedRules` theory, and the consecutive-updates and determinism tests. +- `tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs`: add the end-to-end cases, mixed-inventory, multiple-item, similar-name, negative-quality and no-mutation tests. +- No changes under `src/Core/OriginalCode`. No new packages. No README or CI changes. + +## Risks +- **Shared switch in `ItemMaturingPolicy` and `ItemKind.FromName`.** + - A wrong arm order or a wildcard could change other kinds. + - Mitigation: add only new arms before the default arm, and keep the existing tests unchanged (SC-003). +- **Public surface.** `ItemKind` gains a new nested type. Any external exhaustive `switch` on it would fall to its default arm, and the repository has no such code. +- **Off-by-one on the sell-by boundary.** The case `sellIn = 0` must give +2 (BR-03). Tests cover sellIn 1, 0 and -1. +- **Clamping.** Quality must stay in 0–50. This is handled by the existing `Quality` clamp, and tests cover 0, 1, 49, 50 and negative input. +- **OriginalCode.** FR-013 is verified by checking that the diff touches nothing under `src/Core/OriginalCode`. +- No migrations, persistence or UI are involved. + +## Test plan +Test names follow the repository's `Method_WhenX_Y` style. The issue's suggested names serve as guidance for what each test covers. + +| Requirement | Test | +|---|---| +| FR-001 | `ItemKindTests`: `"Mature Cheese"` maps to `ItemKind.MatureCheese`. A service-level test confirms the Mature Cheese rule is applied (AC-009). | +| FR-002 | `ItemMaturingPolicyTests`, Mature Cheese theory: sellIn 5→4, 1→0, 0→-1, -1→-2. | +| FR-003 | Policy theory: (q10, s5)→11, (q10, s1)→11, (q0, s5)→1, (q1, s5)→2. | +| FR-004 | Policy theory: (q10, s0)→12, (q10, s-1)→12, (q0, s0)→2. | +| FR-005 | Boundary rows sellIn 1 (+1) and 0 (+2) in the policy theory and the service theory. | +| FR-006 | Policy theory: (q49, s5)→50, (q49, s0)→50, (q48, s0)→50. | +| FR-007 | Policy theory: (q50, s5)→50 and (q50, s0)→50. | +| FR-008 | Service test: Mature Cheese with quality -5 and with quality above 50 gives a result in 0–50. A loop over several updates asserts quality stays in range (SC-006). | +| FR-009 | `ItemKindTests` and a service test: `mature cheese`, `Mature Cheese Deluxe` and `MatureCheese` follow the standard rule (for example q10, s5 → q9, s4). | +| FR-010 | The existing theories stay unchanged. A new mixed-inventory test mixes Mature Cheese with Aged Brie, Backstage, Sulfuras, Conjured and a standard item, and compares the non-cheese results with expected values and with `ItemMaturationService` results. | +| FR-011 | Service test: the input `Item` is not modified by `Mature`, and creating the item and reading its fields leaves quality and sellIn unchanged. | +| FR-012 | Service test: two updates from the same start give identical results. | +| FR-013 | Review `git diff --stat` and confirm there are no paths under `src/Core/OriginalCode`. | +| FR-014 | All the tests above. Also covered: consecutive updates (for example q10, s1 over 3 days → q11, q13, q15) and several Mature Cheese items in one inventory. | + +Command from the repository root: +``` +dotnet restore && dotnet build --no-restore && dotnet test --no-build --verbosity normal +``` +To run only the new tests, use `dotnet test --filter "FullyQualifiedName~MatureCheese"`. \ No newline at end of file diff --git a/specs/KAN-8-add-a-new-product-to-gilded-rose/spec.md b/specs/KAN-8-add-a-new-product-to-gilded-rose/spec.md new file mode 100644 index 0000000..4961eb8 --- /dev/null +++ b/specs/KAN-8-add-a-new-product-to-gilded-rose/spec.md @@ -0,0 +1,87 @@ +# Feature specification: Add a New Product to Gilded Rose (Mature Cheese) + +Issue: KAN-8 + +UI work: no + +## User scenarios + +The change applies only to the proposed code (`src/Core/ProposedCode`). The original code (`src/Core/OriginalCode`) stays as it is. The repository has no user interface. The update runs through `ItemMaturationService.Mature`, which today sorts items by name into Standard, Aged Brie, Backstage, Conjured and Sulfuras, and clamps quality to 0–50 (Sulfuras stays at 80). + +### Scenario 1: Cheese improves before its sell-by date +A shopkeeper stocks Mature Cheese and runs the daily update. They want its quality to rise as it ages. +- Given a `Mature Cheese` item with sellIn 5 and quality 10, when the daily update runs, then sellIn is 4 and quality is 11. +- Given a `Mature Cheese` item with sellIn 1 and quality 10, when the daily update runs, then sellIn is 0 and quality is 11. + +### Scenario 2: Cheese improves faster once the sell-by date is reached or passed +- Given a `Mature Cheese` item with sellIn 0 and quality 10, when the daily update runs, then sellIn is -1 and quality is 12. +- Given a `Mature Cheese` item with sellIn -1 and quality 10, when the daily update runs, then sellIn is -2 and quality is 12. + +### Scenario 3: Quality never goes above 50 +- Given a `Mature Cheese` item with sellIn 5 and quality 49, when the daily update runs, then quality is 50. +- Given a `Mature Cheese` item with sellIn 0 and quality 49, when the daily update runs, then sellIn is -1 and quality is 50. +- Given a `Mature Cheese` item with quality 50, when the daily update runs, then quality stays 50. + +### Scenario 4: Quality never goes below 0 +- Given a `Mature Cheese` item loaded with a negative quality, when the daily update runs, then the resulting quality is between 0 and 50. + +### Scenario 5: Other products behave as before +- Given an inventory with Mature Cheese, Aged Brie, Backstage passes, Sulfuras, Conjured and standard items, when the daily update runs, then each non-Mature-Cheese item ends with the same sellIn and quality it had before this change. +- Given an item whose name is similar to but not exactly `Mature Cheese` (for example `mature cheese` or `Mature Cheese Deluxe`), when the daily update runs, then it follows the standard-item rule. + +### Scenario 6: Only the update changes values +- Given a `Mature Cheese` item, when it is created, read or displayed, then its quality and sellIn do not change. +- Given several Mature Cheese items in one inventory, when the daily update runs several days in a row, then each item follows the rules independently and the same start always gives the same result. + +## Requirements + +- FR-001: The system shall recognise the exact product name `Mature Cheese` as a new product type with its own maturing rule. +- FR-002: The system shall decrease the sellIn of a Mature Cheese item by 1 at each update. +- FR-003: When sellIn is greater than 0 at the start of the update, the system shall increase Mature Cheese quality by 1. +- FR-004: When sellIn is 0 or less at the start of the update, the system shall increase Mature Cheese quality by 2. +- FR-005: The system shall decide the quality increase from the sellIn value before it is decremented. +- FR-006: The system shall never store a Mature Cheese quality above 50. If an increase would take it above 50, it shall store 50. +- FR-007: A Mature Cheese item already at quality 50 shall stay at quality 50. +- FR-008: The system shall never leave a Mature Cheese item with quality below 0, even if the item was loaded with invalid legacy data. Invalid quality shall be handled by the application's existing policy, which brings it into the range 0–50 before the rule is applied. +- FR-009: The new behaviour shall apply only to the exact name `Mature Cheese`. Other names shall follow their existing rules. +- FR-010: Existing products (Aged Brie, Backstage passes, Sulfuras, Conjured and standard items) shall keep their current behaviour and outputs. +- FR-011: The system shall not change the quality or sellIn of a Mature Cheese item except during the update operation. +- FR-012: The same starting Mature Cheese item shall always give the same result after an update. +- FR-013: The change shall be made only in the proposed code. The original code shall not be modified. +- FR-014: The Mature Cheese rule shall be isolated from the other product rules and covered by automated unit tests. These shall include the acceptance criteria, edge cases (quality 0, 1, 49 and 50; sellIn above, at and below 0), consecutive updates, multiple items, mixed inventories and similar names. + +## Success criteria + +- SC-001: All six example scenarios of the issue produce the stated ending sellIn and quality in automated tests. +- SC-002: Automated tests cover quality 0, 1, 49 and 50 and sellIn above, at and below 0, and pass. +- SC-003: Existing tests for all other products pass unchanged. +- SC-004: A mixed inventory test shows non-Mature-Cheese items give the same results as before. +- SC-005: A diff of the change shows no edits under `src/Core/OriginalCode`. +- SC-006: Across any sequence of updates, a Mature Cheese quality is never outside 0–50. + +## Acceptance criteria + +- AC-001: When a `Mature Cheese` item with sellIn 5 and quality 10 is updated once, the system shall give sellIn 4 and quality 11. (FR-002, FR-003) +- AC-002: When a `Mature Cheese` item with sellIn 1 and quality 10 is updated once, the system shall give sellIn 0 and quality 11. (FR-003, FR-005) +- AC-003: When a `Mature Cheese` item with sellIn 0 and quality 10 is updated once, the system shall give sellIn -1 and quality 12. (FR-004, FR-005) +- AC-004: When a `Mature Cheese` item with sellIn -1 and quality 10 is updated once, the system shall give sellIn -2 and quality 12. (FR-004) +- AC-005: When a `Mature Cheese` item with sellIn 5 and quality 49 is updated, the system shall give quality 50. (FR-006) +- AC-006: When a `Mature Cheese` item with sellIn 0 and quality 49 is updated, the system shall give quality 50 and sellIn -1. (FR-006) +- AC-007: When a `Mature Cheese` item at quality 50 is updated, the system shall keep quality at 50. (FR-007) +- AC-008: When a `Mature Cheese` item with a negative quality is updated, the system shall give a quality between 0 and 50. (FR-008) +- AC-009: When an item named exactly `Mature Cheese` is updated, the system shall apply the Mature Cheese rule. (FR-001) +- AC-010: When an item with a similar but not exact name such as `mature cheese` or `Mature Cheese Deluxe` is updated, the system shall apply the standard-item rule. (FR-009) +- AC-011: When Aged Brie, Backstage passes, Sulfuras, Conjured and standard items are updated, the system shall give the same sellIn and quality as before this change. (FR-010) +- AC-012: When a Mature Cheese item is created or read, the system shall leave its quality and sellIn unchanged. (FR-011) +- AC-013: When the same Mature Cheese item is updated twice from the same start, the system shall give identical results. (FR-012) +- AC-014: When the change is reviewed, the system shall show no modification to the files under `src/Core/OriginalCode`. (FR-013) +- AC-015: When the unit tests are run, the system shall include passing tests for the Mature Cheese rule, the edge cases and the mixed inventory. (FR-014) + +## Out of scope + +- Any change to `src/Core/OriginalCode`. +- Changes to the rules of Aged Brie, Backstage passes, Sulfuras, Conjured or standard items. +- Special Mature Cheese behaviour such as becoming unsafe or losing value after a maximum age. +- Rejecting invalid imported data. The existing policy of bringing quality into 0–50 is kept. +- Name aliases, case-insensitive matching or other spellings of `Mature Cheese`. +- Any user interface, persistence or import feature. \ No newline at end of file diff --git a/specs/KAN-8-add-a-new-product-to-gilded-rose/tasks.md b/specs/KAN-8-add-a-new-product-to-gilded-rose/tasks.md new file mode 100644 index 0000000..45ce504 --- /dev/null +++ b/specs/KAN-8-add-a-new-product-to-gilded-rose/tasks.md @@ -0,0 +1,38 @@ +# Tasks: Add Mature Cheese product (KAN-8) + +All changes go under `src/Core/ProposedCode` and `tests/Core.Tests`. Nothing under `src/Core/OriginalCode` is edited (FR-013). + +- [ ] T001 [P] Add the `"Mature Cheese"` → `ItemKind.MatureCheese` case to `tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs`. Add `"mature cheese"`, `"Mature Cheese Deluxe"` and `"MatureCheese"` → `ItemKind.Standard`. Use the existing `[Theory]`/`[InlineData]` style and keep the existing cases unchanged. (FR-001, FR-009, AC-009, AC-010) +- [ ] T002 [P] Add a `Mature_WhenKindIsMatureCheese_FollowsExpectedRules` theory to `tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs`, with these rows (quality, sellIn → quality, sellIn): + - (10, 5 → 11, 4) + - (10, 1 → 11, 0) + - (10, 0 → 12, -1) + - (10, -1 → 12, -2) + - (0, 5 → 1) + - (1, 5 → 2) + - (0, 0 → 2) + - (48, 0 → 50) + - (49, 5 → 50) + - (49, 0 → 50, -1) + - (50, 5 → 50) + - (50, 0 → 50) + + Also add a consecutive-updates test (q10, s1 over 3 days gives q11, q13, q15) and a determinism test (two updates from the same start give identical results). Leave the existing tests unchanged. (FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-012, FR-014, AC-001, AC-002, AC-003, AC-004, AC-005, AC-006, AC-007, AC-013, AC-015) +- [ ] T003 [P] Add end-to-end `ItemMaturationService.Mature` tests to `tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs`: + - The acceptance rows for `Mature Cheese`. + - Negative quality (-5) and quality above 50 give a result in 0–50. A multi-day loop keeps quality in 0–50 (SC-006). + - Similar names (`mature cheese`, `Mature Cheese Deluxe`, `MatureCheese`) follow the standard rule (q10, s5 → q9, s4). + - A mixed inventory with Mature Cheese, Aged Brie, Backstage, Sulfuras, Conjured and a standard item. The non-cheese items match their expected values and the current service output. + - Several Mature Cheese items run over several days, each following the rules independently. + - `Mature` does not modify the input `Item`, and creating and reading a Mature Cheese item leaves its quality and sellIn unchanged. + + Leave the existing tests unchanged. (FR-001, FR-005, FR-008, FR-009, FR-010, FR-011, FR-012, FR-014, AC-008, AC-009, AC-010, AC-011, AC-012, AC-013, AC-015) +- [ ] T004 [P] Add `public sealed record MatureCheese : ItemKind;` and the arm `"Mature Cheese" => new MatureCheese()` to `FromName` in `src/Core/ProposedCode/Domain/ItemKind.cs`. Put the new arm before the `_ => new Standard()` arm and leave the existing arms unchanged. (FR-001, FR-009, FR-010, FR-013, AC-009, AC-010) +- [ ] T005 Add the case `ItemKind.MatureCheese => this.MatureCheese(state)` and a private `MatureCheese(ItemState)` method in `src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs`, after T004. The method: + - reads the increase from the pre-decrement sellIn: `state.SellIn.Value > 0 ? 1 : 2`; + - sets `newQuality = state.Quality.Increase(increase)`, which clamps to 50; + - sets `newSellIn = state.SellIn.NextDay()`; + - returns `new(newQuality, newSellIn)`. + + Do not edit the other kind methods, `Quality`, `SellIn`, `ItemState`, `IMaturingPolicy` or `ItemMaturationService`. (FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-008, FR-010, FR-011, FR-012, FR-013, FR-014, AC-001, AC-002, AC-003, AC-004, AC-005, AC-006, AC-007, AC-008) +- [ ] T006 Verify after T001–T005 by running `dotnet restore && dotnet build --no-restore && dotnet test --no-build --verbosity normal` from the repository root. All new and existing tests must pass. Then run `git diff --stat` and confirm it lists no path under `src/Core/OriginalCode`. No files change. (FR-010, FR-013, FR-014, SC-001, SC-002, SC-003, SC-004, SC-005, SC-006, AC-011, AC-014, AC-015) \ No newline at end of file diff --git a/src/Core/ProposedCode/Domain/ItemKind.cs b/src/Core/ProposedCode/Domain/ItemKind.cs index 199f351..63245fd 100644 --- a/src/Core/ProposedCode/Domain/ItemKind.cs +++ b/src/Core/ProposedCode/Domain/ItemKind.cs @@ -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 @@ -29,6 +31,7 @@ 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(), }; } diff --git a/src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs b/src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs index 0221621..6596e83 100644 --- a/src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs +++ b/src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs @@ -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.MatureCheese(state), _ => this.MatureStandard(state), }; } @@ -77,4 +78,13 @@ private ItemState MatureConjured(ItemState state) return new(newQuality, newSellIn); } + + private ItemState MatureCheese(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); + } } \ No newline at end of file diff --git a/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs b/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs index 040e12d..7d2f714 100644 --- a/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs +++ b/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs @@ -19,10 +19,14 @@ 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("mature cheese", typeof(ItemKind.Standard))] + [InlineData("Mature Cheese Deluxe", typeof(ItemKind.Standard))] + [InlineData("MatureCheese", typeof(ItemKind.Standard))] public void FromName_MapsNamesToExpectedKinds(string itemName, Type expectedType) { var result = ItemKind.FromName(itemName); result.Should().BeOfType(expectedType); } -} \ No newline at end of file +} diff --git a/tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs b/tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs index 55d645c..162fd7c 100644 --- a/tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs +++ b/tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs @@ -89,4 +89,57 @@ public void Mature_WhenKindIsSulfuras_PreservesProvidedState() result.Should().Be(state); } -} \ No newline at end of file + + [Theory] + [InlineData(10, 5, 11, 4)] + [InlineData(10, 1, 11, 0)] + [InlineData(10, 0, 12, -1)] + [InlineData(10, -1, 12, -2)] + [InlineData(0, 5, 1, 4)] + [InlineData(1, 5, 2, 4)] + [InlineData(0, 0, 2, -1)] + [InlineData(48, 0, 50, -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); + } + + [Fact] + public void Mature_WhenKindIsMatureCheese_ConsecutiveUpdatesFollowExpectedRules() + { + var kind = new ItemKind.MatureCheese(); + var state = new ItemState(new Quality(10), new SellIn(1)); + var expectedQualities = new[] { 11, 13, 15 }; + var expectedSellIns = new[] { 0, -1, -2 }; + + for (var day = 0; day < expectedQualities.Length; day++) + { + state = this.policy.Mature(kind, state); + + state.Quality.Value.Should().Be(expectedQualities[day]); + state.SellIn.Value.Should().Be(expectedSellIns[day]); + } + } + + [Fact] + public void Mature_WhenKindIsMatureCheese_IsDeterministic() + { + var kind = new ItemKind.MatureCheese(); + var state = new ItemState(new Quality(10), new SellIn(0)); + + var first = this.policy.Mature(kind, state); + var second = this.policy.Mature(kind, state); + + second.Quality.Value.Should().Be(first.Quality.Value); + second.SellIn.Value.Should().Be(first.SellIn.Value); + } +} diff --git a/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs b/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs index 4690ec6..bbc4a39 100644 --- a/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs +++ b/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs @@ -6,11 +6,14 @@ namespace GildedRoseKata.ProposedCode.Tests; +using System.Collections.Generic; using FluentAssertions; using GildedRoseKata.OriginalCode; public class ItemMaturationServiceTests { + private const string MatureCheeseName = "Mature Cheese"; + [Theory] [InlineData("Elixir of the Mongoose", 10, 5, 9, 4)] [InlineData("Aged Brie", 10, 2, 11, 1)] @@ -35,4 +38,213 @@ 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(10, 5, 11, 4)] + [InlineData(10, 1, 11, 0)] + [InlineData(10, 0, 12, -1)] + [InlineData(10, -1, 12, -2)] + [InlineData(49, 5, 50, 4)] + [InlineData(49, 0, 50, -1)] + [InlineData(50, 5, 50, 4)] + [InlineData(50, 0, 50, -1)] + public void Mature_MatureCheese_FollowsAcceptanceRules(int quality, int sellIn, int expectedQuality, int expectedSellIn) + { + var service = new ItemMaturationService(); + var item = NewItem(MatureCheeseName, quality, sellIn); + + var result = service.Mature(item); + + result.Name.Should().Be(MatureCheeseName); + result.Quality.Should().Be(expectedQuality); + result.SellIn.Should().Be(expectedSellIn); + } + + [Theory] + [InlineData(-5, 5)] + [InlineData(-5, 0)] + [InlineData(-5, -3)] + [InlineData(60, 5)] + [InlineData(60, 0)] + [InlineData(60, -3)] + public void Mature_MatureCheese_WithInvalidQuality_ReturnsQualityWithinRange(int quality, int sellIn) + { + var service = new ItemMaturationService(); + + var result = service.Mature(NewItem(MatureCheeseName, quality, sellIn)); + + result.Quality.Should().BeInRange(0, 50); + result.SellIn.Should().Be(sellIn - 1); + } + + [Theory] + [InlineData(0, 10)] + [InlineData(10, 5)] + [InlineData(49, 0)] + [InlineData(50, -5)] + [InlineData(-5, 3)] + [InlineData(60, -2)] + public void Mature_MatureCheese_OverManyDays_KeepsQualityWithinRangeAndDecreasesSellInByOne(int quality, int sellIn) + { + var service = new ItemMaturationService(); + var item = NewItem(MatureCheeseName, quality, sellIn); + + for (var day = 1; day <= 60; day++) + { + var previousSellIn = item.SellIn; + + item = service.Mature(item); + + item.Quality.Should().BeInRange(0, 50); + item.SellIn.Should().Be(previousSellIn - 1); + } + + item.SellIn.Should().Be(sellIn - 60); + } + + [Theory] + [InlineData("mature cheese")] + [InlineData("Mature Cheese Deluxe")] + [InlineData("MatureCheese")] + public void Mature_SimilarNames_FollowStandardRule(string name) + { + var service = new ItemMaturationService(); + + var result = service.Mature(NewItem(name, 10, 5)); + + result.Quality.Should().Be(9); + result.SellIn.Should().Be(4); + } + + [Fact] + public void Mature_MixedInventory_AppliesEachProductsOwnRules() + { + var service = new ItemMaturationService(); + var inventory = new List + { + NewItem(MatureCheeseName, 10, 5), + NewItem("Aged Brie", 10, 2), + NewItem("Backstage passes to a TAFKAL80ETC concert", 20, 10), + NewItem("Sulfuras, Hand of Ragnaros", 80, 0), + NewItem("Conjured Mana Cake", 10, 5), + NewItem("Elixir of the Mongoose", 10, 5), + }; + var expected = new[] + { + (Quality: 11, SellIn: 4), + (Quality: 11, SellIn: 1), + (Quality: 22, SellIn: 9), + (Quality: 80, SellIn: 0), + (Quality: 8, SellIn: 4), + (Quality: 9, SellIn: 4), + }; + + var results = new List(); + foreach (var item in inventory) + { + results.Add(service.Mature(item)); + } + + for (var i = 0; i < inventory.Count; i++) + { + results[i].Name.Should().Be(inventory[i].Name); + results[i].Quality.Should().Be(expected[i].Quality, inventory[i].Name); + results[i].SellIn.Should().Be(expected[i].SellIn, inventory[i].Name); + + if (inventory[i].Name != MatureCheeseName) + { + var again = new ItemMaturationService().Mature(NewItem(inventory[i].Name, inventory[i].Quality, inventory[i].SellIn)); + + results[i].Quality.Should().Be(again.Quality, inventory[i].Name); + results[i].SellIn.Should().Be(again.SellIn, inventory[i].Name); + } + } + } + + [Fact] + public void Mature_SeveralMatureCheeseItems_FollowRulesIndependentlyOverSeveralDays() + { + var service = new ItemMaturationService(); + var starts = new[] + { + (Quality: 10, SellIn: 3), + (Quality: 0, SellIn: 1), + (Quality: 45, SellIn: 0), + (Quality: 49, SellIn: 5), + (Quality: 50, SellIn: -2), + (Quality: 20, SellIn: -1), + }; + var items = new List(); + foreach (var start in starts) + { + items.Add(NewItem(MatureCheeseName, start.Quality, start.SellIn)); + } + + var expectedQualities = new int[starts.Length]; + var expectedSellIns = new int[starts.Length]; + for (var i = 0; i < starts.Length; i++) + { + expectedQualities[i] = starts[i].Quality; + expectedSellIns[i] = starts[i].SellIn; + } + + for (var day = 1; day <= 8; day++) + { + for (var i = 0; i < items.Count; i++) + { + var increase = expectedSellIns[i] > 0 ? 1 : 2; + expectedQualities[i] = System.Math.Min(50, expectedQualities[i] + increase); + expectedSellIns[i]--; + + items[i] = service.Mature(items[i]); + + items[i].Quality.Should().Be(expectedQualities[i], $"item {i} on day {day}"); + items[i].SellIn.Should().Be(expectedSellIns[i], $"item {i} on day {day}"); + } + } + } + + [Fact] + public void Mature_DoesNotMutateInputItem_AndReturnsNewInstance() + { + var service = new ItemMaturationService(); + var item = NewItem(MatureCheeseName, 10, 5); + + var result = service.Mature(item); + + item.Name.Should().Be(MatureCheeseName); + item.Quality.Should().Be(10); + item.SellIn.Should().Be(5); + result.Should().NotBeSameAs(item); + result.Quality.Should().Be(11); + result.SellIn.Should().Be(4); + } + + [Fact] + public void CreatingAndReadingMatureCheeseItem_DoesNotChangeQualityOrSellIn() + { + var item = NewItem(MatureCheeseName, 10, 5); + + var name = item.Name; + var quality = item.Quality; + var sellIn = item.SellIn; + var text = item.ToString(); + + name.Should().Be(MatureCheeseName); + quality.Should().Be(10); + sellIn.Should().Be(5); + text.Should().NotBeNull(); + item.Quality.Should().Be(10); + item.SellIn.Should().Be(5); + } + + private static Item NewItem(string name, int quality, int sellIn) + { + return new Item + { + Name = name, + Quality = quality, + SellIn = sellIn, + }; + } +}