require 'test/unit' require './bsagent' require 'fileutils' class TestMyAgent < Test::Unit::TestCase def setup if File.exist?('bsagent.rb') FileUtils.copy('bsagent.rb', 'bsagent.rb.save') begin FileUtils.copy('bsagent-api.yaml', 'bsagent-api.yaml.save') rescue; end 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_show_api actual, s = @agent.send("show api") assert_match(/ is a synonym for /, actual) end def test_show_one_method_api actual, s = @agent.send("show show_api's api") assert_match(/show api/, actual) end def testswap1 actual, s = @agent.send("AA: your liabilities are Fixed rate borrowing bond 5.375") end def test_buy @agent.send("Farmer: your assets are $10 goods.") @agent.send("Citizen: your assets are $10 deposit.") @agent.send("Bank: your assets are $10 cash and your liabilities are $10 deposit") actual, s = @agent.send("Citizen: buy $10 goods from Farmer for $10 deposit.") actual, s = @agent.send("show Bank and Citizen balance sheets") assert_match(/Citizen.*\$10.00 goods/m, actual) end def test_decrement_entry @agent.send("Bank: your assets are Reserves and your liabilities are deposits.") actual, s = @agent.send("Bank: increment reserves.") assert_match(/Reserves decremented./i, actual) end def teardown File.delete('bsagent.rb') unless !File.exist?('bsagent.rb.save') begin; File.delete('bsagent-api.yaml'); rescue; end if File.exist?('bsagent.rb.save') FileUtils.copy('bsagent.rb.save', 'bsagent.rb') begin; FileUtils.copy('bsagent-api.yaml.save', 'bsagent-api.yaml'); rescue; end end if File.exist?('bsagent.rb') then File.delete('bsagent.rb.save') end if File.exist?('bsagent-api.yaml.save') then File.delete('bsagent-api.yaml.save') end end end