2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assert, assertEquals, assertThrows } from "@std/assert";
|
2021-12-28 15:28:23 +00:00
|
|
|
import { isNil, NIL_UUID, validate, version } from "./mod.ts";
|
2020-08-30 18:39:51 +00:00
|
|
|
|
2023-12-20 21:52:37 +00:00
|
|
|
Deno.test("isNil() checks if a UUID is the nil UUID", () => {
|
2021-02-02 03:16:27 +00:00
|
|
|
const nil = NIL_UUID;
|
|
|
|
const u = "582cbcff-dad6-4f28-888a-e062ae36bafc";
|
|
|
|
assert(isNil(nil));
|
|
|
|
assert(!isNil(u));
|
2020-08-30 18:39:51 +00:00
|
|
|
});
|
2021-12-18 14:22:37 +00:00
|
|
|
|
2023-12-20 21:52:37 +00:00
|
|
|
Deno.test("validate() checks if a string is a valid UUID", () => {
|
2021-12-18 14:22:37 +00:00
|
|
|
const u = "582cbcff-dad6-4f28-888a-e062ae36bafc";
|
|
|
|
const nil = NIL_UUID;
|
|
|
|
assert(validate(u));
|
|
|
|
assert(validate(nil));
|
|
|
|
assert(!validate("not a UUID"));
|
|
|
|
});
|
2021-12-28 15:28:23 +00:00
|
|
|
|
2023-12-20 21:52:37 +00:00
|
|
|
Deno.test("version() detects the RFC version of a UUID", () => {
|
2021-12-28 15:28:23 +00:00
|
|
|
assertEquals(version(NIL_UUID), 0);
|
|
|
|
assertEquals(version("d9428888-122b-11e1-b85c-61cd3cbb3210"), 1);
|
|
|
|
assertEquals(version("109156be-c4fb-41ea-b1b4-efe1671c5836"), 4);
|
|
|
|
assertEquals(version("a981a0c2-68b1-35dc-bcfc-296e52ab01ec"), 3);
|
|
|
|
assertEquals(version("90123e1c-7512-523e-bb28-76fab9f2f73d"), 5);
|
|
|
|
assertThrows(() => version(""));
|
|
|
|
assertThrows(() => version("not a UUID"));
|
|
|
|
assertThrows(() => version("00000000000000000000000000000000"));
|
2024-08-26 04:45:37 +00:00
|
|
|
assertThrows(
|
|
|
|
() =>
|
|
|
|
version(
|
|
|
|
"=Y00a-f*v00b*-00c-00d#-p00f\b-00g-00h-####00i^^^-00j*1*2*3&-L00k-\n00l-/00m-----00n-fg000-00p-00r+",
|
|
|
|
),
|
|
|
|
TypeError,
|
|
|
|
"Cannot detect UUID version: received =Y00a-f*v00b*-00c-00d#-p00f\b-00g-00h-####00i^^^-00j*1*2*3&-L00k-\n00l-/00m-----00n-fg000-00p-00r+",
|
2021-12-28 15:28:23 +00:00
|
|
|
);
|
|
|
|
});
|