test: add unit test for backquote to code

This commit is contained in:
He Linming 2022-09-26 10:40:45 +08:00
parent 39bbfe109d
commit 9604bc06bb
No known key found for this signature in database
GPG Key ID: CCD4C92A1475582F

View File

@ -0,0 +1,154 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/extensions/text_node_extensions.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import '../../infra/test_editor.dart';
void main() async {
setUpAll(() {
TestWidgetsFlutterBinding.ensureInitialized();
});
group('markdown_syntax_to_styled_text.dart', () {
group('convert single backquote to code', () {
Future<void> insertBackquote(
EditorWidgetTester editor, {
int repeat = 1,
}) async {
for (var i = 0; i < repeat; i++) {
await editor.pressLogicKey(
LogicalKeyboardKey.backquote,
);
}
}
testWidgets('`AppFlowy` to code AppFlowy', (tester) async {
const text = '`AppFlowy';
final editor = tester.editor..insertTextNode('');
await editor.startTesting();
await editor.updateSelection(
Selection.single(path: [0], startOffset: 0),
);
final textNode = editor.nodeAtPath([0]) as TextNode;
for (var i = 0; i < text.length; i++) {
await editor.insertText(textNode, text[i], i);
}
await insertBackquote(editor);
final allCode = textNode.allSatisfyCodeInSelection(
Selection.single(
path: [0],
startOffset: 0,
endOffset: textNode.toRawString().length,
),
);
expect(allCode, true);
expect(textNode.toRawString(), 'AppFlowy');
});
testWidgets('App`Flowy` to code AppFlowy', (tester) async {
const text = 'App`Flowy';
final editor = tester.editor..insertTextNode('');
await editor.startTesting();
await editor.updateSelection(
Selection.single(path: [0], startOffset: 0),
);
final textNode = editor.nodeAtPath([0]) as TextNode;
for (var i = 0; i < text.length; i++) {
await editor.insertText(textNode, text[i], i);
}
await insertBackquote(editor);
final allCode = textNode.allSatisfyCodeInSelection(
Selection.single(
path: [0],
startOffset: 3,
endOffset: textNode.toRawString().length,
),
);
expect(allCode, true);
expect(textNode.toRawString(), 'AppFlowy');
});
testWidgets('`` nothing changes', (tester) async {
const text = '`';
final editor = tester.editor..insertTextNode('');
await editor.startTesting();
await editor.updateSelection(
Selection.single(path: [0], startOffset: 0),
);
final textNode = editor.nodeAtPath([0]) as TextNode;
for (var i = 0; i < text.length; i++) {
await editor.insertText(textNode, text[i], i);
}
await insertBackquote(editor);
final allCode = textNode.allSatisfyCodeInSelection(
Selection.single(
path: [0],
startOffset: 0,
endOffset: textNode.toRawString().length,
),
);
expect(allCode, false);
expect(textNode.toRawString(), text);
});
});
group('convert double backquote to code', () {
Future<void> insertBackquote(
EditorWidgetTester editor, {
int repeat = 1,
}) async {
for (var i = 0; i < repeat; i++) {
await editor.pressLogicKey(
LogicalKeyboardKey.backquote,
);
}
}
testWidgets('```AppFlowy`` to code `AppFlowy', (tester) async {
const text = '```AppFlowy`';
final editor = tester.editor..insertTextNode('');
await editor.startTesting();
await editor.updateSelection(
Selection.single(path: [0], startOffset: 0),
);
final textNode = editor.nodeAtPath([0]) as TextNode;
for (var i = 0; i < text.length; i++) {
await editor.insertText(textNode, text[i], i);
}
await insertBackquote(editor);
final allCode = textNode.allSatisfyCodeInSelection(
Selection.single(
path: [0],
startOffset: 1,
endOffset: textNode.toRawString().length,
),
);
expect(allCode, true);
expect(textNode.toRawString(), '`AppFlowy');
});
testWidgets('```` nothing changes', (tester) async {
const text = '```';
final editor = tester.editor..insertTextNode('');
await editor.startTesting();
await editor.updateSelection(
Selection.single(path: [0], startOffset: 0),
);
final textNode = editor.nodeAtPath([0]) as TextNode;
for (var i = 0; i < text.length; i++) {
await editor.insertText(textNode, text[i], i);
}
await insertBackquote(editor);
final allCode = textNode.allSatisfyCodeInSelection(
Selection.single(
path: [0],
startOffset: 0,
endOffset: textNode.toRawString().length,
),
);
expect(allCode, false);
expect(textNode.toRawString(), text);
});
});
});
}