zefyr

line_test.dart 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 boldStyle = NotusStyle().merge(NotusAttribute.bold);
  8. final h1Style = NotusStyle().merge(NotusAttribute.h1);
  9. final h2Style = NotusStyle().merge(NotusAttribute.h2);
  10. final ulStyle = NotusStyle().merge(NotusAttribute.ul);
  11. final bqStyle = NotusStyle().merge(NotusAttribute.bq);
  12. void main() {
  13. group('$LineNode', () {
  14. ContainerNode root;
  15. setUp(() {
  16. root = RootNode();
  17. });
  18. test('empty', () {
  19. final node = LineNode();
  20. expect(node, isEmpty);
  21. expect(node.length, 1);
  22. expect(node.style, NotusStyle());
  23. expect(node.toDelta().toList(), [Operation.insert('\n')]);
  24. });
  25. test('hasEmbed', () {
  26. final node = LineNode();
  27. expect(node.hasEmbed, isFalse);
  28. node.add(EmbedNode());
  29. expect(node.hasEmbed, isTrue);
  30. });
  31. test('nextLine', () {
  32. root.insert(
  33. 0, 'Hello world\nThis is my first multiline\nItem\ndocument.', null);
  34. root.retain(38, 1, ulStyle);
  35. root.retain(43, 1, bqStyle);
  36. LineNode line = root.first;
  37. expect(line.toPlainText(), 'Hello world\n');
  38. var next = line.nextLine;
  39. expect(next.toPlainText(), 'This is my first multiline\n');
  40. next = next.nextLine;
  41. expect(next.toPlainText(), 'Item\n');
  42. next = next.nextLine;
  43. expect(next.toPlainText(), 'document.\n');
  44. expect(next.nextLine, isNull);
  45. });
  46. test('toString', () {
  47. final node = LineNode();
  48. node.insert(0, 'London "Grammar" - Hey Now', null);
  49. node.retain(0, 16, boldStyle);
  50. node.applyAttribute(NotusAttribute.h1);
  51. expect('$node', '¶ ⟨London "Grammar"⟩b → ⟨ - Hey Now⟩ ⏎ {heading: 1}');
  52. });
  53. test('splitAt with multiple text segments', () {
  54. root.insert(0, 'This house is a circus', null);
  55. root.retain(0, 4, boldStyle);
  56. root.retain(16, 6, boldStyle);
  57. LineNode line = root.first;
  58. final newLine = line.splitAt(10);
  59. expect(line.toPlainText(), 'This house\n');
  60. expect(newLine.toPlainText(), ' is a circus\n');
  61. });
  62. test('insert into empty line', () {
  63. final node = LineNode();
  64. node.insert(0, 'London "Grammar" - Hey Now', null);
  65. expect(node, hasLength(27));
  66. expect(node.toDelta(), Delta()..insert('London "Grammar" - Hey Now\n'));
  67. });
  68. test('insert into empty line with styles', () {
  69. final node = LineNode();
  70. node.insert(0, 'London "Grammar" - Hey Now', null);
  71. node.retain(0, 16, boldStyle);
  72. node.applyAttribute(NotusAttribute.h1);
  73. expect(node, hasLength(27));
  74. expect(node.childCount, 2);
  75. final delta = Delta()
  76. ..insert('London "Grammar"', boldStyle.toJson())
  77. ..insert(' - Hey Now')
  78. ..insert('\n', NotusAttribute.h1.toJson());
  79. expect(node.toDelta(), delta);
  80. });
  81. test('insert into non-empty line', () {
  82. final node = LineNode();
  83. node.insert(0, 'Hello world', null);
  84. node.insert(11, '!!!', null);
  85. expect(node, hasLength(15));
  86. expect(node.childCount, 1);
  87. expect(node.toDelta(), Delta()..insert('Hello world!!!\n'));
  88. });
  89. test('insert text with line-break at the end of line', () {
  90. root.insert(0, 'Hello world', null);
  91. root.insert(11, '!!!\n', null);
  92. expect(root.childCount, 2);
  93. LineNode line = root.first;
  94. expect(line, hasLength(15));
  95. expect(line.toDelta(), Delta()..insert('Hello world!!!\n'));
  96. LineNode line2 = root.last;
  97. expect(line2, hasLength(1));
  98. expect(line2.toDelta(), Delta()..insert('\n'));
  99. });
  100. test('insert into second text segment', () {
  101. root.insert(0, 'Hello world', null);
  102. root.retain(6, 5, boldStyle);
  103. root.insert(11, '!!!', null);
  104. LineNode line = root.first;
  105. expect(line, hasLength(15));
  106. final delta = Delta()
  107. ..insert('Hello ')
  108. ..insert('world', boldStyle.toJson())
  109. ..insert('!!!\n');
  110. expect(line.toDelta(), delta);
  111. });
  112. test('format line', () {
  113. root.insert(0, 'Hello world', null);
  114. root.retain(11, 1, h1Style);
  115. LineNode line = root.first;
  116. expect(line, hasLength(12));
  117. final delta = Delta()
  118. ..insert('Hello world')
  119. ..insert('\n', NotusAttribute.h1.toJson());
  120. expect(line.toDelta(), delta);
  121. });
  122. test('format line with inline attributes', () {
  123. root.insert(0, 'Hello world', null);
  124. expect(() {
  125. root.retain(11, 1, boldStyle);
  126. }, throwsA(const TypeMatcher<AssertionError>()));
  127. });
  128. test('format text inside line with block/line attributes', () {
  129. root.insert(0, 'Hello world', null);
  130. expect(() {
  131. root.retain(10, 2, h1Style);
  132. }, throwsA(const TypeMatcher<AssertionError>()));
  133. });
  134. test('format root line to unset block style', () {
  135. final unsetBlock = NotusStyle().put(NotusAttribute.block.unset);
  136. root.insert(0, 'Hello world', null);
  137. root.retain(11, 1, unsetBlock);
  138. expect(root.childCount, 1);
  139. expect(root.first, const TypeMatcher<LineNode>());
  140. LineNode line = root.first;
  141. expect(line.style.contains(NotusAttribute.block), isFalse);
  142. });
  143. test('format multiple empty lines', () {
  144. root.insert(0, 'Hello world\n\n\n', null);
  145. root.retain(11, 3, ulStyle);
  146. expect(root.children, hasLength(2));
  147. BlockNode block = root.first;
  148. expect(block.children, hasLength(3));
  149. expect(block.toPlainText(), 'Hello world\n\n\n');
  150. });
  151. test('delete a line', () {
  152. root.insert(0, 'Hello world', null);
  153. root.delete(0, 12);
  154. expect(root, isEmpty);
  155. // TODO: this should really enforce at least one empty line.
  156. });
  157. test('delete from the middle of a line', () {
  158. root.insert(0, 'Hello world', null);
  159. root.delete(4, 3);
  160. root.delete(6, 1);
  161. expect(root.childCount, 1);
  162. LineNode line = root.first;
  163. expect(line, hasLength(8));
  164. expect(line.childCount, 1);
  165. final lineDelta = Delta()..insert('Hellord\n');
  166. expect(line.toDelta(), lineDelta);
  167. });
  168. test('delete from non-first segment in line', () {
  169. root.insert(0, 'Hello world, Ab cd ef!', null);
  170. root.retain(6, 5, boldStyle);
  171. root.delete(10, 5);
  172. expect(root.childCount, 1);
  173. LineNode line = root.first;
  174. expect(line, hasLength(18));
  175. final lineDelta = Delta()
  176. ..insert('Hello ')
  177. ..insert('worl', boldStyle.toJson())
  178. ..insert(' cd ef!\n');
  179. expect(line.toDelta(), lineDelta);
  180. });
  181. test('delete on multiple lines', () {
  182. root.insert(0, 'delete\nmultiple\nlines', null);
  183. root.retain(21, 1, h2Style);
  184. root.delete(3, 15);
  185. expect(root.childCount, 1);
  186. LineNode line = root.first;
  187. expect(line.childCount, 1);
  188. final delta = Delta()..insert('delnes')..insert('\n', h2Style.toJson());
  189. expect(line.toDelta(), delta);
  190. });
  191. test('delete empty line', () {
  192. root.insert(
  193. 0, 'Hello world\nThis is my first multiline\n\ndocument.', null);
  194. expect(root.childCount, 4);
  195. root.delete(39, 1);
  196. expect(root.childCount, 3);
  197. });
  198. test('delete line-break of non-empty line', () {
  199. root.insert(
  200. 0, 'Hello world\nThis is my first multiline\n\ndocument.', null);
  201. root.retain(39, 1, h2Style);
  202. expect(root.childCount, 4);
  203. root.delete(38, 1);
  204. expect(root.childCount, 3);
  205. LineNode line = root.children.elementAt(1);
  206. expect(line.style.get(NotusAttribute.heading), NotusAttribute.h2);
  207. });
  208. test('insert at the beginning of a line', () {
  209. root.insert(
  210. 0, 'Hello world\nThis is my first multiline\ndocument.', null);
  211. root.insert(12, 'Boom! ', null);
  212. expect(root.childCount, 3);
  213. expect(root.children.elementAt(1), hasLength(33));
  214. });
  215. test('delete last character of a line', () {
  216. root.insert(
  217. 0, 'Hello world\nThis is my first multiline\ndocument.', null);
  218. root.delete(37, 1);
  219. expect(root.childCount, 3);
  220. LineNode line = root.children.elementAt(1);
  221. expect(line.toDelta(), Delta()..insert('This is my first multilin\n'));
  222. });
  223. test('collectStyle', () {
  224. // TODO: need more test cases for collectStyle
  225. root.insert(
  226. 0, 'Hello world\nThis is my first multiline\n\ndocument.', null);
  227. root.retain(38, 1, h2Style);
  228. root.retain(23, 5, boldStyle);
  229. var result = root.lookup(20);
  230. LineNode line = result.node;
  231. var attrs = line.collectStyle(result.offset, 5);
  232. expect(attrs, h2Style);
  233. });
  234. test('collectStyle with embed nodes', () {
  235. root.insert(0, 'Hello world\n\nMore text.\n', null);
  236. var style = NotusStyle();
  237. style = style.put(NotusAttribute.embed.horizontalRule);
  238. root.insert(12, EmbedNode.kPlainTextPlaceholder, style);
  239. var lookup = root.lookup(0);
  240. LineNode line = lookup.node;
  241. var result = line.collectStyle(lookup.offset, 15);
  242. expect(result, isEmpty);
  243. });
  244. });
  245. }