def test_creation_01(self): sj = StringJoiner() expected__str__ = '' self.assertEqual(expected__str__, str(sj)) expected__repr__ = '{"separator": ",", "prefix": "", "suffix": "", ' \ '"nelem": 0, "str_length": 0, "elements": []}' self.assertEqual(expected__repr__, sj.__repr__())
def test_adding_01(self): sj = StringJoiner() sj.add("e1") self.assertEqual("e1", str(sj)) expected__repr__ = '{"separator": ",", "prefix": "", "suffix": "", ' \ '"nelem": 1, "str_length": 2, "elements": ["e1"]}' self.assertEqual(expected__repr__, sj.__repr__())
def test_merge_03(self): sj = StringJoiner(prefix="[", suffix="]") sj.merge([1, 2, 3, "str", 4.0, ","]) self.assertEqual("[1,2,3,str,4.0,,]", str(sj)) expected__repr__ = '{"separator": ",", "prefix": "[", "suffix": "]", ' \ '"nelem": 6, "str_length": 17, "elements": ["1", "2", "3", "str", "4.0", ","]}' self.assertEqual(expected__repr__, sj.__repr__())
def test_adding_04(self): sj = StringJoiner(prefix="[", suffix="]") sj.add("e1").add("e2").add(3) sj.add("e0", 0) self.assertEqual("[e0,e1,e2,3]", str(sj)) expected__repr__ = '{"separator": ",", "prefix": "[", "suffix": "]", ' \ '"nelem": 4, "str_length": 12, "elements": ["e0", "e1", "e2", "3"]}' self.assertEqual(expected__repr__, sj.__repr__())
def test_adding_02(self): sj = StringJoiner() sj.add("e1").add("e2") self.assertEqual("e1,e2", str(sj)) print("Sample Default String Joiner Output:", sj) expected__repr__ = '{"separator": ",", "prefix": "", "suffix": "", ' \ '"nelem": 2, "str_length": 5, "elements": ["e1", "e2"]}' self.assertEqual(expected__repr__, sj.__repr__())
def test_delete_item_02(self): sj = StringJoiner(prefix="[", suffix="]") sj.add("e1").add("e2").add(3) del sj[-3] self.assertEqual("[e2,3]", str(sj)) expected__repr__ = '{"separator": ",", "prefix": "[", "suffix": "]", ' \ '"nelem": 2, "str_length": 6, "elements": ["e2", "3"]}' self.assertEqual(expected__repr__, sj.__repr__())
def test_set_item_04(self): sj = StringJoiner(prefix="[", suffix="]") sj.add("e1").add("e2").add(3) sj[0] = "e11" self.assertEqual("[e11,e2,3]", str(sj)) expected__repr__ = '{"separator": ",", "prefix": "[", "suffix": "]", ' \ '"nelem": 3, "str_length": 10, "elements": ["e11", "e2", "3"]}' self.assertEqual(expected__repr__, sj.__repr__())
def test_removing_01(self): sj = StringJoiner(prefix="[", suffix="]") sj.add("e1").add("e2").add(3) sj.remove("e2") self.assertEqual("[e1,3]", str(sj)) expected__repr__ = '{"separator": ",", "prefix": "[", "suffix": "]", ' \ '"nelem": 2, "str_length": 6, "elements": ["e1", "3"]}' self.assertEqual(expected__repr__, sj.__repr__())
def test_adding_03(self): sj = StringJoiner(prefix="[", suffix="]") sj.add("e1").add("e2").add(3) self.assertEqual("[e1,e2,3]", str(sj)) print( "Sample String Joiner Output (default separator, prefix = [, suffix = ]:", sj) expected__repr__ = '{"separator": ",", "prefix": "[", "suffix": "]", ' \ '"nelem": 3, "str_length": 9, "elements": ["e1", "e2", "3"]}' self.assertEqual(expected__repr__, sj.__repr__())
def test_merge_02(self): sj1 = StringJoiner(prefix="[", suffix="]") sj1.add("e1").add("e2").add(3) sj2 = StringJoiner(prefix="(", suffix=")") sj2.add("o1").add("o2") sj2.merge(sj1, 1) self.assertEqual("(o1,e1,e2,3,o2)", str(sj2)) expected__repr__ = '{"separator": ",", "prefix": "(", "suffix": ")", ' \ '"nelem": 5, "str_length": 15, "elements": ["o1", "e1", "e2", "3", "o2"]}' self.assertEqual(expected__repr__, sj2.__repr__())
def test_set_item_05(self): sj = StringJoiner(prefix="[", suffix="]") sj.add("e1").add("e2").add(3) with self.assertRaises(IndexError): sj[-42] = "something" sj[-3] = "e11" self.assertEqual("[e11,e2,3]", str(sj)) expected__repr__ = '{"separator": ",", "prefix": "[", "suffix": "]", ' \ '"nelem": 3, "str_length": 10, "elements": ["e11", "e2", "3"]}' self.assertEqual(expected__repr__, sj.__repr__()) with self.assertRaises(IndexError): sj[42] = "something"