Skip to content

Commit e8d79ec

Browse files
committed
initial commit
0 parents  commit e8d79ec

19 files changed

+428
-0
lines changed

.github/workflows/main.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Ruby
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
pull_request:
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
name: Ruby ${{ matrix.ruby }}
14+
strategy:
15+
matrix:
16+
ruby:
17+
- '3.2.2'
18+
19+
steps:
20+
- uses: actions/checkout@v3
21+
- name: Set up Ruby
22+
uses: ruby/setup-ruby@v1
23+
with:
24+
ruby-version: ${{ matrix.ruby }}
25+
bundler-cache: true
26+
- name: Run the default task
27+
run: bundle exec rake

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/.bundle/
2+
/.yardoc
3+
/_yardoc/
4+
/coverage/
5+
/doc/
6+
/pkg/
7+
/spec/reports/
8+
/tmp/
9+
10+
# rspec failure tracking
11+
.rspec_status

.rspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--color
3+
--require spec_helper

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [Unreleased]
2+
3+
## [0.1.0] - 2023-12-17
4+
5+
- Initial release

Gemfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
# Specify your gem's dependencies in deep_matching.gemspec
6+
gemspec
7+
8+
gem 'rake', '~> 13.0'
9+
10+
gem 'rspec', '~> 3.0'

Gemfile.lock

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
PATH
2+
remote: .
3+
specs:
4+
deep_matching (0.1.0)
5+
activemodel
6+
rspec
7+
rspec-mocks
8+
9+
GEM
10+
remote: https://rubygems.org/
11+
specs:
12+
activemodel (7.1.2)
13+
activesupport (= 7.1.2)
14+
activesupport (7.1.2)
15+
base64
16+
bigdecimal
17+
concurrent-ruby (~> 1.0, >= 1.0.2)
18+
connection_pool (>= 2.2.5)
19+
drb
20+
i18n (>= 1.6, < 2)
21+
minitest (>= 5.1)
22+
mutex_m
23+
tzinfo (~> 2.0)
24+
base64 (0.2.0)
25+
bigdecimal (3.1.5)
26+
concurrent-ruby (1.2.2)
27+
connection_pool (2.4.1)
28+
diff-lcs (1.5.0)
29+
drb (2.2.0)
30+
ruby2_keywords
31+
i18n (1.14.1)
32+
concurrent-ruby (~> 1.0)
33+
minitest (5.20.0)
34+
mutex_m (0.2.0)
35+
rake (13.1.0)
36+
rspec (3.12.0)
37+
rspec-core (~> 3.12.0)
38+
rspec-expectations (~> 3.12.0)
39+
rspec-mocks (~> 3.12.0)
40+
rspec-core (3.12.2)
41+
rspec-support (~> 3.12.0)
42+
rspec-expectations (3.12.3)
43+
diff-lcs (>= 1.2.0, < 2.0)
44+
rspec-support (~> 3.12.0)
45+
rspec-mocks (3.12.6)
46+
diff-lcs (>= 1.2.0, < 2.0)
47+
rspec-support (~> 3.12.0)
48+
rspec-support (3.12.1)
49+
ruby2_keywords (0.0.5)
50+
tzinfo (2.0.6)
51+
concurrent-ruby (~> 1.0)
52+
53+
PLATFORMS
54+
ruby
55+
56+
DEPENDENCIES
57+
deep_matching!
58+
rake (~> 13.0)
59+
rspec (~> 3.0)
60+
61+
BUNDLED WITH
62+
2.4.19

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 Mark Burns
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# DeepMatching
2+
3+
TODO: Delete this and the text below, and describe your gem
4+
5+
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.
6+
7+
## Installation
8+
9+
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.
10+
11+
Install the gem and add to the application's Gemfile by executing:
12+
13+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14+
15+
If bundler is not being used to manage dependencies, install the gem by executing:
16+
17+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18+
19+
## Usage
20+
21+
TODO: Write usage instructions here
22+
23+
## Development
24+
25+
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.
26+
27+
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).
28+
29+
## Contributing
30+
31+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/deep_matching.
32+
33+
## License
34+
35+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

Rakefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
require "bundler/gem_tasks"
4+
require "rspec/core/rake_task"
5+
6+
RSpec::Core::RakeTask.new(:spec)
7+
8+
task default: :spec

bin/console

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require 'bundler/setup'
5+
require 'deep_matching'
6+
7+
# You can add fixtures and/or initialization code here to make experimenting
8+
# with your gem easier. You can also use a different console, if you like.
9+
10+
require 'irb'
11+
IRB.start(__FILE__)

bin/setup

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
set -vx
5+
6+
bundle install
7+
8+
# Do any other automated setup that you need to do here

deep_matching.gemspec

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'lib/deep_matching/version'
4+
5+
Gem::Specification.new do |spec|
6+
spec.name = 'deep_matching'
7+
spec.version = DeepMatching::VERSION
8+
spec.authors = ['Mark Burns']
9+
spec.email = ['markburns@users.noreply.github.com']
10+
11+
spec.summary = 'Find exactly where your two large nested hashes differ'
12+
spec.description = 'A method to use in RSpec to spot hash differences'
13+
spec.homepage = 'https://github.com/markburns/deep_matching'
14+
spec.license = 'MIT'
15+
spec.required_ruby_version = '>= 3.2'
16+
17+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18+
19+
spec.metadata['homepage_uri'] = spec.homepage
20+
spec.metadata['source_code_uri'] = 'https://github.com/markburns/deep_matching'
21+
spec.metadata['changelog_uri'] = 'https://github.com/markburns/deep_matching/blob/main/CHANGELOG.md'
22+
23+
# Specify which files should be added to the gem when it is released.
24+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25+
spec.files = Dir.chdir(__dir__) do
26+
%x(git ls-files -z).split("\x0").reject do |f|
27+
(File.expand_path(f) == __FILE__) ||
28+
f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
29+
end
30+
end
31+
spec.bindir = 'exe'
32+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33+
spec.require_paths = ['lib']
34+
35+
spec.add_dependency 'activemodel'
36+
spec.add_dependency 'rspec'
37+
spec.add_dependency 'rspec-mocks'
38+
39+
# For more information and examples about making a new gem, check out our
40+
# guide at: https://bundler.io/guides/creating_gem.html
41+
spec.metadata['rubygems_mfa_required'] = 'true'
42+
end

lib/deep_matching.rb

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# frozen_string_literal: true
2+
3+
require 'active_model'
4+
5+
require 'deep_matching/deep_matcher'
6+
require 'deep_matching/nested_expectation_parameters'
7+
8+
module DeepMatching
9+
# I tried writing this as an RSpec matcher, but couldn't easily make it
10+
# find all nested failures with aggregate_failures, rather than just the
11+
# first one
12+
def expect_deep_matching(obj, expected_obj, ignore_list: [], nested_expectation_parameters: nil,
13+
failure_message_extra_context: '')
14+
nested_expectation_parameters ||= NestedExpectationParameters.new(
15+
nesting: [],
16+
ignore_list: ignore_list,
17+
evaluation_context: self,
18+
failure_message_extra_context: failure_message_extra_context
19+
)
20+
21+
return true if deep_matches?(
22+
obj, expected_obj,
23+
nested_expectation_parameters: nested_expectation_parameters
24+
)
25+
26+
failure_message = deep_matching_failure_message_for(
27+
obj,
28+
nested_expectation_parameters.nesting,
29+
nested_expectation_parameters.failure_message_extra_context
30+
)
31+
32+
make_assertion_about!(obj, expected_obj, failure_message, nested_expectation_parameters.evaluation_context)
33+
end
34+
35+
def deep_matches?(obj, expected_obj, nested_expectation_parameters:)
36+
DeepMatcher.new(
37+
obj: obj,
38+
expected_obj: expected_obj,
39+
nested_expectation_parameters: nested_expectation_parameters
40+
).matches?
41+
end
42+
43+
private
44+
45+
def make_assertion_about!(obj, expected_obj, failure_message, evaluation_context)
46+
evaluation_context.instance_eval do
47+
case expected_obj
48+
when RSpec::Mocks::ArgumentMatchers::KindOf
49+
make_assertion_about_kind_of(obj, expected_obj, failure_message)
50+
when Regexp
51+
expect(obj).to match(expected_obj), failure_message.call(:match, expected_obj)
52+
else
53+
expect(obj).to eq(expected_obj), failure_message.call(:eq, expected_obj)
54+
end
55+
end
56+
end
57+
58+
def make_assertion_about_kind_of(obj, expected, _failured_message)
59+
expected = expected.instance_eval { @klass }
60+
expect(obj).to be_a(expected), failure_message.call(:be_a, expected)
61+
end
62+
63+
def deep_matching_failure_message_for(obj, nesting, failure_message_extra_context)
64+
lambda do |comparison, expected|
65+
nesting_message = nesting.length > 1 ? 'Expected nested hash key at' : 'Expected hash key at'
66+
[failure_message_extra_context.presence,
67+
"#{nesting_message} '#{nesting.join('.')}'",
68+
"to #{comparison}",
69+
"#{expected.inspect},",
70+
'but got',
71+
obj.inspect].compact.join("\n")
72+
end
73+
end
74+
end

0 commit comments

Comments
 (0)