From bc3a9b66a19141258a60fed00eb60e9428952bb9 Mon Sep 17 00:00:00 2001 From: Jonatan Heyman Date: Sun, 7 Jul 2024 22:01:19 +0200 Subject: [PATCH] Add test that ensures that getBlocksFromSyntaxTree() and getBlocksFromString() functions produces the same results --- src/editor/block/block.js | 6 +++--- tests/block-parsing.spec.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 tests/block-parsing.spec.js diff --git a/src/editor/block/block.js b/src/editor/block/block.js index 2afd1cb..57e2bb1 100644 --- a/src/editor/block/block.js +++ b/src/editor/block/block.js @@ -25,7 +25,7 @@ let firstBlockDelimiterSize * Return a list of blocks in the document from the syntax tree. * syntaxTreeAvailable() should have been called before this function to ensure the syntax tree is available. */ -function getBlocksFromSyntaxTree(state) { +export function getBlocksFromSyntaxTree(state) { //const timer = startTimer() const blocks = []; const tree = syntaxTree(state, state.doc.length) @@ -72,7 +72,7 @@ function getBlocksFromSyntaxTree(state) { /** * Parse blocks from document's string contents using String.indexOf() */ -function getBlocksFromString(state) { +export function getBlocksFromString(state) { //const timer = startTimer() const blocks = [] const doc = state.doc @@ -138,7 +138,7 @@ function getBlocksFromString(state) { * the blocks are parsed from the string contents of the document, which is much faster * than waiting for the tree parsing to finish. */ -function getBlocks(state) { +export function getBlocks(state) { if (syntaxTreeAvailable(state, state.doc.length)) { return getBlocksFromSyntaxTree(state) } else { diff --git a/tests/block-parsing.spec.js b/tests/block-parsing.spec.js new file mode 100644 index 0000000..ee55478 --- /dev/null +++ b/tests/block-parsing.spec.js @@ -0,0 +1,28 @@ +import { expect, test } from "@playwright/test" +import { EditorState } from "@codemirror/state" + +import { heynoteLang } from "../src/editor/lang-heynote/heynote.js" +import { getBlocksFromSyntaxTree, getBlocksFromString } from "../src/editor/block/block.js" + +test("parse blocks from both syntax tree and string contents", async ({page}) => { + const contents = ` +∞∞∞text +Text Block A +∞∞∞text-a +Text Block B +∞∞∞json-a +{ +"key": "value" +} +∞∞∞python +print("Hello, World!") +` + const state = EditorState.create({ + doc: contents, + extensions: heynoteLang(), + }) + const treeBlocks = getBlocksFromSyntaxTree(state) + const stringBlocks = getBlocksFromString(state) + + expect(treeBlocks).toEqual(stringBlocks) +})