[ next ] [ prev ] [ contents ] XP-Cinti TDD Workshop

Code after Story One

So, where are we? After story one our source code looks like this.

Test Run

  
$ ruby testnet.rb
Loaded suite testnet
Started
......
Finished in 0.006909 seconds.
5 tests, 19 assertions, 0 failures, 0 errors

Unit Tests

# file: testnet.rb
require 'test/unit'
require 'net'

class TestNet < Test::Unit::TestCase
  def setup
    @net = Net.new
  end

  def test_tied
    assert_equal true, ! @net.tied?(1,1)
  end

  def test_tie
    @net.tie(1,1)
    assert_equal true, @net.tied?(1,1)
  end

  def test_multiple_positions
    @net.tie(1,1)
    assert_equal true, @net.tied?(1,1)
    assert_equal false, @net.tied?(1,2)
    assert_equal false, @net.tied?(2,1)
    assert_equal false, @net.tied?(2,2)
  end

  def test_untie
    @net.tie(1,1)
    @net.untie(1,1)
    assert_equal false, @net.tied?(1,1)
  end

  def test_max_matrix_size
    check_location(1,1)
    check_location(1,5)
    check_location(5,1)
    check_location(5,5)
  end

  def check_location(x,y)
    assert_equal false, @net.tied?(x,y)
    @net.tie(x,y)
    assert_equal true, @net.tied?(x,y)
    @net.untie(x,y)
    assert_equal false, @net.tied?(x,y)
  end
end

Net Class

# file: net.rb
class Net
  MAXSIZE=5

  def initialize
    @tied = (1..MAXSIZE).collect {
      (1..MAXSIZE).collect {false}
    }
  end

  def tied?(x,y)
    @tied[x-1][y-1]
  end

  def tie(x,y)
    @tied[x-1][y-1] = true
  end

  def untie(x,y)
    @tied[x-1][y-1] = false
  end
end


[ next ] [ prev ] [ contents ] Copyright 2003 by Jim Weirich.
Some Rights Reserved