author | Kim Alvefur <zash@zash.se> |
Thu, 28 Mar 2024 15:26:57 +0100 | |
changeset 13472 | 98806cac64c3 |
parent 13321 | e6a5f196fc1f |
permissions | -rw-r--r-- |
8239
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 |
-- This tests the format, not the randomness |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 |
|
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 |
local uuid = require "util.uuid"; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 |
|
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 |
describe("util.uuid", function() |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 |
describe("#generate()", function() |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 |
it("should work follow the UUID pattern", function() |
12608
bd9e006a7a74
various: Update IETF RFC URLs for tools.ietf.org transition
Kim Alvefur <zash@zash.se>
parents:
8244
diff
changeset
|
8 |
-- https://www.rfc-editor.org/rfc/rfc4122.html#section-4.4 |
8239
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 |
|
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 |
local pattern = "^" .. table.concat({ |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 |
string.rep("%x", 8), |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 |
string.rep("%x", 4), |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 |
"4" .. -- version |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 |
string.rep("%x", 3), |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 |
"[89ab]" .. -- reserved bits of 1 and 0 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 |
string.rep("%x", 3), |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 |
string.rep("%x", 12), |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 |
}, "%-") .. "$"; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 |
|
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 |
for _ = 1, 100 do |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 |
assert.is_string(uuid.generate():match(pattern)); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 |
end |
13321 | 23 |
|
24 |
assert.truthy(uuid.generate() ~= uuid.generate(), "does not generate the same UUIDv4 twice") |
|
25 |
end); |
|
26 |
end); |
|
27 |
describe("#v7", function() |
|
28 |
it("should also follow the UUID pattern", function() |
|
29 |
local pattern = "^" .. table.concat({ |
|
30 |
string.rep("%x", 8), |
|
31 |
string.rep("%x", 4), |
|
32 |
"7" .. -- version |
|
33 |
string.rep("%x", 3), |
|
34 |
"[89ab]" .. -- reserved bits of 1 and 0 |
|
35 |
string.rep("%x", 3), |
|
36 |
string.rep("%x", 12), |
|
37 |
}, "%-") .. "$"; |
|
38 |
||
39 |
local one = uuid.v7(); -- one before the loop to ensure some time passes |
|
40 |
for _ = 1, 100 do |
|
41 |
assert.is_string(uuid.v7():match(pattern)); |
|
42 |
end |
|
43 |
-- one after the loop when some time should have passed |
|
44 |
assert.truthy(one < uuid.v7(), "should be ordererd") |
|
8239
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 |
end); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 |
end); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 |
end); |