def test_chop(self): delta = Delta().retain(10).retain(10).retain(10).delete(5).delete(5).delete(5) self.assertEqual(delta.get_ops(), [{'retain': 30}, {'delete': 15}]) delta = Delta().delete(5).delete(5).delete(5).retain(10).retain(10).retain(10) delta.chop() self.assertEqual(delta.get_ops(), [{'delete': 15}])
def test_reorder_and_combine(self): delta = Delta().insert('hello').delete(3).insert(' world') self.assertEqual(delta.get_ops(), [{'insert': 'hello world'}, {'delete': 3}]) delta = Delta().insert('hello').delete(3).insert(' world', {'bold': True}) self.assertEqual(delta.get_ops(), [{'insert': 'hello'}, {'insert': ' world', 'attributes': {'bold': True}}, {'delete': 3}]) delta = Delta().delete(3).delete(3).insert('hello').delete(3) self.assertEqual(delta.get_ops(), [{'insert': 'hello'}, {'delete': 9}])
def test_reorder(self): delta = Delta().insert('hello').delete(3) self.assertEqual(delta.get_ops(), [{'insert': 'hello'}, {'delete': 3}]) delta = Delta().delete(3).insert('hello') self.assertEqual(delta.get_ops(), [{'insert': 'hello'}, {'delete': 3}]) delta = Delta().delete(3).delete(3).insert('hello') self.assertEqual(delta.get_ops(), [{'insert': 'hello'}, {'delete': 6}])
def test_chop(self): delta = Delta().retain(10).retain(10).retain(10).delete(5).delete( 5).delete(5) self.assertEqual(delta.get_ops(), [{'retain': 30}, {'delete': 15}]) delta = Delta().delete(5).delete(5).delete(5).retain(10).retain( 10).retain(10) delta.chop() self.assertEqual(delta.get_ops(), [{'delete': 15}])
def test_cant_combine(self): # differing attributes delta = Delta().insert('hello ').insert('world', {'bold': True}) self.assertEqual(delta.get_ops(), [{ 'insert': 'hello ' }, { 'insert': 'world', 'attributes': { 'bold': True } }]) delta = Delta().insert('world', {'bold': True}).insert('hello ') self.assertEqual(delta.get_ops(), [{ 'insert': 'world', 'attributes': { 'bold': True } }, { 'insert': 'hello ' }]) delta = Delta().retain(10).retain(5, {'bold': True}) self.assertEqual(delta.get_ops(), [{ 'retain': 10 }, { 'retain': 5, 'attributes': { 'bold': True } }]) delta = Delta().retain(5, {'bold': True}).retain(10) self.assertEqual(delta.get_ops(), [{ 'retain': 5, 'attributes': { 'bold': True } }, { 'retain': 10 }]) # insert text + insert embed delta = Delta().insert('hello').insert( {'image': 'https://octodex.github.com/images/labtocat.png'}, {'alt': 'Lab Octocat'}) self.assertEqual(delta.get_ops(), [{ 'insert': 'hello' }, { 'insert': { 'image': 'https://octodex.github.com/images/labtocat.png' }, 'attributes': { 'alt': 'Lab Octocat' } }])
def test_simple_combines(self): delta = Delta().insert('hello ').insert('world') self.assertEqual(delta.get_ops(), [{'insert': 'hello world'}]) delta = Delta().delete(10).delete(5) self.assertEqual(delta.get_ops(), [{'delete': 15}]) delta = Delta().retain(10).retain(5) self.assertEqual(delta.get_ops(), [{'retain': 15}]) delta = Delta().retain(10).retain(10).retain(10).delete(5).delete(5).delete(5) self.assertEqual(delta.get_ops(), [{'retain': 30}, {'delete': 15}])
def test_simple_combines(self): delta = Delta().insert('hello ').insert('world') self.assertEqual(delta.get_ops(), [{'insert': 'hello world'}]) delta = Delta().delete(10).delete(5) self.assertEqual(delta.get_ops(), [{'delete': 15}]) delta = Delta().retain(10).retain(5) self.assertEqual(delta.get_ops(), [{'retain': 15}]) delta = Delta().retain(10).retain(10).retain(10).delete(5).delete( 5).delete(5) self.assertEqual(delta.get_ops(), [{'retain': 30}, {'delete': 15}])
def test_cant_combine(self): # differing attributes delta = Delta().insert('hello ').insert('world', {'bold': True}) self.assertEqual(delta.get_ops(), [{'insert': 'hello '}, {'insert': 'world', 'attributes': {'bold': True}}]) delta = Delta().insert('world', {'bold': True}).insert('hello ') self.assertEqual(delta.get_ops(), [{'insert': 'world', 'attributes': {'bold': True}}, {'insert': 'hello '}]) delta = Delta().retain(10).retain(5, {'bold': True}) self.assertEqual(delta.get_ops(), [{'retain': 10}, {'retain': 5, 'attributes': {'bold': True}}]) delta = Delta().retain(5, {'bold': True}).retain(10) self.assertEqual(delta.get_ops(), [{'retain': 5, 'attributes': {'bold': True}}, {'retain': 10}]) # insert text + insert embed delta = Delta().insert('hello').insert({'image': 'https://octodex.github.com/images/labtocat.png'}, {'alt': 'Lab Octocat'}) self.assertEqual(delta.get_ops(), [{'insert': 'hello'}, {'insert': {'image': 'https://octodex.github.com/images/labtocat.png'}, 'attributes': {'alt': 'Lab Octocat'}}])
def test_insert(self): delta = Delta() delta.insert('') self.assertEqual(delta.get_ops(), []) delta = Delta() delta.insert(' ') self.assertEqual(delta.get_ops(), [{'insert': ' '}]) delta = Delta() delta.insert('hello') self.assertEqual(delta.get_ops(), [{'insert': 'hello'}]) delta = Delta() delta.insert('hello', {}) self.assertEqual(delta.get_ops(), [{'insert': 'hello'}]) delta = Delta() delta.insert('hello', {'bold': True}) self.assertEqual(delta.get_ops(), [{'insert': 'hello', 'attributes': {'bold': True}}]) # old Quill format for embeds delta = Delta() delta.insert(1, {'image': 'https://octodex.github.com/images/labtocat.png'}) self.assertEqual(delta.get_ops(), [{'insert': 1, 'attributes': {'image': 'https://octodex.github.com/images/labtocat.png'}}]) # new Quill format for embeds delta = Delta() delta.insert({'image': 'https://octodex.github.com/images/labtocat.png'}, {'alt': 'Lab Octocat'}) self.assertEqual(delta.get_ops(), [{'insert': {'image': 'https://octodex.github.com/images/labtocat.png'}, 'attributes': {'alt': 'Lab Octocat'}}])
def test_delete(self): delta = Delta() delta.delete(0) self.assertEqual(delta.get_ops(), []) delta = Delta() delta.delete(-10) self.assertEqual(delta.get_ops(), []) delta = Delta() delta.delete('whoops') self.assertEqual(delta.get_ops(), []) delta = Delta() delta.delete(15) self.assertEqual(delta.get_ops(), [{'delete': 15}]) delta = Delta() delta.delete(15L) self.assertEqual(delta.get_ops(), [{'delete': 15}])
def test_retain(self): delta = Delta() delta.retain(0) self.assertEqual(delta.get_ops(), []) delta = Delta() delta.retain(-10) self.assertEqual(delta.get_ops(), []) delta = Delta() delta.retain('whoops') self.assertEqual(delta.get_ops(), []) delta = Delta() delta.retain(15) self.assertEqual(delta.get_ops(), [{'retain': 15}]) delta = Delta() delta.retain(15, {}) self.assertEqual(delta.get_ops(), [{'retain': 15}]) delta = Delta() delta.retain(15, {'bold': True}) self.assertEqual(delta.get_ops(), [{ 'retain': 15, 'attributes': { 'bold': True } }])
def test_reorder_and_combine(self): delta = Delta().insert('hello').delete(3).insert(' world') self.assertEqual(delta.get_ops(), [{ 'insert': 'hello world' }, { 'delete': 3 }]) delta = Delta().insert('hello').delete(3).insert( ' world', {'bold': True}) self.assertEqual(delta.get_ops(), [{ 'insert': 'hello' }, { 'insert': ' world', 'attributes': { 'bold': True } }, { 'delete': 3 }]) delta = Delta().delete(3).delete(3).insert('hello').delete(3) self.assertEqual(delta.get_ops(), [{'insert': 'hello'}, {'delete': 9}])
def test_insert(self): delta = Delta() delta.insert('') self.assertEqual(delta.get_ops(), []) delta = Delta() delta.insert(' ') self.assertEqual(delta.get_ops(), [{'insert': ' '}]) delta = Delta() delta.insert('hello') self.assertEqual(delta.get_ops(), [{'insert': 'hello'}]) delta = Delta() delta.insert('hello', {}) self.assertEqual(delta.get_ops(), [{'insert': 'hello'}]) delta = Delta() delta.insert('hello', {'bold': True}) self.assertEqual(delta.get_ops(), [{ 'insert': 'hello', 'attributes': { 'bold': True } }]) # old Quill format for embeds delta = Delta() delta.insert( 1, {'image': 'https://octodex.github.com/images/labtocat.png'}) self.assertEqual(delta.get_ops(), [{ 'insert': 1, 'attributes': { 'image': 'https://octodex.github.com/images/labtocat.png' } }]) # new Quill format for embeds delta = Delta() delta.insert( {'image': 'https://octodex.github.com/images/labtocat.png'}, {'alt': 'Lab Octocat'}) self.assertEqual(delta.get_ops(), [{ 'insert': { 'image': 'https://octodex.github.com/images/labtocat.png' }, 'attributes': { 'alt': 'Lab Octocat' } }])
def test_iterator(self): delta = Delta().retain(20).insert('hello').delete(15).retain(3).insert({'image': 'https://octodex.github.com/images/labtocat.png'}) iterator = OpIterator(delta.get_ops()) self.assertEqual(iterator.peek_length(), 20) self.assertEqual(iterator.peek_type(), 'retain') self.assertEqual(iterator.next(), {'retain': 20}) self.assertEqual(iterator.next(3), {'insert': 'hel'}) self.assertEqual(iterator.peek_length(), 2) self.assertEqual(iterator.next(), {'insert': 'lo'}) self.assertEqual(iterator.next(), {'delete': 15}) self.assertEqual(iterator.has_next(), True) self.assertEqual(iterator.next(4), {'retain': 3}) self.assertEqual(iterator.peek_length(), 1) self.assertEqual(iterator.next(5), {'insert': {'image': 'https://octodex.github.com/images/labtocat.png'}})
def test_iterator(self): delta = Delta().retain(20).insert('hello').delete(15).retain(3).insert( {'image': 'https://octodex.github.com/images/labtocat.png'}) iterator = OpIterator(delta.get_ops()) self.assertEqual(iterator.peek_length(), 20) self.assertEqual(iterator.peek_type(), 'retain') self.assertEqual(iterator.next(), {'retain': 20}) self.assertEqual(iterator.next(3), {'insert': 'hel'}) self.assertEqual(iterator.peek_length(), 2) self.assertEqual(iterator.next(), {'insert': 'lo'}) self.assertEqual(iterator.next(), {'delete': 15}) self.assertEqual(iterator.has_next(), True) self.assertEqual(iterator.next(4), {'retain': 3}) self.assertEqual(iterator.peek_length(), 1) self.assertEqual(iterator.next(5), { 'insert': { 'image': 'https://octodex.github.com/images/labtocat.png' } })
def test_constructor(self): delta = Delta() self.assertEqual(delta.get_ops(), []) delta = Delta([]) self.assertEqual(delta.get_ops(), []) delta = Delta([{'delete': 3}]) self.assertEqual(delta.get_ops(), [{'delete': 3}]) existing_delta = Delta([{'delete': 3}]) delta = Delta(existing_delta) self.assertEqual(delta.get_ops(), [{'delete': 3}]) delta = Delta({'ops': [{'delete': 3}]}) self.assertEqual(delta.get_ops(), [{'delete': 3}]) delta = Delta('whoops') self.assertEqual(delta.get_ops(), [])
def test_retain(self): delta = Delta() delta.retain(0) self.assertEqual(delta.get_ops(), []) delta = Delta() delta.retain(-10) self.assertEqual(delta.get_ops(), []) delta = Delta() delta.retain('whoops') self.assertEqual(delta.get_ops(), []) delta = Delta() delta.retain(15) self.assertEqual(delta.get_ops(), [{'retain': 15}]) delta = Delta() delta.retain(15, {}) self.assertEqual(delta.get_ops(), [{'retain': 15}]) delta = Delta() delta.retain(15, {'bold': True}) self.assertEqual(delta.get_ops(), [{'retain': 15, 'attributes': {'bold': True}}])