zefyr

line_test.dart 9.3KB

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