Initial refactors/tests

This commit is contained in:
Jason Rall 2020-07-02 08:28:58 -06:00
parent fbe24e35b5
commit 7ce46a24a7
5 changed files with 4892 additions and 41 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ obj
vendor
.idea
*.iml
.nyc_output/

4814
js-mocha/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "Gilded Rose kata in Javascript with Mocha",
"scripts": {
"test": "mocha --compilers js:babel/register"
"test": "mocha --compilers js:babel/register --watch"
},
"repository": {
"type": "git",
@ -23,6 +23,8 @@
"devDependencies": {
"babel": "^5.8.23",
"chai": "^4.2.0",
"mocha": "^5.2.0"
"mocha": "^5.2.0",
"nodemon": "^2.0.4",
"nyc": "^15.1.0"
}
}

View File

@ -11,54 +11,48 @@ class Shop {
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;
}
this.items.forEach((item)=> {
if (['Aged Brie', 'Backstage passes to a TAFKAL80ETC concert'].indexOf(item.name) === -1 ) {
if (item.quality > 0 && item.name !== 'Sulfuras, Hand of Ragnaros') {
this.decreaseQuality(item);
}
} 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 (item.quality < 50) {
this.increaseQuality(item)
if (item.name === 'Backstage passes to a TAFKAL80ETC concert' &&
(item.sellIn < 6 ||item.sellIn < 11) && item.quality < 50) {
this.increaseQuality(item);
}
}
}
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
this.items[i].sellIn = this.items[i].sellIn - 1;
if (item.name !== 'Sulfuras, Hand of Ragnaros') {
item.sellIn = item.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;
}
}
if (item.sellIn < 0) {
if (item.name !== 'Aged Brie') {
if (['Backstage passes to a TAFKAL80ETC concert', 'Sulfuras, Hand of Ragnaros'].indexOf(item.name) === -1 &&
item.quality > 0) {
this.decreaseQuality(item);
} else {
this.items[i].quality = this.items[i].quality - this.items[i].quality;
item.quality = item.quality - item.quality;
}
} else {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1;
if (item.quality < 50) {
this.increaseQuality(item)
}
}
}
}
})
return this.items;
}
increaseQuality(item) {
item.quality = item.quality + 1;
}
decreaseQuality(item) {
item.quality = item.quality - 1;
}
}
module.exports = {
Item,

View File

@ -1,11 +1,51 @@
var {expect} = require('chai');
var {Shop, Item} = require('../src/gilded_rose.js');
function getItems(items = []) {
const gildedRose = new Shop(items)
return gildedRose.updateQuality();
}
describe("Gilded Rose", function() {
function assertFirstItemEquals(items, item) {
expect(items[0].name).to.equal(item.name)
expect(items[0].sellIn).to.equal(item.sellIn)
expect(items[0].quality).to.equal(item.quality)
}
it("should foo", function() {
const gildedRose = new Shop([ new Item("foo", 0, 0) ]);
const items = gildedRose.updateQuality();
expect(items[0].name).to.equal("fixme");
const items = getItems([ new Item("foo", 0, 0) ]);
expect(items[0].name).to.equal("foo");
});
it("should be empty", function () {
const items = getItems();
expect(items).to.be.an('array').that.is.empty;
});
it("should accept aged Aged Brie", () => {
const items = getItems([ new Item("Aged Brie", 0, 0) ])
assertFirstItemEquals(items, {name: 'Aged Brie', sellIn: -1, quality: 2});
})
it("Sulfuras, Hand of Ragnaros", () => {
const items = getItems([new Item("Sulfuras, Hand of Ragnaros", 0, 0) ])
assertFirstItemEquals(items, {name: 'Sulfuras, Hand of Ragnaros', sellIn: 0, quality: 0});
})
it('should accept backstage passes to TAFAL80ETC', ()=>{
const items = getItems([new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49)]);
assertFirstItemEquals(items, {name: 'Backstage passes to a TAFKAL80ETC concert', sellIn: 4, quality: 50});
})
it('should subtract quality from itself when sell in is 0 and name is TAFAL80ETC', ()=>{
const items = getItems([new Item("Backstage passes to a TAFKAL80ETC concert", 0, 50)]);
assertFirstItemEquals(items, { name: 'Backstage passes to a TAFKAL80ETC concert', sellIn: -1, quality: 0});
})
it('should increase quality by 1 when backstage passes to TAFAL80ETC', ()=>{
const items = getItems([new Item("Backstage passes to a TAFKAL80ETC concert", 12, 49)]);
assertFirstItemEquals(items, { name: 'Backstage passes to a TAFKAL80ETC concert', sellIn: 11, quality: 50});
})
it('should decrease quality by 2 when name unheard of', ()=>{
const items = getItems([new Item("Backstage passes to a TAFKAL80ETC concert", -4, 49)]);
assertFirstItemEquals(items, {name: 'Backstage passes to a TAFKAL80ETC concert', sellIn: -5, quality: 0});
})
it('should decrease quality by 2 when name unheard of', ()=>{
const items = getItems([new Item("unheard of", -4, 49)]);
assertFirstItemEquals(items, {name: 'unheard of', sellIn: -5, quality: 47});
})
});