# hasNoETest.py P.Conrad for CS5NM, Fall 2008, UC Santa Barbara # Incorporates code from Think Python, by Allen B. Downey, from Chapter 9 # Shows how to use check_expect testing with string functions ################################################################ # Here is the function definition from Chapter 9 (section 9.3) ################################################################ def has_no_e(word): for letter in word: if letter == 'e': return False return True ##################################### # Here is our check_expect function ##################################### # check_expect checks whether a function produces the expected result # consumes: # test: a string describing the test # check: the value we are testing (typically result of a function call) # expect: the value we are expecting (typically a literal value) # produces: # nothing (there is no return value) # side effect: # prints a message indicating whether the test passed or failed def check_expect(test,check,expect): if (check == expect): print "Test " + test + " passed." else: print "Test " + test + " failed: expected " + str(expect) + \ " but I got " + str(check) ############################################################### # Here are tests to see if has_no_e(word) functions correctly ############################################################### check_expect("has_no_e('Fred')",has_no_e('Fred'),False) check_expect("has_no_e('Jane')",has_no_e('Jane'),False) check_expect("has_no_e('Santa')",has_no_e('Santa'),True) check_expect("has_no_e('Barbara')",has_no_e('Barbara'),True) check_expect("has_no_e('Goleta')",has_no_e('Goleta'),False) check_expect("has_no_e('Edward')",has_no_e('Edward'),False) check_expect("has_no_e('Ice Cream')",has_no_e('Ice Cream'),False)