Skip to content

Commit 5f7a249

Browse files
committed
Add unittests to the utils module
Also introduces a unittest configuration in the DUB package. Closes #7.
1 parent 4146446 commit 5f7a249

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

dub.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
"vibe-d:tls": "notls"
3333
}
3434
},
35+
{
36+
"name": "unittest",
37+
"targetName": "rlv-unittest",
38+
"excludedSourceFiles": ["source/reloadedvibes/app.d"],
39+
"subConfigurations": {
40+
"vibe-d:tls": "notls"
41+
}
42+
},
3543
{
3644
"name": "library",
3745
"targetType": "library",

source/reloadedvibes/utils.d

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
+/
99
module reloadedvibes.utils;
1010

11-
import std.algorithm : canFind;
11+
import std.algorithm : canFind, count;
1212
import std.ascii : isDigit;
1313
import std.conv : to;
1414
import std.string : indexOf, isNumeric;
@@ -56,6 +56,12 @@ bool isIPv6(string address) nothrow @nogc
5656
return false;
5757
}
5858

59+
unittest
60+
{
61+
assert("127.0.0.1".isIPv6 == false);
62+
assert("::1".isIPv6);
63+
}
64+
5965
/++
6066
Tries to parse a socket string
6167
@@ -131,3 +137,40 @@ bool tryParseSocket(string s, out Socket socket)
131137
socket.port = cast(ushort)(portInt);
132138
return true;
133139
}
140+
141+
unittest
142+
{
143+
import std.conv : to;
144+
import std.typecons : tuple;
145+
146+
auto sockets = [
147+
// dfmt off
148+
tuple("127.0.0.1:3001", true, Socket("127.0.0.1", 3001)),
149+
tuple("1.2.3.4:56", true, Socket("1.2.3.4", 56)),
150+
tuple("127.0.0.1:123456", false, Socket()),
151+
tuple("[::1]:80", true, Socket("::1", 80)),
152+
tuple("[1]]:3001", false, Socket()),
153+
tuple("10.0.0.1", false, Socket()),
154+
tuple("[2001:db8:1234:0000:0000:0000:0000:0000]:443", true, Socket("2001:db8:1234:0000:0000:0000:0000:0000", 443)),
155+
tuple("[2001:db8::1]", false, Socket()),
156+
tuple(":1", false, Socket()),
157+
tuple("::11", false, Socket()),
158+
tuple("12.1:10", false, Socket()),
159+
tuple("1.2.3.4:-56", false, Socket()),
160+
// dfmt on
161+
];
162+
163+
foreach (idx, s; sockets)
164+
{
165+
Socket x;
166+
167+
immutable r = tryParseSocket(s[0], x);
168+
169+
assert(r == s[1], "Unexpected parser result: [" ~ idx.to!string ~ "] " ~ s[0] ~ " -> " ~ r.to!string);
170+
171+
if (r)
172+
{
173+
assert(x == s[2], "Wrongly parsed: [" ~ idx.to!string ~ "] " ~ s[0]);
174+
}
175+
}
176+
}

0 commit comments

Comments
 (0)