def test_append(self): space = Space('hello world') space.append('foo', 'bar') space.set('foo2', 'bar') space.append('foo', 'two') self.assertEqual(space.get('foo'), 'bar') self.assertEqual(space.length(), 4)
def testDeepCopy(self): space1 = Space('hello world') space2 = space1.clone() self.assertEqual(space2.get('hello'), 'world', 'Clone should work') space2.set('hello', 'mom') self.assertEqual(space1.get('hello'), 'world', 'Clone makes deep copy') space3 = space1 space3.set('hello', 'dad') self.assertEqual(space1.get('hello'), 'dad', '= makes shallow copy') space1.set('anotherSpace', Space('123 456')) self.assertEqual(space3.get('anotherSpace 123'), '456')
def test__str__(self): space = Space("hello world") self.assertEqual(space.__str__(), 'hello world\n') space.set('foo', 'bar') self.assertEqual(space.__str__(), 'foo bar\nhello world\n') space2 = Space('john\n age 5') self.assertEqual(space2.__str__(), 'john\n age 5\n') space2.set('multiline', 'hello\nworld') self.assertEqual(space2.__str__(), 'john\n age 5\nmultiline hello\n world\n') space2.set('other', 'foobar') self.assertEqual(space2.__str__(), 'john\n age 5\nmultiline hello\n world\nother foobar\n')
def test_set(self): space = Space('hello world') self.assertEqual(space.get('hello'), 'world') self.assertTrue(isinstance(space.set('hello', 'mom'), Space), 'set returns self for chaining') intSpace = Space() intSpace.set(2, 'hi') self.assertEqual(intSpace.get(2), 'hi') emptyStringSpace = Space() emptyStringSpace.set('boom', '') self.assertEqual(emptyStringSpace.get('boom'), '') # this is important, value should have been changed self.assertEqual(space.get('hello'), 'mom') space.set('head style color', 'blue') self.assertEqual(space.get('head style color'), 'blue')
def testGetSet(self): space = Space('Hello World') self.assertEqual(space.get("Hello"), 'World', 'Property should be accessible') self.assertEqual(type(space.get("Hello")), str, 'Leaf should be string') space.set('foo', 'bar') self.assertEqual(space.get('foo'), 'bar', 'Space should be modifiable' )