mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2025-12-12 20:32:15 +00:00
merge
This commit is contained in:
commit
2092815e48
1
GildedRose/.gitignore
vendored
Normal file
1
GildedRose/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.pyc
|
||||
@ -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.
|
||||
|
||||
|
||||
@ -1,8 +1,12 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
class GildedRose(object):
|
||||
|
||||
def update_quality(items):
|
||||
for item in items:
|
||||
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":
|
||||
@ -30,7 +34,6 @@ def update_quality(items):
|
||||
else:
|
||||
if item.quality < 50:
|
||||
item.quality = item.quality + 1
|
||||
return items
|
||||
|
||||
|
||||
class Item:
|
||||
|
||||
@ -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()
|
||||
|
||||
2
GildedRose/ruby/.rspec
Normal file
2
GildedRose/ruby/.rspec
Normal file
@ -0,0 +1,2 @@
|
||||
--colour
|
||||
--format nested
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
12
GildedRose/ruby/gilded_rose_tests.rb
Normal file
12
GildedRose/ruby/gilded_rose_tests.rb
Normal file
@ -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
|
||||
Loading…
Reference in New Issue
Block a user