From 1c364d8136a480628f635dc4b0989e04f8e20593 Mon Sep 17 00:00:00 2001 From: AlanProphettSoftwire Date: Thu, 25 Sep 2025 10:47:50 +0100 Subject: [PATCH 1/4] add unit tests for conjured mana cake case Co-authored-by: tmgls --- .../jest/__snapshots__/approvals.spec.ts.snap | 8 +-- TypeScript/test/jest/gilded-rose.spec.ts | 51 +++++++++++++++---- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/TypeScript/test/jest/__snapshots__/approvals.spec.ts.snap b/TypeScript/test/jest/__snapshots__/approvals.spec.ts.snap index d8351cf..b606f3b 100644 --- a/TypeScript/test/jest/__snapshots__/approvals.spec.ts.snap +++ b/TypeScript/test/jest/__snapshots__/approvals.spec.ts.snap @@ -34,7 +34,7 @@ Sulfuras, Hand of Ragnaros, -1, 80 Backstage passes to a TAFKAL80ETC concert, 14, 21 Backstage passes to a TAFKAL80ETC concert, 9, 50 Backstage passes to a TAFKAL80ETC concert, 4, 50 -Conjured Mana Cake, 2, 5 +Conjured Mana Cake, 2, 4 -------- day 2 -------- name, sellIn, quality @@ -46,7 +46,7 @@ Sulfuras, Hand of Ragnaros, -1, 80 Backstage passes to a TAFKAL80ETC concert, 13, 22 Backstage passes to a TAFKAL80ETC concert, 8, 50 Backstage passes to a TAFKAL80ETC concert, 3, 50 -Conjured Mana Cake, 1, 4 +Conjured Mana Cake, 1, 2 -------- day 3 -------- name, sellIn, quality @@ -58,7 +58,7 @@ Sulfuras, Hand of Ragnaros, -1, 80 Backstage passes to a TAFKAL80ETC concert, 12, 23 Backstage passes to a TAFKAL80ETC concert, 7, 50 Backstage passes to a TAFKAL80ETC concert, 2, 50 -Conjured Mana Cake, 0, 3 +Conjured Mana Cake, 0, 0 -------- day 4 -------- name, sellIn, quality @@ -70,7 +70,7 @@ Sulfuras, Hand of Ragnaros, -1, 80 Backstage passes to a TAFKAL80ETC concert, 11, 24 Backstage passes to a TAFKAL80ETC concert, 6, 50 Backstage passes to a TAFKAL80ETC concert, 1, 50 -Conjured Mana Cake, -1, 1 +Conjured Mana Cake, -1, 0 -------- day 5 -------- name, sellIn, quality diff --git a/TypeScript/test/jest/gilded-rose.spec.ts b/TypeScript/test/jest/gilded-rose.spec.ts index f10420b..b45236a 100644 --- a/TypeScript/test/jest/gilded-rose.spec.ts +++ b/TypeScript/test/jest/gilded-rose.spec.ts @@ -1,14 +1,6 @@ import { Item, GildedRose } from '@/gilded-rose'; -const POSSIBLE_ITEMS: String[] = ['Aged Brie', 'Sulfuras, Hand of Ragnaros', 'Backstage passes to a TAFKAL80ETC concert'] - -// describe('Gilded Rose', () => { -// it('should foo', () => { -// const gildedRose = new GildedRose([new Item('foo', 0, 0)]); -// const items = gildedRose.updateQuality(); -// expect(items[0].name).toBe('foo'); -// }); -// }); +const POSSIBLE_ITEMS: String[] = ['Aged Brie', 'Sulfuras, Hand of Ragnaros', 'Backstage passes to a TAFKAL80ETC concert', 'Conjured Mana Cake'] type expectedValuesAfterUpdateSet = [name: string, sellIn: number, quality: number, expectedSellIn: number, expectedQuality: number]; @@ -18,7 +10,7 @@ it.each([ ['Base Case Item - at zero boundary', 1, 1, 0, 0], //at zero boundary ['Base Case Item - twice the speed', 0, 6, -1, 4], //twice the speed ['Base Case Item - sellIn goes negative', 0, 1, -1, 0], //sellIn goes negative - ['Aged Brie', 15, 5, 14, 6], //Brie increases in + ['Aged Brie', 15, 5, 14, 6], //Brie increases in ['Aged Brie', 0, 5, -1, 7], //Out of date brie twice as good ['Aged Brie', -9, 5, -10, 7], //Long out of date brie twice as good ['Aged Brie', -9, 50, -10, 50], //Long out of date brie twice as good @@ -34,6 +26,9 @@ it.each([ ['Backstage passes to a TAFKAL80ETC concert', 5, 49, 4, 50], // more than 5 days but quality at max ['Backstage passes to a TAFKAL80ETC concert', 1, 49, 0, 50], // more than one day but quality at max ['Backstage passes to a TAFKAL80ETC concert', 0, 49, -1, 0], // day has passed but quality at max + ['Conjured Mana Cake', 3, 6, 2, 4], //Conjured item standard speed + ['Conjured Mana Cake', 0, 6, -1, 2], //Conjured item twice the speed + ['Conjured Mana Cake', 0, 3, -1, 0], //Conjured item twice the speed but not below zero ])('should return %s %i %i %i %i', (name, sellIn, quality, expectedSellIn, expectedQuality) => { const gildedRose = new GildedRose([new Item(name, sellIn, quality)]); const items = gildedRose.updateQuality(); @@ -177,4 +172,40 @@ describe('updateQuality', () => { expect(items[0].sellIn).toBe(-8); expect(items[0].quality).toBe(50); }); + + test('should decrease in sellIn and decrease quality by 2 if item is Conjured Mana Cake', () => { + const gildedRose = new GildedRose([new Item( + 'Conjured Mana Cake', + 3, + 6 + )]); + const items = gildedRose.updateQuality(); + expect(items[0].name).toBe('Conjured Mana Cake'); + expect(items[0].sellIn).toBe(2); + expect(items[0].quality).toBe(4); + }); + + test('should decrease in sellIn and decrease quality by 4 if SellIn negative and item is Conjured Mana Cake', () => { + const gildedRose = new GildedRose([new Item( + 'Conjured Mana Cake', + -1, + 6 + )]); + const items = gildedRose.updateQuality(); + expect(items[0].name).toBe('Conjured Mana Cake'); + expect(items[0].sellIn).toBe(-2); + expect(items[0].quality).toBe(2); + }); + + test('should stop decrease when quality is 0 and item is Conjured Mana Cake', () => { + const gildedRose = new GildedRose([new Item( + 'Conjured Mana Cake', + 3, + 0 + )]); + const items = gildedRose.updateQuality(); + expect(items[0].name).toBe('Conjured Mana Cake'); + expect(items[0].sellIn).toBe(2); + expect(items[0].quality).toBe(0); + }); }); From 975df004612fce60ba8bf4a047ad80a25eb60568 Mon Sep 17 00:00:00 2001 From: AlanProphettSoftwire Date: Thu, 25 Sep 2025 10:48:19 +0100 Subject: [PATCH 2/4] implement logic for conjured mama cake Co-authored-by: tmgls --- TypeScript/app/gilded-rose.ts | 79 +++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/TypeScript/app/gilded-rose.ts b/TypeScript/app/gilded-rose.ts index f25eeaf..a57ff8a 100644 --- a/TypeScript/app/gilded-rose.ts +++ b/TypeScript/app/gilded-rose.ts @@ -25,44 +25,51 @@ export class GildedRose { this.items[i].sellIn = this.items[i].sellIn - 1; - if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') { - if (this.items[i].sellIn < 0) { - this.items[i].quality = 0 - continue; - } + switch (this.items[i].name) { + case 'Backstage passes to a TAFKAL80ETC concert': + if (this.items[i].sellIn < 0) { + this.items[i].quality = 0 + break; + } - if (this.items[i].sellIn >= 10) { - this.items[i].quality = this.items[i].quality + 1 - } - else if (this.items[i].sellIn >= 5) { - this.items[i].quality = this.items[i].quality + 2 - } - else { - this.items[i].quality = this.items[i].quality + 3 - } + if (this.items[i].sellIn >= 10) { + this.items[i].quality = this.items[i].quality + 1 + } + else if (this.items[i].sellIn >= 5) { + this.items[i].quality = this.items[i].quality + 2 + } + else { + this.items[i].quality = this.items[i].quality + 3 + } - if (this.items[i].quality > 50) { - this.items[i].quality = 50 - } - continue; - } - - - if (this.items[i].name === 'Aged Brie') { - if (this.items[i].sellIn < 0) { - this.items[i].quality = Math.min(this.items[i].quality + 2, 50); - } - else { - this.items[i].quality = Math.min(this.items[i].quality + 1, 50); - } - continue; - } - - if (this.items[i].sellIn < 0) { - this.items[i].quality = Math.max(0, this.items[i].quality - 2); - } - else { - this.items[i].quality = Math.max(0, this.items[i].quality - 1); + if (this.items[i].quality > 50) { + this.items[i].quality = 50 + } + break; + case 'Aged Brie': + if (this.items[i].sellIn < 0) { + this.items[i].quality = Math.min(this.items[i].quality + 2, 50); + } + else { + this.items[i].quality = Math.min(this.items[i].quality + 1, 50); + } + break; + case 'Conjured Mana Cake': + if (this.items[i].sellIn < 0) { + this.items[i].quality = Math.max(0, this.items[i].quality - 4); + } + else { + this.items[i].quality = Math.max(0, this.items[i].quality - 2); + } + break; + default: + if (this.items[i].sellIn < 0) { + this.items[i].quality = Math.max(0, this.items[i].quality - 2); + } + else { + this.items[i].quality = Math.max(0, this.items[i].quality - 1); + } + break; } } return this.items; From 9d61329d0a0924e19fe1db78a41f9276dea61470 Mon Sep 17 00:00:00 2001 From: AlanProphettSoftwire Date: Thu, 25 Sep 2025 10:58:32 +0100 Subject: [PATCH 3/4] remove unused list of possible items --- TypeScript/test/jest/gilded-rose.spec.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/TypeScript/test/jest/gilded-rose.spec.ts b/TypeScript/test/jest/gilded-rose.spec.ts index b45236a..61b497a 100644 --- a/TypeScript/test/jest/gilded-rose.spec.ts +++ b/TypeScript/test/jest/gilded-rose.spec.ts @@ -1,7 +1,5 @@ import { Item, GildedRose } from '@/gilded-rose'; -const POSSIBLE_ITEMS: String[] = ['Aged Brie', 'Sulfuras, Hand of Ragnaros', 'Backstage passes to a TAFKAL80ETC concert', 'Conjured Mana Cake'] - type expectedValuesAfterUpdateSet = [name: string, sellIn: number, quality: number, expectedSellIn: number, expectedQuality: number]; it.each([ From 504123d950fb05cefc33be529b15f6461b7ba342 Mon Sep 17 00:00:00 2001 From: AlanProphettSoftwire Date: Thu, 25 Sep 2025 11:04:58 +0100 Subject: [PATCH 4/4] fix code formatting --- TypeScript/app/gilded-rose.ts | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/TypeScript/app/gilded-rose.ts b/TypeScript/app/gilded-rose.ts index a57ff8a..9601dd4 100644 --- a/TypeScript/app/gilded-rose.ts +++ b/TypeScript/app/gilded-rose.ts @@ -19,54 +19,49 @@ export class GildedRose { updateQuality() { for (let i = 0; i < this.items.length; i++) { - if (this.items[i].name === 'Sulfuras, Hand of Ragnaros') { + if (this.items[i].name === "Sulfuras, Hand of Ragnaros") { continue; } this.items[i].sellIn = this.items[i].sellIn - 1; switch (this.items[i].name) { - case 'Backstage passes to a TAFKAL80ETC concert': + case "Backstage passes to a TAFKAL80ETC concert": if (this.items[i].sellIn < 0) { - this.items[i].quality = 0 + this.items[i].quality = 0; break; } if (this.items[i].sellIn >= 10) { - this.items[i].quality = this.items[i].quality + 1 - } - else if (this.items[i].sellIn >= 5) { - this.items[i].quality = this.items[i].quality + 2 - } - else { - this.items[i].quality = this.items[i].quality + 3 + this.items[i].quality = this.items[i].quality + 1; + } else if (this.items[i].sellIn >= 5) { + this.items[i].quality = this.items[i].quality + 2; + } else { + this.items[i].quality = this.items[i].quality + 3; } if (this.items[i].quality > 50) { - this.items[i].quality = 50 + this.items[i].quality = 50; } break; - case 'Aged Brie': + case "Aged Brie": if (this.items[i].sellIn < 0) { this.items[i].quality = Math.min(this.items[i].quality + 2, 50); - } - else { + } else { this.items[i].quality = Math.min(this.items[i].quality + 1, 50); } break; - case 'Conjured Mana Cake': + case "Conjured Mana Cake": if (this.items[i].sellIn < 0) { this.items[i].quality = Math.max(0, this.items[i].quality - 4); - } - else { + } else { this.items[i].quality = Math.max(0, this.items[i].quality - 2); } break; default: if (this.items[i].sellIn < 0) { this.items[i].quality = Math.max(0, this.items[i].quality - 2); - } - else { + } else { this.items[i].quality = Math.max(0, this.items[i].quality - 1); } break;