unittest ObjectPath concat & allow strings without leading /

This commit is contained in:
WebFreak001 2017-10-20 12:47:31 +02:00
parent eeab026852
commit 92447921a7

View file

@ -18,7 +18,7 @@ public import ddbus.exception : wrapErrors, DBusException;
struct ObjectPath { struct ObjectPath {
private string _value; private string _value;
this(string objPath) @safe { this(string objPath) pure @safe {
enforce(isValid(objPath)); enforce(isValid(objPath));
_value = objPath; _value = objPath;
} }
@ -41,20 +41,26 @@ struct ObjectPath {
} }
ObjectPath opBinary(string op : "~")(string rhs) const pure @safe { ObjectPath opBinary(string op : "~")(string rhs) const pure @safe {
return opBinary!"~"(ObjectPath(rhs)); if (!rhs.startsWith("/"))
return opBinary!"~"(ObjectPath("/" ~ rhs));
else
return opBinary!"~"(ObjectPath(rhs));
} }
ObjectPath opBinary(string op : "~")(ObjectPath rhs) const pure @safe { ObjectPath opBinary(string op : "~")(ObjectPath rhs) const pure @safe
if (!_value.length || _value[$ - 1] != '/') { in {
ObjectPath ret; assert(ObjectPath.isValid(_value) && ObjectPath.isValid(rhs._value));
// Is safe if both sides were safe and isValid enforces a starting / } out (v) {
assert(ObjectPath.isValid(v._value));
} body {
ObjectPath ret;
if (_value == "/")
ret._value = rhs._value;
else
ret._value = _value ~ rhs._value; ret._value = _value ~ rhs._value;
return ret;
} else { return ret;
ObjectPath ret;
ret._value = _value[0 .. $ - 1] ~ rhs._value;
return ret;
}
} }
void opOpAssign(string op : "~")(string rhs) pure @safe { void opOpAssign(string op : "~")(string rhs) pure @safe {
@ -98,6 +104,19 @@ unittest {
obj.toHash().assertEqual(path.hashOf); obj.toHash().assertEqual(path.hashOf);
} }
unittest {
import dunit.toolkit;
ObjectPath a = ObjectPath("/org/freedesktop");
a.assertEqual(ObjectPath("/org/freedesktop"));
a ~= ObjectPath("/UPower");
a.assertEqual(ObjectPath("/org/freedesktop/UPower"));
a ~= "Device";
a.assertEqual(ObjectPath("/org/freedesktop/UPower/Device"));
(a ~ "0").assertEqual(ObjectPath("/org/freedesktop/UPower/Device/0"));
a.assertEqual(ObjectPath("/org/freedesktop/UPower/Device"));
}
/// Structure allowing typeless parameters /// Structure allowing typeless parameters
struct DBusAny { struct DBusAny {
/// DBus type of the value (never 'v'), see typeSig!T /// DBus type of the value (never 'v'), see typeSig!T