ruby Guilded Rose Kata al WeArePeople

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Guilded Rose Kata al WeArePeople相关的知识,希望对你有一定的参考价值。

# The Gilded Rose Code Kata

This is a Ruby version of the Gilded Rose Kata, found
[here](http://www.iamnotmyself.com/2011/02/13/RefactorThisTheGildedRoseKata.aspx
I Am Not Myself).

This is a refactorying kata, so you will be starting with a legacy
code base.  To work the Kata, clone this git repository and checkout
the tag 'start-here'. Read the description below for the "rules"
involving this kata.

## Changes from the original

This Ruby version follows the original code very closely, but has the
following changes:

* The original had no tests.  Since this is a refactoring kata, I felt
  the tests were important and provide a fairly complete test suite.
  Just delete the tests if you wish to "go it alone".

* The original used a hard coded set of "items", presumably for
  testing the code.  Since I a test suite, the hard coded values were
  not of much use.  I also changed the interface to accept a list of
  items as a parameter rather than a hard coded constant.

You can read
[the original kata article](http://www.iamnotmyself.com/2011/02/13/RefactorThisTheGildedRoseKata.aspx
I Am Not Myself) for more details.

## Git Branches

* The 'master' branch contains the starting point for the kata.  It is
  also tagged as 'start-here'.

* The 'solution1' branch is my first solution for this kata.

Hope you enjoy this.     -- Jim Weirich


# Original Description of the Gilded Rose

Hi and welcome to team Gilded Rose. As you know, we are a small inn
with a prime location in a prominent city ran by a friendly innkeeper
named Allison. We also buy and sell only the finest
goods. Unfortunately, our goods are constantly degrading in quality as
they approach their sell by date. We have a system in place that
updates our inventory for us. It was developed by a no-nonsense type
named Leeroy, who has moved on to new adventures. Your task is to add
the new feature to our system so that we can begin selling a new
category of items. First an introduction to our system:

- All items have a SellIn value which denotes the number of days we
  have to sell the item
- All items have a Quality value which denotes how valuable the item
  is
- At the end of each day our system lowers both values for every item

Pretty simple, right? Well this is where it gets interesting:

  - Once the sell by date has passed, Quality degrades twice as fast
  - The Quality of an item is never negative
  - "Aged Brie" actually increases in Quality the older it gets
  - The Quality of an item is never more than 50
  - "Sulfuras", being a legendary item, never has to be sold or
    decreases in Quality
  - "Backstage passes", like aged brie, increases in Quality as it's
    SellIn value approaches; Quality increases by 2 when there are 10
    days or less and by 3 when there are 5 days or less but Quality
    drops to 0 after the concert

We have recently signed a supplier of conjured items. This requires an update to our system:

- "Conjured" items degrade in Quality twice as fast as normal items

Feel free to make any changes to the UpdateQuality method and add any
new code as long as everything still works correctly. However, do not
alter the Item class or Items property as those belong to the goblin
in the corner who will insta-rage and one-shot you as he doesn't
believe in shared code ownership (you can make the UpdateQuality
method and Items property static if you like, we'll cover for
you). Your work needs to be completed by Friday, February 18, 2011
08:00:00 AM PST.

Just for clarification, an item can never have its Quality increase
above 50, however "Sulfuras" is a legendary item and as such its
Quality is 80 and it never alters.

task :default => :spec

task :spec do
  sh "rspec ."
end
class DefaultItem
  attr_reader :item
  def initialize(item)
    @item = item
  end

  def update
    update_sell_in
    update_quality
  end

  protected

  def update_sell_in
    item.sell_in -= 1
  end

  def update_quality
    item.quality -= (item.sell_in < 0 ? 2 : 1) if item.quality > 0
  end
end

class Sulfuras < DefaultItem
  def update_quality
  end

  def update_sell_in
  end
end

class AgedBrie < DefaultItem
  def update_quality
    item.quality += (item.sell_in < 0 ? 2 : 1) if item.quality < 50
  end
end

class Backstage < DefaultItem
  def update_quality
    if item.sell_in < 0
      item.quality = 0
    else
      item.quality += quality_increase if item.quality < 50
    end
  end

  private

  def quality_increase
    increase = 1
    increase += 1 if item.sell_in < 10
    increase += 1 if item.sell_in < 5
    increase
  end
end

class Conjured < DefaultItem
  def update_quality
    item.quality -= 2 if item.quality > 0
  end
end

module ItemUpdater
  CLASS_NAMES = Hash.new(DefaultItem).merge(
    'Aged Brie'                                 => AgedBrie,
    'Backstage passes to a TAFKAL80ETC concert' => Backstage,
    'Sulfuras, Hand of Ragnaros'                => Sulfuras,
    'Conjured Mana Cake'                        => Conjured
  )

  def self.factory(item)
    CLASS_NAMES[item.name].new(item)
  end
end

def update_quality(items)
  items.each { |item| ItemUpdater.factory(item).update }
end

# DO NOT CHANGE THINGS BELOW -----------------------------------------

Item = Struct.new(:name, :sell_in, :quality)

# We use the setup in the spec rather than the following for testing.
#
# Items = [
#   Item.new("+5 Dexterity Vest", 10, 20),
#   Item.new("Aged Brie", 2, 0),
#   Item.new("Elixir of the Mongoose", 5, 7),
#   Item.new("Sulfuras, Hand of Ragnaros", 0, 80),
#   Item.new("Backstage passes to a TAFKAL80ETC concert", 15, 20),
#   Item.new("Conjured Mana Cake", 3, 6),
# ]
require 'rspec/given'
require 'gilded_rose'

describe "#update_quality" do

  context "with a single" do
    Given(:initial_sell_in) { 5 }
    Given(:initial_quality) { 10 }
    Given(:item) { Item.new(name, initial_sell_in, initial_quality) }

    When { update_quality([item]) }

    context "normal item" do
      Given(:name) { "NORMAL ITEM" }

      context "before sell date" do
        Then { item.quality.should == initial_quality-1 }
        Then { item.sell_in.should == initial_sell_in-1 }
      end

      context "on sell date" do
        Given(:initial_sell_in) { 0 }
        Then { item.quality.should == initial_quality-2 }
        Then { item.sell_in.should == initial_sell_in-1 }
      end

      context "after sell date" do
        Given(:initial_sell_in) { -10 }
        Then { item.quality.should == initial_quality-2 }
        Then { item.sell_in.should == initial_sell_in-1 }
      end

      context "of zero quality" do
        Given(:initial_quality) { 0 }
        Then { item.quality.should == 0 }
      end
    end

    context "Aged Brie" do
      Given(:name) { "Aged Brie" }

      context "before sell date" do
        Then { item.quality.should == initial_quality+1 }
        Then { item.sell_in.should == initial_sell_in-1 }

        context "with max quality" do
          Given(:initial_quality) { 50 }
          Then { item.quality.should == initial_quality }
          Then { item.sell_in.should == initial_sell_in-1 }
        end
      end

      context "on sell date" do
        Given(:initial_sell_in) { 0 }
        Then { item.quality.should == initial_quality+2 }
        Then { item.sell_in.should == initial_sell_in-1 }

        context "with max quality" do
          Given(:initial_quality) { 50 }
          Then { item.quality.should == initial_quality }
          Then { item.sell_in.should == initial_sell_in-1 }
        end
      end

      context "after sell date" do
        Given(:initial_sell_in) { -10 }
        Then { item.quality.should == initial_quality+2 }
        Then { item.sell_in.should == initial_sell_in-1 }

        context "with max quality" do
          Given(:initial_quality) { 50 }
          Then { item.quality.should == initial_quality }
          Then { item.sell_in.should == initial_sell_in-1 }
        end
      end
    end

    context "Sulfuras" do
      Given(:initial_quality) { 80 }
      Given(:name) { "Sulfuras, Hand of Ragnaros" }

      context "before sell date" do
        Then { item.quality.should == initial_quality }
        Then { item.sell_in.should == initial_sell_in }
      end

      context "on sell date" do
        Given(:initial_sell_in) { 0 }
        Then { item.quality.should == initial_quality }
        Then { item.sell_in.should == initial_sell_in }
      end

      context "after sell date" do
        Given(:initial_sell_in) { -10 }
        Then { item.quality.should == initial_quality }
        Then { item.sell_in.should == initial_sell_in }
      end
    end

    context "Backstage pass" do
      Given(:name) { "Backstage passes to a TAFKAL80ETC concert" }

      context "long before sell date" do
        Given(:initial_sell_in) { 11 }
        Then { item.quality.should == initial_quality+1 }
        Then { item.sell_in.should == initial_sell_in-1 }

        context "at max quality" do
          Given(:initial_quality) { 50 }
          Then { item.quality.should == initial_quality }
        end
      end

      context "medium close to sell date (upper bound)" do
        Given(:initial_sell_in) { 10 }
        Then { item.quality.should == initial_quality+2 }
        Then { item.sell_in.should == initial_sell_in-1 }

        context "at max quality" do
          Given(:initial_quality) { 50 }
          Then { item.quality.should == initial_quality }
        end
      end

      context "medium close to sell date (lower bound)" do
        Given(:initial_sell_in) { 6 }
        Then { item.quality.should == initial_quality+2 }
        Then { item.sell_in.should == initial_sell_in-1 }

        context "at max quality" do
          Given(:initial_quality) { 50 }
          Then { item.quality.should == initial_quality }
        end
      end

      context "very close to sell date (upper bound)" do
        Given(:initial_sell_in) { 5 }
        Then { item.quality.should == initial_quality+3 }
        Then { item.sell_in.should == initial_sell_in-1 }

        context "at max quality" do
          Given(:initial_quality) { 50 }
          Then { item.quality.should == initial_quality }
        end
      end

      context "very close to sell date (lower bound)" do
        Given(:initial_sell_in) { 1 }
        Then { item.quality.should == initial_quality+3 }
        Then { item.sell_in.should == initial_sell_in-1 }

        context "at max quality" do
          Given(:initial_quality) { 50 }
          Then { item.quality.should == initial_quality }
        end
      end

      context "on sell date" do
        Given(:initial_sell_in) { 0 }
        Then { item.quality.should == 0 }
        Then { item.sell_in.should == initial_sell_in-1 }
      end

      context "after sell date" do
        Given(:initial_sell_in) { -10 }
        Then { item.quality.should == 0 }
        Then { item.sell_in.should == initial_sell_in-1 }
      end
    end

    context "conjured item" do
      Given(:name) { "Conjured Mana Cake" }

      context "before the sell date" do
        Given(:initial_sell_in) { 5 }
        Then { item.quality.should == initial_quality-2 }
        Then { item.sell_in.should == initial_sell_in-1 }

        context "at zero quality" do
          Given(:initial_quality) { 0 }
          Then { item.quality.should == initial_quality }
        end
      end

      context "on sell date" do
        Given(:initial_sell_in) { 0 }
        Then { item.quality.should == initial_quality-2 }
        Then { item.sell_in.should == initial_sell_in-1 }

        context "at zero quality" do
          Given(:initial_quality) { 0 }
          Then { item.quality.should == initial_quality }
        end
      end

      context "after sell date" do
        Given(:initial_sell_in) { -10 }
        Then { item.quality.should == initial_quality-2 }
        Then { item.sell_in.should == initial_sell_in-1 }

        context "at zero quality" do
          Given(:initial_quality) { 0 }
          Then { item.quality.should == initial_quality }
        end
      end
    end
  end

  context "with several objects" do
    Given(:items) {
      [
        Item.new("NORMAL ITEM", 5, 10),
        Item.new("Aged Brie", 3, 10),
      ]
    }

    When { update_quality(items) }

    Then { items[0].quality.should == 9 }
    Then { items[0].sell_in.should == 4 }

    Then { items[1].quality.should == 11 }
    Then { items[1].sell_in.should == 2 }
  end
end

以上是关于ruby Guilded Rose Kata al WeArePeople的主要内容,如果未能解决你的问题,请参考以下文章

Ruby rose

全球最帅女孩Ruby Rose

LES圈T神ruby rose,到底多少直人会认识?

ruby rose

欣赏▏Ruby Rose

为什么我们爱Ruby Rose这样的女孩?