refactor(builder): Add failing test for ellipsis
Adds failing tests for the bug described in #1194
This commit is contained in:
parent
028b1413ef
commit
4b83468eb9
@ -59,6 +59,8 @@ class builder {
|
||||
string background_hex();
|
||||
string foreground_hex();
|
||||
|
||||
string get_label_text(const label_t& label);
|
||||
|
||||
void tag_open(syntaxtag tag, const string& value);
|
||||
void tag_open(attribute attr);
|
||||
void tag_close(syntaxtag tag);
|
||||
|
@ -95,5 +95,7 @@ endif()
|
||||
|
||||
# }}}
|
||||
|
||||
# Export source file list so that it can be used for test compilation
|
||||
set(files ${files} PARENT_SCOPE)
|
||||
set(libs ${libs} PARENT_SCOPE)
|
||||
set(dirs ${dirs} PARENT_SCOPE)
|
||||
|
@ -206,11 +206,7 @@ void builder::node(const label_t& label, bool add_space) {
|
||||
return;
|
||||
}
|
||||
|
||||
string text{label->get()};
|
||||
|
||||
if (label->m_maxlen > 0 && string_util::char_len(text) > label->m_maxlen) {
|
||||
text = string_util::utf8_truncate(std::move(text), label->m_maxlen) + "...";
|
||||
}
|
||||
auto text = get_label_text(label);
|
||||
|
||||
// if ((label->m_overline.empty() && m_tags[syntaxtag::o] > 0) || (m_tags[syntaxtag::o] > 0 && label->m_margin > 0))
|
||||
// overline_close();
|
||||
@ -556,6 +552,16 @@ string builder::foreground_hex() {
|
||||
return m_foreground;
|
||||
}
|
||||
|
||||
string builder::get_label_text(const label_t& label) {
|
||||
string text{label->get()};
|
||||
|
||||
if (label->m_maxlen > 0 && string_util::char_len(text) > label->m_maxlen) {
|
||||
text = string_util::utf8_truncate(std::move(text), label->m_maxlen) + "...";
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert directive to change value of given tag
|
||||
*/
|
||||
|
@ -44,7 +44,10 @@ function(unit_test file tests)
|
||||
# unit_test function become cleaner
|
||||
SET(sources "")
|
||||
FOREACH(f ${BIN_SOURCES})
|
||||
# Do not add main.cpp, because it will override the main function
|
||||
if(NOT "${f}" STREQUAL "main.cpp")
|
||||
LIST(APPEND sources "../src/${f}")
|
||||
endif()
|
||||
ENDFOREACH(f)
|
||||
|
||||
string(REPLACE "/" "_" testname ${file})
|
||||
@ -84,5 +87,9 @@ unit_test(components/command_line unit_tests
|
||||
utils/string.cpp)
|
||||
unit_test(components/bar unit_tests)
|
||||
|
||||
unit_test(components/builder unit_tests
|
||||
SOURCES
|
||||
${files})
|
||||
|
||||
# Compile all unit tests with 'make all_unit_tests'
|
||||
add_custom_target("all_unit_tests" DEPENDS ${unit_tests})
|
||||
|
79
tests/unit_tests/components/builder.cpp
Normal file
79
tests/unit_tests/components/builder.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include <tuple>
|
||||
|
||||
#include "common/test.hpp"
|
||||
#include "components/builder.hpp"
|
||||
#include "components/types.hpp"
|
||||
#include "utils/factory.hpp"
|
||||
#include "drawtypes/label.hpp"
|
||||
|
||||
using namespace polybar;
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* \brief Testing-only subclass of builder to change access level
|
||||
*/
|
||||
class TestableBuilder : public builder {
|
||||
using builder::builder;
|
||||
public: using builder::get_label_text;
|
||||
};
|
||||
|
||||
class Builder : public ::testing::Test {
|
||||
protected:
|
||||
|
||||
/**
|
||||
* Generic bar settings
|
||||
*
|
||||
* Builder only needs spacing and background
|
||||
*/
|
||||
bar_settings m_bar{};
|
||||
TestableBuilder m_builder{m_bar};
|
||||
};
|
||||
|
||||
// GetLabelTextTest {{{
|
||||
|
||||
/**
|
||||
* \brief Class for parameterized tests on get_label_text
|
||||
*
|
||||
* The first element of the pair is the expected returned text, the second
|
||||
* element is a triple containing the original label text, m_ellipsis and
|
||||
* m_maxlen, in that order
|
||||
*/
|
||||
class GetLabelTextTest :
|
||||
public Builder,
|
||||
public ::testing::WithParamInterface<pair<string, tuple<string, bool, int>>> {};
|
||||
|
||||
vector<pair<string, tuple<string, bool, int>>> get_label_text_list = {
|
||||
{"...", make_tuple("abcd", true, 3)},
|
||||
{"abc", make_tuple("abcdefgh", false, 3)},
|
||||
{"a...", make_tuple("abcdefgh", true, 4)},
|
||||
{"abcd...", make_tuple("abcdefgh", true, 7)},
|
||||
{"abcdefgh", make_tuple("abcdefgh", true, 8)},
|
||||
};
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(Inst, GetLabelTextTest,
|
||||
::testing::ValuesIn(get_label_text_list),);
|
||||
|
||||
TEST_P(GetLabelTextTest, correctness) {
|
||||
label_t m_label = factory_util::shared<label>(get<0>(GetParam().second));
|
||||
m_label->m_ellipsis = get<1>(GetParam().second);
|
||||
m_label->m_maxlen = get<2>(GetParam().second);
|
||||
|
||||
auto text = m_builder.get_label_text(m_label);
|
||||
EXPECT_EQ(GetParam().first, text);
|
||||
|
||||
EXPECT_LE(text.length(), m_label->m_maxlen) << "Returned text is longer than maxlen";
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Tests, if get_label_text throws an exception, when ellipsis is
|
||||
* turned on and m_maxlen is lower than 3 (length of the ellipsis)
|
||||
*/
|
||||
TEST_F(GetLabelTextTest, throwsException) {
|
||||
label_t m_label = factory_util::shared<label>("abcdef");
|
||||
m_label->m_ellipsis = true;
|
||||
m_label->m_maxlen = 2;
|
||||
|
||||
EXPECT_ANY_THROW(m_builder.get_label_text(m_label));
|
||||
}
|
||||
|
||||
// }}}
|
Loading…
Reference in New Issue
Block a user