mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-18 07:51:29 +00:00
Refactored code, added unit tests and added conjured items
This commit is contained in:
parent
bc7de77181
commit
1843127222
@ -17,53 +17,58 @@ export class GildedRose {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
min(a: number, b: number) {
|
||||
return (a < b) ? a : b;
|
||||
}
|
||||
|
||||
max(a: number, b: number) {
|
||||
return (a > b) ? a : b;
|
||||
}
|
||||
|
||||
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;
|
||||
this.items[i].sellIn--;
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
switch(this.items[i].name) {
|
||||
case 'Aged Brie':
|
||||
if (this.items[i].sellIn < 0) {
|
||||
this.items[i].quality += 2;
|
||||
} else {
|
||||
this.items[i].quality = 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
|
||||
break;
|
||||
case 'Backstage passes to a TAFKAL80ETC concert':
|
||||
if (this.items[i].sellIn > 10) {
|
||||
this.items[i].quality++;
|
||||
} else if (this.items[i].sellIn > 5) {
|
||||
this.items[i].quality += 2;
|
||||
} else if (this.items[i].sellIn > 0) {
|
||||
this.items[i].quality += 3;
|
||||
} else {
|
||||
this.items[i].quality = 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'Sulfuras, Hand of Ragnaros':
|
||||
break;
|
||||
case 'Conjured':
|
||||
if (this.items[i].sellIn > 0) {
|
||||
this.items[i].quality -= 2;
|
||||
} else {
|
||||
this.items[i].quality -= 4;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (this.items[i].sellIn > 0) {
|
||||
this.items[i].quality--;
|
||||
} else {
|
||||
this.items[i].quality -= 2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this.items[i].quality = this.min(50, this.max(0, this.items[i].quality));
|
||||
}
|
||||
return this.items;
|
||||
}
|
||||
}
|
||||
|
||||
5328
TypeScript/package-lock.json
generated
5328
TypeScript/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -5,6 +5,72 @@ describe('Gilded Rose', () => {
|
||||
it('should foo', () => {
|
||||
const gildedRose = new GildedRose([new Item('foo', 0, 0)]);
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[0].name).to.equal('fixme');
|
||||
expect(items[0].name).to.equal('foo');
|
||||
});
|
||||
it('should degrade', () => {
|
||||
const gildedRose = new GildedRose([new Item('Cheese', 5, 20)]);
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[0].quality).to.equal(19);
|
||||
});
|
||||
it('should degrade twice as fast past the expiration date', () => {
|
||||
const gildedRose = new GildedRose([new Item('Cheese', 0, 20)]);
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[0].quality).to.equal(18);
|
||||
});
|
||||
it('should not have negative quality', () => {
|
||||
const gildedRose = new GildedRose([new Item('test', 0, 0)]);
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[0].quality).to.equal(0);
|
||||
});
|
||||
it('should increase quality for aged brie', () => {
|
||||
const gildedRose = new GildedRose([new Item('Aged Brie', 2, 25)]);
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[0].quality).to.equal(26);
|
||||
});
|
||||
it('should not increase quality past 50', () => {
|
||||
const gildedRose = new GildedRose([new Item('Aged Brie', 0, 50)]);
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[0].quality).to.equal(50);
|
||||
});
|
||||
it('should not decrease quality or sellIn value for Sulfuras', () => {
|
||||
const gildedRose = new GildedRose([new Item('Sulfuras, Hand of Ragnaros', 30, 50)]);
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[0].quality).to.equal(50);
|
||||
expect(items[0].sellIn).to.equal(30);
|
||||
});
|
||||
it('should increase quality for backstage passes', () => {
|
||||
const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 20, 30)]);
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[0].quality).to.equal(31);
|
||||
});
|
||||
it('should increase quality for backstage passes by 2 when 10 days are left', () => {
|
||||
const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 10, 30)]);
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[0].quality).to.equal(32);
|
||||
});
|
||||
it('should increase quality for backstage passes by 3 when 5 days are left', () => {
|
||||
const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 5, 40)]);
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[0].quality).to.equal(43);
|
||||
});
|
||||
it('should set quality for backstage passes to 0 after the date', () => {
|
||||
const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 0, 30)]);
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[0].quality).to.equal(0);
|
||||
});
|
||||
it('should increase quality by 2 for aged brie past sellin', () => {
|
||||
const gildedRose = new GildedRose([new Item('Aged Brie', 0, 30)]);
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[0].quality).to.equal(32);
|
||||
});
|
||||
it('should decrease Conjured quality by 2', () => {
|
||||
const gildedRose = new GildedRose([new Item('Conjured', 10, 30)]);
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[0].quality).to.equal(28);
|
||||
});
|
||||
it('should decrease Conjured quality by 4 past sellin', () => {
|
||||
const gildedRose = new GildedRose([new Item('Conjured', 0, 30)]);
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[0].quality).to.equal(26);
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user