From 2e7f803e02942b414ddfef206c1e5088508ef475 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Tue, 8 Nov 2022 15:44:40 +0800 Subject: [PATCH] test: markdown encoder test --- .../delta_markdown_encoder_test.dart | 0 .../document_markdown_encoder_test.dart | 137 ++++++++++++++++++ .../parser/image_node_parser_test.dart | 17 +++ .../encoder/parser/text_node_parser_test.dart | 95 ++++++++++++ 4 files changed, 249 insertions(+) rename frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/{ => encoder}/delta_markdown_encoder_test.dart (100%) create mode 100644 frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/document_markdown_encoder_test.dart create mode 100644 frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/parser/image_node_parser_test.dart create mode 100644 frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/parser/text_node_parser_test.dart diff --git a/frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/delta_markdown_encoder_test.dart b/frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/delta_markdown_encoder_test.dart similarity index 100% rename from frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/delta_markdown_encoder_test.dart rename to frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/delta_markdown_encoder_test.dart diff --git a/frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/document_markdown_encoder_test.dart b/frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/document_markdown_encoder_test.dart new file mode 100644 index 0000000000..a81ff10a62 --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/document_markdown_encoder_test.dart @@ -0,0 +1,137 @@ +import 'dart:convert'; + +import 'package:appflowy_editor/appflowy_editor.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() async { + group('document_markdown_encoder.dart', () { + const example = ''' +{ + "document": { + "type": "editor", + "children": [ + { + "type": "text", + "attributes": { + "subtype": "heading", + "heading": "h2" + }, + "delta": [ + { "insert": "👋 " }, + { "insert": "Welcome to", "attributes": { "bold": true } }, + { "insert": " " }, + { + "insert": "AppFlowy Editor", + "attributes": { + "href": "appflowy.io", + "italic": true, + "bold": true + } + } + ] + }, + { "type": "text", "delta": [] }, + { + "type": "text", + "delta": [ + { "insert": "AppFlowy Editor is a " }, + { "insert": "highly customizable", "attributes": { "bold": true } }, + { "insert": " " }, + { "insert": "rich-text editor", "attributes": { "italic": true } }, + { "insert": " for " }, + { "insert": "Flutter", "attributes": { "underline": true } } + ] + }, + { + "type": "text", + "attributes": { "checkbox": true, "subtype": "checkbox" }, + "delta": [{ "insert": "Customizable" }] + }, + { + "type": "text", + "attributes": { "checkbox": true, "subtype": "checkbox" }, + "delta": [{ "insert": "Test-covered" }] + }, + { + "type": "text", + "attributes": { "checkbox": false, "subtype": "checkbox" }, + "delta": [{ "insert": "more to come!" }] + }, + { "type": "text", "delta": [] }, + { + "type": "text", + "attributes": { "subtype": "quote" }, + "delta": [{ "insert": "Here is an example you can give a try" }] + }, + { "type": "text", "delta": [] }, + { + "type": "text", + "delta": [ + { "insert": "You can also use " }, + { + "insert": "AppFlowy Editor", + "attributes": { + "italic": true, + "bold": true, + "backgroundColor": "0x6000BCF0" + } + }, + { "insert": " as a component to build your own app." } + ] + }, + { "type": "text", "delta": [] }, + { + "type": "text", + "attributes": { "subtype": "bulleted-list" }, + "delta": [{ "insert": "Use / to insert blocks" }] + }, + { + "type": "text", + "attributes": { "subtype": "bulleted-list" }, + "delta": [ + { + "insert": "Select text to trigger to the toolbar to format your notes." + } + ] + }, + { "type": "text", "delta": [] }, + { + "type": "text", + "delta": [ + { + "insert": "If you have questions or feedback, please submit an issue on Github or join the community along with 1000+ builders!" + } + ] + } + ] + } +} +'''; + setUpAll(() { + TestWidgetsFlutterBinding.ensureInitialized(); + }); + + test('parser document', () async { + final data = Map.from(json.decode(example)); + final document = Document.fromJson(data); + final result = DocumentMarkdownEncoder().convert(document); + expect(result, ''' +## 👋 **Welcome to** ***[AppFlowy Editor](appflowy.io)*** + +AppFlowy Editor is a **highly customizable** _rich-text editor_ for Flutter +- [x] Customizable +- [x] Test-covered +- [ ] more to come! + +> Here is an example you can give a try + +You can also use ***AppFlowy Editor*** as a component to build your own app. + +* Use / to insert blocks +* Select text to trigger to the toolbar to format your notes. + +If you have questions or feedback, please submit an issue on Github or join the community along with 1000+ builders! +'''); + }); + }); +} diff --git a/frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/parser/image_node_parser_test.dart b/frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/parser/image_node_parser_test.dart new file mode 100644 index 0000000000..77102c8310 --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/parser/image_node_parser_test.dart @@ -0,0 +1,17 @@ +import 'package:appflowy_editor/appflowy_editor.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() async { + group('image_node_parser.dart', () { + test('parser image node', () { + final node = Node( + type: 'image', + attributes: { + 'image_src': 'https://appflowy.io', + }, + ); + final result = const ImageNodeParser().transform(node); + expect(result, '![](https://appflowy.io)'); + }); + }); +} diff --git a/frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/parser/text_node_parser_test.dart b/frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/parser/text_node_parser_test.dart new file mode 100644 index 0000000000..0d7c540a2b --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/parser/text_node_parser_test.dart @@ -0,0 +1,95 @@ +import 'package:appflowy_editor/appflowy_editor.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() async { + group('text_node_parser.dart', () { + const text = 'Welcome to AppFlowy'; + + test('heading style', () { + final h1 = TextNode( + delta: Delta(operations: [TextInsert(text)]), + attributes: { + BuiltInAttributeKey.subtype: BuiltInAttributeKey.heading, + BuiltInAttributeKey.heading: BuiltInAttributeKey.h1, + }, + ); + final h2 = TextNode( + delta: Delta(operations: [TextInsert(text)]), + attributes: { + BuiltInAttributeKey.subtype: BuiltInAttributeKey.heading, + BuiltInAttributeKey.heading: BuiltInAttributeKey.h2, + }, + ); + final h3 = TextNode( + delta: Delta(operations: [TextInsert(text)]), + attributes: { + BuiltInAttributeKey.subtype: BuiltInAttributeKey.heading, + BuiltInAttributeKey.heading: BuiltInAttributeKey.h3, + }, + ); + expect(const TextNodeParser().transform(h1), '# $text'); + expect(const TextNodeParser().transform(h2), '## $text'); + expect(const TextNodeParser().transform(h3), '### $text'); + }); + + test('bulleted list style', () { + final node = TextNode( + delta: Delta(operations: [TextInsert(text)]), + attributes: { + BuiltInAttributeKey.subtype: BuiltInAttributeKey.bulletedList, + }, + ); + expect(const TextNodeParser().transform(node), '* $text'); + }); + + test('number list style', () { + final node = TextNode( + delta: Delta(operations: [TextInsert(text)]), + attributes: { + BuiltInAttributeKey.subtype: BuiltInAttributeKey.numberList, + BuiltInAttributeKey.number: 1, + }, + ); + expect(const TextNodeParser().transform(node), '1. $text'); + }); + + test('checkbox style', () { + final checkbox = TextNode( + delta: Delta(operations: [TextInsert(text)]), + attributes: { + BuiltInAttributeKey.subtype: BuiltInAttributeKey.checkbox, + BuiltInAttributeKey.checkbox: true, + }, + ); + final unCheckbox = TextNode( + delta: Delta(operations: [TextInsert(text)]), + attributes: { + BuiltInAttributeKey.subtype: BuiltInAttributeKey.checkbox, + BuiltInAttributeKey.checkbox: false, + }, + ); + expect(const TextNodeParser().transform(checkbox), '- [x] $text'); + expect(const TextNodeParser().transform(unCheckbox), '- [ ] $text'); + }); + + test('quote style', () { + final node = TextNode( + delta: Delta(operations: [TextInsert(text)]), + attributes: { + BuiltInAttributeKey.subtype: BuiltInAttributeKey.quote, + }, + ); + expect(const TextNodeParser().transform(node), '> $text'); + }); + + test('code block style', () { + final node = TextNode( + delta: Delta(operations: [TextInsert(text)]), + attributes: { + BuiltInAttributeKey.subtype: 'code-block', + }, + ); + expect(const TextNodeParser().transform(node), '```\n$text\n```'); + }); + }); +}