#!/usr/bin/python # Python Test Script # # Simple text based game # # Authors: # Ryan Asper # Adrianne Williams # Import Libraries import sys, imp, atexit sys.path.append("/home/courses/cs3214/software/pexpect-dpty/"); import pexpect, shellio, signal, time, os, re, proc_check # Usage Usage = "Usage: fun-game_test.py eshoutput.py /plugin_directory/" if len(sys.argv) != 3 or not sys.argv[1].endswith("eshoutput.py") or not os.path.isdir(sys.argv[2]): print Usage sys.exit(0) print "Running Test..." #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 definitions_scriptname = sys.argv[1] def_module = imp.load_source('', definitions_scriptname) # Plugin directory plugin_dir = sys.argv[2] logfile = None if hasattr(def_module, 'logfile'): logfile = def_module.logfile # Spawn instance of the shell c = pexpect.spawn(def_module.shell + " -p " + plugin_dir, drainpty=True, logfile=logfile) #atextit.register(force_shell_termination, shell_process=c) # Set timeout for expect() statements c.timeout = 2 ################################################## ############# Test Winning Scenario ############## # Invoke Plugin c.sendline("playgame") response = "Welcome! Thank you for choosing our fun text-based game." assert c.expect(response) == 0, "Pluging didn't start correctly" # Start Game c.sendline("start") assert c.expect("walkingdead> ") == 0, "Prompt wasn't printed correctly" # Try Path to Success c.sendline("1") c.sendline("2") response = "You get outside just as a bunch of zombies try and grab you." assert c.expect(response) == 0, "Incorrect ending scenario." ################################################## ############# Test Losing Scenario ############## # Restart game c.sendline("play") response = "Welcome! Thank you for choosing our fun text-based game." assert c.expect(response) == 0, "Pluging didn't restart correctly" c.sendline("start") assert c.expect("walkingdead> ") == 0, "Prompt wasn't printed correctly" # Try Path to Failure c.sendline("3") response = "It was a zombie.. You died :\(" assert c.expect(response) == 0, "Incorrect ending scenario." # Exit Test c.close(force=True) shellio.success()