Ver código fonte

A `package_id` type

default_compile_flags
vector-of-bool 5 anos atrás
pai
commit
4eb8c508f1
3 arquivos alterados com 107 adições e 0 exclusões
  1. +21
    -0
      src/dds/package_id.cpp
  2. +28
    -0
      src/dds/package_id.hpp
  3. +58
    -0
      src/dds/package_id.test.cpp

+ 21
- 0
src/dds/package_id.cpp Ver arquivo

@@ -0,0 +1,21 @@
#include <dds/package_id.hpp>

#include <spdlog/fmt/fmt.h>

#include <tuple>

using namespace dds;

package_id package_id::parse(std::string_view s) {
auto at_pos = s.find('@');
if (at_pos == s.npos) {
throw std::runtime_error(fmt::format("Invalid package ID string '{}'", s));
}

auto name = s.substr(0, at_pos);
auto ver_str = s.substr(at_pos + 1);

return {std::string(name), semver::version::parse(ver_str)};
}

std::string package_id::to_string() const noexcept { return name + "@" + version.to_string(); }

+ 28
- 0
src/dds/package_id.hpp Ver arquivo

@@ -0,0 +1,28 @@
#pragma once

#include <semver/version.hpp>

#include <string>
#include <string_view>
#include <tuple>

namespace dds {

struct package_id {
std::string name;
semver::version version;

static package_id parse(std::string_view);
std::string to_string() const noexcept;

auto tie() const noexcept { return std::tie(name, version); }

friend bool operator<(const package_id& lhs, const package_id& rhs) noexcept {
return lhs.tie() < rhs.tie();
}
friend bool operator==(const package_id& lhs, const package_id& rhs) noexcept {
return lhs.tie() == rhs.tie();
}
};

} // namespace dds

+ 58
- 0
src/dds/package_id.test.cpp Ver arquivo

@@ -0,0 +1,58 @@
#include <dds/package_id.hpp>

#include <catch2/catch.hpp>

TEST_CASE("Package package ID strings") {
struct case_ {
std::string_view string;
std::string_view expect_name;
std::string_view expect_version;
};
auto [id_str, exp_name, exp_ver] = GENERATE(Catch::Generators::values<case_>({
{"foo@1.2.3", "foo", "1.2.3"},
{"foo@1.2.3-beta", "foo", "1.2.3-beta"},
{"foo@1.2.3-alpha", "foo", "1.2.3-alpha"},
}));

auto pk_id = dds::package_id::parse(id_str);
CHECK(pk_id.to_string() == id_str);
CHECK(pk_id.name == exp_name);
CHECK(pk_id.version.to_string() == exp_ver);
}

TEST_CASE("Package ordering") {
enum order {
less_than,
equivalent_to,
greater_than,
};
struct case_ {
std::string_view lhs_pkg;
order ord;
std::string_view rhs_pkg;
};

auto [lhs_str, ord, rhs_str] = GENERATE(Catch::Generators::values<case_>({
{"foo@1.2.3", greater_than, "bar@1.2.3"},
{"foo@1.2.3", greater_than, "foo@1.2.2"},
{"foo@1.2.3", equivalent_to, "foo@1.2.3"},
{"foo@1.2.3", equivalent_to, "foo@1.2.3+build-meta"},
{"foo@1.2.3", greater_than, "foo@1.2.3-alpha"},
{"foo@1.2.3-alpha.1", greater_than, "foo@1.2.3-alpha"},
{"foo@1.2.3-alpha.2", greater_than, "foo@1.2.3-alpha.1"},
{"foo@1.2.3-beta.2", greater_than, "foo@1.2.3-alpha.1"},
{"foo@0.1.2-alpha", less_than, "foo@1.0.0"},
}));

auto lhs = dds::package_id::parse(lhs_str);
auto rhs = dds::package_id::parse(rhs_str);

if (ord == less_than) {
CHECK(lhs < rhs);
} else if (ord == equivalent_to) {
CHECK(lhs == rhs);
} else if (ord == greater_than) {
CHECK_FALSE(lhs == rhs);
CHECK_FALSE(lhs < rhs);
}
}

Carregando…
Cancelar
Salvar