def testDocumentModify(self): blip = self.new_blip(blipId=ROOT_BLIP_ID) blip.all().replace('a text with text and then some text') blip[7].insert('text ') blip.all('text').replace('thing') self.assertEquals('a thing thing with thing and then some thing', blip.text)
def testAnnotationHandling(self): key = 'style/fontWeight' def get_bold(): for an in blip.annotations[key]: if an.value == 'bold': return an return None json = ('[{"range":{"start":3,"end":6},"name":"%s","value":"bold"}]' % key) blip = self.new_blip(blipId=ROOT_BLIP_ID, annotations=simplejson.loads(json)) self.assertEquals(1, len(blip.annotations)) self.assertNotEqual(None, get_bold().value) self.assertTrue(key in blip.annotations) # extend the bold annotation by adding: blip.range(5, 8).annotate(key, 'bold') self.assertEquals(1, len(blip.annotations)) self.assertEquals(8, get_bold().end) # clip by adding a same keyed: blip[4:12].annotate(key, 'italic') self.assertEquals(2, len(blip.annotations[key])) self.assertEquals(4, get_bold().end) # now split the italic one: blip.range(6, 7).clear_annotation(key) self.assertEquals(3, len(blip.annotations[key])) # clear the whole thing blip.all().clear_annotation(key) # getting to the key should now throw an exception self.assertRaises(KeyError, blip.annotations.__getitem__, key)
def testIteration(self): blip = self.new_blip(blipId=ROOT_BLIP_ID) blip.all().replace('aaa 012 aaa 345 aaa 322') count = 0 prev = -1 for start, end in blip.all('aaa'): count += 1 self.assertTrue(prev < start) prev = start self.assertEquals(3, count)
def testAnnotationHandling(self): key = 'style/fontWeight' def get_bold(): for an in blip.annotations[key]: if an.value == 'bold': return an return None json = ('[{"range":{"start":3,"end":6},"name":"%s","value":"bold"}]' % key) blip = self.new_blip(blipId=ROOT_BLIP_ID, annotations=simplejson.loads(json)) self.assertEquals(1, len(blip.annotations)) self.assertNotEqual(None, get_bold().value) self.assertTrue(key in blip.annotations) # extend the bold annotation by adding: blip.range(5, 8).annotate(key, 'bold') self.assertEquals(1, len(blip.annotations)) self.assertEquals(8, get_bold().end) # clip by adding a same keyed: blip[4:12].annotate(key, 'italic') self.assertEquals(2, len(blip.annotations[key])) self.assertEquals(4, get_bold().end) # now split the italic one: blip.range(6, 7).clear_annotation(key) self.assertEquals(3, len(blip.annotations[key])) # test names and iteration self.assertEquals(1, len(blip.annotations.names())) self.assertEquals(3, len([x for x in blip.annotations])) blip[3:5].annotate('foo', 'bar') self.assertEquals(2, len(blip.annotations.names())) self.assertEquals(4, len([x for x in blip.annotations])) blip[3:5].clear_annotation('foo') # clear the whole thing blip.all().clear_annotation(key) # getting to the key should now throw an exception self.assertRaises(KeyError, blip.annotations.__getitem__, key)
def testSearchWithNoMatchShouldNotGenerateOperation(self): blip = self.new_blip(blipId=ROOT_BLIP_ID) self.assertEqual(-1, blip.text.find(':(')) self.assertEqual(0, len(self.operation_queue)) blip.all(':(').replace(':)') self.assertEqual(0, len(self.operation_queue))
def testReplace(self): blip = self.new_blip(blipId=ROOT_BLIP_ID) blip.all().replace('\nxxxx') blip.all('yyy').replace('zzz') self.assertEqual('\nxxxx', blip.text)