Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir-P committed Jun 12, 2024
1 parent 225f476 commit d34e9bd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/fleather/lib/src/widgets/autoformats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class _MarkdownInlineShortcuts extends AutoFormat {
change: change,
undo: undo,
undoPositionCandidate: position - (rule.length * 2),
selection: TextSelection.collapsed(offset: position - rule.length),
selection: TextSelection.collapsed(offset: position - (rule.length * 2) + 1),
undoSelection: TextSelection.collapsed(offset: position + 1),
);
}
Expand Down
52 changes: 52 additions & 0 deletions packages/fleather/test/widgets/autoformats_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,58 @@ void main() {
ParchmentAttribute.block.code.value);
});

test('Detects italic shortcut', () {
final document = ParchmentDocument.fromJson([
{'insert': 'Some long text\n**Test*that continues\n'}
]);
final performed = autoformats.run(document, 22, ' ');
expect(performed, isTrue);
expect(autoformats.selection, const TextSelection.collapsed(offset: 21));
final attributes = document.toDelta().toList()[1].attributes;
expect(attributes![ParchmentAttribute.italic.key], isTrue);
});

test('Detects bold shortcut', () {
final document = ParchmentDocument.fromJson([
{'insert': 'Some long text\n**Test**that continues\n'}
]);
final performed = autoformats.run(document, 23, ' ');
expect(performed, isTrue);
expect(autoformats.selection, const TextSelection.collapsed(offset: 20));
final attributes = document.toDelta().toList()[1].attributes;
expect(attributes![ParchmentAttribute.bold.key], isTrue);
});

test('Detects inline code shortcut', () {
const text = 'Some long text\n`Test`that continues\n';
final document = ParchmentDocument.fromJson([
{'insert': text}
]);
final performed = autoformats.run(document, 21, ' ');
expect(performed, isTrue);
expect(autoformats.selection, const TextSelection.collapsed(offset: 20));
final attributes = document.toDelta().toList()[1].attributes;
expect(attributes![ParchmentAttribute.inlineCode.key], isTrue);
final undoSelection = autoformats.undoActive(document);
expect(undoSelection, const TextSelection.collapsed(offset: 22));
expect(document.toDelta().first.data, text);
});

test('Detects strikethrough shortcut', () {
const text = 'Some long text\n~~Test~~that continues\n';
final document = ParchmentDocument.fromJson([
{'insert': text}
]);
final performed = autoformats.run(document, 23, '\n');
expect(performed, isTrue);
expect(autoformats.selection, const TextSelection.collapsed(offset: 20));
final attributes = document.toDelta().toList()[1].attributes;
expect(attributes![ParchmentAttribute.strikethrough.key], isTrue);
final undoSelection = autoformats.undoActive(document);
expect(undoSelection, const TextSelection.collapsed(offset: 24));
expect(document.toDelta().first.data, text);
});

test('No trigger of detection if inserting other than space', () {
final document = ParchmentDocument.fromJson([
{'insert': 'Some long text\n* \nthat continues\n'}
Expand Down

0 comments on commit d34e9bd

Please sign in to comment.