#!/usr/bin/python # # Tests for the rps (Rock-Paper-Scissors) esh plug-in # # Test the (rps) Rock Paper Scissors plugin for # correct functionality for wins, losses, and stats. # # To run this test on your own shell, simply run: # # ~cs3214/public_html/spring2015/projects/student-plugins/shb279_rpc80/rps/rps_test.py eshoutput.py # # from the directory in which your "esh" and your "eshoutput.py" is located. # #====== # Setup #====== import sys, imp, atexit, os sys.path.append("/home/courses/cs3214/software/pexpect-dpty/"); import pexpect, shellio, signal, time, os, re, proc_check # Determine the path this file is in thisdir = os.path.dirname(os.path.realpath(__file__)) #Ensure the shell process is terminated def force_shell_termination(shell_process): c.close(force=True) # pulling in the regular expression and other definitions # this should be the eshoutput.py file of the hosting shell, see usage above definitions_scriptname = sys.argv[1] def_module = imp.load_source('', definitions_scriptname) # you can define logfile=open("log.txt", "w") in your eshoutput.py if you want logging! logfile = None if hasattr(def_module, 'logfile'): logfile = def_module.logfile #spawn an instance of the shell, note the -p flags c = pexpect.spawn(def_module.shell, drainpty=True, logfile=logfile, args=['-p', thisdir]) atexit.register(force_shell_termination, shell_process=c) # set timeout for all following 'expect*' calls to 2 seconds c.timeout = 2 # ensure that shell prints expected prompt assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt (1)" #======== # Testing #======== # Run the rps plug-in c.sendline("rps") # Throw a 'rock' command c.sendline("r") assert c.expect('Computer threw ROCK | PAPER | SCISSORS') == 0, \ "expected rps %s" % ("Computer threw ROCK | PAPER | SCISSORS") # Test the secret "shotgun" throw c.sendline("!") assert c.expect_exact('BANG! SHOTGUN!') == 0, \ "expected rps %s" % ('BANG! SHOTGUN!') # Test the 'stats' command c.sendline("#") assert c.expect('WINS: .*') == 0, \ "expected rps %s" % ('WINS: #') # Test the 'quit' command c.sendline("q") assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt (1)" # Exit the test shellio.success()