deep_matching 0.1.0.pre.alpha.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +38 -0
- data/Rakefile +8 -0
- data/lib/deep_matching/deep_matcher.rb +72 -0
- data/lib/deep_matching/nested_expectation_parameters.rb +6 -0
- data/lib/deep_matching/version.rb +5 -0
- data/lib/deep_matching.rb +74 -0
- data/sig/deep_matching.rbs +4 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a1c4618fd1c4b6e162e57faeeae9097d2b2bce2a6c1fa729aa2fa1b404030e2f
|
4
|
+
data.tar.gz: 9f1f06687ec0224054cf6fba236c376cdd2b592e67e74a21adf18c9c10b3f452
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2ec4b92888949bd63f4a3516ba19b9ecc794676bb360b8fd17eaeb7bcfc52455032db482a91b2c8acc40d34d12f4fceef1d85f6782765c96995876fead140b9e
|
7
|
+
data.tar.gz: 802c4767423fe761460b413fa0e78d393c301743b6c73f84b94a3c98538d086f9836f1ee61beebe7c87efe40adb4ed32a7c21cc085d64bae2707237592c7f4a8
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -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.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# DeepMatching
|
2
|
+
|
3
|
+
This allows you to get detailed error messages on exactly where your heavily nested hashes differ
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
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.
|
8
|
+
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
10
|
+
|
11
|
+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
12
|
+
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
|
+
|
15
|
+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require 'deep_matching'
|
21
|
+
RSpec.configure do |config|
|
22
|
+
config.include DeepMatching
|
23
|
+
end
|
24
|
+
```
|
25
|
+
|
26
|
+
## Development
|
27
|
+
|
28
|
+
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.
|
29
|
+
|
30
|
+
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).
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/deep_matching.
|
35
|
+
|
36
|
+
## License
|
37
|
+
|
38
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
|
3
|
+
module DeepMatching
|
4
|
+
class DeepMatcher
|
5
|
+
include DeepMatching
|
6
|
+
include ActiveModel::Model
|
7
|
+
attr_accessor :nested_expectation_parameters, :actual_object, :expected_object
|
8
|
+
|
9
|
+
delegate :evaluation_context, :nesting, :ignore_list, to: :nested_expectation_parameters
|
10
|
+
|
11
|
+
def matches?
|
12
|
+
return true if ignore?
|
13
|
+
|
14
|
+
case expected_object
|
15
|
+
when Array
|
16
|
+
array_matches?
|
17
|
+
when Hash
|
18
|
+
hash_matches?
|
19
|
+
else
|
20
|
+
false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def array_matches?
|
25
|
+
within_example do |actual_object, expected_object, nested_expectation_parameters|
|
26
|
+
expected_object.each_with_index do |expected_value, index|
|
27
|
+
child_level_expect(actual_object, index, expected_value, nested_expectation_parameters)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def hash_matches?
|
33
|
+
within_example do |actual_object, expected_object, nested_expectation_parameters|
|
34
|
+
expected_object.each do |key, expected_value|
|
35
|
+
child_level_expect(actual_object, key, expected_value, nested_expectation_parameters)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_indifferent(hash)
|
41
|
+
hash.respond_to?(:with_indifferent_access) ? hash.with_indifferent_access : hash
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def child_level_expect(actual_object, key, expected_value, nested_expectation_parameters)
|
47
|
+
nested = nested_expectation_parameters.dup
|
48
|
+
nested.nesting += [key]
|
49
|
+
|
50
|
+
expect_deep_matching(actual_object&.try(:[], key), expected_value, nested_expectation_parameters: nested)
|
51
|
+
end
|
52
|
+
|
53
|
+
# this exists so we can make assertions within the example
|
54
|
+
# but have the benefits of using a class
|
55
|
+
def within_example
|
56
|
+
# this is so we have access to the current self from within
|
57
|
+
# the instance_eval
|
58
|
+
this = self
|
59
|
+
|
60
|
+
evaluation_context.instance_eval do
|
61
|
+
actual_object = this.to_indifferent(this.actual_object)
|
62
|
+
expected_object = this.to_indifferent(this.expected_object)
|
63
|
+
|
64
|
+
yield actual_object, expected_object, this.nested_expectation_parameters
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def ignore?
|
69
|
+
nesting.in?(ignore_list)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -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(actual_object, expected_object, 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
|
+
actual_object, expected_object,
|
23
|
+
nested_expectation_parameters: nested_expectation_parameters
|
24
|
+
)
|
25
|
+
|
26
|
+
failure_message = deep_matching_failure_message_for(
|
27
|
+
actual_object,
|
28
|
+
nested_expectation_parameters.nesting,
|
29
|
+
nested_expectation_parameters.failure_message_extra_context
|
30
|
+
)
|
31
|
+
|
32
|
+
make_assertion_about!(actual_object, expected_object, failure_message, nested_expectation_parameters.evaluation_context)
|
33
|
+
end
|
34
|
+
|
35
|
+
def deep_matches?(actual_object, expected_object, nested_expectation_parameters:)
|
36
|
+
DeepMatcher.new(
|
37
|
+
actual_object:,
|
38
|
+
expected_object:,
|
39
|
+
nested_expectation_parameters:
|
40
|
+
).matches?
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def make_assertion_about!(actual_object, expected_object, failure_message, evaluation_context)
|
46
|
+
evaluation_context.instance_eval do
|
47
|
+
case expected_object
|
48
|
+
when RSpec::Mocks::ArgumentMatchers::KindOf
|
49
|
+
make_assertion_about_kind_of(actual_object, expected_object, failure_message)
|
50
|
+
when Regexp
|
51
|
+
expect(actual_object).to match(expected_object), failure_message.call(:match, expected_object)
|
52
|
+
else
|
53
|
+
expect(actual_object).to eq(expected_object), failure_message.call(:eq, expected_object)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def make_assertion_about_kind_of(actual_object, expected, _failured_message)
|
59
|
+
expected = expected.instance_eval { @klass }
|
60
|
+
expect(actual_object).to be_a(expected), failure_message.call(:be_a, expected)
|
61
|
+
end
|
62
|
+
|
63
|
+
def deep_matching_failure_message_for(actual_object, 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
|
+
actual_object.inspect].compact.join("\n")
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deep_matching
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.pre.alpha.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mark Burns
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-12-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-mocks
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: A method to use in RSpec to spot hash differences
|
84
|
+
email:
|
85
|
+
- markburns@users.noreply.github.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".rspec"
|
91
|
+
- CHANGELOG.md
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/deep_matching.rb
|
96
|
+
- lib/deep_matching/deep_matcher.rb
|
97
|
+
- lib/deep_matching/nested_expectation_parameters.rb
|
98
|
+
- lib/deep_matching/version.rb
|
99
|
+
- sig/deep_matching.rbs
|
100
|
+
homepage: https://github.com/markburns/deep_matching
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata:
|
104
|
+
allowed_push_host: https://rubygems.org
|
105
|
+
homepage_uri: https://github.com/markburns/deep_matching
|
106
|
+
source_code_uri: https://github.com/markburns/deep_matching
|
107
|
+
changelog_uri: https://github.com/markburns/deep_matching/blob/main/CHANGELOG.md
|
108
|
+
rubygems_mfa_required: 'true'
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.2'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">"
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: 1.3.1
|
123
|
+
requirements: []
|
124
|
+
rubygems_version: 3.4.19
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Find exactly where your two large nested hashes differ
|
128
|
+
test_files: []
|