Skip to content

Commit 3f63fed

Browse files
Spec case updates (#17)
* [create-pull-request] automated change * run main on all pushes * revert * fix specs * bump version * commit `Gemfile.lock` * remove debug lines --------- Co-authored-by: broothie <[email protected]> Co-authored-by: broothie <[email protected]>
1 parent cf94855 commit 3f63fed

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
typeid (0.2.1)
4+
typeid (0.2.2)
55
uuid7 (~> 0.2.0)
66

77
GEM

lib/typeid.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class Error < StandardError; end
3232
# @param string [String] string representation of a +TypeID+
3333
# @return [TypeID]
3434
def self.from_string(string)
35-
case string.split("_")
36-
in [suffix]
35+
case string.rpartition("_")
36+
in ["", "", suffix]
3737
from("", suffix)
3838

39-
in [prefix, suffix]
39+
in [prefix, "_", suffix]
4040
raise Error, "prefix cannot be empty when there's a separator" if prefix.empty?
4141

4242
from(prefix, suffix)
@@ -83,7 +83,8 @@ def initialize(
8383
suffix: TypeID::UUID.generate(timestamp: timestamp).base32
8484
)
8585
raise Error, "prefix length cannot be greater than #{MAX_PREFIX_LENGTH}" if prefix.length > MAX_PREFIX_LENGTH
86-
raise Error, "prefix must be lowercase ASCII characters" unless prefix.match?(/^[a-z]*$/)
86+
raise Error, "prefix must be lowercase ASCII characters" unless prefix.match?(/^[a-z_]*$/)
87+
raise Error, "prefix cannot start or end with an underscore" if prefix.start_with?("_") || prefix.end_with?("_")
8788
raise Error, "suffix must be #{TypeID::UUID::Base32::ENCODED_STRING_LENGTH} characters" unless suffix.length == TypeID::UUID::Base32::ENCODED_STRING_LENGTH
8889
raise Error, "suffix must only contain the letters in '#{TypeID::UUID::Base32::ALPHABET}'" unless suffix.chars.all? { |char| TypeID::UUID::Base32::ALPHABET.include?(char) }
8990
raise Error, "suffix must start with a 0-7 digit to avoid overflows" unless ("0".."7").cover?(suffix.chars.first)

lib/typeid/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class TypeID < String
2-
VERSION = "0.2.1".freeze
2+
VERSION = "0.2.2".freeze
33
end

spec/invalid.yml

+15-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Each example contains an invalid TypeID string. Implementations are expected
55
# to throw an error when attempting to parse/validate these strings.
66
#
7-
# Last updated: 2023-07-05
7+
# Last updated: 2024-04-10 (for version 0.3.0 of the spec)
88

99
- name: prefix-uppercase
1010
typeid: "PREFIX_00000000000000000000000000"
@@ -18,9 +18,10 @@
1818
typeid: "pre.fix_00000000000000000000000000"
1919
description: "The prefix can't have symbols, it needs to be alphabetic"
2020

21-
- name: prefix-underscore
22-
typeid: "pre_fix_00000000000000000000000000"
23-
description: "The prefix can't have symbols, it needs to be alphabetic"
21+
# Test removed in v0.3.0 – we now allow underscores in the prefix
22+
# - name: prefix-underscore
23+
# typeid: "pre_fix_00000000000000000000000000"
24+
# description: "The prefix can't have symbols, it needs to be alphabetic"
2425

2526
- name: prefix-non-ascii
2627
typeid: "préfix_00000000000000000000000000"
@@ -86,3 +87,13 @@
8687
# This is the first suffix that overflows into 129 bits
8788
typeid: "prefix_8zzzzzzzzzzzzzzzzzzzzzzzzz"
8889
description: "The suffix should encode at most 128-bits"
90+
91+
# Tests below were added in v0.3.0 when we started allowing '_' within the
92+
# type prefix.
93+
- name: prefix-underscore-start
94+
typeid: "_prefix_00000000000000000000000000"
95+
description: "The prefix can't start with an underscore"
96+
97+
- name: prefix-underscore-end
98+
typeid: "prefix__00000000000000000000000000"
99+
description: "The prefix can't end with an underscore"

spec/valid.yml

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# note that not all of them are UUIDv7s. When *generating* new random typeids,
2424
# implementations should always use UUIDv7s.
2525
#
26-
# Last updated: 2023-07-05
26+
# Last updated: 2024-04-10 (for version 0.3.0 of the spec)
2727

2828
- name: nil
2929
typeid: "00000000000000000000000000"
@@ -64,3 +64,10 @@
6464
typeid: "prefix_01h455vb4pex5vsknk084sn02q"
6565
prefix: "prefix"
6666
uuid: "01890a5d-ac96-774b-bcce-b302099a8057"
67+
68+
# Tests below were added in v0.3.0 when we started allowing '_' within the
69+
# type prefix.
70+
- name: prefix-underscore
71+
typeid: "pre_fix_00000000000000000000000000"
72+
prefix: "pre_fix"
73+
uuid: "00000000-0000-0000-0000-000000000000"

0 commit comments

Comments
 (0)