*To*: coders@xxxxxxxxxxx*Subject*: [coders] [NRS] Test code*From*: Matthew Palmer <mpalmer@xxxxxxxxxxx>*Date*: Wed, 21 Jun 2006 00:10:46 +1000*User-agent*: Mutt/1.5.9i

Unit testing, yeah yeah! As discussed tonight, I've got all of the test suites below you'll need to do some serious testing of your methods. Before you start doing any of this, you'll need to move your bodgy_rounding() method to a file called nrs_payroll.rb, and then save each of these test suites to separate files in the same directory as your nrs_payroll.rb file. To run a test suite, just point Ruby at it and it should print useful output like what you saw in my test runs tonight. For example, to run the first test suite, just run "ruby ./simple_br_test.rb". So, test suite one -- the first test method is simply a conversion of our home-grown assert_equal testing to the Test::Unit system. Your bodgy_rounding() method should satisfy these requirements easily. The second method is the extra negative assertions we worked out, and your bodgy_rounding method will have to be enhanced with some extra logic to handle this case. Remember to re-run your tests regularly to ensure that you haven't broken something. ---[BEGIN simple_br_test.rb]--- require 'test/unit' require 'nrs_payroll' class SimpleBodgyRoundingTest < Test::Unit::TestCase def test_bodgy_rounding assert_equal(10, bodgy_rounding(12)) assert_equal(15, bodgy_rounding(13)) assert_equal(0, bodgy_rounding(1)) assert_equal(0, bodgy_rounding(0)) assert_equal(5, bodgy_rounding(7)) assert_equal(10, bodgy_rounding(7.01)) end def test_negative_bodgy_rounding assert_equal(0, bodgy_rounding(-1)) assert_equal(0, bodgy_rounding(-2)) assert_equal(0, bodgy_rounding(-2.99)) assert_equal(-3, bodgy_rounding(-3)) assert_equal(-7.39, bodgy_rounding(-7.39)) assert_equal(-10, bodgy_rounding(-10)) assert_equal(-71.39, bodgy_rounding(-71.39)) assert_equal(-72.39, bodgy_rounding(-72.39)) assert_equal(-73.39, bodgy_rounding(-73.39)) assert_equal(-74.39, bodgy_rounding(-74.39)) end end ---[END simple_br_test.rb]--- Next, we have the modification of bodgy_rounding() to return an array of two values instead of a single number, representing both the rounded value, and the amount that was added/removed to get to the rounded value. You will need to modify the return statement of your bodgy_rounding() implementation, and perhaps work out where to hook the leftover calculation, but if you're doing major surgery to your code, you're probably doing it wrong. ---[BEGIN complex_br_test.rb]--- require 'test/unit' require 'nrs_payroll' class ComplexBodgyRoundingTest < Test::Unit::TestCase def test_bodgy_rounding assert_equal([10, -2], bodgy_rounding(12)) assert_equal([15, 2], bodgy_rounding(13)) assert_equal([0, -1], bodgy_rounding(1)) assert_equal([0, 0], bodgy_rounding(0)) assert_equal([5, -2], bodgy_rounding(7)) assert_equal([10, 2.99], bodgy_rounding(7.01)) end def test_negative_bodgy_rounding assert_equal([0, 1], bodgy_rounding(-1)) assert_equal([0, 2], bodgy_rounding(-2)) assert_equal([0, 2.99], bodgy_rounding(-2.99)) assert_equal([-3, 0], bodgy_rounding(-3)) assert_equal([-7.39, 0], bodgy_rounding(-7.39)) assert_equal([-10, 0], bodgy_rounding(-10)) assert_equal([-71.39, 0], bodgy_rounding(-71.39)) assert_equal([-72.39, 0], bodgy_rounding(-72.39)) assert_equal([-73.39, 0], bodgy_rounding(-73.39)) assert_equal([-74.39, 0], bodgy_rounding(-74.39)) end end ---[END complex_br_test.rb]--- And finally, you need to add a new method to your nrs_payroll.rb file called money_breakup(), which takes your dollar amount and returns the number of different denomination notes needed -- optimised, always, for the least number of notes. Since we haven't covered error handling just yet, I've left out the invalid inputs for this test suite, however we will be looking at that next week. ---[START money_breakup_test.rb]--- require 'test/unit' require 'nrs_payroll' class MoneyBreakupTest < Test::Unit::TestCase def test_money_breakup assert_equal([0, 0, 0, 0], money_breakup(0)) assert_equal([1, 0, 0, 0], money_breakup(5)) assert_equal([0, 1, 0, 0], money_breakup(10)) assert_equal([0, 0, 1, 0], money_breakup(20)) assert_equal([0, 0, 0, 1], money_breakup(50)) assert_equal([1, 0, 2, 0], money_breakup(45)) assert_equal([1, 1, 1, 1], money_breakup(85)) assert_equal([1, 0, 2, 1], money_breakup(95)) assert_equal([1, 0, 0, 2], money_breakup(105)) assert_equal([1, 0, 2, 2], money_breakup(145)) assert_equal([0, 0, 0, 2], money_breakup(100)) assert_equal([0, 0, 0, 20], money_breakup(1000)) assert_equal([1, 0, 0, 20], money_breakup(1005)) assert_equal([1, 0, 0, 20], money_breakup(1005)) assert_equal([0, 1, 1, 20], money_breakup(1030)) assert_equal([1, 1, 1, 20], money_breakup(1035)) assert_equal([1, 0, 2, 20], money_breakup(1045)) end end ---[END money_breakup_test.rb]--- And that's basically it for the week. Have fun with it. - Matt -- A friend is someone you can call to help you move. A best friend is someone you can call to help you move a body.

**Follow-Ups**:**Re: [coders] [NRS] Test code***From:*Graham Smith

**Re: [coders] [NRS] Test code***From:*O Plameras

**Messages sorted by:**[ date | thread ]- Prev by Date:
**Re: [coders] The l33test codes evah! (Or how computed goto broke my brain!)** - Next by Date:
**[coders] ri's Definition of Methods Confusing** - Previous by thread:
**Re: [coders] The l33test codes evah! (Or how computed goto broke my brain!)** - Next by thread:
**Re: [coders] [NRS] Test code**