zefyr

text_test.dart 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 = NotusStyle().merge(NotusAttribute.bold);
  8. final boldUnsetStyle = NotusStyle().put(NotusAttribute.bold.unset);
  9. final italicStyle = NotusStyle().merge(NotusAttribute.italic);
  10. void main() {
  11. group('$TextNode', () {
  12. LineNode line;
  13. TextNode node;
  14. setUp(() {
  15. line = LineNode();
  16. node = TextNode('London "Grammar"');
  17. line.add(node);
  18. });
  19. test('new empty text', () {
  20. final node = TextNode();
  21. expect(node.value, isEmpty);
  22. expect(node.length, 0);
  23. expect(node.style, NotusStyle());
  24. expect(node.toDelta(), isEmpty);
  25. });
  26. test('toString', () {
  27. node.applyAttribute(NotusAttribute.bold);
  28. node.applyAttribute(NotusAttribute.link.fromString('link'));
  29. expect('$node', '⟨London "Grammar"⟩ab');
  30. });
  31. test('new text with contents', () {
  32. expect(node.value, isNotEmpty);
  33. expect(node.length, 16);
  34. expect(node.toDelta().toList(), [Operation.insert('London "Grammar"')]);
  35. });
  36. test('insert at the end', () {
  37. node.insert(16, '!!!', null);
  38. expect(node.value, 'London "Grammar"!!!');
  39. });
  40. test('delete tail', () {
  41. node.delete(6, 10);
  42. expect(node.value, 'London');
  43. });
  44. test('format substring', () {
  45. node.retain(8, 7, boldStyle);
  46. expect(line.children, hasLength(3));
  47. expect(line.children.elementAt(0), hasLength(8));
  48. expect(line.children.elementAt(1), hasLength(7));
  49. expect(line.children.elementAt(2), hasLength(1));
  50. });
  51. test('format full segment', () {
  52. node.retain(0, 16, boldStyle);
  53. expect(line.childCount, 1);
  54. expect(node.value, 'London "Grammar"');
  55. expect(node.style.values, [NotusAttribute.bold]);
  56. });
  57. test('format with multiple styles', () {
  58. line.retain(0, 6, boldStyle);
  59. line.retain(0, 6, italicStyle);
  60. expect(line.childCount, 2);
  61. });
  62. test('format to remove attribute', () {
  63. line.retain(0, 6, boldStyle);
  64. line.retain(0, 6, boldUnsetStyle);
  65. expect(line.childCount, 1);
  66. expect(node.value, 'London "Grammar"');
  67. expect(node.style, isEmpty);
  68. });
  69. test('format intersecting nodes', () {
  70. line.retain(0, 6, boldStyle);
  71. line.retain(3, 10, italicStyle);
  72. expect(line.childCount, 4);
  73. expect(line.children.elementAt(0), hasLength(3));
  74. expect(line.children.elementAt(1), hasLength(3));
  75. expect(line.children.elementAt(2), hasLength(7));
  76. expect(line.children.elementAt(3), hasLength(3));
  77. });
  78. test('insert in formatted node', () {
  79. line.retain(0, 6, boldStyle);
  80. expect(line.childCount, 2);
  81. line.insert(3, 'don', null);
  82. expect(line.childCount, 4);
  83. final b = boldStyle.toJson();
  84. expect(
  85. line.children.elementAt(0).toDelta(),
  86. Delta()..insert('Lon', b),
  87. );
  88. expect(
  89. line.children.elementAt(1).toDelta(),
  90. Delta()..insert('don'),
  91. );
  92. expect(
  93. line.children.elementAt(2).toDelta(),
  94. Delta()..insert('don', b),
  95. );
  96. });
  97. });
  98. }