# THIS CODE IS SUPPOSED TO: # Function check_phone checks whether a string entered by the user # is a valid phone number or not. A valid phone number must have # the following characteristics: # # - It must start with a three digit positive integer between parentheses, # that doesn't start with preceding zeroes. # - The area code is followed by either a space or a dash # - After the area code, a non-negative three digit number will be read in # (all zeroes here is okay). # - A single space will follow those three digits, followed by four more digits. # - Finally, the phone number will end with an optional extension. # This will either start with x or X, # followed by a single space, followed by any four digits. # # Write test cases to find an unknown bug in the function. # # Hint: Break the input into pieces (consider the given characteristics). # For each piece, what would "valid" (or "invalid") piece look like? # # Do not look at the code. # You only need a specification to create test cases. # import re def check_phone(phone): result = False regex = re.compile(r"\([0-9]{3}\)[- ]?[0-9]{3} [0-9]{4}([xX] [0-9]{4})?") temp = regex.search(phone) if temp != None and len(temp.group(0)) == len(phone): result = True return result actualResults = [] expectedResults = [] #WRITE YOUR TESTS BELOW FOLLOWING THE FORMAT actualResults.append(check_phone("(333) 333 4444x 5555")) expectedResults.append(True) actualResults.append(check_phone("(333) 333 4444x5555")) expectedResults.append(False) actualResults.append(check_phone("(333) 333 4444")) expectedResults.append(True) #===================================# ''' ctualResults.append(check_phone("(333) 333 4444X 5555")) expectedResults.append(True) actualResults.append(check_phone("(033) 333 4444X 5555")) # find bug expectedResults.append(False) actualResults.append(check_phone("(33) 333 4444x 5555")) expectedResults.append(False) actualResults.append(check_phone("333) 333 4444x 5555")) expectedResults.append(False) actualResults.append(check_phone("(333 333 4444x 5555")) expectedResults.append(False) actualResults.append(check_phone("(333)-333 4444x 5555")) expectedResults.append(True) actualResults.append(check_phone("(333).333 4444x 5555")) expectedResults.append(False) actualResults.append(check_phone("(33) -33 4444x 5555")) expectedResults.append(False) actualResults.append(check_phone("(33) 33 4444x 5555")) expectedResults.append(False) actualResults.append(check_phone("(-333) 3334444x 5555")) expectedResults.append(False) actualResults.append(check_phone("(33) 333 4444x 5555")) expectedResults.append(False) ''' #===================================# ctr = 0 found = False while ctr < len(actualResults): actual = actualResults[ctr] expected = expectedResults[ctr] print(str(expected) + " : " + str(actual)) if (actual != expected): found = True ctr = ctr + 1 if (found == True): print("Your test suite found the bug!!") else: print("Try writing more tests, you didn't find the bug yet.") ''' correct code r"\([1-9][0-9]{2}\)[- ]?[0-9]{3} [0-9]{4}([xX] [0-9]{4})?" '''