#!/usr/bin/python # # Test the calc plugin # # by Cyndy Ejanda and Arunima Singh # import sys, imp, atexit sys.path.append("/home/courses/cs3214/software/pexpect-dpty/"); import pexpect, shellio, signal, time, os, re, proc_check #Ensure the shell process is terminated def force_shell_termination(shell_process): c.close(force=True) #pulling in the regular expression and other definitions definitions_scriptname = sys.argv[1] plugin_dir = sys.argv[2] def_module = imp.load_source('', definitions_scriptname) logfile = None if hasattr(def_module, 'logfile'): logfile = def_module.logfile #spawn an instance of the shell c = pexpect.spawn(def_module.shell + plugin_dir, drainpty=True, logfile=logfile) atexit.register(force_shell_termination, shell_process=c) c.timeout = 2 ############################ Test ############################# # test for errors # user did not enter the required number of arguments c.sendline("calc") assert c.expect("calc error: incorrect usage") == 0, "calc error: incorrect usage" c.sendline("calc a") assert c.expect("calc error: incorrect usage") == 0, "calc error: incorrect usage" # user entered incorrect option c.sendline("calc z") assert c.expect("calc error: incorrect usage") == 0, "calc error: incorrect usage" # start calculating # add c.sendline("calc a 1 2") assert c.expect("sum is 3") == 0, "calc error: unexpected output" c.sendline("calc a -1 2") assert c.expect("sum is 1") == 0, "calc error: unexpected output" # subtract c.sendline("calc s 2 1") assert c.expect("difference is 1") == 0, "calc error: unexpected output" c.sendline("calc s -1 2") assert c.expect("difference is -3") == 0, "calc error: unexpected output" # multiply c.sendline("calc m 2 1") assert c.expect("product is 2") == 0, "calc error: unexpected output" c.sendline("calc m -1 2") assert c.expect("product is -2") == 0, "calc error: unexpected output" # divide c.sendline("calc d 2 1") assert c.expect("quotient is 2") == 0, "calc error: unexpected output" c.sendline("calc d -4 2") assert c.expect("quotient is -2") == 0, "calc error: unexpected output" c.sendline("calc d 10 3") assert c.expect("quotient is 3") == 0, "calc error: unexpected output" c.sendline("exit") assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters" shellio.success()