| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 | // Copyright (c) 2018, the Zefyr project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:test/test.dart';
import 'package:quill_delta/quill_delta.dart';
import 'package:notus/notus.dart';
final ul = NotusAttribute.ul.toJson();
final bold = NotusAttribute.bold.toJson();
void main() {
  group('$CatchAllInsertRule', () {
    final rule = new CatchAllInsertRule();
    test('applies change as-is', () {
      final doc = new Delta()..insert('Document\n');
      final actual = rule.apply(doc, 8, '!');
      final expected = new Delta()
        ..retain(8)
        ..insert('!');
      expect(actual, expected);
    });
  });
  group('$PreserveLineStyleOnSplitRule', () {
    final rule = new PreserveLineStyleOnSplitRule();
    test('skips at the beginning of a document', () {
      final doc = new Delta()..insert('One\n');
      final actual = rule.apply(doc, 0, '\n');
      expect(actual, isNull);
    });
    test('applies in a block', () {
      final doc = new Delta()
        ..insert('One and two')
        ..insert('\n', ul)
        ..insert('Three')
        ..insert('\n', ul);
      final actual = rule.apply(doc, 8, '\n');
      final expected = new Delta()
        ..retain(8)
        ..insert('\n', ul);
      expect(actual, isNotNull);
      expect(actual, expected);
    });
  });
  group('$ResetLineFormatOnNewLineRule', () {
    final rule = const ResetLineFormatOnNewLineRule();
    test('applies when line-break is inserted at the end of line', () {
      final doc = new Delta()
        ..insert('Hello world')
        ..insert('\n', NotusAttribute.h1.toJson());
      final actual = rule.apply(doc, 11, '\n');
      expect(actual, isNotNull);
      final expected = new Delta()
        ..retain(11)
        ..insert('\n', NotusAttribute.h1.toJson())
        ..retain(1, NotusAttribute.heading.unset.toJson());
      expect(actual, expected);
    });
    test('applies without style reset if not needed', () {
      final doc = new Delta()..insert('Hello world\n');
      final actual = rule.apply(doc, 11, '\n');
      expect(actual, isNotNull);
      final expected = new Delta()
        ..retain(11)
        ..insert('\n');
      expect(actual, expected);
    });
    test('applies at the beginning of a document', () {
      final doc = new Delta()..insert('\n', NotusAttribute.h1.toJson());
      final actual = rule.apply(doc, 0, '\n');
      expect(actual, isNotNull);
      final expected = new Delta()
        ..insert('\n', NotusAttribute.h1.toJson())
        ..retain(1, NotusAttribute.heading.unset.toJson());
      expect(actual, expected);
    });
    test('applies and keeps block style', () {
      final style = NotusAttribute.ul.toJson();
      style.addAll(NotusAttribute.h1.toJson());
      final doc = new Delta()..insert('Hello world')..insert('\n', style);
      final actual = rule.apply(doc, 11, '\n');
      expect(actual, isNotNull);
      final expected = new Delta()
        ..retain(11)
        ..insert('\n', style)
        ..retain(1, NotusAttribute.heading.unset.toJson());
      expect(actual, expected);
    });
    test('applies to a line in the middle of a document', () {
      final doc = new Delta()
        ..insert('Hello \nworld!\nMore lines here.')
        ..insert('\n', NotusAttribute.h2.toJson());
      final actual = rule.apply(doc, 30, '\n');
      expect(actual, isNotNull);
      final expected = new Delta()
        ..retain(30)
        ..insert('\n', NotusAttribute.h2.toJson())
        ..retain(1, NotusAttribute.heading.unset.toJson());
      expect(actual, expected);
    });
  });
  group('$AutoExitBlockRule', () {
    final rule = new AutoExitBlockRule();
    test('applies when line-break is inserted on empty line in a block', () {
      final ul = NotusAttribute.ul.toJson();
      final doc = new Delta()
        ..insert('Item 1')
        ..insert('\n', ul)
        ..insert('Item 2')
        ..insert('\n\n', ul);
      final actual = rule.apply(doc, 14, '\n');
      expect(actual, isNotNull);
      final expected = new Delta()
        ..retain(14)
        ..retain(1, NotusAttribute.block.unset.toJson());
      expect(actual, expected);
    });
    test('applies only on empty line', () {
      final ul = NotusAttribute.ul.toJson();
      final doc = new Delta()..insert('Item 1')..insert('\n', ul);
      final actual = rule.apply(doc, 6, '\n');
      expect(actual, isNull);
    });
    test('applies at the beginning of a document', () {
      final ul = NotusAttribute.ul.toJson();
      final doc = new Delta()..insert('\n', ul);
      final actual = rule.apply(doc, 0, '\n');
      expect(actual, isNotNull);
      final expected = new Delta()
        ..retain(1, NotusAttribute.block.unset.toJson());
      expect(actual, expected);
    });
    test('ignores non-empty line at the beginning of a document', () {
      final ul = NotusAttribute.ul.toJson();
      final doc = new Delta()..insert('Text\n', ul);
      final actual = rule.apply(doc, 0, '\n');
      expect(actual, isNull);
    });
  });
  group('$PreserveInlineStylesRule', () {
    final rule = new PreserveInlineStylesRule();
    test('apply', () {
      final doc = new Delta()
        ..insert('Doc with ')
        ..insert('bold', bold)
        ..insert(' text');
      final actual = rule.apply(doc, 13, 'er');
      final expected = new Delta()
        ..retain(13)
        ..insert('er', bold);
      expect(expected, actual);
    });
    test('apply at the beginning of a document', () {
      final doc = new Delta()..insert('Doc with ');
      final actual = rule.apply(doc, 0, 'A ');
      expect(actual, isNull);
    });
  });
  group('$AutoFormatLinksRule', () {
    final rule = new AutoFormatLinksRule();
    final link = NotusAttribute.link.fromString('https://example.com').toJson();
    test('apply simple', () {
      final doc = new Delta()..insert('Doc with link https://example.com');
      final actual = rule.apply(doc, 33, ' ');
      final expected = new Delta()
        ..retain(14)
        ..retain(19, link)
        ..insert(' ');
      expect(expected, actual);
    });
    test('applies only to insert of single space', () {
      final doc = new Delta()..insert('Doc with link https://example.com');
      final actual = rule.apply(doc, 33, '/');
      expect(actual, isNull);
    });
    test('applies for links at the beginning of line', () {
      final doc = new Delta()..insert('Doc with link\nhttps://example.com');
      final actual = rule.apply(doc, 33, ' ');
      final expected = new Delta()
        ..retain(14)
        ..retain(19, link)
        ..insert(' ');
      expect(expected, actual);
    });
    test('ignores if already formatted as link', () {
      final doc = new Delta()
        ..insert('Doc with link\n')
        ..insert('https://example.com', link);
      final actual = rule.apply(doc, 33, ' ');
      expect(actual, isNull);
    });
  });
  group('$PreserveBlockStyleOnPasteRule', () {
    final rule = new PreserveBlockStyleOnPasteRule();
    test('applies in a block', () {
      final doc = new Delta()
        ..insert('One and two')
        ..insert('\n', ul)
        ..insert('Three')
        ..insert('\n', ul);
      final actual = rule.apply(doc, 8, 'also \n');
      final expected = new Delta()
        ..retain(8)
        ..insert('also ')
        ..insert('\n', ul);
      expect(actual, isNotNull);
      expect(actual, expected);
    });
  });
}
 |