zefyr

insert_rules_test.dart 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 ul = NotusAttribute.ul.toJson();
  8. final bold = NotusAttribute.bold.toJson();
  9. void main() {
  10. group('$CatchAllInsertRule', () {
  11. final rule = new CatchAllInsertRule();
  12. test('applies change as-is', () {
  13. final doc = new Delta()..insert('Document\n');
  14. final actual = rule.apply(doc, 8, '!');
  15. final expected = new Delta()
  16. ..retain(8)
  17. ..insert('!');
  18. expect(actual, expected);
  19. });
  20. });
  21. group('$PreserveLineStyleOnSplitRule', () {
  22. final rule = new PreserveLineStyleOnSplitRule();
  23. test('skips at the beginning of a document', () {
  24. final doc = new Delta()..insert('One\n');
  25. final actual = rule.apply(doc, 0, '\n');
  26. expect(actual, isNull);
  27. });
  28. test('applies in a block', () {
  29. final doc = new Delta()
  30. ..insert('One and two')
  31. ..insert('\n', ul)
  32. ..insert('Three')
  33. ..insert('\n', ul);
  34. final actual = rule.apply(doc, 8, '\n');
  35. final expected = new Delta()
  36. ..retain(8)
  37. ..insert('\n', ul);
  38. expect(actual, isNotNull);
  39. expect(actual, expected);
  40. });
  41. });
  42. group('$ResetLineFormatOnNewLineRule', () {
  43. final rule = const ResetLineFormatOnNewLineRule();
  44. test('applies when line-break is inserted at the end of line', () {
  45. final doc = new Delta()
  46. ..insert('Hello world')
  47. ..insert('\n', NotusAttribute.h1.toJson());
  48. final actual = rule.apply(doc, 11, '\n');
  49. expect(actual, isNotNull);
  50. final expected = new Delta()
  51. ..retain(11)
  52. ..insert('\n', NotusAttribute.h1.toJson())
  53. ..retain(1, NotusAttribute.heading.unset.toJson());
  54. expect(actual, expected);
  55. });
  56. test('applies without style reset if not needed', () {
  57. final doc = new Delta()..insert('Hello world\n');
  58. final actual = rule.apply(doc, 11, '\n');
  59. expect(actual, isNotNull);
  60. final expected = new Delta()
  61. ..retain(11)
  62. ..insert('\n');
  63. expect(actual, expected);
  64. });
  65. test('applies at the beginning of a document', () {
  66. final doc = new Delta()
  67. ..insert('\n', NotusAttribute.h1.toJson());
  68. final actual = rule.apply(doc, 0, '\n');
  69. expect(actual, isNotNull);
  70. final expected = new Delta()
  71. ..insert('\n', NotusAttribute.h1.toJson())
  72. ..retain(1, NotusAttribute.heading.unset.toJson());
  73. expect(actual, expected);
  74. });
  75. test('applies and keeps block style', () {
  76. final style = NotusAttribute.ul.toJson();
  77. style.addAll(NotusAttribute.h1.toJson());
  78. final doc = new Delta()..insert('Hello world')..insert('\n', style);
  79. final actual = rule.apply(doc, 11, '\n');
  80. expect(actual, isNotNull);
  81. final expected = new Delta()
  82. ..retain(11)
  83. ..insert('\n', style)
  84. ..retain(1, NotusAttribute.heading.unset.toJson());
  85. expect(actual, expected);
  86. });
  87. test('applies to a line in the middle of a document', () {
  88. final doc = new Delta()
  89. ..insert('Hello \nworld!\nMore lines here.')
  90. ..insert('\n', NotusAttribute.h2.toJson());
  91. final actual = rule.apply(doc, 30, '\n');
  92. expect(actual, isNotNull);
  93. final expected = new Delta()
  94. ..retain(30)
  95. ..insert('\n', NotusAttribute.h2.toJson())
  96. ..retain(1, NotusAttribute.heading.unset.toJson());
  97. expect(actual, expected);
  98. });
  99. });
  100. group('$AutoExitBlockRule', () {
  101. final rule = new AutoExitBlockRule();
  102. test('applies when line-break is inserted on empty line in a block', () {
  103. final ul = NotusAttribute.ul.toJson();
  104. final doc = new Delta()
  105. ..insert('Item 1')
  106. ..insert('\n', ul)
  107. ..insert('Item 2')
  108. ..insert('\n\n', ul);
  109. final actual = rule.apply(doc, 14, '\n');
  110. expect(actual, isNotNull);
  111. final expected = new Delta()
  112. ..retain(14)
  113. ..retain(1, NotusAttribute.block.unset.toJson());
  114. expect(actual, expected);
  115. });
  116. test('applies only on empty line', () {
  117. final ul = NotusAttribute.ul.toJson();
  118. final doc = new Delta()..insert('Item 1')..insert('\n', ul);
  119. final actual = rule.apply(doc, 6, '\n');
  120. expect(actual, isNull);
  121. });
  122. test('applies at the beginning of a document', () {
  123. final ul = NotusAttribute.ul.toJson();
  124. final doc = new Delta()..insert('\n', ul);
  125. final actual = rule.apply(doc, 0, '\n');
  126. expect(actual, isNotNull);
  127. final expected = new Delta()
  128. ..retain(1, NotusAttribute.block.unset.toJson());
  129. expect(actual, expected);
  130. });
  131. test('ignores non-empty line at the beginning of a document', () {
  132. final ul = NotusAttribute.ul.toJson();
  133. final doc = new Delta()..insert('Text\n', ul);
  134. final actual = rule.apply(doc, 0, '\n');
  135. expect(actual, isNull);
  136. });
  137. });
  138. group('$PreserveInlineStylesRule', () {
  139. final rule = new PreserveInlineStylesRule();
  140. test('apply', () {
  141. final doc = new Delta()
  142. ..insert('Doc with ')
  143. ..insert('bold', bold)
  144. ..insert(' text');
  145. final actual = rule.apply(doc, 13, 'er');
  146. final expected = new Delta()
  147. ..retain(13)
  148. ..insert('er', bold);
  149. expect(expected, actual);
  150. });
  151. test('apply at the beginning of a document', () {
  152. final doc = new Delta()..insert('Doc with ');
  153. final actual = rule.apply(doc, 0, 'A ');
  154. expect(actual, isNull);
  155. });
  156. });
  157. group('$AutoFormatLinksRule', () {
  158. final rule = new AutoFormatLinksRule();
  159. final link = NotusAttribute.link.fromString('https://example.com').toJson();
  160. test('apply simple', () {
  161. final doc = new Delta()..insert('Doc with link https://example.com');
  162. final actual = rule.apply(doc, 33, ' ');
  163. final expected = new Delta()
  164. ..retain(14)
  165. ..retain(19, link)
  166. ..insert(' ');
  167. expect(expected, actual);
  168. });
  169. test('applies only to insert of single space', () {
  170. final doc = new Delta()..insert('Doc with link https://example.com');
  171. final actual = rule.apply(doc, 33, '/');
  172. expect(actual, isNull);
  173. });
  174. test('applies for links at the beginning of line', () {
  175. final doc = new Delta()..insert('Doc with link\nhttps://example.com');
  176. final actual = rule.apply(doc, 33, ' ');
  177. final expected = new Delta()
  178. ..retain(14)
  179. ..retain(19, link)
  180. ..insert(' ');
  181. expect(expected, actual);
  182. });
  183. test('ignores if already formatted as link', () {
  184. final doc = new Delta()
  185. ..insert('Doc with link\n')
  186. ..insert('https://example.com', link);
  187. final actual = rule.apply(doc, 33, ' ');
  188. expect(actual, isNull);
  189. });
  190. });
  191. }