From ab6be24de4915b1ac3f8124b7c6550b4b471c94c Mon Sep 17 00:00:00 2001 From: Jesper Date: Wed, 13 Apr 2022 16:25:18 +0200 Subject: [PATCH] Replace loop with .forEach --- TypeScript/app/gilded-rose.ts | 52 +++++++------- TypeScript/test/jest/gilded-rose.spec.ts | 87 +++++++++++++++++++++--- 2 files changed, 105 insertions(+), 34 deletions(-) diff --git a/TypeScript/app/gilded-rose.ts b/TypeScript/app/gilded-rose.ts index db58d678..ce929369 100644 --- a/TypeScript/app/gilded-rose.ts +++ b/TypeScript/app/gilded-rose.ts @@ -18,51 +18,51 @@ export class GildedRose { } 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 + this.items.forEach((item, i) => { + if (item.name != 'Aged Brie' && item.name != 'Backstage passes to a TAFKAL80ETC concert') { + if (item.quality > 0) { + if (item.name != 'Sulfuras, Hand of Ragnaros') { + item.quality = item.quality - 1 } } } 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 (item.quality < 50) { + item.quality = item.quality + 1 + if (item.name == 'Backstage passes to a TAFKAL80ETC concert') { + if (item.sellIn < 11) { + if (item.quality < 50) { + item.quality = item.quality + 1 } } - if (this.items[i].sellIn < 6) { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 + if (item.sellIn < 6) { + if (item.quality < 50) { + item.quality = item.quality + 1 } } } } } - if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { - this.items[i].sellIn = this.items[i].sellIn - 1; + if (item.name != 'Sulfuras, Hand of Ragnaros') { + item.sellIn = item.sellIn - 1; } - 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 + if (item.sellIn < 0) { + if (item.name != 'Aged Brie') { + if (item.name != 'Backstage passes to a TAFKAL80ETC concert') { + if (item.quality > 0) { + if (item.name != 'Sulfuras, Hand of Ragnaros') { + item.quality = item.quality - 1 } } } else { - this.items[i].quality = this.items[i].quality - this.items[i].quality + item.quality = item.quality - item.quality } } else { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 + if (item.quality < 50) { + item.quality = item.quality + 1 } } } - } + }) return this.items; } diff --git a/TypeScript/test/jest/gilded-rose.spec.ts b/TypeScript/test/jest/gilded-rose.spec.ts index 65330750..ead1d129 100644 --- a/TypeScript/test/jest/gilded-rose.spec.ts +++ b/TypeScript/test/jest/gilded-rose.spec.ts @@ -1,9 +1,80 @@ -import { Item, GildedRose } from '@/gilded-rose'; +import { Item, GildedRose } from '@/gilded-rose' -describe('Gilded Rose', () => { - it('should foo', () => { - const gildedRose = new GildedRose([new Item('foo', 0, 0)]); - const items = gildedRose.updateQuality(); - expect(items[0].name).toBe('fixme'); - }); -}); +/** + * A copy of the unrefactored GildedRose class which will be used for acceptance tests + */ +export class AcceptanceGildedRose { + items: Array; + + constructor(items = [] as Array) { + 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 + } + } + } 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 + } + } + } + } + } + if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { + this.items[i].sellIn = this.items[i].sellIn - 1; + } + 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 this.items; + } +} + +test('acceptance tests', () => { + const names = ['Aged Brie', 'Backstage passes to a TAFKAL80ETC concert', 'Sulfuras, Hand of Ragnaros'] + const sellIns = [-1, 0, 1, 2, 5, 6, 7, 10, 11, 12, 13, 14, 15] + const qualities = [-1, 1, 2, 48, 49, 50, 51] + + for (const name of names) { + for (const sellIn of sellIns) { + for (const quality of qualities) { + const gildedRose = new GildedRose([new Item(name, sellIn, quality)]) + const acceptanceGildedRose = new AcceptanceGildedRose([new Item(name, sellIn, quality)]) + + expect(gildedRose.updateQuality()).toEqual(acceptanceGildedRose.updateQuality()) + expect(gildedRose.items).toEqual(acceptanceGildedRose.items) + } + } + } +})