From c937dcaf79120cf40314142a51bf8c67efac4f77 Mon Sep 17 00:00:00 2001 From: Alex Ocanoaia Date: Sun, 6 Apr 2025 12:45:17 +0300 Subject: [PATCH 1/2] test/jest/gilded-rose.spec.ts: Add more tests --- test/jest/gilded-rose.spec.ts | 68 +++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/test/jest/gilded-rose.spec.ts b/test/jest/gilded-rose.spec.ts index 613639f..0c80c37 100644 --- a/test/jest/gilded-rose.spec.ts +++ b/test/jest/gilded-rose.spec.ts @@ -9,7 +9,7 @@ describe('Gilded Rose', () => { const items = gildedRose.updateQuality(); // Assert - expect(items[0].name).toBe('bar'); + expect(items[0].name).toBe('foo'); }); it('sword quality drops by 1', () => { @@ -20,6 +20,70 @@ describe('Gilded Rose', () => { const items = gildedRose.updateQuality(); // Assert - expect(items[0].quality).toBe(1); + expect(items[0].quality).toBe(0); }) + + it('the quality should increase with 2', () => { + const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 7, 0)]); + + const items = gildedRose.updateQuality(); + + expect(items[0].quality).toBe(2); + }) + + it('the quality should increase with 1', () => { + const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 13, 0)]); + + const items = gildedRose.updateQuality(); + + expect(items[0].quality).toBe(1); + }) + + + it('the quality should be the same', () => { + const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 3, 51)]); + + const items = gildedRose.updateQuality(); + + expect(items[0].quality).toBe(51); + }) + + + it('the sellIn should be negative an the quality drops with 1', () => { + const gildedRose = new GildedRose([new Item('Hello', 0, 1)]); + + const items = gildedRose.updateQuality(); + + expect(items[0].quality).toBe(0); + expect(items[0].sellIn).toBe(-1); + + }) + + it('the sellIn should drop with 1 and the quality will be equal with 0', () => { + const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 0, 39)]); + + const items = gildedRose.updateQuality(); + + expect(items[0].sellIn).toBe(-1); + expect(items[0].quality).toBe(0); + }) + + it('the sellIn should drop with 1 and the quality will be increased with 2', () => { + const gildedRose = new GildedRose([new Item('Aged Brie', 0, 39)]); + + const items = gildedRose.updateQuality(); + + expect(items[0].sellIn).toBe(-1); + expect(items[0].quality).toBe(41); + }) + + it('the sellIn should drop with 1 and the quality will be decreased with 2', () => { + const gildedRose = new GildedRose([new Item('Hello', 0, 243)]); + + const items = gildedRose.updateQuality(); + + expect(items[0].sellIn).toBe(-1); + expect(items[0].quality).toBe(241); + }) + }); From 0a516b3e2ae177dca61426136ba027a4f6b9ec0a Mon Sep 17 00:00:00 2001 From: Alex Ocanoaia Date: Sun, 6 Apr 2025 12:46:09 +0300 Subject: [PATCH 2/2] app/gilded-rose.ts: Make the code nicer --- app/gilded-rose.ts | 92 ++++++++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 39 deletions(-) diff --git a/app/gilded-rose.ts b/app/gilded-rose.ts index ee55134..4079c69 100644 --- a/app/gilded-rose.ts +++ b/app/gilded-rose.ts @@ -17,53 +17,67 @@ export class GildedRose { this.items = items; } - updateQuality() { - for (let i = 0; i < this.items.length; i++) { - if (this.items[i].name != 'Aged Brie' && this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') { - if (this.items[i].quality > 0) { - if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { - this.items[i].quality = this.items[i].quality - 1 + modifyQualityIfSellInNegative(name: string, quality: number) { + if (name != 'Aged Brie') { + if (name != 'Backstage passes to a TAFKAL80ETC concert') { + if (quality > 0) { + if (name != 'Sulfuras, Hand of Ragnaros') { + quality-- } } } else { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 - if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') { - if (this.items[i].sellIn < 11) { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 - } - } - if (this.items[i].sellIn < 6) { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 - } - } - } - } + quality = 0 } - if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { - this.items[i].sellIn = this.items[i].sellIn - 1; + } else { + if (quality < 50) { + quality++ } - if (this.items[i].sellIn < 0) { - if (this.items[i].name != 'Aged Brie') { - if (this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') { - if (this.items[i].quality > 0) { - if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { - this.items[i].quality = this.items[i].quality - 1 - } - } - } else { - this.items[i].quality = this.items[i].quality - this.items[i].quality - } - } else { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 + } + return quality; + } + + modifyQuality(name: string, sellIn: number ,quality: number) { + quality = quality + 1 + if (name == 'Backstage passes to a TAFKAL80ETC concert') { + if (sellIn < 11) { + if (quality < 50) { + quality++ + } + } + if (sellIn < 6) { + if (quality < 50) { + quality++ + } + } + } + return quality; + } + + updateQuality() { + for (let i = 0; i < this.items.length; i++) { + var name: string = this.items[i].name; + var sellIn: number = this.items[i].sellIn; + var quality: number = this.items[i].quality; + + if (name != 'Aged Brie' && name != 'Backstage passes to a TAFKAL80ETC concert') { + if (quality > 0) { + if (name != 'Sulfuras, Hand of Ragnaros') { + quality-- } } + } else if (quality < 50) { + quality = this.modifyQuality(name, sellIn, quality) } - } + if (name != 'Sulfuras, Hand of Ragnaros') { + sellIn--; + } + if (sellIn < 0) { + quality = this.modifyQualityIfSellInNegative(name, quality) + } + this.items[i].sellIn = sellIn; + this.items[i].quality = quality; + } return this.items; } -} \ No newline at end of file +}