Hello folks,Tonight I tried out some of that good old extreme programming TDD stuffwith mixed results.
I was initially surprised how much it helped get things started butI noticed after a while that it kind of petered out because I wasn'tsure just how to test the code that I was writing.
Naturally, you will say, I should have kept to writing tests first.Well the damage is done. So now I would like to ask for suggestionson how to write a test for this code so I can learn for next time.Also, if I have done anything really stupid in the code and thereis a more rubyish way to do this please feel free to set me strait.
Thank you very much!
Here is my test:
require 'test/unit'require 'Lab'class TestLab < Test::Unit::TestCase def setup@maze = <<MAZE_STRING##########################s--###----#####-#####-#####-#---##-##--#-##-----####-#-#-#--##-##-##-###-##---#-#-#-###-##-##-##--##-#---#-#---#-##----##-###-#-#-#-#-#-#-##-#########-#-#-#-#-#---##--------######-###---###########-######-#####-###########-######------------------e##########################MAZE_STRING
@t = Tracker.new(@maze) end def test_grid assert_equal @maze[25].chr,"\n","uhoh" end def test_tracker assert_equal @t.track(@maze,27),28,"doesnt track" assert_equal @t.track(@maze,28),29,"doesnt track" endend
And Now the Code:
class Tracker def initialize(map) @forest = map end def track(path,mark) prey = '*-es' if prey.index(path[mark+1]) mark+1 elsif prey.index(path[mark+26]) mark+26 elsif prey.index(path[mark-1]) mark-1 elsif prey.index(path[mark-26]) mark-26 end end def get_it! for_teh_win = nil @s = @forest.index('s') until for_teh_win @s = track(@forest,@s) if @forest[@s].chr == 'e' for_teh_win = "WOOT!" else @forest[@s] = '*' end end puts @forest endendme = Tracker.new(maze)me.get_it!
--Alex Combashttp://noodlejunkie.com/