require 'test/unit' require 'MyBot' require 'fileutils' class TestMyBot < Test::Unit::TestCase def setup if File.exist?('MyAgent.rb') FileUtils.copy('MyAgent.rb', 'MyAgent.rb.save') begin FileUtils.copy('MyAgent-api.yaml', 'MyAgent-api.yaml.save') rescue; end end @agent = MyBot.new end def test_name actual, s = @agent.send("what is your name?") assert_match(/mybot/, actual) end def test_volume actual, s = @agent.send("MyBot: what is your volume?") assert_match(/1/, actual) end def test_rules actual, s = @agent.send("MyBot: loud") assert_match(/0/, actual) actual, s = @agent.send("MyBot: if response =~ /default response/i then response = 'foo!' end") assert_match(/Okay/, actual) actual, s = @agent.send("testing") assert_match(/foo!/, actual) actual, s = @agent.send("MyBot: if response =~ /foo!/ then response = 'bar' end") assert_match(/Okay/, actual) actual, s = @agent.send("testing") assert_match(/bar/, actual) actual, s = @agent.send("MyBot: delete rule 1") assert_match(/Okay/, actual) actual, s = @agent.send("testing") assert_match(/default response/i, actual) actual, s = @agent.send("MyBot: if response =~ /default response/i then response = 'fubar' end") assert_match(/Okay/, actual) actual, s = @agent.send("testing") assert_match(/fubar/, actual) actual, s = @agent.send("MyBot: delete all rules") assert_match(/Okay/, actual) actual, s = @agent.send("MyBot: show rules") assert_match(//, actual) actual, s = @agent.send("testing") assert_match(/Default response/, actual) actual, s = @agent.send("what is the debug mode?") assert_match(/off/i, actual) actual, s = @agent.send("mybot: turn debug mode on") assert_match(/on/i, actual) actual, s = @agent.send("mybot: what is the debug mode?") assert_match(/on/i, actual) actual, s = @agent.send("mybot: please turn off the debug mode.") assert_match(/off/i, actual) actual, s = @agent.send("mybot: what is the debug mode?") assert_match(/off/i, actual) end def teardown File.delete('MyAgent.rb') unless !File.exist?('MyAgent.rb.save') begin; File.delete('MyAgent-api.yaml'); rescue; end if File.exist?('MyAgent.rb.save') FileUtils.copy('MyAgent.rb.save', 'MyAgent.rb') begin; FileUtils.copy('MyAgent-api.yaml.save', 'MyAgent-api.yaml'); rescue; end end if File.exist?('MyAgent.rb') then File.delete('MyAgent.rb.save') end if File.exist?('MyAgent-api.yaml.save') then File.delete('MyAgent-api.yaml.save') end end end