operation_test.dart 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. });
  21. test("transform path delta", () {
  22. expect(transformPath([0, 1], [0, 1], 5), [0, 6]);
  23. });
  24. });
  25. group('transform operation', () {
  26. test('insert + insert', () {
  27. final t = transformOperation(
  28. InsertOperation(path: [
  29. 0,
  30. 1
  31. ], value: Node(type: "node", attributes: {}, children: LinkedList())),
  32. InsertOperation(
  33. path: [0, 1],
  34. value:
  35. Node(type: "node", attributes: {}, children: LinkedList())));
  36. expect(t.path, [0, 2]);
  37. });
  38. test('delete + delete', () {
  39. final t = transformOperation(
  40. DeleteOperation(
  41. path: [0, 1],
  42. removedValue:
  43. Node(type: "node", attributes: {}, children: LinkedList())),
  44. DeleteOperation(
  45. path: [0, 2],
  46. removedValue:
  47. Node(type: "node", attributes: {}, children: LinkedList())));
  48. expect(t.path, [0, 1]);
  49. });
  50. });
  51. test('transform transaction builder', () {
  52. final item1 = Node(type: "node", attributes: {}, children: LinkedList());
  53. final item2 = Node(type: "node", attributes: {}, children: LinkedList());
  54. final item3 = Node(type: "node", attributes: {}, children: LinkedList());
  55. final root = Node(
  56. type: "root",
  57. attributes: {},
  58. children: LinkedList()
  59. ..addAll([
  60. item1,
  61. item2,
  62. item3,
  63. ]));
  64. final state = EditorState(
  65. document: StateTree(root: root), renderPlugins: RenderPlugins());
  66. expect(item1.path, [0]);
  67. expect(item2.path, [1]);
  68. expect(item3.path, [2]);
  69. final tb = TransactionBuilder(state);
  70. tb.deleteNode(item1);
  71. tb.deleteNode(item2);
  72. tb.deleteNode(item3);
  73. final transaction = tb.finish();
  74. expect(transaction.operations[0].path, [0]);
  75. expect(transaction.operations[1].path, [0]);
  76. expect(transaction.operations[2].path, [0]);
  77. });
  78. }