diff --git a/src/tests/config.toml b/src/tests/config.toml new file mode 100644 index 0000000..6cf6a90 --- /dev/null +++ b/src/tests/config.toml @@ -0,0 +1,8 @@ +[general] +lang = "en" +title = "Blog" + +[contact] +author = "author" +email = "email@example.com" +signal = "signal url" diff --git a/src/tests/image.png b/src/tests/image.png new file mode 100644 index 0000000..49abd93 Binary files /dev/null and b/src/tests/image.png differ diff --git a/src/tests/no_metadata_post.md b/src/tests/no_metadata_post.md new file mode 100644 index 0000000..115a838 --- /dev/null +++ b/src/tests/no_metadata_post.md @@ -0,0 +1,2 @@ +# This markdown has no metadata. +That's bad. Don't do it. It's for tests only. diff --git a/src/tests/post.md b/src/tests/post.md new file mode 100644 index 0000000..dfeaa5f --- /dev/null +++ b/src/tests/post.md @@ -0,0 +1,41 @@ +--- +author: "joseph" +date: "2026-01-01" +title: "Example of a blog post" +tags: ["blog", "test", "lalilulelo"] +--- +# This is an example post + +In this example we will do a couple of cool markdown things. + +## This is a h2 header +*This text is written in italic!* +### Look at this! +**This text is BOLD!** + +## Hello world! +```cpp +#include + +int main() { + std::cout << "Hello world!" << std::endl; +} +``` +This is super cool! + +## Time to link some random website! +This [should be clickable](https://example.com) + +## Image test +![this should show an image](./image.png) + +## This is an ordered list +1. one +2. six +3. seven + +## This is an unordered one +- six +- nine +- six +- nine diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp new file mode 100644 index 0000000..214e5d8 --- /dev/null +++ b/src/tests/tests.cpp @@ -0,0 +1,87 @@ +#include "../config.hpp" +#include "../markdown.hpp" +#include "../utils.hpp" +#include +#include +#include +#include + +TEST_CASE("Checking config file read", "[config]") { + const std::string FILE_NAME = "tests/config.toml"; + + Config config = get_config(FILE_NAME); + + REQUIRE(config.contact.author == "author"); + REQUIRE(config.contact.email == "email@example.com"); + REQUIRE(config.contact.signal == "signal url"); + REQUIRE(config.general.lang == "en"); + REQUIRE(config.general.title == "Blog"); +} + +TEST_CASE("convert_string_to_set parses inline YAML string list") { + const std::string input = "[\"haha\", \"dupa\", \"xdd\"]"; + const std::set output = {"haha", "dupa", "xdd"}; + REQUIRE(convert_string_to_set(input) == output); +} + +TEST_CASE("get_metadata function data correctness test") { + const std::string FILE_NAME = "./tests//post.md"; + Metadata result = get_metadata(FILE_NAME).value(); + + std::string expected_author = "joseph"; + std::string expected_date = "2026-01-01"; + std::string expected_title = "Example of a blog post"; + std::set expected_tags = {"blog", "test", "lalilulelo"}; + + REQUIRE(result.author == expected_author); + REQUIRE(result.date == expected_date); + REQUIRE(result.title == expected_title); + + for (const auto &tag : expected_tags) { + REQUIRE(result.tags.count(tag)); + } +} + +TEST_CASE("get_metadata function test with a file that doesn't have metadata") { + const std::string FILE_NAME = ".tests/no_metadata_post.md"; + std::optional result = get_metadata(FILE_NAME); + REQUIRE(result == std::nullopt); +} + +TEST_CASE("get_html function parsing test") { + const std::string INPUT = "./tests/post.md"; + const std::string EXPECTED_OUTPUT = R"HTML(

This is an example post

+

In this example we will do a couple of cool markdown things.

+

This is a h2 header

+

This text is written in italic!

+

Look at this!

+

This text is BOLD!

+

Hello world!

+
#include <iostream>
+
+int main() {
+    std::cout << "Hello world!" << std::endl;
+}
+
+

This is super cool!

+

Time to link some random website!

+

This should be clickable

+

Image test

+

this should show an image

+

This is an ordered list

+
    +
  1. one
  2. +
  3. six
  4. +
  5. seven
  6. +
+

This is an unordered one

+
    +
  • six
  • +
  • nine
  • +
  • six
  • +
  • nine
  • +
+)HTML"; + + REQUIRE(get_html(INPUT) == EXPECTED_OUTPUT); +}