ChannelMessage

- Added `setFrom(string from)`
- Made this validatable

Channels (unittests)

- Added some validation testing
This commit is contained in:
Tristan B. Velloza Kildaire 2023-11-22 00:13:05 +02:00
parent 2d1db7b1ef
commit ff21d16a51
1 changed files with 46 additions and 1 deletions

View File

@ -66,8 +66,10 @@ public final class ChannelEnumerateReply : Command
} }
} }
import davinci.base.components : Validatable;
// NOTE: Can be used for reception AND request (sending) // NOTE: Can be used for reception AND request (sending)
public final class ChannelMessage : Command public final class ChannelMessage : Command, Validatable
{ {
private string[] to; private string[] to;
private string from; private string from;
@ -87,6 +89,11 @@ public final class ChannelMessage : Command
this.data = data; this.data = data;
} }
public void setFrom(string from)
{
this.from = from;
}
public void setTo(string[] to) public void setTo(string[] to)
{ {
foreach(string curTo; to) foreach(string curTo; to)
@ -140,9 +147,47 @@ public final class ChannelMessage : Command
{ {
return getStatus() == Status.GOOD; return getStatus() == Status.GOOD;
} }
public bool validate(ref string reason)
{
// If this is a request
if(this.status == Status.UNSET)
{
if(to.length > 0 && from.length > 0)
{
return true;
}
else
{
reason = "Recipients list is empty or the from field is empty";
return false;
}
}
// TODO: Implement the rest
return false;
}
} }
unittest
{
string reason;
// Valid case of a CHANNEL_SEND_MESSAGE
ChannelMessage chanMesg = new ChannelMessage();
chanMesg.setTo(["deavmi", "gustav"]);
chanMesg.setFrom("testUser");
assert(chanMesg.validate(reason));
// In-valid case of a CHANNEL_SEND_MESSAGE
chanMesg = new ChannelMessage();
chanMesg.setTo(cast(string[])[]);
chanMesg.setFrom("testUser");
assert(!chanMesg.validate(reason));
assert(reason.length > 0);
}
public enum MembershipMode public enum MembershipMode
{ {
JOIN, JOIN,