Skip to content

Commit

Permalink
update spec files (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
broothie authored Jul 7, 2023
1 parent 2d80ced commit f183308
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/typeid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def initialize(
raise Error, "prefix must be lowercase ASCII characters" unless prefix.match?(/^[a-z]*$/)
raise Error, "suffix must be #{SUFFIX_LENGTH} characters" unless suffix.length == SUFFIX_LENGTH
raise Error, "suffix must only contain the letters in '#{TypeID::UUID::Base32::ALPHABET}'" unless suffix.chars.all? { |char| TypeID::UUID::Base32::ALPHABET.include?(char) }
raise Error, "suffix must start with a 0-7 digit to avoid overflows" unless ("0".."7").cover?(suffix.chars.first)

@prefix = prefix
@suffix = suffix
Expand Down
9 changes: 7 additions & 2 deletions spec/invalid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Each example contains an invalid TypeID string. Implementations are expected
# to throw an error when attempting to parse/validate these strings.
#
# Last updated: 2023-06-29
# Last updated: 2023-07-05

- name: prefix-uppercase
typeid: "PREFIX_00000000000000000000000000"
Expand All @@ -31,7 +31,7 @@
description: "The prefix can't have any spaces"

- name: prefix-64-chars
# 123456789 123456789 123456789 123456789 123456789 123456789 1234
# 123456789 123456789 123456789 123456789 123456789 123456789 1234
typeid: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl_00000000000000000000000000"
description: "The prefix can't be 64 characters, it needs to be 63 characters or less"

Expand Down Expand Up @@ -81,3 +81,8 @@
# This example would be valid if we were using the crockford hyphenation rules
typeid: "prefix_123456789-0123456789-0123456"
description: "The suffix can't ignore hyphens as in the crockford encoding"

- name: suffix-overflow
# This is the first suffix that overflows into 129 bits
typeid: "prefix_8zzzzzzzzzzzzzzzzzzzzzzzzz"
description: "The should encode at most 128-bits"
6 changes: 6 additions & 0 deletions spec/lib/typeid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
it { is_expected.to eq string }
end

describe ".nil" do
subject(:type_id) { TypeID.nil }

it { is_expected.to eq "00000000000000000000000000" }
end

describe "#string" do
it { is_expected.to eq "#{prefix}_#{suffix}" }
end
Expand Down
9 changes: 7 additions & 2 deletions spec/valid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
# decoding and re-encoding the id, the result is the same as the original.
#
# In other words, the following property should always hold:
# random_typeid: encode(decode(random_typeid))
# random_typeid == encode(decode(random_typeid))
#
# Finally, while implementations should be able to decode the values below,
# note that not all of them are UUIDv7s. When *generating* new random typeids,
# implementations should always use UUIDv7s.
#
# Last updated: 2023-06-29
# Last updated: 2023-07-05

- name: nil
typeid: "00000000000000000000000000"
Expand All @@ -50,6 +50,11 @@
prefix: ""
uuid: "00000000-0000-0000-0000-000000000020"

- name: max-valid
typeid: "7zzzzzzzzzzzzzzzzzzzzzzzzz"
prefix: ""
uuid: "ffffffff-ffff-ffff-ffff-ffffffffffff"

- name: valid-alphabet
typeid: "prefix_0123456789abcdefghjkmnpqrs"
prefix: "prefix"
Expand Down

0 comments on commit f183308

Please sign in to comment.