Add action_context tests

This commit is contained in:
patrick96 2021-01-16 13:13:13 +01:00 committed by Patrick Ziegler
parent d89096c191
commit b5bdbdf6cb
4 changed files with 145 additions and 6 deletions

View File

@ -136,6 +136,7 @@ namespace tags {
size_t num_actions() const;
// TODO provide better interface for adjusting the start positions of actions
std::vector<action_block>& get_blocks();
protected:

View File

@ -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

View File

@ -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));
}

View File

@ -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_context> action_ctxt = make_unique<action_context>();
unique_ptr<action_context> m_action_ctxt = make_unique<action_context>();
unique_ptr<dispatch> parser = make_unique<dispatch>(logger(loglevel::NONE), *action_ctxt);
unique_ptr<dispatch> m_dispatch = make_unique<dispatch>(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");
}