zefyr

format_rules_test.dart 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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('$ResolveLineFormatRule', () {
  11. final rule = new ResolveLineFormatRule();
  12. test('apply', () {
  13. final doc = new Delta()..insert('Correct\nLine\nStyle\nRule\n');
  14. final actual = rule.apply(doc, 0, 20, NotusAttribute.ul);
  15. expect(actual, isNotNull);
  16. final ul = NotusAttribute.ul.toJson();
  17. final expected = new Delta()
  18. ..retain(7)
  19. ..retain(1, ul)
  20. ..retain(4)
  21. ..retain(1, ul)
  22. ..retain(5)
  23. ..retain(1, ul)
  24. ..retain(4)
  25. ..retain(1, ul);
  26. expect(actual, expected);
  27. });
  28. test('apply with zero length (collapsed selection)', () {
  29. final doc = new Delta()..insert('Correct\nLine\nStyle\nRule\n');
  30. final actual = rule.apply(doc, 0, 0, NotusAttribute.ul);
  31. expect(actual, isNotNull);
  32. final ul = NotusAttribute.ul.toJson();
  33. final expected = new Delta()..retain(7)..retain(1, ul);
  34. expect(actual, expected);
  35. });
  36. test('apply with zero length in the middle of a line', () {
  37. final ul = NotusAttribute.ul.toJson();
  38. final doc = new Delta()
  39. ..insert('Title\nOne')
  40. ..insert('\n', ul)
  41. ..insert('Two')
  42. ..insert('\n', ul)
  43. ..insert('Three!\n');
  44. final actual = rule.apply(doc, 7, 0, NotusAttribute.ul);
  45. final expected = new Delta()..retain(9)..retain(1, ul);
  46. expect(actual, expected);
  47. });
  48. });
  49. group('$ResolveInlineFormatRule', () {
  50. final rule = new ResolveInlineFormatRule();
  51. test('apply', () {
  52. final doc = new Delta()..insert('Correct\nLine\nStyle\nRule\n');
  53. final actual = rule.apply(doc, 0, 20, NotusAttribute.bold);
  54. expect(actual, isNotNull);
  55. final b = NotusAttribute.bold.toJson();
  56. final expected = new Delta()
  57. ..retain(7, b)
  58. ..retain(1)
  59. ..retain(4, b)
  60. ..retain(1)
  61. ..retain(5, b)
  62. ..retain(1)
  63. ..retain(1, b);
  64. expect(actual, expected);
  65. });
  66. });
  67. group('$FormatLinkAtCaretPositionRule', () {
  68. final rule = new FormatLinkAtCaretPositionRule();
  69. test('apply', () {
  70. final link =
  71. NotusAttribute.link.fromString('https://github.com/memspace/bold');
  72. final newLink =
  73. NotusAttribute.link.fromString('https://github.com/memspace/zefyr');
  74. final doc = new Delta()
  75. ..insert('Visit our ')
  76. ..insert('website', link.toJson())
  77. ..insert(' for more details.\n');
  78. final actual = rule.apply(doc, 13, 0, newLink);
  79. expect(actual, isNotNull);
  80. final expected = new Delta()..retain(10)..retain(7, newLink.toJson());
  81. expect(actual, expected);
  82. });
  83. });
  84. }