def test_equality(self): """Tests equality operator. """ a = AString("equal") b = AString("equal") self.assertEqual(a, b) self.assertEqual(AString(""), AString(None))
def test_prepend_strings(self): """Tests prepending strings """ test_string = "test" a_string = AString(test_string) # Prepend a list of strings prepend_list = ["a", "bc", "def"] output = a_string.prepend(prepend_list) self.assertEqual(str(output), "a_bc_def_test") # Prepend a single string self.assertEqual(a_string.prepend("abc"), "abc_test")
def test_append_strings(self): """Tests appending strings """ test_string = "test" a_string = AString(test_string) # Append date output = a_string.append_today() today = datetime.datetime.today() self.assertEqual(output, "%s_%s%s%s" % ( test_string, str(today.year), str(today.month).zfill(2), str(today.day).zfill(2) ))
def test_remove_escape_sequence(self): """Tests removing ANSI escape sequence. """ test_string = "test\r\n\x1b[00m\x1b[01;31mHello.World\x1b[00m\r\n\x1b[01;31m" self.assertEqual( AString(test_string).remove_escape_sequence(), "test\r\nHello.World\r\n" )
def test_remove_non_alphanumeric(self): """Tests removing characters. """ test_string = "!te\nst.123" self.assertEqual( AString(test_string).remove_non_alphanumeric(), "test123" )
def test_remove_non_ascii(self): """Tests removing non ASCII characters. """ test_string = "!!te‘’st\n" self.assertEqual( AString(test_string).remove_non_ascii(), "!!test\n" )
def test_str_methods(self): """Tests calling methods of python str with AString """ # Tests cases for methods returning a string test_cases = [ # (input_value, method_to_call, expected_output) ("test string", "title", "Test String"), ("test String", "lower", "test string"), ] for t in test_cases: value = t[0] attr = t[1] expected = t[2] x = AString(value) actual = getattr(x, attr)() self.assertEqual(type(actual), AString) self.assertEqual(str(actual), expected, "Test Failed. Attribute: %s" % attr) # Test split() method, which returns a list arr = AString("test string").split(" ") self.assertTrue(isinstance(arr, list), "split() should return a list.") self.assertEqual(len(arr), 2, "split() should return a list of 2 elements.") self.assertEqual(type(arr[0]), AString) self.assertEqual(type(arr[1]), AString) self.assertEqual(arr[0], "test", "split() Error.") self.assertEqual(arr[1], "string", "split() Error.") # Test the partition() method, which returns a 3-tuple arr = AString("test partition string").partition("partition") self.assertTrue(isinstance(arr, tuple), "partition() should return a tuple.") self.assertEqual(len(arr), 3, "partition() should return a list of 3 elements.") self.assertEqual(arr[0], "test ", "partition() Error.") self.assertEqual(arr[1], "partition", "partition() Error.") self.assertEqual(arr[2], " string", "partition() Error.") # Test endswith() method self.assertTrue(AString("test string").endswith("ing"), "endswith() Error.")