Refactoring

This commit is contained in:
Bastien Maillard 2017-09-28 12:42:41 +02:00
parent 6f972d0c5f
commit 30145790d6
2 changed files with 187 additions and 60 deletions

View File

@ -1,9 +1,115 @@
describe("Gilded Rose", function() {
describe('Gilded Rose', function() {
it("should foo", function() {
const gildedRose = new Shop([ new Item("foo", 0, 0) ]);
const items = gildedRose.updateQuality();
expect(items[0].name).toEqual("fixme");
});
describe('with default item', function() {
const item = new Item('foo', 0, 0)
const items = [item]
const gildedRose = new Shop([item])
});
it('has foo item', function() {
expect(items[0].name).toEqual('foo')
})
it('has foo item with 0 sellIn', function() {
expect(items[0].sellIn).toEqual(0)
})
it('has foo item with 0 quality', function() {
expect(items[0].quality).toEqual(0)
})
})
describe('#downgradeQuality', function() {
const item = new Item('foo', 10, 20)
const items = [item]
const gildedRose = new Shop([item])
it('downgrade quality by 1', function() {
gildedRose.downgradeQuality(items[0])
expect(items[0].quality).toEqual(19)
})
it('downgrade quality by 5', function() {
gildedRose.downgradeQuality(items[0], 5)
expect(items[0].quality).toEqual(14)
})
})
describe('#upgradeQuality', function() {
const item = new Item('foo', 10, 20)
const items = [item]
const gildedRose = new Shop([item])
it('upgrade quality by 1', function() {
gildedRose.upgradeQuality(items[0])
expect(items[0].quality).toEqual(21)
})
it('upgrade quality by 5', function() {
gildedRose.upgradeQuality(items[0], 5)
expect(items[0].quality).toEqual(26)
})
})
describe('#downgradeSellIn', function() {
const item = new Item('foo', 10, 20)
const items = [item]
const gildedRose = new Shop([item])
it('downgrade quality by 1', function() {
gildedRose.downgradeSellIn(items[0])
expect(items[0].sellIn).toEqual(9)
})
it('downgrade quality by 5', function() {
gildedRose.downgradeSellIn(items[0], 5)
expect(items[0].sellIn).toEqual(4)
})
})
describe('#hasQuality', function() {
const item = new Item('foo', 10, 20)
const items = [item]
const gildedRose = new Shop([item])
it('has quality', function() {
const hasQuality = gildedRose.hasQuality(items[0])
expect(hasQuality).toEqual(true)
})
})
describe('#commonQuality', function() {
const item = new Item('foo', 10, 20)
const items = [item]
const gildedRose = new Shop([item])
it('is a common quality', function() {
const commonQuality = gildedRose.commonQuality(items[0])
expect(commonQuality).toEqual(true)
})
})
describe('after updateQuality', function() {
const item = new Item('foo', 10, 20)
const items = [item]
const gildedRose = new Shop([item])
gildedRose.updateQuality()
it('has a good quality value', function() {
expect(items[0].quality).toEqual(19)
})
it('has a good sell in value', function() {
expect(items[0].sellIn).toEqual(9)
})
// TODO: Add custom tests with custom names, etc.
})
})

View File

@ -1,62 +1,83 @@
class Item {
constructor(name, sellIn, quality){
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
constructor(name, sellIn, quality) {
this.name = name
this.sellIn = sellIn
this.quality = quality
}
}
class Shop {
constructor(items=[]){
this.items = items;
constructor(items = []) {
this.items = items
}
updateQuality() {
for (var 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;
downgradeQuality(item, value = 1) {
item.quality -= value
}
upgradeQuality(item, value = 1) {
item.quality += value
}
downgradeSellIn(item, value = 1) {
item.sellIn -= value
}
hasQuality(item) {
return item.quality > 0
}
commonQuality(item) {
return item.quality < 50
}
updateQuality() {
this.items.map((item, i) => {
if (
$.inArray(item.name, [
'Aged Brie',
'Backstage passes to a TAFKAL80ETC concert',
'Sulfuras, Hand of Ragnaros',
]) == -1 && this.hasQuality(item)
) {
this.downgradeQuality(item)
}
else {
if (this.commonQuality(item)) {
this.upgradeQuality(item)
if (item.name == 'Backstage passes to a TAFKAL80ETC concert') {
if (item.sellIn < 11 && this.commonQuality(item))
this.upgradeQuality(item)
if (item.sellIn < 6 && this.commonQuality(item))
this.upgradeQuality(item)
}
}
}
if (item.name != 'Sulfuras, Hand of Ragnaros') this.downgradeSellIn(item)
if (item.sellIn < 0) {
if (item.name != 'Aged Brie') {
if (item.name != 'Backstage passes to a TAFKAL80ETC concert') {
if (item.name != 'Sulfuras, Hand of Ragnaros' &&
this.hasQuality(item))
this.downgradeQuality(item)
}
else {
this.downgradeQuality(item, item.quality)
}
}
else if (this.commonQuality(item)) {
this.upgradeQuality(item)
}
}
})
return this.items
}
}