Skip to content

Commit 15234e1

Browse files
author
thk123
committed
Require components be lower case and non-empty
1 parent 4fa9921 commit 15234e1

File tree

2 files changed

+35
-54
lines changed

2 files changed

+35
-54
lines changed

src/util/structured_data.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,23 @@ Author: Thomas Kiley
1111
#include "string_utils.h"
1212
#include <algorithm>
1313

14-
labelt::labelt(std::vector<std::string> components)
15-
{
16-
auto to_lower_string = [](const std::string &s) -> std::string {
17-
std::string lower_case = s;
18-
std::for_each(
19-
lower_case.begin(), lower_case.end(), [](char &c) { c = ::tolower(c); });
20-
return lower_case;
21-
};
22-
this->components = make_range(components)
23-
.map(to_lower_string)
24-
.collect<std::vector<std::string>>();
14+
labelt::labelt(std::vector<std::string> components) : components(components)
15+
{
16+
PRECONDITION(!components.empty());
17+
PRECONDITION(std::all_of(
18+
components.begin(), components.end(), [](const std::string &component) {
19+
return !component.empty() &&
20+
std::all_of(component.begin(), component.end(), [](const char &c) {
21+
return !(::isalpha(c)) || ::islower(c);
22+
});
23+
}));
2524
}
2625
std::string labelt::camel_case() const
2726
{
2827
std::ostringstream output;
29-
if(!components.empty())
30-
{
31-
output << *components.begin();
32-
join_strings(
33-
output, std::next(components.begin()), components.end(), "", capitalize);
34-
}
28+
output << *components.begin();
29+
join_strings(
30+
output, std::next(components.begin()), components.end(), "", capitalize);
3531
return output.str();
3632
}
3733
std::string labelt::snake_case() const
@@ -49,15 +45,12 @@ std::string labelt::kebab_case() const
4945
std::string labelt::pretty() const
5046
{
5147
std::ostringstream output;
52-
if(!components.empty())
53-
{
54-
const auto range =
55-
make_range(components.begin(), std::next(components.begin()))
56-
.map(capitalize)
57-
.concat(make_range(std::next(components.begin()), components.end()));
48+
const auto range =
49+
make_range(components.begin(), std::next(components.begin()))
50+
.map(capitalize)
51+
.concat(make_range(std::next(components.begin()), components.end()));
5852

59-
join_strings(output, range.begin(), range.end(), ' ');
60-
}
53+
join_strings(output, range.begin(), range.end(), ' ');
6154
return output.str();
6255
}
6356
bool labelt::operator<(const labelt &other) const

unit/util/structured_data.cpp

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,8 @@ Author: Thomas Kiley
1111

1212
TEST_CASE("label", "[core][util][structured_data][label]")
1313
{
14-
SECTION("Empty label")
15-
{
16-
labelt empty_label({});
17-
REQUIRE(empty_label.camel_case() == "");
18-
REQUIRE(empty_label.kebab_case() == "");
19-
REQUIRE(empty_label.snake_case() == "");
20-
REQUIRE(empty_label.pretty() == "");
21-
}
14+
const cbmc_invariants_should_throwt invariant_throw_during_tests{};
15+
2216
SECTION("Single element")
2317
{
2418
labelt empty_label({"hello"});
@@ -35,42 +29,36 @@ TEST_CASE("label", "[core][util][structured_data][label]")
3529
REQUIRE(empty_label.snake_case() == "hello_goodbye");
3630
REQUIRE(empty_label.pretty() == "Hello goodbye");
3731
}
38-
SECTION("Odd casing elements")
32+
SECTION("Valid labels")
3933
{
40-
labelt empty_label({"HelLo", "Goodbye"});
41-
REQUIRE(empty_label.camel_case() == "helloGoodbye");
42-
REQUIRE(empty_label.kebab_case() == "hello-goodbye");
43-
REQUIRE(empty_label.snake_case() == "hello_goodbye");
44-
REQUIRE(empty_label.pretty() == "Hello goodbye");
34+
labelt numbered_label({"hello", "1"});
35+
REQUIRE(numbered_label.camel_case() == "hello1");
36+
REQUIRE(numbered_label.kebab_case() == "hello-1");
37+
REQUIRE(numbered_label.snake_case() == "hello_1");
38+
REQUIRE(numbered_label.pretty() == "Hello 1");
39+
}
40+
SECTION("Invalid components")
41+
{
42+
REQUIRE_THROWS_AS(labelt{{}}, invariant_failedt);
43+
std::vector<std::string> starts_with_upper{"Hello"};
44+
REQUIRE_THROWS_AS(labelt{starts_with_upper}, invariant_failedt);
45+
std::vector<std::string> contains_upper{"Hello"};
46+
REQUIRE_THROWS_AS(labelt{contains_upper}, invariant_failedt);
47+
REQUIRE_THROWS_AS(labelt{{""}}, invariant_failedt);
4548
}
4649
}
4750

4851
TEST_CASE("Label equality", "[core][util][structured_data][label]")
4952
{
50-
labelt empty_label({});
5153
labelt single_label({"a"});
52-
labelt capital_label({"A"});
5354
labelt b_label({"b"});
5455
labelt multi_label({"b", "c", "d"});
5556
labelt multi_label2({"b", "d", "d"});
5657

57-
REQUIRE(empty_label < single_label);
58-
REQUIRE(empty_label < capital_label);
59-
REQUIRE(empty_label < b_label);
60-
REQUIRE(empty_label < multi_label);
61-
REQUIRE(empty_label < multi_label2);
62-
63-
REQUIRE_FALSE(single_label < capital_label);
64-
REQUIRE_FALSE(capital_label < single_label);
65-
6658
REQUIRE(single_label < b_label);
6759
REQUIRE(single_label < multi_label);
6860
REQUIRE(single_label < multi_label2);
6961

70-
REQUIRE(capital_label < b_label);
71-
REQUIRE(capital_label < multi_label);
72-
REQUIRE(capital_label < multi_label2);
73-
7462
REQUIRE(b_label < multi_label);
7563
REQUIRE(b_label < multi_label2);
7664

0 commit comments

Comments
 (0)