Skip to content

Commit

Permalink
refactor(builder): No exception when maxlen < 3
Browse files Browse the repository at this point in the history
The check of the maxlen and ellipsis condition was also moved to the
label creation, this way get_label_text doesn't need to care about the
restrictions placed on maxlen and ellipsis
  • Loading branch information
patrick96 committed May 29, 2018
1 parent 4933e6b commit 5b6e00a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
14 changes: 13 additions & 1 deletion include/drawtypes/label.hpp
@@ -1,5 +1,7 @@
#pragma once

#include <cassert>

#include "common.hpp"
#include "components/config.hpp"
#include "components/types.hpp"
Expand Down Expand Up @@ -37,6 +39,14 @@ namespace drawtypes {
int m_font{0};
side_values m_padding{0U,0U};
side_values m_margin{0U,0U};

/*
* If m_ellipsis is true, m_maxlen MUST be larger or equal to the length of
* the ellipsis (3), everything else is a programming error
*
* load_label should take care of this, but be aware, if you are creating
* labels in a different way.
*/
size_t m_maxlen{0_z};
bool m_ellipsis{true};

Expand All @@ -55,7 +65,9 @@ namespace drawtypes {
, m_ellipsis(ellipsis)
, m_text(text)
, m_tokenized(m_text)
, m_tokens(forward<vector<token>>(tokens)) {}
, m_tokens(forward<vector<token>>(tokens)) {
assert(!m_ellipsis || (m_maxlen == 0 || m_maxlen >= 3));
}

string get() const;
operator bool();
Expand Down
10 changes: 2 additions & 8 deletions src/components/builder.cpp
Expand Up @@ -561,14 +561,8 @@ string builder::get_label_text(const label_t& label) {

size_t maxlen = label->m_maxlen;

if (maxlen > 0 && string_util::char_len(text) > maxlen ) {
if(label->m_ellipsis) {
if(maxlen < 3) {
throw application_error(sstream()
<< "Label has maxlen (" << maxlen
<< ") that is smaller than size of ellipsis(3)");
}

if (maxlen > 0 && string_util::char_len(text) > maxlen) {
if (label->m_ellipsis) {
text = string_util::utf8_truncate(std::move(text), maxlen - 3) + "...";
}
else {
Expand Down
15 changes: 13 additions & 2 deletions src/drawtypes/label.cpp
Expand Up @@ -221,6 +221,17 @@ namespace drawtypes {
}
}

size_t maxlen = conf.get(section, name + "-maxlen", 0_z);
bool ellipsis = conf.get(section, name + "-ellipsis", true);

if(ellipsis && maxlen > 0 && maxlen < 3) {
logger::make().err(sstream() << "Label " << section << "." << name
<< " has maxlen " << maxlen
<< ", which is smaller than length of ellipsis (3), disabling ellipsis...");

ellipsis = false;
}

// clang-format off
return factory_util::shared<label>(text,
conf.get(section, name + "-foreground", ""s),
Expand All @@ -230,8 +241,8 @@ namespace drawtypes {
conf.get(section, name + "-font", 0),
padding,
margin,
conf.get(section, name + "-maxlen", 0_z),
conf.get(section, name + "-ellipsis", true),
maxlen,
ellipsis,
move(tokens));
// clang-format on
}
Expand Down
12 changes: 0 additions & 12 deletions tests/unit_tests/components/builder.cpp
Expand Up @@ -65,16 +65,4 @@ TEST_P(GetLabelTextTest, correctness) {
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));
}

// }}}

0 comments on commit 5b6e00a

Please sign in to comment.