Skip to content

Improve Test Coverage for GildedRose UpdateQuality Functionality #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 109 additions & 69 deletions app/gilded-rose.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,109 @@
export class Item {
name: string;
sellIn: number;
quality: number;

constructor(name, sellIn, quality) {
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
}
}

export class GildedRose {
items: Array<Item>;

constructor(items = [] as Array<Item>) {
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;
}
}
export class Item {
name: string;
sellIn: number;
quality: number;

static readonly MIN_SELL_IN = 0;
static readonly MIN_QUALITY = 0;
static readonly MAX_QUALITY = 50;

constructor(name: string, sellIn: number, quality: number) {
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
}
}

export class GildedRose {
items: Item[];

constructor(items: Item[] = []) {
this.items = items;
}

/**
* Update the quality of all items.
*
* "Backstage passes" – increase in quality as the sellIn date approaches.
* "Conjured" – degrade in quality twice as fast.
* "Aged Brie" – increases in quality as it gets older.
* "Sulfuras" – not updated.
*
* @returns {Item[]} - The updated list of items.
*/
updateQuality(): Item[] {
this.items.forEach(item => {
this.updateItem(item);
item.sellIn = Math.max(Item.MIN_SELL_IN, item.sellIn);
this.clampQuality(item);
});
return this.items;
}

/**
* Update a single item's quality and sellIn value.
* Sulfuras items are not updated.
*
* @param {Item} item - The item to update.
*/
private updateItem(item: Item): void {
const name = item.name.toLowerCase();

if (name.includes('sulfuras')) {
this.clampQuality(item);
item.sellIn = Math.max(Item.MIN_SELL_IN, item.sellIn);
return;
}

if (name.includes('aged brie')) {
this.updateAgedBrie(item);
} else if (name.includes('backstage passes')) {
this.updateBackstagePasses(item);
} else if (name.includes('conjured')) {
this.updateConjured(item);
} else {
this.updateNormal(item);
}

item.sellIn--;
}

private clampQuality(item: Item): void {
item.quality = Math.max(Item.MIN_QUALITY, Math.min(Item.MAX_QUALITY, item.quality));
}

private updateNormal(item: Item): void {
const factor = item.sellIn <= Item.MIN_SELL_IN ? 2 : 1;
this.decrease(item, factor);
}

private updateConjured(item: Item): void {
const factor = item.sellIn <= Item.MIN_SELL_IN ? 4 : 2;
this.decrease(item, factor);
}

private updateAgedBrie(item: Item): void {
const factor = item.sellIn <= Item.MIN_SELL_IN ? 2 : 1;
this.increase(item, factor);
}

private updateBackstagePasses(item: Item): void {
if (item.sellIn < 0) {
item.quality = Item.MIN_QUALITY;
} else if (item.sellIn <= 5) {
this.increase(item, 3);
} else if (item.sellIn <= 10) {
this.increase(item, 2);
} else {
this.increase(item, 1);
}
}

private increase(item: Item, amount: number): void {
item.quality = Math.min(Item.MAX_QUALITY, item.quality + amount);
}

private decrease(item: Item, amount: number): void {
item.quality = Math.max(Item.MIN_QUALITY, item.quality - amount);
}
}
37 changes: 22 additions & 15 deletions test/golden-master-text-test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { Item, GildedRose } from '../app/gilded-rose';

console.log("OMGHAI!")

const items = [
new Item("+5 Dexterity Vest", 10, 20), //
new Item("Aged Brie", 2, 0), //
new Item("Elixir of the Mongoose", 5, 7), //
new Item("Sulfuras, Hand of Ragnaros", 0, 80), //
new Item("Sulfuras, Hand of Ragnaros", -1, 80),
new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49),
// this conjured item does not work properly yet
new Item("Conjured Mana Cake", 3, 6)];

// Other items - Normal
new Item("+5 Dexterity Vest", 10, 20), // 0
new Item("Aged Brie", 2, 0), // 1
new Item("Elixir of the Mongoose", 5, 7), // 2
new Item("Griphon Wings", 5, 42), // 3
new Item("Normal Item", 3, 6), // 4
// Aged Brie
new Item("Aged Brie", 2, 0), // 5
new Item("Aged Brie", 0, 0), // 6
// Backstage passes
new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20), // 9
new Item("Backstage passes to a TAFKAL81ETC concert", 10, 49), // 10
new Item("Backstage passes to a TAFKAL82ETC concert", 5, 49), // 11
new Item("Backstage passes to a TAFKAL83ETC concert", 0, 5), // 12
// Conjured
new Item("Conjured Mana Cake", 3, 6), // 13
new Item("Conjured Mana Cake", 4, 7) // 14
];

const gildedRose = new GildedRose(items);

Expand All @@ -26,8 +31,10 @@ for (let i = 0; i < days + 1; i++) {
console.log("-------- day ".padStart(25) + i + " --------");
console.log("name".padStart(24).padEnd(45) + "| sellIn |" + "quality");
items.forEach(element => {
console.log(element.name.padEnd(45) + '|' + element.sellIn.toString().padStart(5).padEnd(8) + '|' + element.quality.toString().padStart(4));

console.log(
element.name.padEnd(45) + '|' +
element.sellIn.toString().padStart(5).padEnd(8) + '|' +
element.quality.toString().padStart(4));
});
console.log();
gildedRose.updateQuality();
Expand Down
Loading