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

Code after Story Two

Story two made us change the way our Net object worked in some fundamental ways. But the changes were not difficult and the unit tests caught anything we did not think of during the test.

Here's how the code looks now.

Test Run

  
$ ruby testnet.rb 
Loaded suite testnet
Started
.....
Finished in 0.006673 seconds.
4 tests, 24 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_initially_empty
    assert_equal :EMPTY, @net[1,1]
  end

  def test_place_token
    @net[1,1] = :BLACK
    assert_equal :BLACK, @net[1,1]
    @net[1,1] = :WHITE
    assert_equal :WHITE, @net[1,1]
    @net[1,1] = :EMPTY
    assert_equal :EMPTY, @net[1,1]
  end

  def test_multiple_positions
    @net[1,1] = :BLACK
    assert_equal :BLACK, @net[1,1]
    assert_equal :EMPTY, @net[1,2]
    assert_equal :EMPTY, @net[2,1]
    assert_equal :EMPTY, @net[2,2]
  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 :EMPTY, @net[x,y]
    @net[x,y] = :BLACK
    assert_equal :BLACK, @net[x,y]
    @net[x,y] = :WHITE
    assert_equal :WHITE, @net[x,y]
    @net[x,y] = :EMPTY
    assert_equal :EMPTY, @net[x,y]
  end
end

Net Class

# file: net.rb
class Net
  MAXSIZE=5

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

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

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


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