class TestStringList_StartWithString(unittest.TestCase): """ Test basic functionality of a StringList when the initial value is a string. """ def setUp(self): from stringlist import StringList self.sl = StringList('hello world') self.sl2 = StringList() def test_empty_stringlist(self): """ What happens when a StringList is initialized with no parameters? """ self.assertEquals(self.sl2, "") self.sl2 += 'foo' self.assertEquals(self.sl2, "foo") def test_empty_stringlist_convert(self): """ What happens when a StringList is initialized with no parameters and append() is called? """ self.sl2.append('foo') self.assertEquals(self.sl2, ['foo']) def test_string_split(self): """ Does split() work as expected? """ self.assertEquals(self.sl.split(' '), ['hello', 'world']) def test_string_array(self): """ Do the string index/slice methods work? """ self.assertEquals(self.sl[1:3], 'el') self.assertEquals(self.sl[0], 'h') def test_string_addition(self): """ Can we add two stringlist objects together, if they are strings? """ new_sl = self.sl + " wow!" self.assertEquals(new_sl, "hello world wow!") # make sure the original property isn't altered. self.assertEquals(self.sl, "hello world") def test_string_iaddition(self): """ Can we do an inline addition of another string? """ self.sl += " zoinks!" self.assertEquals(self.sl, "hello world zoinks!") def test_string_conversion(self): """ Convert from a string to an array """ self.sl.append('foo bar') self.assertEquals(self.sl, ['hello world', 'foo bar']) def test_iteration(self): """ Use the StringList in a loop """ output = [] for x in self.sl: output.append(x) self.assertEquals(output, ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']) self.assertEquals([x for x in self.sl], ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']) def test_contains(self): """ Does x in StringList work? """ self.assertTrue('e' in self.sl) self.assertFalse('x' in self.sl) def test_delete(self): """ Test del StringList[x] - should raise an exception """ with self.assertRaises(TypeError): del self.sl[1] def test_index_assignment(self): """ Test StringList[x] = 'c' - should raise an exception """ with self.assertRaises(TypeError): self.sl[1] = 'd' def test_length(self): """ len(StringList) """ self.assertEquals(len(self.sl), 11) def test_inline_addition_integer(self): """ StringList += 100 """ with self.assertRaises(TypeError): self.sl += 100 def test_addition_object(self): """ StringList + 100 """ with self.assertRaises(TypeError): new_sl = self.sl + 100 def test_cast_to_string(self): """ str(StringList) """ self.assertEquals(str(self.sl), "'hello world'")