zefyr

block_test.dart 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright (c) 2018, the Zefyr project authors. Please see the AUTHORS file
  2. // for details. All rights reserved. Use of this source code is governed by a
  3. // BSD-style license that can be found in the LICENSE file.
  4. import 'package:notus/notus.dart';
  5. import 'package:quill_delta/quill_delta.dart';
  6. import 'package:test/test.dart';
  7. final ulAttrs = NotusStyle().merge(NotusAttribute.ul);
  8. final olAttrs = NotusStyle().merge(NotusAttribute.ol);
  9. final h1Attrs = NotusStyle().merge(NotusAttribute.h1);
  10. void main() {
  11. group('$BlockNode', () {
  12. ContainerNode root;
  13. setUp(() {
  14. root = RootNode();
  15. });
  16. test('empty', () {
  17. final node = BlockNode();
  18. expect(node, isEmpty);
  19. expect(node.length, 0);
  20. expect(node.style, NotusStyle());
  21. });
  22. test('toString', () {
  23. final line = LineNode();
  24. line.add(TextNode('London "Grammar"'));
  25. final block = BlockNode();
  26. block.applyAttribute(NotusAttribute.ul);
  27. block.add(line);
  28. final expected = '§ {ul}\n └ ¶ ⟨London "Grammar"⟩ ⏎';
  29. expect('$block', expected);
  30. });
  31. test('unwrapLine from first block', () {
  32. root.insert(0, 'One\nTwo\nThree', null);
  33. root.retain(3, 1, ulAttrs);
  34. root.retain(7, 1, ulAttrs);
  35. root.retain(13, 1, ulAttrs);
  36. expect(root.childCount, 1);
  37. BlockNode block = root.first;
  38. LineNode line = block.children.elementAt(1);
  39. block.unwrapLine(line);
  40. expect(root.children, hasLength(3));
  41. expect(root.children.elementAt(0), const TypeMatcher<BlockNode>());
  42. expect(root.children.elementAt(1), line);
  43. expect(root.children.elementAt(2), block);
  44. });
  45. test('format first line as list', () {
  46. root.insert(0, 'Hello world', null);
  47. root.retain(11, 1, ulAttrs);
  48. expect(root.childCount, 1);
  49. BlockNode block = root.first;
  50. expect(block.style.get(NotusAttribute.block), NotusAttribute.ul);
  51. expect(block.childCount, 1);
  52. expect(block.first, const TypeMatcher<LineNode>());
  53. LineNode line = block.first;
  54. final delta = Delta()
  55. ..insert('Hello world')
  56. ..insert('\n', ulAttrs.toJson());
  57. expect(line.toDelta(), delta);
  58. });
  59. test('format second line as list', () {
  60. root.insert(0, 'Hello world\nAb cd ef!', null);
  61. root.retain(21, 1, ulAttrs);
  62. expect(root.childCount, 2);
  63. BlockNode block = root.last;
  64. expect(block.style.get(NotusAttribute.block), NotusAttribute.ul);
  65. expect(block.childCount, 1);
  66. expect(block.first, const TypeMatcher<LineNode>());
  67. });
  68. test('format two sibling lines as list', () {
  69. root.insert(0, 'Hello world\nAb cd ef!', null);
  70. root.retain(11, 1, ulAttrs);
  71. root.retain(21, 1, ulAttrs);
  72. expect(root.childCount, 1);
  73. BlockNode block = root.first;
  74. expect(block.style.get(NotusAttribute.block), NotusAttribute.ul);
  75. expect(block.childCount, 2);
  76. expect(block.first, const TypeMatcher<LineNode>());
  77. expect(block.last, const TypeMatcher<LineNode>());
  78. });
  79. test('format to split first line from block', () {
  80. root.insert(
  81. 0, 'London Grammar Songs\nHey now\nStrong\nIf You Wait', null);
  82. root.retain(20, 1, h1Attrs);
  83. root.retain(28, 1, ulAttrs);
  84. root.retain(35, 1, ulAttrs);
  85. root.retain(47, 1, ulAttrs);
  86. expect(root.childCount, 2);
  87. root.retain(28, 1, olAttrs);
  88. expect(root.childCount, 3);
  89. final expected = Delta()
  90. ..insert('London Grammar Songs')
  91. ..insert('\n', NotusAttribute.h1.toJson())
  92. ..insert('Hey now')
  93. ..insert('\n', NotusAttribute.ol.toJson())
  94. ..insert('Strong')
  95. ..insert('\n', ulAttrs.toJson())
  96. ..insert('If You Wait')
  97. ..insert('\n', ulAttrs.toJson());
  98. expect(root.toDelta(), expected);
  99. });
  100. test('format to split last line from block', () {
  101. root.insert(
  102. 0, 'London Grammar Songs\nHey now\nStrong\nIf You Wait', null);
  103. root.retain(20, 1, h1Attrs);
  104. root.retain(28, 1, ulAttrs);
  105. root.retain(35, 1, ulAttrs);
  106. root.retain(47, 1, ulAttrs);
  107. expect(root.childCount, 2);
  108. root.retain(47, 1, olAttrs);
  109. expect(root.childCount, 3);
  110. final expected = Delta()
  111. ..insert('London Grammar Songs')
  112. ..insert('\n', NotusAttribute.h1.toJson())
  113. ..insert('Hey now')
  114. ..insert('\n', ulAttrs.toJson())
  115. ..insert('Strong')
  116. ..insert('\n', ulAttrs.toJson())
  117. ..insert('If You Wait')
  118. ..insert('\n', NotusAttribute.ol.toJson());
  119. expect(root.toDelta(), expected);
  120. });
  121. test('format to split middle line from block', () {
  122. root.insert(
  123. 0, 'London Grammar Songs\nHey now\nStrong\nIf You Wait', null);
  124. root.retain(20, 1, h1Attrs);
  125. root.retain(28, 1, ulAttrs);
  126. root.retain(35, 1, ulAttrs);
  127. root.retain(47, 1, ulAttrs);
  128. expect(root.childCount, 2);
  129. root.retain(35, 1, olAttrs);
  130. expect(root.childCount, 4);
  131. final expected = Delta()
  132. ..insert('London Grammar Songs')
  133. ..insert('\n', NotusAttribute.h1.toJson())
  134. ..insert('Hey now')
  135. ..insert('\n', ulAttrs.toJson())
  136. ..insert('Strong')
  137. ..insert('\n', NotusAttribute.ol.toJson())
  138. ..insert('If You Wait')
  139. ..insert('\n', ulAttrs.toJson());
  140. expect(root.toDelta(), expected);
  141. });
  142. test('insert line-break at the begining of the document', () {
  143. root.insert(
  144. 0, 'London Grammar Songs\nHey now\nStrong\nIf You Wait', null);
  145. root.retain(20, 1, ulAttrs);
  146. root.retain(28, 1, ulAttrs);
  147. root.retain(35, 1, ulAttrs);
  148. root.retain(47, 1, ulAttrs);
  149. expect(root.childCount, 1);
  150. root.insert(0, '\n', null);
  151. expect(root.childCount, 2);
  152. });
  153. });
  154. }