From 73c1d52e5f861e59286ba7aa879befe911048337 Mon Sep 17 00:00:00 2001 From: Daniel Pepper Date: Wed, 8 Jun 2022 17:31:54 -0700 Subject: [PATCH] move to "rspec/uuid" --- README.md | 3 ++- lib/rspec-uuid.rb | 46 +-------------------------------------- lib/rspec-uuid/version.rb | 3 --- lib/rspec/uuid.rb | 45 ++++++++++++++++++++++++++++++++++++++ lib/rspec/uuid/version.rb | 5 +++++ rspec-uuid.gemspec | 4 ++-- 6 files changed, 55 insertions(+), 51 deletions(-) delete mode 100644 lib/rspec-uuid/version.rb create mode 100644 lib/rspec/uuid.rb create mode 100644 lib/rspec/uuid/version.rb diff --git a/README.md b/README.md index 270f538..2a033b8 100644 --- a/README.md +++ b/README.md @@ -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) } diff --git a/lib/rspec-uuid.rb b/lib/rspec-uuid.rb index 6134b20..f48538b 100644 --- a/lib/rspec-uuid.rb +++ b/lib/rspec-uuid.rb @@ -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" diff --git a/lib/rspec-uuid/version.rb b/lib/rspec-uuid/version.rb deleted file mode 100644 index ba55358..0000000 --- a/lib/rspec-uuid/version.rb +++ /dev/null @@ -1,3 +0,0 @@ -module RSpecUUID - VERSION = "0.4.0" -end diff --git a/lib/rspec/uuid.rb b/lib/rspec/uuid.rb new file mode 100644 index 0000000..6134b20 --- /dev/null +++ b/lib/rspec/uuid.rb @@ -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 diff --git a/lib/rspec/uuid/version.rb b/lib/rspec/uuid/version.rb new file mode 100644 index 0000000..6af3a17 --- /dev/null +++ b/lib/rspec/uuid/version.rb @@ -0,0 +1,5 @@ +module RSpec + module UUID + VERSION = "0.4.0" + end +end diff --git a/rspec-uuid.gemspec b/rspec-uuid.gemspec index 8257062..30e5974 100644 --- a/rspec-uuid.gemspec +++ b/rspec-uuid.gemspec @@ -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|