operation_test.dart 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import 'dart:collection';
  2. import 'package:flowy_editor/document/node.dart';
  3. import 'package:flutter_test/flutter_test.dart';
  4. import 'package:flowy_editor/operation/operation.dart';
  5. import 'package:flowy_editor/operation/transaction_builder.dart';
  6. import 'package:flowy_editor/editor_state.dart';
  7. import 'package:flowy_editor/document/state_tree.dart';
  8. import 'package:flowy_editor/render/render_plugins.dart';
  9. void main() {
  10. group('transform path', () {
  11. test('transform path changed', () {
  12. expect(transformPath([0, 1], [0, 1]), [0, 2]);
  13. expect(transformPath([0, 1], [0, 2]), [0, 3]);
  14. expect(transformPath([0, 1], [0, 2, 7, 8, 9]), [0, 3, 7, 8, 9]);
  15. expect(transformPath([0, 1, 2], [0, 0, 7, 8, 9]), [0, 0, 7, 8, 9]);
  16. });
  17. test("transform path not changed", () {
  18. expect(transformPath([0, 1, 2], [0, 0, 7, 8, 9]), [0, 0, 7, 8, 9]);
  19. expect(transformPath([0, 1, 2], [0, 1]), [0, 1]);
  20. expect(transformPath([1, 1], [1, 0]), [1, 0]);
  21. });
  22. test("transform path delta", () {
  23. expect(transformPath([0, 1], [0, 1], 5), [0, 6]);
  24. });
  25. });
  26. group('transform operation', () {
  27. test('insert + insert', () {
  28. final t = transformOperation(
  29. InsertOperation(path: [
  30. 0,
  31. 1
  32. ], value: Node(type: "node", attributes: {}, children: LinkedList())),
  33. InsertOperation(
  34. path: [0, 1],
  35. value:
  36. Node(type: "node", attributes: {}, children: LinkedList())));
  37. expect(t.path, [0, 2]);
  38. });
  39. test('delete + delete', () {
  40. final t = transformOperation(
  41. DeleteOperation(
  42. path: [0, 1],
  43. removedValue:
  44. Node(type: "node", attributes: {}, children: LinkedList())),
  45. DeleteOperation(
  46. path: [0, 2],
  47. removedValue:
  48. Node(type: "node", attributes: {}, children: LinkedList())));
  49. expect(t.path, [0, 1]);
  50. });
  51. });
  52. test('transform transaction builder', () {
  53. final item1 = Node(type: "node", attributes: {}, children: LinkedList());
  54. final item2 = Node(type: "node", attributes: {}, children: LinkedList());
  55. final item3 = Node(type: "node", attributes: {}, children: LinkedList());
  56. final root = Node(
  57. type: "root",
  58. attributes: {},
  59. children: LinkedList()
  60. ..addAll([
  61. item1,
  62. item2,
  63. item3,
  64. ]));
  65. final state = EditorState(
  66. document: StateTree(root: root), renderPlugins: RenderPlugins());
  67. expect(item1.path, [0]);
  68. expect(item2.path, [1]);
  69. expect(item3.path, [2]);
  70. final tb = TransactionBuilder(state);
  71. tb.deleteNode(item1);
  72. tb.deleteNode(item2);
  73. tb.deleteNode(item3);
  74. final transaction = tb.finish();
  75. expect(transaction.operations[0].path, [0]);
  76. expect(transaction.operations[1].path, [0]);
  77. expect(transaction.operations[2].path, [0]);
  78. });
  79. }