mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 15:01:28 +00:00
cognizant code refactor exercise
This commit is contained in:
parent
f7a3312e6b
commit
091d455e2a
6025
js-jest/package-lock.json
generated
6025
js-jest/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,67 +1,101 @@
|
|||||||
class Item {
|
class Item {
|
||||||
constructor(name, sellIn, quality){
|
constructor(name, sellIn, quality) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.sellIn = sellIn;
|
this.sellIn = sellIn;
|
||||||
this.quality = quality;
|
this.quality = quality;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const itemType = {
|
||||||
|
PASS: "Backstage passes to a TAFKAL80ETC concert",
|
||||||
|
BRIE: "Aged Brie",
|
||||||
|
SULFURAS: "Sulfuras, Hand of Ragnaros",
|
||||||
|
CONJURED: "Conjured Mana Cake",
|
||||||
|
};
|
||||||
|
|
||||||
|
function updateBrie(item) {
|
||||||
|
if (item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
}
|
||||||
|
item.sellIn = item.sellIn - 1;
|
||||||
|
if (item.sellIn < 0 && item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
}
|
||||||
|
lessAndGreaterThan(item);
|
||||||
|
}
|
||||||
|
function updateHand(item) {}
|
||||||
|
function updateNormal(item) {
|
||||||
|
item.sellIn = item.sellIn - 1;
|
||||||
|
if (item.quality > 0) {
|
||||||
|
item.quality = item.quality - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.sellIn < 0 && item.quality > 0) {
|
||||||
|
item.quality = item.quality - 1;
|
||||||
|
}
|
||||||
|
lessAndGreaterThan(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
function updatePass(item) {
|
||||||
|
if (item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
if (item.sellIn < 11 && item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
}
|
||||||
|
if (item.sellIn < 6 && item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
item.sellIn = item.sellIn - 1;
|
||||||
|
if (item.sellIn < 0) {
|
||||||
|
item.quality = item.quality - item.quality;
|
||||||
|
}
|
||||||
|
lessAndGreaterThan(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
const lessAndGreaterThan = (item) => {
|
||||||
|
if (item.quality > 50) item.quality = 50;
|
||||||
|
if (item.quality < 0) item.quality = 0;
|
||||||
|
};
|
||||||
|
|
||||||
class Shop {
|
class Shop {
|
||||||
constructor(items=[]){
|
constructor(items = []) {
|
||||||
this.items = items;
|
this.items = items;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateQuality() {
|
updateQuality() {
|
||||||
for (let i = 0; i < this.items.length; i++) {
|
for (const item of this.items) {
|
||||||
if (this.items[i].name != 'Aged Brie' && this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') {
|
// Set a degradation multiplier to 2 if expiration date has passed
|
||||||
if (this.items[i].quality > 0) {
|
// Otherwise set it to 1 (no multiplier)
|
||||||
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
|
|
||||||
this.items[i].quality = this.items[i].quality - 1;
|
let degradationMultiplier = item.sellIn < 0 ? 2 : 1;
|
||||||
}
|
|
||||||
}
|
switch (item.name) {
|
||||||
} else {
|
case itemType.BRIE:
|
||||||
if (this.items[i].quality < 50) {
|
updateBrie(item);
|
||||||
this.items[i].quality = this.items[i].quality + 1;
|
continue;
|
||||||
if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') {
|
case itemType.PASS:
|
||||||
if (this.items[i].sellIn < 11) {
|
updatePass(item);
|
||||||
if (this.items[i].quality < 50) {
|
continue;
|
||||||
this.items[i].quality = this.items[i].quality + 1;
|
case itemType.SULFURAS:
|
||||||
}
|
continue;
|
||||||
}
|
case itemType.CONJURED:
|
||||||
if (this.items[i].sellIn < 6) {
|
item.quality -= 2 * degradationMultiplier;
|
||||||
if (this.items[i].quality < 50) {
|
item.sellIn--;
|
||||||
this.items[i].quality = this.items[i].quality + 1;
|
lessAndGreaterThan(item);
|
||||||
}
|
continue;
|
||||||
}
|
default:
|
||||||
}
|
item.quality -= 1 * degradationMultiplier;
|
||||||
}
|
item.sellIn--;
|
||||||
}
|
lessAndGreaterThan(item);
|
||||||
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
|
continue;
|
||||||
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;
|
return this.items;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
Item,
|
Item,
|
||||||
Shop
|
Shop,
|
||||||
}
|
};
|
||||||
|
|||||||
@ -1,9 +1,94 @@
|
|||||||
const {Shop, Item} = require("../src/gilded_rose");
|
const { Shop, Item } = require("../src/gilded_rose");
|
||||||
|
|
||||||
describe("Gilded Rose", function() {
|
const gildedRose = new Shop([
|
||||||
it("should foo", function() {
|
new Item("+5 Dexterity Vest", 10, 20),
|
||||||
const gildedRose = new Shop([new Item("foo", 0, 0)]);
|
new Item("Aged Brie", 2, 0),
|
||||||
const items = gildedRose.updateQuality();
|
new Item("Sulfuras, Hand of Ragnaros", 0, 35),
|
||||||
expect(items[0].name).toBe("fixme");
|
new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
|
||||||
|
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 45),
|
||||||
|
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 32),
|
||||||
|
new Item("Conjured Mana Cake", 3, 6),
|
||||||
|
new Item("Expired Pears", -1, 11),
|
||||||
|
new Item("Overpriced Trinket", 10, 55),
|
||||||
|
new Item("Conjured Mana Cake", -3, 6),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const items = gildedRose.updateQuality();
|
||||||
|
|
||||||
|
describe("After one updateQuality call", function () {
|
||||||
|
it("Miscellaneous item should have decremented sellIn by 1", function () {
|
||||||
|
expect(items[0].sellIn).toBe(9);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Miscellaneous item should have decremented quality by 1", function () {
|
||||||
|
expect(items[0].quality).toBe(19);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Expired item should have decremented quality by 2", function () {
|
||||||
|
expect(items[7].quality).toBe(9);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Overpriced item should have reset quality to 50", function () {
|
||||||
|
expect(items[8].quality).toBe(50);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("After one updateQuality call, Aged Brie", function () {
|
||||||
|
it("should have decremented sellIn by 1", function () {
|
||||||
|
expect(items[1].sellIn).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should have incremented quality by 1", function () {
|
||||||
|
expect(items[1].quality).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("After one updateQuality call, Sulfuras", function () {
|
||||||
|
it("should have same sellIn", function () {
|
||||||
|
expect(items[2].sellIn).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should have same quality", function () {
|
||||||
|
expect(items[2].quality).toBe(35);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("After one updateQuality call", function () {
|
||||||
|
it("First backstage pass should have decremented sellIn by 1", function () {
|
||||||
|
expect(items[3].sellIn).toBe(14);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("First backstage pass should have incremented quality by 1", function () {
|
||||||
|
expect(items[3].quality).toBe(21);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Second backstage pass should have decremented sellIn by 1", function () {
|
||||||
|
expect(items[4].sellIn).toBe(9);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Second backstage pass should have incremented quality by 2", function () {
|
||||||
|
expect(items[4].quality).toBe(47);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Third backstage pass should have decremented sellIn by 1", function () {
|
||||||
|
expect(items[5].sellIn).toBe(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Third backstage pass should have incremented quality by 3", function () {
|
||||||
|
expect(items[5].quality).toBe(35);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("After one updateQuality call, Conjured item", function () {
|
||||||
|
it("should have decremented sellIn by 1", function () {
|
||||||
|
expect(items[6].sellIn).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should have decremented quality by 2", function () {
|
||||||
|
expect(items[6].quality).toBe(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should have decremented quality by 4 if expired", function () {
|
||||||
|
expect(items[9].quality).toBe(2);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user