require 'test/unit' require 'calcbot' require 'fileutils' require 'net/http' BasicSocket.do_not_reverse_lookup = true class TestMyBot < Test::Unit::TestCase def setup if File.exist?('calcagent.rb') FileUtils.copy('calcagent.rb', 'calcagent.rb.save') begin FileUtils.copy('calcagent-api.yaml', 'calcagent-api.yaml.save') rescue; end end @agent = MyBot.new end def test_name actual, s = @agent.send("what is your name?") assert_match(/calcbot/, actual) end def test_volume actual, s = @agent.send("CalcBot: what is your volume?") assert_match(/1/, actual) end def test_rules actual, s = @agent.send("CalcBot: loud") assert_match(/0/, actual) actual, s = @agent.send("CalcBot: delete all rules") assert_match(/Okay/i, actual) actual, s = @agent.send("CalcBot: 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("CalcBot: if response =~ /foo!/ then response = 'bar' end") assert_match(/Okay/, actual) actual, s = @agent.send("testing") assert_match(/bar/, actual) actual, s = @agent.send("CalcBot: delete rule 1") assert_match(/Okay/, actual) actual, s = @agent.send("testing") assert_match(/default response/i, actual) actual, s = @agent.send("CalcBot: 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("CalcBot: delete all rules") assert_match(/Okay/, actual) actual, s = @agent.send("CalcBot: show rules") assert_match(//, actual) actual, s = @agent.send("testing") assert_match(/Default response/, actual) end def teardown File.delete('calcagent.rb') unless !File.exist?('calcagent.rb.save') begin; File.delete('calcagent-api.yaml'); rescue; end if File.exist?('calcagent.rb.save') FileUtils.copy('calcagent.rb.save', 'calcagent.rb') begin; FileUtils.copy('calcagent-api.yaml.save', 'calcagent-api.yaml'); rescue; end end if File.exist?('calcagent.rb') then File.delete('calcagent.rb.save') end if File.exist?('calcagent-api.yaml.save') then File.delete('calcagent-api.yaml.save') end end end