Skip to content

Instantly share code, notes, and snippets.

@joealba
Last active May 18, 2017 06:30
Show Gist options
  • Select an option

  • Save joealba/85e97de45f79bace40e3 to your computer and use it in GitHub Desktop.

Select an option

Save joealba/85e97de45f79bace40e3 to your computer and use it in GitHub Desktop.
Roman numeral kata
Roman Numeral Converter -- TDD Practice
Using your language of choice, create a RomanNumeralConverter class with a decimal_to_roman method that takes one positive integer argument and returns that argument represented as a Roman numeral.
However, there's one hitch. You can't write any code until you have a failing test case. We've started you off with the first test in multiple languages. Now you must think about what test you would add next, then write the code to make the test pass. Work with your team to think about the additional tests that it would take to make you confident that -- if these tests all passed -- you'd have code that would work in a sufficient number of edge cases. (Covering up to a max value of 3000 is sufficient for this exercise.)
Sure, you could cheat and Google for one of the elegant solutions that are available. But the point of this exercise isn't to find that most elegant solution... It's to practice test-driven development and involve the non-coders in thinking up edge cases that your tests should cover (and *not* cover).
For extra credit, write the roman_to_decimal version too.
#########################################
## JUnit
public class RomanNumeralConverterTest {
@Test
public void one() {
Assert.assertEquals("1", "I", RomanNumeralConverter.decimalToRoman(1));
}
}
#########################################
## Groovy
@Speck
class RomanNumeral {
def "Testing Roman Numeral Converter"() {
expect:
r == RomanNumeralConverter.decimalToRoman( d )
where:
[r, d] << ["I", 1]
[r, d] << ["V", 5]
}
}
#########################################
## NUnit
[TestFixture]
public class RomanNumeralConverterTest
{
[Test]
public void When_passed_1_should_convert_to_i()
{
Assert.AreEqual("I", RomanNumeralConverter.DecimalToRoman("1"));
}
}
#########################################
## Ruby RSpec
describe RomanNumeralConverter do
let(:converter) { RomanNumeralConverter.new }
it "converts 1 to I" do
expect(converter.decimal_to_roman(1)).to eq('I')
end
end
#########################################
## Python
import unittest
from src.roman_numeral_converter import RomanNumeralConverter
class RomanNumeralConverterTest(unittest.TestCase):
def setUp(self):
self.my_converter = RomanNumeralConverter()
def test_converts_one(self):
self.assertEquals(self.my_converter.decimal_to_roman(1), "I")
if __name__ == '__main__':
unittest.main()
#########################################
<?php
require_once "PHPUnit/Autoload.php";
class RomanNumeralConverter {
public function decimal_to_roman($num) {
return 'I';
}
}
class RomanNumeralConverterTest extends PHPUnit_Framework_TestCase {
protected $converter;
protected function setUp() {
$this->converter = new RomanNumeralConverter();
}
public function testOne() {
$this->assertEquals($this->converter->decimal_to_roman(1), 'I');
}
}
?>
##############################
## CREDITS:
https://github.com/jimweirich/presentation_kata_and_analysis/
https://github.com/Celtis/roman-numeral-kata-php/
https://github.com/jfpdazey/python-roman-numerals-kata/
http://nosuchblogger.com/post/37/code-kata-roman-numerals-c-part-1
http://securesoftwaredev.com/2011/12/05/practicing-tdd-using-the-roman-numerals-kata/
http://css.dzone.com/articles/roman-numerals-kata-tdd-and
https://github.com/ruby-fatecsp/dojos/blob/8cb15023eec6d4a5c9cdcf1723adff1ed5864a03/roman_to_numerals/lib/roman_to_num.rb
http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment