diff --git a/GildedRose/.gitignore b/GildedRose/.gitignore new file mode 100644 index 00000000..7e99e367 --- /dev/null +++ b/GildedRose/.gitignore @@ -0,0 +1 @@ +*.pyc \ No newline at end of file diff --git a/GildedRose/README.md b/GildedRose/README.md index fba63187..09d6f98e 100644 --- a/GildedRose/README.md +++ b/GildedRose/README.md @@ -10,21 +10,26 @@ You could write some unit tests yourself, using the requirements to identify sui Alternatively, use the "Text-Based" tests provided in this repository. (Read more about that in the next section) -I've also set this kata up on [cyber-dojo](http://cyber-dojo.com), so you can get going really quickly for the following languages and test frameworks: +I've also set this kata up on [cyber-dojo](http://cyber-dojo.com) for several languages, so you can get going really quickly: - [Cucumber, Java](http://cyber-dojo.com/forker/fork/0F82D4BA89?avatar=gorilla&tag=45) - for this one I've also written some step definitions for you - [JUnit, Java](http://cyber-dojo.com/forker/fork/751DD02C4C?avatar=snake&tag=4) - [C#](http://cyber-dojo.com/forker/fork/107907AD1E?avatar=alligator&tag=13) +- [Ruby](http://cyber-dojo.com/forker/fork/A8943EAF92?avatar=hippo&tag=9) +- [RSpec, Ruby](http://cyber-dojo.com/forker/fork/8E58B0AD16?avatar=raccoon&tag=3) +- [Python](http://cyber-dojo.com/forker/fork/297041AA7A?avatar=lion&tag=4) -Whichever testing approach you choose, the idea of the exercise is to do some deliberate practice, and improve your Refactoring skills. The idea is not to re-write the code from scratch, but rather to practice taking small steps, running the tests often, and incrementally improving the design. -## Text-Based Testing -This is a testing approach which is very useful when refactoring legacy code. The basic idea is to create tests that use the text which the code produces. Before you change the code, you run it, and save the output as a "Golden Copy". Then after you change the code, you run it again, and compare the output against the Golden Copy. Any differences, and the test fails. +Whichever testing approach you choose, the idea of the exercise is to do some deliberate practice, and improve your skills at designing test cases and refactoring. The idea is not to re-write the code from scratch, but rather to practice designing tests, taking small steps, running the tests often, and incrementally improving the design. + +## Text-Based Approval Testing + +This is a testing approach which is very useful when refactoring legacy code. Before you change the code, you run it, and gather the output of the code as a plain text file. You review the text, and if it correctly describes the behaviour as you understand it, you can "approve" it, and save it as a "Golden Master". Then after you change the code, you run it again, and compare the new output against the Golden Master. Any differences, and the test fails. It's basically the same idea as "assertEquals(expected, actual)" in a unit test, except the text you are comparing is typically much longer, and the "expected" value is saved from actual output, rather than being defined in advance. -Typically a piece of legacy code may not produce suitable textual output from the start, so you may need to modify it before you can write your first text-based test. That could involve inserting log statements into the code, or just writing a "main" method that executes the code and prints out what the result is afterwards. It's this latter approach we are using here to test GildedRose. +Typically a piece of legacy code may not produce suitable textual output from the start, so you may need to modify it before you can write your first text-based approval test. That could involve inserting log statements into the code, or just writing a "main" method that executes the code and prints out what the result is afterwards. It's this latter approach we are using here to test GildedRose. The Text-Based tests in this repository are designed to be used with the tool "TextTest" (http://texttest.org). This tool helps you to organize and run text-based tests. There is more information in the README file in the "texttests" subdirectory. diff --git a/GildedRose/python/gilded_rose.py b/GildedRose/python/gilded_rose.py index 67ea3849..69459ac3 100755 --- a/GildedRose/python/gilded_rose.py +++ b/GildedRose/python/gilded_rose.py @@ -1,38 +1,41 @@ # -*- coding: utf-8 -*- +class GildedRose(object): -def update_quality(items): - for item in items: - if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert": - if item.quality > 0: - if item.name != "Sulfuras, Hand of Ragnaros": - item.quality = item.quality - 1 - else: - if item.quality < 50: - item.quality = item.quality + 1 - if item.name == "Backstage passes to a TAFKAL80ETC concert": - if item.sell_in < 11: - if item.quality < 50: - item.quality = item.quality + 1 - if item.sell_in < 6: - if item.quality < 50: - item.quality = item.quality + 1 - if item.name != "Sulfuras, Hand of Ragnaros": - item.sell_in = item.sell_in - 1 - if item.sell_in < 0: - if item.name != "Aged Brie": - if item.name != "Backstage passes to a TAFKAL80ETC concert": - if item.quality > 0: - if item.name != "Sulfuras, Hand of Ragnaros": - item.quality = item.quality - 1 - else: - item.quality = item.quality - item.quality + def __init__(self, items): + self.items = items + + def update_quality(self): + for item in self.items: + if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert": + if item.quality > 0: + if item.name != "Sulfuras, Hand of Ragnaros": + item.quality = item.quality - 1 else: if item.quality < 50: item.quality = item.quality + 1 - return items - + if item.name == "Backstage passes to a TAFKAL80ETC concert": + if item.sell_in < 11: + if item.quality < 50: + item.quality = item.quality + 1 + if item.sell_in < 6: + if item.quality < 50: + item.quality = item.quality + 1 + if item.name != "Sulfuras, Hand of Ragnaros": + item.sell_in = item.sell_in - 1 + if item.sell_in < 0: + if item.name != "Aged Brie": + if item.name != "Backstage passes to a TAFKAL80ETC concert": + if item.quality > 0: + if item.name != "Sulfuras, Hand of Ragnaros": + item.quality = item.quality - 1 + else: + item.quality = item.quality - item.quality + else: + if item.quality < 50: + item.quality = item.quality + 1 + class Item: def __init__(self, name, sell_in, quality): self.name = name diff --git a/GildedRose/python/test_gilded_rose.py b/GildedRose/python/test_gilded_rose.py index e615c2ca..51480d19 100644 --- a/GildedRose/python/test_gilded_rose.py +++ b/GildedRose/python/test_gilded_rose.py @@ -1,14 +1,15 @@ # -*- coding: utf-8 -*- import unittest -from gilded_rose import Item, update_quality + +from gilded_rose import Item, GildedRose class GildedRoseTest(unittest.TestCase): def test_foo(self): items = [Item("foo", 0, 0)] - update_quality(items) + gilded_rose = GildedRose(items) + gilded_rose.update_quality() self.assertEquals("fixme", items[0].name) - -if __name__ == "__main__": +if __name__ == '__main__': unittest.main() diff --git a/GildedRose/ruby/.rspec b/GildedRose/ruby/.rspec new file mode 100644 index 00000000..0d9a0dd8 --- /dev/null +++ b/GildedRose/ruby/.rspec @@ -0,0 +1,2 @@ +--colour +--format nested diff --git a/GildedRose/ruby/gilded_rose.rb b/GildedRose/ruby/gilded_rose.rb index 1107408a..76cd93c4 100755 --- a/GildedRose/ruby/gilded_rose.rb +++ b/GildedRose/ruby/gilded_rose.rb @@ -1,7 +1,11 @@ class GildedRose - def update_quality(items) - items.each do |item| + def initialize(items) + @items = items + end + + def update_quality() + @items.each do |item| if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert" if item.quality > 0 if item.name != "Sulfuras, Hand of Ragnaros" @@ -46,7 +50,6 @@ class GildedRose end end end - items end end @@ -62,4 +65,4 @@ class Item def to_s() "#{@name}, #{@sell_in}, #{@quality}" end -end +end \ No newline at end of file diff --git a/GildedRose/ruby/gilded_rose_spec.rb b/GildedRose/ruby/gilded_rose_spec.rb index e6dce6f4..9d86c230 100644 --- a/GildedRose/ruby/gilded_rose_spec.rb +++ b/GildedRose/ruby/gilded_rose_spec.rb @@ -5,7 +5,7 @@ describe GildedRose do describe "#update_quality" do it "does not change the name" do items = [Item.new("foo", 0, 0)] - GildedRose.new().update_quality(items) + GildedRose.new(items).update_quality() items[0].name.should == "fixme" end end diff --git a/GildedRose/ruby/gilded_rose_tests.rb b/GildedRose/ruby/gilded_rose_tests.rb new file mode 100644 index 00000000..2e1b70d1 --- /dev/null +++ b/GildedRose/ruby/gilded_rose_tests.rb @@ -0,0 +1,12 @@ +require File.join(File.dirname(__FILE__), 'gilded_rose') +require 'test/unit' + +class TestUntitled < Test::Unit::TestCase + + def test_foo + items = [Item.new("foo", 0, 0)] + GildedRose.new(items).update_quality() + assert_equal items[0].name, "fixme" + end + +end \ No newline at end of file