From b5bdbdf6cb41bfe7fb6916fb661b3394b0cb47c0 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Sat, 16 Jan 2021 13:13:13 +0100
Subject: [PATCH] Add action_context tests
---
include/tags/context.hpp | 1 +
tests/CMakeLists.txt | 1 +
tests/unit_tests/tags/action_context.cpp | 137 +++++++++++++++++++++++
tests/unit_tests/tags/dispatch.cpp | 12 +-
4 files changed, 145 insertions(+), 6 deletions(-)
create mode 100644 tests/unit_tests/tags/action_context.cpp
diff --git a/include/tags/context.hpp b/include/tags/context.hpp
index 4c292874..d202e950 100644
--- a/include/tags/context.hpp
+++ b/include/tags/context.hpp
@@ -136,6 +136,7 @@ namespace tags {
size_t num_actions() const;
+ // TODO provide better interface for adjusting the start positions of actions
std::vector& get_blocks();
protected:
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index d0271077..464b29c8 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -64,6 +64,7 @@ add_unit_test(drawtypes/ramp)
add_unit_test(drawtypes/iconset)
add_unit_test(tags/parser)
add_unit_test(tags/dispatch)
+add_unit_test(tags/action_context)
# Run make check to build and run all unit tests
add_custom_target(check
diff --git a/tests/unit_tests/tags/action_context.cpp b/tests/unit_tests/tags/action_context.cpp
new file mode 100644
index 00000000..8f61c27f
--- /dev/null
+++ b/tests/unit_tests/tags/action_context.cpp
@@ -0,0 +1,137 @@
+#include "tags/context.hpp"
+
+#include "common/test.hpp"
+#include "components/logger.hpp"
+
+using namespace polybar;
+using namespace std;
+using namespace tags;
+
+TEST(ActionCtxtTest, dblClick) {
+ action_context ctxt;
+
+ ctxt.action_open(mousebtn::DOUBLE_LEFT, "", alignment::LEFT);
+ ctxt.action_close(mousebtn::DOUBLE_LEFT, alignment::LEFT);
+
+ ASSERT_TRUE(ctxt.has_double_click());
+
+ ctxt.reset();
+
+ ctxt.action_open(mousebtn::DOUBLE_MIDDLE, "", alignment::LEFT);
+ ctxt.action_close(mousebtn::DOUBLE_MIDDLE, alignment::LEFT);
+
+ ASSERT_TRUE(ctxt.has_double_click());
+
+ ctxt.action_open(mousebtn::DOUBLE_RIGHT, "", alignment::LEFT);
+ ctxt.action_close(mousebtn::DOUBLE_RIGHT, alignment::LEFT);
+
+ ASSERT_TRUE(ctxt.has_double_click());
+}
+
+TEST(ActionCtxtTest, closing) {
+ action_context ctxt;
+
+ auto id1 = ctxt.action_open(mousebtn::LEFT, "", alignment::LEFT);
+ auto id2 = ctxt.action_open(mousebtn::RIGHT, "", alignment::CENTER);
+ auto id3 = ctxt.action_open(mousebtn::RIGHT, "", alignment::LEFT);
+ auto id4 = ctxt.action_open(mousebtn::MIDDLE, "", alignment::LEFT);
+
+ EXPECT_NE(NO_ACTION, id1);
+ EXPECT_NE(NO_ACTION, id2);
+ EXPECT_NE(NO_ACTION, id3);
+ EXPECT_NE(NO_ACTION, id4);
+
+ EXPECT_EQ(make_pair(id1, mousebtn::LEFT), ctxt.action_close(mousebtn::LEFT, alignment::LEFT));
+ EXPECT_EQ(make_pair(id4, mousebtn::MIDDLE), ctxt.action_close(mousebtn::NONE, alignment::LEFT));
+ EXPECT_EQ(make_pair(id3, mousebtn::RIGHT), ctxt.action_close(mousebtn::NONE, alignment::LEFT));
+ EXPECT_EQ(make_pair(id2, mousebtn::RIGHT), ctxt.action_close(mousebtn::NONE, alignment::CENTER));
+
+ EXPECT_EQ(4, ctxt.num_actions());
+}
+
+TEST(ActionCtxtTest, overlapping) {
+ action_context ctxt;
+
+ /*
+ * clang-format off
+ *
+ * Sets up the following overlapping of actions:
+ * 0123456
+ * 1: [--) (LEFT)
+ * 2: [----) (MIDDLE)
+ * 3: [--) (RIGHT)
+ * clang-format on
+ */
+
+ auto id1 = ctxt.action_open(mousebtn::LEFT, "", alignment::LEFT);
+ auto id2 = ctxt.action_open(mousebtn::MIDDLE, "", alignment::LEFT);
+ auto id3 = ctxt.action_open(mousebtn::RIGHT, "", alignment::LEFT);
+ EXPECT_EQ(make_pair(id1, mousebtn::LEFT), ctxt.action_close(mousebtn::LEFT, alignment::LEFT));
+ EXPECT_EQ(make_pair(id3, mousebtn::RIGHT), ctxt.action_close(mousebtn::RIGHT, alignment::LEFT));
+ EXPECT_EQ(make_pair(id2, mousebtn::MIDDLE), ctxt.action_close(mousebtn::MIDDLE, alignment::LEFT));
+
+ ctxt.set_start(id1, 0);
+ ctxt.set_end(id1, 3);
+ ctxt.set_start(id2, 1);
+ ctxt.set_end(id2, 6);
+ ctxt.set_start(id3, 2);
+ ctxt.set_end(id3, 5);
+
+ auto actions = ctxt.get_actions(2);
+
+ EXPECT_EQ(id1, actions[mousebtn::LEFT]);
+ EXPECT_EQ(id2, actions[mousebtn::MIDDLE]);
+ EXPECT_EQ(id3, actions[mousebtn::RIGHT]);
+
+ EXPECT_EQ(3, ctxt.num_actions());
+}
+
+TEST(ActionCtxtTest, stacking) {
+ action_context ctxt;
+
+ /*
+ * clang-format off
+ *
+ * Sets up the following stacked actions:
+ * 012345678
+ * 1: [-------)
+ * 2: [-----)
+ * 3: [--)
+ * clang-format on
+ */
+
+ auto id1 = ctxt.action_open(mousebtn::LEFT, "", alignment::LEFT);
+ auto id2 = ctxt.action_open(mousebtn::LEFT, "", alignment::LEFT);
+ auto id3 = ctxt.action_open(mousebtn::LEFT, "", alignment::LEFT);
+ EXPECT_EQ(make_pair(id3, mousebtn::LEFT), ctxt.action_close(mousebtn::NONE, alignment::LEFT));
+ EXPECT_EQ(make_pair(id2, mousebtn::LEFT), ctxt.action_close(mousebtn::NONE, alignment::LEFT));
+ EXPECT_EQ(make_pair(id1, mousebtn::LEFT), ctxt.action_close(mousebtn::NONE, alignment::LEFT));
+
+ ctxt.set_start(id1, 0);
+ ctxt.set_end(id1, 8);
+ ctxt.set_start(id2, 1);
+ ctxt.set_end(id2, 7);
+ ctxt.set_start(id3, 3);
+ ctxt.set_end(id3, 6);
+
+ EXPECT_EQ(id1, ctxt.has_action(mousebtn::LEFT, 0));
+ EXPECT_EQ(id2, ctxt.has_action(mousebtn::LEFT, 1));
+ EXPECT_EQ(id2, ctxt.has_action(mousebtn::LEFT, 2));
+ EXPECT_EQ(id3, ctxt.has_action(mousebtn::LEFT, 3));
+ EXPECT_EQ(id3, ctxt.has_action(mousebtn::LEFT, 4));
+ EXPECT_EQ(id3, ctxt.has_action(mousebtn::LEFT, 5));
+ EXPECT_EQ(id2, ctxt.has_action(mousebtn::LEFT, 6));
+ EXPECT_EQ(id1, ctxt.has_action(mousebtn::LEFT, 7));
+
+ EXPECT_EQ(3, ctxt.num_actions());
+}
+
+TEST(ActionCtxtTest, cmd) {
+ action_context ctxt;
+
+ string cmd = "foobar";
+
+ auto id = ctxt.action_open(mousebtn::DOUBLE_RIGHT, cmd.substr(), alignment::RIGHT);
+
+ ASSERT_EQ(cmd, ctxt.get_action(id));
+}
diff --git a/tests/unit_tests/tags/dispatch.cpp b/tests/unit_tests/tags/dispatch.cpp
index 3c322206..a3c5c35f 100644
--- a/tests/unit_tests/tags/dispatch.cpp
+++ b/tests/unit_tests/tags/dispatch.cpp
@@ -24,15 +24,15 @@ class MockRenderer : public renderer_interface {
class TestableDispatch : public dispatch {};
-class Dispatch : public ::testing::Test {
+class DispatchTest : public ::testing::Test {
protected:
- unique_ptr action_ctxt = make_unique();
+ unique_ptr m_action_ctxt = make_unique();
- unique_ptr parser = make_unique(logger(loglevel::NONE), *action_ctxt);
+ unique_ptr m_dispatch = make_unique(logger(loglevel::NONE), *m_action_ctxt);
};
-TEST_F(Dispatch, ignoreFormatting) {
- MockRenderer r(*action_ctxt);
+TEST_F(DispatchTest, ignoreFormatting) {
+ MockRenderer r(*m_action_ctxt);
{
InSequence seq;
@@ -43,5 +43,5 @@ TEST_F(Dispatch, ignoreFormatting) {
bar_settings settings;
- parser->parse(settings, r, "%{O10}abc%{F-}foo");
+ m_dispatch->parse(settings, r, "%{O10}abc%{F-}foo");
}