Skip to content

Commit

Permalink
.of_version chain
Browse files Browse the repository at this point in the history
  • Loading branch information
dpep committed Jun 9, 2022
1 parent c15bde6 commit d166656
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/rspec-uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

RSpec::Matchers.define :be_a_uuid do |version: nil|
match do |actual|
raise ArgumentError if @version && version

return false unless actual.is_a?(String)

# https://www.uuidtools.com/what-is-uuid
matches = actual.match /^\h{8}-\h{4}-(\h{4})-\h{4}-\h{12}$/
return false unless matches

version ||= @version
if version
# 1st nibble of 3rd section
@actual_version = matches[1].to_i(16) >> 12
Expand All @@ -18,6 +21,10 @@
end
end

chain :of_version do |version|
@version = version
end

description do
version ? "a UUID v#{version}" : "a UUID"
end
Expand Down
19 changes: 19 additions & 0 deletions spec/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@
expect(uuid_v3).to be_a_uuid(version: 4)
}.to fail_including("expected a UUID v4, found a UUID v3")
end

describe ".of_version" do
it { expect(uuid_v3).to be_a_uuid.of_version(3) }

it { expect(uuid_v4).to be_a_uuid.of_version(4) }
it { expect(uuid_v4).not_to be_a_uuid.of_version(3) }

it "requires a version argument" do
expect {
expect(uuid_v4).to be_a_uuid.of_version
}.to raise_error(ArgumentError)
end

it "can not be used simultaneously with kwarg version" do
expect {
expect(uuid_v4).to be_a_uuid(version: 4).of_version(4)
}.to raise_error(ArgumentError)
end
end
end

it "is composable" do
Expand Down

0 comments on commit d166656

Please sign in to comment.