Skip to content

Instantly share code, notes, and snippets.

@thejessleigh
Created March 19, 2014 16:32
Show Gist options
  • Save thejessleigh/9645595 to your computer and use it in GitHub Desktop.
Save thejessleigh/9645595 to your computer and use it in GitHub Desktop.
function Item(name, sell_in, quality) {
this.name = name;
this.sell_in = sell_in;
this.quality = quality;
}
var items = []
var DEXTERITY_VEST = "+5 Dexterity Vest";
var AGED_BRIE = "Aged Brie";
var ELIXIR = "Elixir of the Mongoose";
var SULFURAS = "Sulfuras, Hand of Ragnaros";
var BACKSTAGE_PASSES = "Backstage passes to a TAFKAL80ETC concert";
var CAKE = "Conjured Mana Cake";
var MAX_QUALITY = 50;
var MIN_QUALITY = 0;
items.push(new Item(DEXTERITY_VEST, 10, 20));
items.push(new Item(AGED_BRIE, 2, 0));
items.push(new Item(ELIXIR, 5, 7));
items.push(new Item(SULFURAS, 0, 80));
items.push(new Item(BACKSTAGE_PASSES, 15, 20));
items.push(new Item(CAKE, 3, 6));
function update_quality() {
for (var i = 0; i < items.length; i++) {
if(item_is_sulfuras(items[i])) {
continue;
}
advance_time(i);
if(items[i].name === BACKSTAGE_PASSES){
update_backstage_passes(items[i], i);
continue;
}
if (item_is_generic(items[i])) {
decrease_generic_quality(i);
} else {
increase_rare_quality(i)
}
if (items[i].sell_in < 0) {
if(item_is_generic(items[i])) {
decrease_generic_quality(i);
} else {
increase_rare_quality(i);
}
}
}
}
var update_backstage_passes = function(item, i){
if(item.sell_in < 0) {
item.quality = MIN_QUALITY;
} else if(item.sell_in < 5) {
increase_quality(i, 3);
} else if (item.sell_in < 10) {
increase_quality(i, 2);
} else {
increase_quality(i, 1);
}
}
var item_is_generic = function(item) {
return item.name != AGED_BRIE && item.name != BACKSTAGE_PASSES;
}
var item_is_sulfuras = function(item) {
return item.name == SULFURAS;
}
var decrease_generic_quality = function(i){
if (items[i].quality > MIN_QUALITY) {
items[i].quality = items[i].quality - 1
}
}
var advance_time = function(i){
items[i].sell_in = items[i].sell_in - 1;
}
var increase_rare_quality = function(i){
if (items[i].quality < MAX_QUALITY) {
increase_quality(i, 1);
}
}
var increase_quality = function(i, factor) {
items[i].quality = items[i].quality + factor;
}
describe("Gilded Rose", function() {
beforeEach(function(){
items = []
items.push(new Item('+5 Dexterity Vest', 10, 20));
items.push(new Item('Aged Brie', 2, 0));
items.push(new Item('Elixir of the Mongoose', 5, 7));
items.push(new Item('Sulfuras, Hand of Ragnaros', 0, 80));
items.push(new Item('Backstage passes to a TAFKAL80ETC concert', 15, 20));
items.push(new Item('Conjured Mana Cake', 3, 6));
})
describe("items", function(){
it("should have a sell_in value", function(){
expect(items[0].sell_in).toBe(10);
});
it("should have a quality value", function(){
expect(items[0].quality).toBe(20);
});
});
describe("update quality", function(){
it("sell_in date for generic items decreases one at the end of the day", function() {
update_quality()
expect(items[0].sell_in).toBe(9);
});
it("an item's quality should never be negative", function(){
for (var i = 0; i < 8; i++ ){
update_quality();
}
expect(items[2].quality).toBe(0);
});
it("Aged Brie increases in quality over time", function(){
update_quality();
expect(items[1].quality).toBe(1);
});
it("a generic item's quality should never exceed 50", function(){
for (var i = 0; i < 51; i++) {
update_quality();
}
expect(items[1].quality).toBe(50);
});
it("Sulfuras's sell_in value never decreases", function(){
update_quality();
expect(items[3].sell_in).toBe(0);
});
it("Sulfuras's quality never alters", function(){
update_quality();
expect(items[3].quality).toBe(80);
});
it("Backstage passes increase in quality as sell_in value decreases", function(){
update_quality();
expect(items[4].quality).toBe(21);
});
it("Backstage passes increase in quality twice as fast if sell_in is less than 10", function(){
for (var i = 0; i < 6; i++){
update_quality();
}
expect(items[4].quality).toBe(27);
});
it("Backstage passes increase in quality thrice as fast if sell_in is less than 5", function(){
for (var i = 0; i < 11; i++){
update_quality();
}
expect(items[4].quality).toBe(38);
});
it("Backstage passes quality drops to 0 when the sell_in date is 0", function(){
for (var i = 0; i < 16; i++){
update_quality();
}
expect(items[4].quality).toBe(0);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment