Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
markburns committed Dec 24, 2023
0 parents commit e8d79ec
Show file tree
Hide file tree
Showing 19 changed files with 428 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Ruby

on:
push:
branches:
- main

pull_request:

jobs:
build:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby:
- '3.2.2'

steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run the default task
run: bundle exec rake
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## [Unreleased]

## [0.1.0] - 2023-12-17

- Initial release
10 changes: 10 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in deep_matching.gemspec
gemspec

gem 'rake', '~> 13.0'

gem 'rspec', '~> 3.0'
62 changes: 62 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
PATH
remote: .
specs:
deep_matching (0.1.0)
activemodel
rspec
rspec-mocks

GEM
remote: https://rubygems.org/
specs:
activemodel (7.1.2)
activesupport (= 7.1.2)
activesupport (7.1.2)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
base64 (0.2.0)
bigdecimal (3.1.5)
concurrent-ruby (1.2.2)
connection_pool (2.4.1)
diff-lcs (1.5.0)
drb (2.2.0)
ruby2_keywords
i18n (1.14.1)
concurrent-ruby (~> 1.0)
minitest (5.20.0)
mutex_m (0.2.0)
rake (13.1.0)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
ruby2_keywords (0.0.5)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)

PLATFORMS
ruby

DEPENDENCIES
deep_matching!
rake (~> 13.0)
rspec (~> 3.0)

BUNDLED WITH
2.4.19
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 Mark Burns

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# DeepMatching

TODO: Delete this and the text below, and describe your gem

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/deep_matching`. To experiment with that code, run `bin/console` for an interactive prompt.

## Installation

TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.

Install the gem and add to the application's Gemfile by executing:

$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG

## Usage

TODO: Write usage instructions here

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/deep_matching.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task default: :spec
11 changes: 11 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'deep_matching'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

require 'irb'
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
42 changes: 42 additions & 0 deletions deep_matching.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require_relative 'lib/deep_matching/version'

Gem::Specification.new do |spec|
spec.name = 'deep_matching'
spec.version = DeepMatching::VERSION
spec.authors = ['Mark Burns']
spec.email = ['[email protected]']

spec.summary = 'Find exactly where your two large nested hashes differ'
spec.description = 'A method to use in RSpec to spot hash differences'
spec.homepage = 'https://github.com/markburns/deep_matching'
spec.license = 'MIT'
spec.required_ruby_version = '>= 3.2'

spec.metadata['allowed_push_host'] = 'https://rubygems.org'

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = 'https://github.com/markburns/deep_matching'
spec.metadata['changelog_uri'] = 'https://github.com/markburns/deep_matching/blob/main/CHANGELOG.md'

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(__dir__) do
%x(git ls-files -z).split("\x0").reject do |f|
(File.expand_path(f) == __FILE__) ||
f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
end
end
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency 'activemodel'
spec.add_dependency 'rspec'
spec.add_dependency 'rspec-mocks'

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
spec.metadata['rubygems_mfa_required'] = 'true'
end
74 changes: 74 additions & 0 deletions lib/deep_matching.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

require 'active_model'

require 'deep_matching/deep_matcher'
require 'deep_matching/nested_expectation_parameters'

module DeepMatching
# I tried writing this as an RSpec matcher, but couldn't easily make it
# find all nested failures with aggregate_failures, rather than just the
# first one
def expect_deep_matching(obj, expected_obj, ignore_list: [], nested_expectation_parameters: nil,
failure_message_extra_context: '')
nested_expectation_parameters ||= NestedExpectationParameters.new(
nesting: [],
ignore_list: ignore_list,
evaluation_context: self,
failure_message_extra_context: failure_message_extra_context
)

return true if deep_matches?(
obj, expected_obj,
nested_expectation_parameters: nested_expectation_parameters
)

failure_message = deep_matching_failure_message_for(
obj,
nested_expectation_parameters.nesting,
nested_expectation_parameters.failure_message_extra_context
)

make_assertion_about!(obj, expected_obj, failure_message, nested_expectation_parameters.evaluation_context)
end

def deep_matches?(obj, expected_obj, nested_expectation_parameters:)
DeepMatcher.new(
obj: obj,
expected_obj: expected_obj,
nested_expectation_parameters: nested_expectation_parameters
).matches?
end

private

def make_assertion_about!(obj, expected_obj, failure_message, evaluation_context)
evaluation_context.instance_eval do
case expected_obj
when RSpec::Mocks::ArgumentMatchers::KindOf
make_assertion_about_kind_of(obj, expected_obj, failure_message)
when Regexp
expect(obj).to match(expected_obj), failure_message.call(:match, expected_obj)
else
expect(obj).to eq(expected_obj), failure_message.call(:eq, expected_obj)
end
end
end

def make_assertion_about_kind_of(obj, expected, _failured_message)
expected = expected.instance_eval { @klass }
expect(obj).to be_a(expected), failure_message.call(:be_a, expected)
end

def deep_matching_failure_message_for(obj, nesting, failure_message_extra_context)
lambda do |comparison, expected|
nesting_message = nesting.length > 1 ? 'Expected nested hash key at' : 'Expected hash key at'
[failure_message_extra_context.presence,
"#{nesting_message} '#{nesting.join('.')}'",
"to #{comparison}",
"#{expected.inspect},",
'but got',
obj.inspect].compact.join("\n")
end
end
end
Loading

0 comments on commit e8d79ec

Please sign in to comment.