zefyr

line_test.dart 8.7KB

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