def test_delta_as_data(self): delta = Delta(ops=[ Insert('abc', None), Retain(1, {'color': 'red'}), Delete(4), Insert('def', {'bold': True}), Retain(6, None) ]) expected = [{ 'insert': 'abc' }, { 'retain': 1, 'attributes': { 'color': 'red' } }, { 'delete': 4 }, { 'insert': 'def', 'attributes': { 'bold': True } }, { 'retain': 6 }] self.assertListEqual(delta.as_data(), expected)
def test_insert_from_dict(self): assert load_operation({'insert': 1}) == Insert(1, None) assert load_operation({'insert': 'foo'}) == Insert('foo', None) assert load_operation({ 'insert': 'foo', 'attributes': { 'bold': True } }) == Insert('foo', {'bold': True})
def test_insert_constructor(self): op = Insert('foo', None) assert op.value == 'foo' assert op.attributes is None assert op.length == 3 op = Insert(1, None) assert op.value == 1 assert op.attributes is None
def test_insert_in_middle_of_text(self): a = Delta().insert('Hello') b = Delta().retain(3).insert('X') assert a.ops == [Insert('Hello', None)] assert b.ops == [Retain(3, None), Insert('X', None)] expected = Delta().insert('HelXlo') compose = a.compose(b) assert compose.ops == expected.ops, f'{expected} not {compose}'
def test_foreach(self): spy_mock = mock.MagicMock() for op in self.delta.ops: spy_mock(op) assert spy_mock.call_count == 3 assert spy_mock.call_args_list == [ mock.call(Insert('Hello', None)), mock.call(Insert({'image': True}, None)), mock.call(Insert('World !', None)) ]
def test_insert_embed(self): op = Insert({'image': 'photo.jpg'}, None) assert op.value == {'image': 'photo.jpg'} assert op.attributes is None assert op.length == 1
def test_retain_from_dict(self): assert load_operation({'retain': 1}) == Retain(1, None) assert load_operation({ 'retain': 1, 'attributes': { 'bold': True } }) == Insert(1, {'bold': True})
def test_insert_as_data(self): assert Insert('foo', None).as_data() == {'insert': 'foo'} assert Insert('foo', {}).as_data() == {'insert': 'foo'} assert Insert('foo', { 'bold': True }).as_data() == { 'insert': 'foo', 'attributes': { 'bold': True } } assert Insert(1, { 'img': 'image.jpg' }).as_data() == { 'insert': 1, 'attributes': { 'img': 'image.jpg' } }
def test_delta_as_string(self): delta = Delta(ops=[ Insert('abc', None), Retain(1, {'color': 'red'}), Delete(3), ]) assert str(delta) == ('[{"insert": "abc"}, ' '{"retain": 1, "attributes": {"color": "red"}}, ' '{"delete": 3}]')
def test_sum_operation(self): op = Delete(1) + Delete(2) assert op == Delete(3) op = reduce(lambda o, other: o + other, [Delete(1), Delete(2), Delete(3)]) assert op == Delete(6) with pytest.raises(ValueError) as err: Delete(1) + Insert('foo', None) assert err.match('Operations are not the same type')
def test_sum_operation(self): op = Retain(1, None) + Retain(2, None) assert op == Retain(3, None) op = reduce(lambda o, other: o + other, [Retain(1, None), Retain(2, None), Retain(3, None)]) assert op == Retain(6, None) op = Retain(1, {'bold': True}) + Retain(2, {'bold': True}) assert op == Retain(3, {'bold': True}) with pytest.raises(ValueError) as err: Retain(1, None) + Insert('foo', None) assert err.match('Operations are not the same type') with pytest.raises(ValueError) as err: Retain(1, {'bold': True}) + Retain(1, {'bold': False}) assert err.match('operations with different attributes')
def test_insert_attributes(self): op = Insert('foo', {'bold': True}) assert op.attributes == {'bold': True}
def test_parse(self): op = Insert.fromdict({'insert': 'foo'}) assert op.value == 'foo' assert op.attributes is None
def test_insert_str_json(self): assert str(Insert('foo', None)) == '{"insert": "foo"}' assert str(Insert('foo', {})) == '{"insert": "foo"}' assert str(Insert( 'foo', {'bold': True })) == '{"insert": "foo", "attributes": {"bold": true}}'
def test_sum_operation(self): op = Insert('foo', None) + Insert(' bar', None) assert op == Insert('foo bar', None) op = reduce( lambda o, other: o + other, [Insert('foo', None), Insert(' ', None), Insert('bar', None)]) assert op == Insert('foo bar', None) op = Insert('foo', {'bold': True}) + Insert(' bar', {'bold': True}) assert op == Insert('foo bar', {'bold': True}) with pytest.raises(ValueError) as err: Insert('foo', None) + 'bar' assert err.match('Operations are not the same type') with pytest.raises(ValueError) as err: Insert('foo', {'bold': True}) + Insert(' bar', {'bold': False}) assert err.match('operations with different attributes')
def test_as_json(self): op = Insert('foo', {'bold': True}) assert op.as_json( ) == '{"insert": "foo", "attributes": {"bold": true}}'
def test_as_data(self): op = Insert('foo', {'bold': True}) assert op.as_data() == {'insert': 'foo', 'attributes': {'bold': True}}