Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
Correct version constraint decode and renable additional testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottBailey committed Mar 6, 2023
1 parent 2c4df7f commit f617a17
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
20 changes: 15 additions & 5 deletions src/version_constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ inline std::string_view trim(std::string_view s) {
auto last = std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {return !std::isspace(ch);});
// Convert the reverse iter last to the forward iterator containing end using base().
// See: https://en.cppreference.com/w/cpp/iterator/reverse_iterator/base
return std::string_view{first, s.size() - 1 - (last - s.rbegin())};
return std::string_view{first, last.base() - first};
}


Expand Down Expand Up @@ -106,9 +106,19 @@ void version_constraint::load(std::string_view sin, std::ostream& os) {

if (el_list.size() == 1) {
// One member MUST be a unique.
auto ver_str = trim(el_list[0]);
m_constraints.emplace_back(constraint{ version(ver_str), version(), bounds_inclusivity::unique });
continue;
auto ver_str = std::string(trim(el_list[0]));

if(std::isdigit(ver_str[0])) {
m_constraints.emplace_back(constraint{ version(ver_str), version(), bounds_inclusivity::unique });
continue;
}

// first char must have been an operation, right?
auto first_digit = ver_str.find_first_of("0123456789");
if(first_digit != ver_str.npos) {
el_list.push_back( ver_str.substr(first_digit) );
el_list[0] = ver_str.substr(0,first_digit);
}
}

if (el_list.size() == 2) {
Expand Down Expand Up @@ -215,7 +225,7 @@ void version_constraint::load(std::string_view sin, std::ostream& os) {
}

os << __FILE__ << ":" << __LINE__ << " Failed to decode version_constraint: \"" << sin << "\" Too many elements in: \"";

for (const auto& e : element) {
os << e;
}
Expand Down
17 changes: 13 additions & 4 deletions tests/version_constraint_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

using namespace antler::project;

#if 0
struct constraint_entry {
std::string ver; // left comparison
std::string constraint; // right comparison
Expand Down Expand Up @@ -41,11 +40,17 @@ const std::vector<constraint_entry> compare_list = {
{ "2.2", ">= 2.0.12, < 2.1 | >= 2.1.3, < 2.2 | >= 2.2.3, < 2.3 | >= 2.3.2, < 3 | >= 3.2", false},
{ "2.3", ">= 2.0.12, < 2.1 | >= 2.1.3, < 2.2 | >= 2.2.3, < 2.3 | >= 2.3.2, < 3 | >= 3.2", false},
{ "3.0", ">= 2.0.12, < 2.1 | >= 2.1.3, < 2.2 | >= 2.2.3, < 2.3 | >= 2.3.2, < 3 | >= 3.2", false},
{ "3.2-rc0", ">= 2.0.12, < 2.1 | >= 2.1.3, < 2.2 | >= 2.2.3, < 2.3 | >= 2.3.2, < 3 | >= 3.2", false},
// version does not support prerelease or build metadata at this time, so don't test for it.
// { "3.2-rc0", ">= 2.0.12, < 2.1 | >= 2.1.3, < 2.2 | >= 2.2.3, < 2.3 | >= 2.3.2, < 3 | >= 3.2", false},
};
#endif

inline bool test_constraint(std::string_view v, std::string_view c) { return version_constraint{c}.test(version{v}); }
inline bool test_constraint(std::string_view v, std::string_view c, bool expect) {
if (version_constraint{c}.test(version{v}) == expect)
return true;
std::cerr << "Failed: ver: " << v << " " << c << " expect: " << expect << "\n";
return false;
}

TEST_CASE("Testing version constraints") {
REQUIRE(test_constraint({"999"}, {""}));
Expand All @@ -54,4 +59,8 @@ TEST_CASE("Testing version constraints") {
REQUIRE(test_constraint({"1.0.0"}, {">=1.0.0"}));
REQUIRE(!test_constraint({"1.0.0"}, {"<1.0.0"}));
REQUIRE(!test_constraint({"1.0.0"}, {">1.0.0"}));
}

for(const auto& a : compare_list) {
REQUIRE(test_constraint(a.ver, a.constraint, a.result));
}
}

0 comments on commit f617a17

Please sign in to comment.