labels: implement parse_delta_labels
Convenience function for parsing a series of delta labels; add tests as well.
This commit is contained in:
@ -100,6 +100,26 @@ Mu::Labels::parse_delta_label(const std::string &expr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Result<DeltaLabelVec>
|
||||||
|
Mu::Labels::parse_delta_labels(const std::string& exprs,
|
||||||
|
const std::string sepa)
|
||||||
|
{
|
||||||
|
|
||||||
|
DeltaLabelVec deltas{};
|
||||||
|
for (const auto& expr: split(exprs, sepa)) {
|
||||||
|
if (auto delta = parse_delta_label(expr); !delta)
|
||||||
|
return Err(std::move(delta.error()));
|
||||||
|
else
|
||||||
|
deltas.emplace_back(*delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(std::move(deltas));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct cmp_delta_label { // can not yet be a λ in C++17
|
struct cmp_delta_label { // can not yet be a λ in C++17
|
||||||
bool operator()(const DeltaLabel& dl1, const DeltaLabel& dl2) const {
|
bool operator()(const DeltaLabel& dl1, const DeltaLabel& dl2) const {
|
||||||
return dl1.second < dl2.second;
|
return dl1.second < dl2.second;
|
||||||
@ -182,6 +202,29 @@ test_parse_delta_label()
|
|||||||
g_assert_false(!!parse_delta_label("-😨"));
|
g_assert_false(!!parse_delta_label("-😨"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_parse_delta_labels()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
const auto deltas = parse_delta_labels("+foo,-bar@cuux",",");
|
||||||
|
assert_valid_result(deltas);
|
||||||
|
|
||||||
|
g_assert_true(deltas->at(0).first == Delta::Add);
|
||||||
|
assert_equal(deltas->at(0).second, "foo");
|
||||||
|
|
||||||
|
g_assert_true(deltas->at(1).first == Delta::Remove);
|
||||||
|
assert_equal(deltas->at(1).second, "bar@cuux");
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const auto expr = parse_delta_labels("+foo @boo🐂", " ");
|
||||||
|
g_assert_false(!!expr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_validate_label()
|
test_validate_label()
|
||||||
{
|
{
|
||||||
@ -229,18 +272,17 @@ test_updated_labels()
|
|||||||
assert_eq({}, delta_labels({"-fnorb", "-fnorb", "+whiteward", "+altesia", "+fnorb"}),
|
assert_eq({}, delta_labels({"-fnorb", "-fnorb", "+whiteward", "+altesia", "+fnorb"}),
|
||||||
{"altesia", "fnorb", "whiteward"}, delta_labels({"+altesia", "+fnorb", "+whiteward"}));
|
{"altesia", "fnorb", "whiteward"}, delta_labels({"+altesia", "+fnorb", "+whiteward"}));
|
||||||
|
|
||||||
|
|
||||||
assert_eq({"piranesi", "hyperion", "mordor", "piranesi"}, delta_labels({}),
|
assert_eq({"piranesi", "hyperion", "mordor", "piranesi"}, delta_labels({}),
|
||||||
{"hyperion", "mordor", "piranesi"}, delta_labels({}));
|
{"hyperion", "mordor", "piranesi"}, delta_labels({}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char* argv[])
|
main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
mu_test_init(&argc, &argv);
|
mu_test_init(&argc, &argv);
|
||||||
|
|
||||||
g_test_add_func("/message/labels/parse-delta-label", test_parse_delta_label);
|
g_test_add_func("/message/labels/parse-delta-label", test_parse_delta_label);
|
||||||
|
g_test_add_func("/message/labels/parse-delta-labels", test_parse_delta_labels);
|
||||||
g_test_add_func("/message/labels/validate-label", test_validate_label);
|
g_test_add_func("/message/labels/validate-label", test_validate_label);
|
||||||
g_test_add_func("/message/labels/updated-labels", test_updated_labels);
|
g_test_add_func("/message/labels/updated-labels", test_updated_labels);
|
||||||
|
|
||||||
|
|||||||
@ -46,6 +46,18 @@ using DeltaLabelVec = std::vector<DeltaLabel>;
|
|||||||
*/
|
*/
|
||||||
Result<DeltaLabel> parse_delta_label(const std::string& expr);
|
Result<DeltaLabel> parse_delta_label(const std::string& expr);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a series of label expressions, with some separator
|
||||||
|
*
|
||||||
|
* This also validates the labels, as per valid_label()
|
||||||
|
*
|
||||||
|
* @param exprs label expressions
|
||||||
|
*
|
||||||
|
* @return a result with either a DeltaLabel or an error
|
||||||
|
*/
|
||||||
|
Result<DeltaLabelVec> parse_delta_labels(const std::string& exprs,
|
||||||
|
const std::string sepa);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is the label (without +/- prefix) valid?
|
* Is the label (without +/- prefix) valid?
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user