#include "draw.h" #include "md-parser.h" #include "mock-middleware.h" #include "gmock/gmock.h" #include "gtest/gtest.h" #include #include namespace { class DrawFixture : public testing::Test { protected: void SetUp() override { Gtk::Application::create(); } }; TEST_F(DrawFixture, TestDrawSetText) { // Given std::string text = "Hello world!"; MockMiddleware middleware; Draw draw(middleware); // When draw.set_text(text); // Then ASSERT_EQ(draw.get_text(), text); } TEST_F(DrawFixture, TestDrawDocument) { // Given std::string markdown = "**Hello** *world*"; cmark_node* doc = Parser::parse_content(markdown); MockMiddleware middleware; Draw draw(middleware); // When draw.set_document(doc); // Then std::string result = draw.get_text(); ASSERT_EQ(result, "Hello world\n\n"); } TEST_F(DrawFixture, TestDrawTextTags) { // Given std::string italic_tag_name = "italic"; std::string bold_tag_name = "bold"; std::string heading1_tag_name = "heading1"; std::string heading2_tag_name = "heading2"; std::string heading3_tag_name = "heading3"; std::string heading4_tag_name = "heading4"; std::string heading5_tag_name = "heading5"; std::string heading6_tag_name = "heading6"; std::string strikethrough_tag_name = "strikethrough"; std::string superscript_tag_name = "superscript"; std::string subscript_tag_name = "subscript"; std::string code_tag_name = "code"; std::string quote_tag_name = "quote"; std::string highlight_tag_name = "highlight"; MockMiddleware middleware; Draw draw(middleware); // When auto tagTable = draw.get_buffer()->get_tag_table(); auto propery_italic = tagTable->lookup(italic_tag_name); auto propery_bold = tagTable->lookup(bold_tag_name); auto propery_heading1 = tagTable->lookup(heading1_tag_name); auto propery_heading2 = tagTable->lookup(heading2_tag_name); auto propery_heading3 = tagTable->lookup(heading3_tag_name); auto propery_heading4 = tagTable->lookup(heading4_tag_name); auto propery_heading5 = tagTable->lookup(heading5_tag_name); auto propery_heading6 = tagTable->lookup(heading6_tag_name); auto propery_strikethrough = tagTable->lookup(strikethrough_tag_name); auto propery_superscript = tagTable->lookup(superscript_tag_name); auto propery_subscript = tagTable->lookup(subscript_tag_name); auto propery_code = tagTable->lookup(code_tag_name); auto propery_quote = tagTable->lookup(quote_tag_name); auto propery_highlight = tagTable->lookup(highlight_tag_name); // Then ASSERT_NE(propery_italic.get(), nullptr); ASSERT_EQ(propery_italic->property_name().get_value(), italic_tag_name); ASSERT_NE(propery_bold.get(), nullptr); ASSERT_EQ(propery_bold->property_name().get_value(), bold_tag_name); ASSERT_NE(propery_heading1.get(), nullptr); ASSERT_EQ(propery_heading1->property_name().get_value(), heading1_tag_name); ASSERT_NE(propery_heading2.get(), nullptr); ASSERT_EQ(propery_heading2->property_name().get_value(), heading2_tag_name); ASSERT_NE(propery_heading3.get(), nullptr); ASSERT_EQ(propery_heading3->property_name().get_value(), heading3_tag_name); ASSERT_NE(propery_heading4.get(), nullptr); ASSERT_EQ(propery_heading4->property_name().get_value(), heading4_tag_name); ASSERT_NE(propery_heading5.get(), nullptr); ASSERT_EQ(propery_heading5->property_name().get_value(), heading5_tag_name); ASSERT_NE(propery_heading6.get(), nullptr); ASSERT_EQ(propery_heading6->property_name().get_value(), heading6_tag_name); ASSERT_NE(propery_strikethrough.get(), nullptr); ASSERT_EQ(propery_strikethrough->property_name().get_value(), strikethrough_tag_name); ASSERT_NE(propery_superscript.get(), nullptr); ASSERT_EQ(propery_superscript->property_name().get_value(), superscript_tag_name); ASSERT_NE(propery_subscript.get(), nullptr); ASSERT_EQ(propery_subscript->property_name().get_value(), subscript_tag_name); ASSERT_NE(propery_code.get(), nullptr); ASSERT_EQ(propery_code->property_name().get_value(), code_tag_name); ASSERT_NE(propery_quote.get(), nullptr); ASSERT_EQ(propery_quote->property_name().get_value(), quote_tag_name); ASSERT_NE(propery_highlight.get(), nullptr); ASSERT_EQ(propery_highlight->property_name().get_value(), highlight_tag_name); } TEST_F(DrawFixture, TestDrawTextAttributes) { // Given std::string markdown = "**bold**~~strikethrough~~^up^%down%`code`"; gsize length = 0; cmark_node* doc = Parser::parse_content(markdown); MockMiddleware middleware; Draw draw(middleware); // When draw.set_document(doc); auto buffer = draw.get_buffer(); // Using the built-in formatter guint8* data = buffer->serialize(buffer, "application/x-gtk-text-buffer-rich-text", buffer->begin(), buffer->end(), length); // Convert data to string std::string string_data(data, data + length); // Then // Bold attribute std::string expect_attr1 = ""; // Strikethrough attribute std::string expect_attr2 = ""; // Super-/subcript attribute std::string expect_attr3 = ""; // Subscript attributes std::string expect_attr5 = ""; // Code attributes std::string expect_attr6 = ""; std::string expect_attr7 = ""; std::string expect_attr8 = ""; EXPECT_THAT(string_data, testing::HasSubstr(expect_attr1)); EXPECT_THAT(string_data, testing::HasSubstr(expect_attr2)); EXPECT_THAT(string_data, testing::HasSubstr(expect_attr3)); EXPECT_THAT(string_data, testing::HasSubstr(expect_attr4)); EXPECT_THAT(string_data, testing::HasSubstr(expect_attr5)); EXPECT_THAT(string_data, testing::HasSubstr(expect_attr6)); EXPECT_THAT(string_data, testing::HasSubstr(expect_attr7)); EXPECT_THAT(string_data, testing::HasSubstr(expect_attr8)); } } // namespace