From 8d2c04821ff751fd6971154fe062392ae5116c33 Mon Sep 17 00:00:00 2001 From: Peter Elliott Date: Sun, 29 Aug 2021 13:19:12 -0700 Subject: [PATCH] Tests: Test LibMarkdown against commonmark test suite TestCommonmark runs the CommonMark test suite (https://spec.commonmark.org/0.30/spec.json) against LibMarkdown. Currently 44/652 tests pass. --- Base/home/anon/.config/Tests.ini | 2 +- Meta/CMake/commonmark_spec.cmake | 9 ++++++ Meta/Lagom/CMakeLists.txt | 8 +++++ Tests/CMakeLists.txt | 1 + Tests/LibMarkdown/CMakeLists.txt | 6 ++++ Tests/LibMarkdown/TestCommonmark.cpp | 46 ++++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 Meta/CMake/commonmark_spec.cmake create mode 100644 Tests/LibMarkdown/CMakeLists.txt create mode 100644 Tests/LibMarkdown/TestCommonmark.cpp diff --git a/Base/home/anon/.config/Tests.ini b/Base/home/anon/.config/Tests.ini index 6483b81641..54cf3dd08a 100644 --- a/Base/home/anon/.config/Tests.ini +++ b/Base/home/anon/.config/Tests.ini @@ -1,7 +1,7 @@ [Global] SkipDirectories=Kernel/Legacy SkipRegex=^ue-.*$ -SkipTests=test-web +SkipTests=test-web TestCommonmark NotTestsPattern=^.*(txt|frm|inc)$ [test-js] diff --git a/Meta/CMake/commonmark_spec.cmake b/Meta/CMake/commonmark_spec.cmake new file mode 100644 index 0000000000..b0c8965998 --- /dev/null +++ b/Meta/CMake/commonmark_spec.cmake @@ -0,0 +1,9 @@ +option(ENABLE_COMMONMARK_SPEC_DOWNLOAD "Enable download of commonmark test suite at build time" ON) + +set(MARKDOWN_TEST_PATH ${CMAKE_BINARY_DIR}/commonmark.spec.json) +set(MARKDOWN_TEST_URL https://spec.commonmark.org/0.30/spec.json) + +if(ENABLE_COMMONMARK_SPEC_DOWNLOAD) + file(DOWNLOAD ${MARKDOWN_TEST_URL} ${MARKDOWN_TEST_PATH}) + install(FILES ${MARKDOWN_TEST_PATH} DESTINATION home/anon) +endif() diff --git a/Meta/Lagom/CMakeLists.txt b/Meta/Lagom/CMakeLists.txt index eb97b68ea2..39cbdd17f9 100644 --- a/Meta/Lagom/CMakeLists.txt +++ b/Meta/Lagom/CMakeLists.txt @@ -518,6 +518,14 @@ if (BUILD_LAGOM) ) set_tests_properties(JS PROPERTIES ENVIRONMENT SERENITY_SOURCE_DIR=${SERENITY_PROJECT_ROOT}) + # Markdown + include(${SERENITY_PROJECT_ROOT}/Meta/CMake/commonmark_spec.cmake) + file(GLOB LIBMARKDOWN_TEST_SOURCES CONFIGURE_DEPENDS "../../Tests/LibMarkdown/*.cpp") + foreach(source ${LIBMARKDOWN_TEST_SOURCES}) + lagom_test(${source} LIBS LagomMarkdown) + endforeach() + set_tests_properties(TestCommonmark PROPERTIES DISABLED YES) + # test-wasm add_executable(test-wasm_lagom ../../Tests/LibWasm/test-wasm.cpp diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index e22f1b8e6f..6d6f35eb65 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -9,6 +9,7 @@ add_subdirectory(LibGfx) add_subdirectory(LibIMAP) add_subdirectory(LibJS) add_subdirectory(LibM) +add_subdirectory(LibMarkdown) add_subdirectory(LibPthread) add_subdirectory(LibRegex) add_subdirectory(LibSQL) diff --git a/Tests/LibMarkdown/CMakeLists.txt b/Tests/LibMarkdown/CMakeLists.txt new file mode 100644 index 0000000000..a9c5d179a1 --- /dev/null +++ b/Tests/LibMarkdown/CMakeLists.txt @@ -0,0 +1,6 @@ +include(${SERENITY_PROJECT_ROOT}/Meta/CMake/commonmark_spec.cmake) +file(GLOB TEST_SOURCES CONFIGURE_DEPENDS "*.cpp") + +foreach(source ${TEST_SOURCES}) + serenity_test(${source} LibMarkdown LIBS LibMarkdown) +endforeach() diff --git a/Tests/LibMarkdown/TestCommonmark.cpp b/Tests/LibMarkdown/TestCommonmark.cpp new file mode 100644 index 0000000000..764d206672 --- /dev/null +++ b/Tests/LibMarkdown/TestCommonmark.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2021, Peter Elliott + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +TEST_SETUP +{ + auto file = Core::File::construct("/home/anon/commonmark.spec.json"); + if (!file->open(Core::OpenMode::ReadOnly)) { + file = Core::File::construct("./commonmark.spec.json"); + VERIFY(file->open(Core::OpenMode::ReadOnly)); + } + + String test_data(file->read_all(), AK::ShouldChomp::NoChomp); + + auto tests = JsonParser(test_data).parse().value().as_array(); + for (size_t i = 0; i < tests.size(); ++i) { + auto testcase = tests[i].as_object(); + + auto name = String::formatted("{}_ex{}_{}..{}", + testcase.get("section"), + testcase.get("example"), + testcase.get("start_line"), + testcase.get("end_line")); + + String markdown = testcase.get("markdown").as_string(); + String html = testcase.get("html").as_string(); + + Test::TestSuite::the().add_case(adopt_ref(*new Test::TestCase( + name, [markdown, html]() { + auto document = Markdown::Document::parse(markdown); + EXPECT_EQ(document->render_to_inline_html(), html); + }, + false))); + } +}