def test_no_ccns(self): """ Shouldn't be any raw CCNs in num_safe(text). 'text' is imported from ccn_safety.py. """ pattern = r"(\d{4}(-| )\d{4}(-| )\d{4}(-| )\d{4})" self.assertIsNone(re.search(pattern, num_safe(text)))
def test_iter(self): """ Need some practice with re.finditer() """ pattern = r"((X{4}-){3}\d{4})" m = re.finditer(pattern, num_safe(newtext)) expected = [ 'XXXX-XXXX-XXXX-3456', 'XXXX-XXXX-XXXX-8765', 'XXXX-XXXX-XXXX-5791' ] self.assertEqual([a.group() for a in m], expected)
def test_iter(self): """ Need some practice with re.finditer() """ pattern = r"((X{4}-){3}\d{4})" m = re.finditer(pattern, num_safe(newtext)) expected = [ 'XXXX-XXXX-XXXX-3456', 'XXXX-XXXX-XXXX-8765', 'XXXX-XXXX-XXXX-5791'] self.assertEqual([ a.group() for a in m ], expected)
def test_re_mojo(self): """ Test that 'findall(pattern, num_safe(newtext))' gets returned with properly fixed CCNs" """ pattern = r"(X{4}-X{4}-X{4}-\d{4})" test = re.findall(pattern, num_safe(newtext)) self.assertEqual(test[0], 'XXXX-XXXX-XXXX-3456') self.assertEqual(test[1], 'XXXX-XXXX-XXXX-8765') self.assertEqual(test[2], 'XXXX-XXXX-XXXX-5791') self.assertEqual(len(test), 3)
def test_txt(self): "Entire passages with ccn's should be correct." self.assertEqual(num_safe(txt2test), fixed_txt)
def test_sub(self): "Tests that simple substitutions are okay" self.assertEqual(num_safe("1234-1234-1234-1234"), "XXXX-XXXX-XXXX-1234") self.assertEqual(num_safe("1234 1234 1234 1234"), "XXXX-XXXX-XXXX-1234")