$LOAD_PATH.unshift(File.dirname(__FILE__)) require 'test_helper' =begin rdoc Tests for the Eliza module (the RubyLabs version of Joseph Weizenbaum's ELIZA program). The tests make and exercise Pattern and Rule objects used to transform English sentences. Tests should try expressions that will be typed interactively by students exploring ways of defining rules, and the calls made when rules are read from a file. =end include ElizaLab class TestEliza < Test::Unit::TestCase def test_00_banner print "\nElizaLab" end # A Pattern object represents a sentence pattern, which associates a regular # expression with a set of possible responses. # Example from MARS tests, showing how to build a "source file" and assemble it: # def test_xx # source = [ # "@label mov #0, #1 ; label needs to be alphanumeric", # "l@bel mov #0, #1 ; label needs to be alphanumeric", # "mov #0, #1 ; unlabeled line must start with space", # "label @bogus #0, #1 ; opcode with non-alphanumeric", # "label b@gus #0, #1 ; opcode with non-alphanumeric", # "label muv #0, #1 ; unknown opcode", # " mov #b@gus, #1 ; messed up operand", # " mov #0 #1 ; missing comma", # " EQU 3 ; missing label on pseudo-op", # "x EQU 3x ; arg not an integer", # " END foo ; undefined label", # ] # # name, code, symbols, errors = MARS.assemble(source) # # assert_equal source.length, errors.length # every line generates an error # assert_equal 0, code.length # should be no instructions generated # end # The simplest pattern has a literal word, and cycles through sentences when applied # to inputs that contain that word def test_01_literal p = Pattern.new("father", ["yes", "no"]) assert p.match("father knows best") assert_equal "yes", p.apply("father knows best") assert_equal "no", p.apply("father knows best") assert_equal "yes", p.apply("father knows best") assert_nil p.apply("who's your daddy") end # Literals in a pattern are bracketed by \b anchors def test_02_anchors p = Pattern.new("father", ["yes"]) assert_nil p.apply("he is a grandfather") assert_equal "yes", p.apply("my father-in-law") assert_nil p.apply("land of my fathers") end # Test the add_response method def test_03_add_response p = Pattern.new("father") p.add_response("one") p.add_response("two") assert_equal "one", p.apply("father knows best") assert_equal "two", p.apply("my father-in-law") end # Test word groups def test_04_groups p = Pattern.new("father|mother") assert p.match("father knows best") assert p.match("my mother the car") end # Match parts used in responses def test_05_match_parts p = Pattern.new("I (like|love|adore) my (dog|cat|ducks)") p.add_response("You $1 your $2?") assert_equal "You like your cat?", p.apply("I like my cat") assert_equal "You love your ducks?", p.apply("I love my ducks") end # Wild card pattern def test_06_wildcard p = Pattern.new("I like (.*)") p.add_response("Why do you like $1?") assert_equal "Why do you like to drink beer?", p.apply("I like to drink beer") assert_equal "Why do you like that car?", p.apply("I like that car") end # Postprocessing def test_07_postprocess p = Pattern.new("I am (.*)") p.add_response("Are you really $1?") assert_equal "Are you really afraid of your dog?", p.apply("I am afraid of your dog") Eliza.post["I"] = "you" Eliza.post["your"] = "my" assert_equal "Are you really afraid of my dog?", p.apply("I am afraid of your dog") assert_equal "Are you really sorry you dropped my computer?", p.apply("I am sorry I dropped your computer") end # Preprocessing def test_08_preprocess p = Pattern.new("I am (.*)") p.add_response("Are you really $1?") assert_nil p.apply("I'm sorry") Eliza.pre["I'm"] = "I am" assert_equal "Are you really sorry?", p.apply("I'm sorry") end # Case insensitive matches def test_09_case p = Pattern.new("I am (.*)") p.add_response("Are you really $1?") assert_equal "Are you really sorry?", p.apply("i am sorry") Eliza.pre["I'm"] = "I am" assert_equal "Are you really sorry?", p.apply("i'm sorry") end # Load the 'doctor' script, try a few sentences -- this test is highly dependent on the rules # in the script.... # def test_10_doctor # assert Eliza.load(:doctor) # assert_equal "How do you do? Please state your problem.", Eliza.transform("hello") # assert_equal "Do computers worry you?", Eliza.transform("I don't trust computers") # assert_equal "Why are you concerned over my hat?", Eliza.transform("I like your hat") # assert_equal "How do you know you can't do it?", Eliza.transform("I can't do it") # end # Examples of lines that should be flagged as errors: # unknown keyword: #:foo bar # Missing arguments, first arg not a var name: #:alias #:alias foo bar baz end