require 'test/unit' begin require 'logicbot' rescue LoadError require_relative 'logicbot' end BasicSocket.do_not_reverse_lookup = true class TestLogicBot < Test::Unit::TestCase def setup @agent = LogicBot.new actual, s = @agent.send("reset graph") assert_match(/Okay/, actual) end def test_save_mode begin; File.delete("test_graph.yaml"); rescue; end #@actual, s = @agent.send("set save mode off") #assert_match(/off/, actual) actual, s = @agent.send("mercury is the first planet from the sun") actual, s = @agent.send("quit") assert_match(/save.*graph/, actual) actual, s = @agent.send("save the graph to test_graph.yaml") assert_match(/test_graph.yaml saved/, actual) assert(File.exist?("test_graph.yaml")) actual, s = @agent.send("quit") assert_match(/(?:quit|bye)/i, actual) end def test_save_mode2 begin; File.delete("test_graph.yaml"); rescue; end actual, s = @agent.send("jupiter is a planet") assert_match(/Okay/, actual) actual, s = @agent.send("quit") assert_match(/Do.*save.*graph/, actual) actual, s = @agent.send("no") assert_match(/(?:quit|bye)/, actual) end def test_save_mode3 actual, s = @agent.send("load graph graph.yaml") actual, s = @agent.send("forget that mars is a planet") actual, s = @agent.send("set save mode off") actual, s = @agent.send("mars is a planet") actual, s = @agent.send("quit") assert_match(/do.*save.*graph/i, actual) actual, s = @agent.send("no") assert_match(/^(?:quit|bye)$/, actual) end def test_rules actual, s = @agent.send("logicagent: if input =~ /\\bmy\\b/ then response.gsub!(/\\bmy\\b/, 'your') end") assert_match(/Okay/i, actual) actual, s = @agent.send("My table is orange") actual, s = @agent.send("orange is a color") actual, s = @agent.send("what color is my table?") assert_match(/your table is orange/i, actual) actual, s = @agent.send("My table is round") actual, s = @agent.send("it = my table") actual, s = @agent.send("round is a shape") actual, s = @agent.send("what shape is it?") assert_match(/\bround\b/, actual) actual, s = @agent.send("quit") assert_match(/Do.*save.*graph/, actual) actual, s = @agent.send("no") assert_match(/(?:quit|bye)/, actual) end def test_pronouns actual, s = @agent.send("my appt is at 2:30pm") actual, s = @agent.send("when is my appt?") assert_match(/2:30pm/, actual) end def test_nothing actual, s = @agent.send("a sandwich is better than nothing") actual, s = @agent.send("nothing is better than a million dollars") actual, s = @agent.send("is a sandwich better than nothing?") assert_match(/Yes/i, actual) actual, s = @agent.send("is a sandwich better than a million dollars?") assert_match(/no knowledge/, actual) end def test_jack_loves_jill actual, s = @agent.send("Everybody loves somebody.") assert_match(/Okay/i, actual) actual, s = @agent.send("Everybody loves a lover.") assert_match(/okay/i, actual) actual, s = @agent.send("love = loves") actual, s = @agent.send("logicbot: if response =~ / is love / then response.sub!(/ is love /, ' loves ') end") actual, s = @agent.send('logicbot: if input =~ /if (.*) loves (.*), then (.*) is a lover/i then grp1 = $1; r,s = self.send("does #{$1} love #{$2}?"); if r =~ /^Yes/i then self.send("#{grp1} = a lover."); response = "Okay, #{grp1} is a lover."; end; if response == nil then response = "Okay" end; end') assert_match(/Okay/i, actual) actual, s = @agent.send('logicbot: if input =~ /(.*) loves (.*)/ then self.send("if #{$1} loves #{$2}, then #{$1} is a lover.") end') assert_match(/Okay/i, actual) actual, s = @agent.send("Everybody includes Jill.") actual, s = @agent.send("If Jill loves somebody, then Jill is a lover.") assert_match(/Okay/i, actual) actual, s = @agent.send("Does Jack love Jill?") assert_match(/no knowledge/, actual) actual, s = @agent.send("Everybody includes Jack.") actual, s = @agent.send("Does Jack love Jill?") assert_match(/^Yes/i, actual) end def test_is_X_a_parent_of_Y actual, s = @agent.send("logicbot: if input =~ /(.*) (is a parent of) (.*)/ then self.send(\"\#{$1} is an ancestor of \#{$3}\") end") actual, s = @agent.send("John is a parent of Joe.") actual, s = @agent.send("Joe is a parent of Sue.") actual, s = @agent.send("is John a parent of Sue?") assert_match(/no knowledge/i, actual) actual, s = @agent.send("is John an ancestor of Sue?") assert_match(/Yes/i, actual) actual, s = @agent.send("Jack = John") actual, s = @agent.send("is Jack a parent of Joe?") assert_match(/Yes/i, actual) actual, s = @agent.send("is Jack an ancestor of Sue?") assert_match(/Yes/i, actual) actual, s = @agent.send("Sue = Susan") actual, s = @agent.send("is Jack an ancestor of Susan?") assert_match(/Yes/i, actual) actual, s = @agent.send("why is Jack an ancestor of Susan?") assert_match(/because/i, actual) actual, s = @agent.send("is Jack a parent of Susan?") assert_match(/no knowledge/i, actual) # not working #actual, s = @agent.send("why is Jack a parent of Susan?") #assert_match(/no knowledge/i, actual) end def teardown begin; File.delete("test_graph.yaml"); rescue; end end end