tests: Add test for util.uuid (checks that the output format is correct)
authorKim Alvefur <zash@zash.se>
Sun, 10 Jan 2016 03:35:00 +0100
changeset 7076 31fa6770019c
parent 7075 3121054e665f
child 7077 3ff83773ffc0
child 7081 ec17115e3721
tests: Add test for util.uuid (checks that the output format is correct)
tests/test.lua
tests/test_util_uuid.lua
--- a/tests/test.lua	Sun Jan 10 03:25:16 2016 +0100
+++ b/tests/test.lua	Sun Jan 10 03:35:00 2016 +0100
@@ -22,6 +22,7 @@
 	dotest "util.sasl.scram"
 	dotest "util.cache"
 	dotest "util.throttle"
+	dotest "util.uuid"
 
 	dosingletest("test_sasl.lua", "latin1toutf8");
 	dosingletest("test_utf8.lua", "valid");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_util_uuid.lua	Sun Jan 10 03:35:00 2016 +0100
@@ -0,0 +1,24 @@
+-- This tests the format, not the randomness
+
+-- https://tools.ietf.org/html/rfc4122#section-4.4
+
+local pattern = "^" .. table.concat({
+	string.rep("%x", 8),
+	string.rep("%x", 4),
+	"4" .. -- version
+	string.rep("%x", 3),
+	"[89ab]" .. -- reserved bits of 1 and 0
+	string.rep("%x", 3),
+	string.rep("%x", 12),
+}, "%-") .. "$";
+
+function generate(generate)
+	for i = 1, 100 do
+		assert_is(generate():match(pattern));
+	end
+end
+
+function seed(seed)
+	assert_equal(seed("random string here"), nil, "seed doesn't return anything");
+end
+