Skip to content

Commit

Permalink
Add unittests to the utils module
Browse files Browse the repository at this point in the history
Also introduces a unittest configuration in the DUB package.
Closes #7.
  • Loading branch information
0xEAB committed May 29, 2019
1 parent 4146446 commit 5f7a249
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
8 changes: 8 additions & 0 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
"vibe-d:tls": "notls"
}
},
{
"name": "unittest",
"targetName": "rlv-unittest",
"excludedSourceFiles": ["source/reloadedvibes/app.d"],
"subConfigurations": {
"vibe-d:tls": "notls"
}
},
{
"name": "library",
"targetType": "library",
Expand Down
45 changes: 44 additions & 1 deletion source/reloadedvibes/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
+/
module reloadedvibes.utils;

import std.algorithm : canFind;
import std.algorithm : canFind, count;
import std.ascii : isDigit;
import std.conv : to;
import std.string : indexOf, isNumeric;
Expand Down Expand Up @@ -56,6 +56,12 @@ bool isIPv6(string address) nothrow @nogc
return false;
}

unittest
{
assert("127.0.0.1".isIPv6 == false);
assert("::1".isIPv6);
}

/++
Tries to parse a socket string
Expand Down Expand Up @@ -131,3 +137,40 @@ bool tryParseSocket(string s, out Socket socket)
socket.port = cast(ushort)(portInt);
return true;
}

unittest
{
import std.conv : to;
import std.typecons : tuple;

auto sockets = [
// dfmt off
tuple("127.0.0.1:3001", true, Socket("127.0.0.1", 3001)),
tuple("1.2.3.4:56", true, Socket("1.2.3.4", 56)),
tuple("127.0.0.1:123456", false, Socket()),
tuple("[::1]:80", true, Socket("::1", 80)),
tuple("[1]]:3001", false, Socket()),
tuple("10.0.0.1", false, Socket()),
tuple("[2001:db8:1234:0000:0000:0000:0000:0000]:443", true, Socket("2001:db8:1234:0000:0000:0000:0000:0000", 443)),
tuple("[2001:db8::1]", false, Socket()),
tuple(":1", false, Socket()),
tuple("::11", false, Socket()),
tuple("12.1:10", false, Socket()),
tuple("1.2.3.4:-56", false, Socket()),
// dfmt on
];

foreach (idx, s; sockets)
{
Socket x;

immutable r = tryParseSocket(s[0], x);

assert(r == s[1], "Unexpected parser result: [" ~ idx.to!string ~ "] " ~ s[0] ~ " -> " ~ r.to!string);

if (r)
{
assert(x == s[2], "Wrongly parsed: [" ~ idx.to!string ~ "] " ~ s[0]);
}
}
}

0 comments on commit 5f7a249

Please sign in to comment.