TestCommonmark.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) 2021, Peter Elliott <pelliott@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/JsonArray.h>
  7. #include <AK/JsonObject.h>
  8. #include <AK/JsonParser.h>
  9. #include <AK/String.h>
  10. #include <LibCore/File.h>
  11. #include <LibMarkdown/Document.h>
  12. #include <LibTest/TestCase.h>
  13. #include <LibTest/TestSuite.h>
  14. TEST_SETUP
  15. {
  16. auto file = Core::File::construct("/home/anon/commonmark.spec.json");
  17. if (!file->open(Core::OpenMode::ReadOnly)) {
  18. file = Core::File::construct("./commonmark.spec.json");
  19. VERIFY(file->open(Core::OpenMode::ReadOnly));
  20. }
  21. String test_data(file->read_all(), AK::ShouldChomp::NoChomp);
  22. auto tests = JsonParser(test_data).parse().value().as_array();
  23. for (size_t i = 0; i < tests.size(); ++i) {
  24. auto testcase = tests[i].as_object();
  25. auto name = String::formatted("{}_ex{}_{}..{}",
  26. testcase.get("section"),
  27. testcase.get("example"),
  28. testcase.get("start_line"),
  29. testcase.get("end_line"));
  30. String markdown = testcase.get("markdown").as_string();
  31. String html = testcase.get("html").as_string();
  32. Test::TestSuite::the().add_case(adopt_ref(*new Test::TestCase(
  33. name, [markdown, html]() {
  34. auto document = Markdown::Document::parse(markdown);
  35. EXPECT_EQ(document->render_to_inline_html(), html);
  36. },
  37. false)));
  38. }
  39. }