require 'test/unit' require 'todoagent' require 'fileutils' class TestMyAgent < Test::Unit::TestCase def setup if File.exist?('todoagent.rb') FileUtils.copy('todoagent.rb', 'todoagent.rb.save') begin FileUtils.copy('todoagent-api.yaml', 'todoagent-api.yaml.save') rescue; end end if File.exist?("todos.txt") FileUtils.move("todos.txt", "todos.txt.save") end @agent = MyAgent.new end def test_defining_hello actual, s = @agent.send("add pattern /^hello/, hello") actual, s = @agent.send("hello") actual, s = @agent.send("> def hello(pattern=nil, input=nil)") actual, s = @agent.send("> if @count == nil; @count = 1; return 'I am ready for my first lesson.'; end") actual, s = @agent.send("> return 'Hello again.'") actual, s = @agent.send("> end # method_def") actual, s = @agent.send("hello") assert_match(/lesson/, actual) actual, s = @agent.send("hi is a synonym for hello") assert_match(/Okay/, actual) actual, s = @agent.send("hi") assert_match(/again/, actual) end def test_todo actual, s = @agent.send("TODO: fix todoagent") assert_match(/Okay/, actual) actual, s = @agent.send("show todos") assert_match(/fix todoagent/, actual) actual, s = @agent.send("delete item 1") assert_match(/Okay/, actual) actual, s = @agent.send("show todos") if actual =~ /fix todoagent/ then assert_match(/blah/, 'foo') end end def teardown File.delete('todoagent.rb') unless !File.exist?('todoagent.rb.save') begin; File.delete('todoagent-api.yaml'); rescue; end if File.exist?('todoagent.rb.save') FileUtils.copy('todoagent.rb.save', 'todoagent.rb') begin; FileUtils.copy('todoagent-api.yaml.save', 'todoagent-api.yaml'); rescue; end end if File.exist?('todoagent.rb') then File.delete('todoagent.rb.save') end if File.exist?('todoagent-api.yaml.save') then File.delete('todoagent-api.yaml.save') end # now handle todos.txt begin if File.exist?('todos.txt') then File.delete('todos.txt') end rescue; end # why does this throw a "Permission Denied" error? if File.exist?('todos.txt.save') FileUtils.copy('todos.txt.save', 'todos.txt') end if File.exist?('todos.txt.save') then File.delete('todos.txt.save') end end end