Skip to content

Commit

Permalink
Added more tests, refactored the code and added the CI script
Browse files Browse the repository at this point in the history
Signed-off-by: Carpinisan George <[email protected]>
  • Loading branch information
george17c committed Nov 11, 2024
1 parent a2a2aff commit b800027
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 42 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
name: Run Tests

on:
push:
branches:
- main

pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@2

- name: Setup
uses: actions/setup-node@v2
with:
node-version: '18'

- name: Install dependencies
run: |
sudo apt install npm
npm install
- name: Run the tests
run: npm run test:jest
73 changes: 33 additions & 40 deletions app/gilded-rose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,46 @@ export class GildedRose {
this.items = items;
}

updateConcertTicket(item: Item) {
item.quality += 1
if (item.sellIn < 11)
item.quality += 1

if (item.sellIn < 6)
item.quality += 1

if (item.quality >= 50)
item.quality = 50
}

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
}
}
if (this.items[i].name == 'Sulfuras, Hand of Ragnaros')
break

this.items[i].sellIn -= 1

if (this.items[i].quality == 50 || this.items[i].quality == 0)
break

if (this.items[i].name == 'Aged Brie') {
this.items[i].quality += 1
break
} else if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') {
this.updateConcertTicket(this.items[i])
} else if (this.items[i].name == 'Conjured') {
this.items[i].quality -= 2
} 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].quality -= 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
}
}
if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert')
this.items[i].quality = 0
}

}

return this.items;
}
}
}
39 changes: 37 additions & 2 deletions test/jest/gilded-rose.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Gilded Rose', () => {
const items = gildedRose.updateQuality();

// Assert
expect(items[0].name).toBe('bar');
expect(items[0].name).toBe('foo');
});

it('sword quality drops by 1', () => {
Expand All @@ -20,6 +20,41 @@ describe('Gilded Rose', () => {
const items = gildedRose.updateQuality();

// Assert
expect(items[0].quality).toBe(1);
expect(items[0].quality).toBe(0);
})

it('Sulfuras quality doesn\'t drop', () => {
const gildedRose = new GildedRose([new Item('Sulfuras, Hand of Ragnaros', 0, 80)]);

const items = gildedRose.updateQuality();

expect(items[0].quality).toBe(80);
})

it('Aged brie increases in Quality', () => {
const gildedRose = new GildedRose([new Item('Aged Brie', 10, 30)]);

const items = gildedRose.updateQuality();

expect(items[0].quality).toBe(31);
})

it('Item quality doesn\'t exceed 50/drop below 0', () => {
const gildedRose1 = new GildedRose([new Item('Shield', 10, 0)]);
const gildedRose2 = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 5, 48)]);

const item1 = gildedRose1.updateQuality();
const item2 = gildedRose2.updateQuality();

expect(item1[0].quality).toBe(0);
expect(item2[0].quality).toBe(50);
})

it('Conjured items degrade twice as fast', () => {
const gildedRose = new GildedRose([new Item('Conjured', 5, 20)]);

const items = gildedRose.updateQuality();

expect(items[0].quality).toBe(18);
})
});

0 comments on commit b800027

Please sign in to comment.