From 38f551f8849f4a5712c8d8e1014c846d288fe8e6 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Wed, 10 Oct 2018 17:03:51 +0200
Subject: [PATCH] test(parser): Add tests for parse_action_cmd
---
src/components/parser.cpp | 5 ++++
tests/CMakeLists.txt | 9 +++++++
tests/unit_tests/components/parser.cpp | 37 ++++++++++++++++++++++++++
3 files changed, 51 insertions(+)
create mode 100644 tests/unit_tests/components/parser.cpp
diff --git a/src/components/parser.cpp b/src/components/parser.cpp
index c5231928..59561c7d 100644
--- a/src/components/parser.cpp
+++ b/src/components/parser.cpp
@@ -232,6 +232,11 @@ mousebtn parser::parse_action_btn(const string& data) {
/**
* Process action command string
+ *
+ * data is the action cmd surrounded by unescaped colons followed by an
+ * arbitrary string
+ *
+ * Returns everything inside the unescaped colons as is
*/
string parser::parse_action_cmd(string&& data) {
if (data[0] != ':') {
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index b3efec93..e06269f9 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -100,5 +100,14 @@ unit_test(components/builder unit_tests
SOURCES
${files})
+unit_test(components/parser unit_tests
+ SOURCES
+ components/parser.cpp
+ utils/factory.cpp
+ utils/string.cpp
+ events/signal_emitter.cpp
+ events/signal_receiver.cpp
+ )
+
# Compile all unit tests with 'make all_unit_tests'
add_custom_target("all_unit_tests" DEPENDS ${unit_tests})
diff --git a/tests/unit_tests/components/parser.cpp b/tests/unit_tests/components/parser.cpp
new file mode 100644
index 00000000..76a669e3
--- /dev/null
+++ b/tests/unit_tests/components/parser.cpp
@@ -0,0 +1,37 @@
+#include "common/test.hpp"
+#include "events/signal_emitter.hpp"
+#include "components/parser.hpp"
+
+using namespace polybar;
+
+class TestableParser : public parser {
+ using parser::parser;
+ public: using parser::parse_action_cmd;
+};
+
+class Parser : public ::testing::Test {
+ protected:
+ TestableParser m_parser{signal_emitter::make()};
+};
+/**
+ * The first element of the pair is the expected return text, the second element
+ * is the input to parse_action_cmd
+ */
+class ParseActionCmd :
+ public Parser,
+ public ::testing::WithParamInterface> {};
+
+vector> parse_action_cmd_list = {
+ {"abc", ":abc:\\abc"},
+ {"abc\\:", ":abc\\::\\abc"},
+ {"\\:\\:\\:", ":\\:\\:\\::\\abc"},
+};
+
+INSTANTIATE_TEST_CASE_P(Inst, ParseActionCmd,
+ ::testing::ValuesIn(parse_action_cmd_list),);
+
+TEST_P(ParseActionCmd, correctness) {
+ auto input = GetParam().second;
+ auto result = m_parser.parse_action_cmd(std::move(input));
+ EXPECT_EQ(GetParam().first, result);
+}