Skip to content

Commit

Permalink
check that titles are trimmed
Browse files Browse the repository at this point in the history
titles starting or ending with whitespace are awkward, they should be
trimmed by the application
  • Loading branch information
arnemertz committed Aug 28, 2021
1 parent 23212d6 commit bcba289
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/domain/domain_error.cpp
Expand Up @@ -17,6 +17,8 @@ std::string error_message(fix::domain::domain_error error) {
return "title may not contain '\\' or '`'";
case domain_error::TITLE_HAS_NON_ASCII_CHARS:
return "title may not contain non-ASCII characters";
case domain_error::TITLE_NOT_TRIMMED:
return "title may not start or end with whitespace";
}
return "unknown domain error";
}
Expand Down
1 change: 1 addition & 0 deletions src/domain/domain_error.hpp
Expand Up @@ -13,6 +13,7 @@ enum class domain_error {
TITLE_HAS_NON_PRINTABLES = 12,
TITLE_HAS_SPECIAL_PUNCTUATION = 13,
TITLE_HAS_NON_ASCII_CHARS = 14,
TITLE_NOT_TRIMMED = 15,
};

template<typename T>
Expand Down
6 changes: 6 additions & 0 deletions src/domain/title.cpp
Expand Up @@ -3,6 +3,7 @@
#include <cctype>
#include <climits>
#include <range/v3/algorithm/any_of.hpp>
#include <range/v3/view/trim.hpp>

using namespace fix::domain;

Expand All @@ -12,6 +13,11 @@ constexpr size_t MAX_LENGTH = 120;
title::title(std::string_view text) : text{text} {}

expected<title> title::create(std::string_view text) {
const auto trimmed_length = ranges::size(text | ranges::views::trim([](char c) { return isspace(c); }));
if (trimmed_length != text.length()) {
return unexpected(domain_error::TITLE_NOT_TRIMMED);
}

if (text.length() < MIN_LENGTH) {
return unexpected(domain_error::TITLE_TOO_SHORT);
}
Expand Down
6 changes: 6 additions & 0 deletions test/domain/title_test.cpp
Expand Up @@ -74,3 +74,9 @@ TEST_CASE("Titles have a restricted character set") {
FailsWithMessage("title may not contain non-ASCII characters"));
}
}

TEST_CASE("Title text may not start or end with whitespace") {
const auto untrimmed_title = GENERATE(" may not start with blanks"sv, "nor end with blanks "sv,
"\t\ttabs are whitespace"sv, "linebreaks, too\n"sv);
CHECK_THAT(title::create(untrimmed_title), FailsWithMessage("title may not start or end with whitespace"));
}

0 comments on commit bcba289

Please sign in to comment.