zefyr

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