Skip to content

Commit

Permalink
move to "rspec/uuid"
Browse files Browse the repository at this point in the history
  • Loading branch information
dpep committed Jun 9, 2022
1 parent d166656 commit 73c1d52
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 51 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ rspec-uuid
An RSpec matcher for UUIDs.

```ruby
require "rspec-uuid"
require "rspec/uuid"


it { is_expected.to be_a_uuid }

# check the version
it { is_expected.to be_a_uuid(version: 4) }
it { is_expected.to be_a_uuid.of_version(4) }

# compose with other matchers
it { expect(data).to include(uuid: a_uuid) }
Expand Down
46 changes: 1 addition & 45 deletions lib/rspec-uuid.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1 @@
require "rspec/expectations"

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

version == @actual_version
else
true
end
end

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

description do
version ? "a UUID v#{version}" : "a UUID"
end

failure_message do
if @actual_version
"expected #{description}, found a UUID v#{@actual_version}"
else
"expected #{description}"
end
end

failure_message_when_negated do
"did not expect #{description}"
end
end

RSpec::Matchers.alias_matcher :a_uuid, :be_a_uuid
require "rspec/uuid"
3 changes: 0 additions & 3 deletions lib/rspec-uuid/version.rb

This file was deleted.

45 changes: 45 additions & 0 deletions lib/rspec/uuid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require "rspec/expectations"

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

version == @actual_version
else
true
end
end

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

description do
version ? "a UUID v#{version}" : "a UUID"
end

failure_message do
if @actual_version
"expected #{description}, found a UUID v#{@actual_version}"
else
"expected #{description}"
end
end

failure_message_when_negated do
"did not expect #{description}"
end
end

RSpec::Matchers.alias_matcher :a_uuid, :be_a_uuid
5 changes: 5 additions & 0 deletions lib/rspec/uuid/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module RSpec
module UUID
VERSION = "0.4.0"
end
end
4 changes: 2 additions & 2 deletions rspec-uuid.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package_name = File.basename(__FILE__).split(".")[0]
require File.expand_path("lib/#{package_name}/version", __dir__)
load Dir.glob("lib/**/version.rb")[0]

package = RSpecUUID
package = RSpec::UUID


Gem::Specification.new do |s|
Expand Down

0 comments on commit 73c1d52

Please sign in to comment.