Implementation of TypeID in Lua
at main 4.0 kB view raw
1local TypeID = require("typeid") 2local json = require("dkjson") 3 4describe("TypeID", function() 5 local function read_json_file(filename) 6 local file = io.open(filename, "r") 7 if not file then error("Could not open file: " .. filename) end 8 local content = file:read("*all") 9 file:close() 10 return json.decode(content) 11 end 12 13 describe("valid prefixes", function() 14 it("accepts empty prefix", function() 15 local id = TypeID.generate("") 16 assert.are.equal("", id.prefix) 17 end) 18 19 it("accepts 'user' prefix", function() 20 local id = TypeID.generate("user") 21 assert.are.equal("user", id.prefix) 22 end) 23 24 it("accepts 'my_type' prefix", function() 25 local id = TypeID.generate("my_type") 26 assert.are.equal("my_type", id.prefix) 27 end) 28 29 it("accepts 'a_b_c' prefix", function() 30 local id = TypeID.generate("a_b_c") 31 assert.are.equal("a_b_c", id.prefix) 32 end) 33 34 it("accepts 'my__type' prefix", function() 35 local id = TypeID.generate("my__type") 36 assert.are.equal("my__type", id.prefix) 37 end) 38 39 it("accepts single character 'a' prefix", function() 40 local id = TypeID.generate("a") 41 assert.are.equal("a", id.prefix) 42 end) 43 44 it("accepts 'abcd' prefix", function() 45 local id = TypeID.generate("abcd") 46 assert.are.equal("abcd", id.prefix) 47 end) 48 end) 49 50 describe("invalid prefixes", function() 51 it("rejects '_foo' prefix (starts with underscore)", function() 52 assert.has_error(function() 53 TypeID.generate("_foo") 54 end, "type prefix cannot start or end with an underscore") 55 end) 56 57 it("rejects 'bar_' prefix (ends with underscore)", function() 58 assert.has_error(function() 59 TypeID.generate("bar_") 60 end, "type prefix cannot start or end with an underscore") 61 end) 62 63 it("rejects '_' prefix (single underscore)", function() 64 assert.has_error(function() 65 TypeID.generate("_") 66 end, "type prefix cannot start or end with an underscore") 67 end) 68 69 it("rejects 'FOO' prefix (uppercase letters)", function() 70 assert.has_error(function() 71 TypeID.generate("FOO") 72 end, "type prefix must be lowercase ASCII characters") 73 end) 74 end) 75 76 describe("valid.json test cases", function() 77 local valid_cases = read_json_file("spec/valid.json") 78 79 for _, test_case in ipairs(valid_cases) do 80 it("accepts " .. test_case.name, function() 81 local typeid_str = test_case.typeid 82 local expected_prefix = test_case.prefix 83 local expected_uuid = test_case.uuid 84 85 local id = TypeID.parse(typeid_str) 86 assert.are.equal(expected_prefix, id.prefix) 87 88 -- test that we can create the same TypeID from the UUID 89 if expected_uuid ~= "00000000-0000-0000-0000-000000000000" then 90 local id_from_uuid = TypeID.from_uuid_string(expected_prefix, expected_uuid) 91 assert.are.equal(tostring(id), tostring(id_from_uuid)) 92 end 93 end) 94 end 95 end) 96 97 describe("invalid.json test cases", function() 98 local invalid_cases = read_json_file("spec/invalid.json") 99 100 for _, test_case in ipairs(invalid_cases) do 101 it("rejects " .. test_case.name .. ": " .. test_case.description, function() 102 local typeid_str = test_case.typeid 103 104 -- bleh, really should test the specific validation errors 105 assert.has_error(function() 106 TypeID.parse(typeid_str) 107 end) 108 end) 109 end 110 end) 111 112 describe("uuid method", function() 113 it("converts TypeID to UUID string format", function() 114 local id = TypeID.new("user", "01h455vb4pex5vsknk084sn02q") 115 116 local uuid_str = id:uuid() 117 assert.truthy(uuid_str:match("^%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x$")) 118 119 -- round trip 120 local known_id = TypeID.from_uuid_string("test", "01893726-efee-7f02-8fbf-9f2b7bc2f910") 121 local uuid_known = known_id:uuid() 122 assert.are.equal("01893726-efee-7f02-8fbf-9f2b7bc2f910", uuid_known) 123 end) 124 end) 125end)