rspec-hash_matchers 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a77222bcc3f5c23033cf02c2cccf606fe7c8a7dc
|
4
|
+
data.tar.gz: 33e3b8b32d907ed44d8422928489c2354a591cff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f8445aaa7510ecb5f87c04fb5f3f3d0f986a373c5da8995bda392493dbdef5a7e10fe8fb9fc342849490456aada33b1b15a5fc694d322f59c33f80d36d5fbe2
|
7
|
+
data.tar.gz: 78742e3779c7174f6ceeb586e2c3fc7655e6ff34245bbdcded0b79b72c42e0683d5adc41659c86c2cd32d31f601b33d186b7a642495d0722131d1766e07b3b69
|
data/README.md
CHANGED
@@ -50,7 +50,36 @@ specify do
|
|
50
50
|
end
|
51
51
|
```
|
52
52
|
|
53
|
-
See [the spec](spec/rspec/hash_matchers/
|
53
|
+
See [the spec](spec/rspec/hash_matchers/contain_exactly_keys_spec.rb) for all
|
54
|
+
examples of matches and mismatches.
|
55
|
+
|
56
|
+
### Include keys (in array)
|
57
|
+
|
58
|
+
Like RSpec's `include`, but for hash and array of hashes.
|
59
|
+
|
60
|
+
* Order never matters
|
61
|
+
* Allows any mix of keys and keys with values (make sure you put the keys with
|
62
|
+
values last)
|
63
|
+
* Must contain exact array entries, but allows additional hash keys
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
specify do
|
67
|
+
# Hash
|
68
|
+
expect(a: 1, b: 2, c: 3, d: 4).to include_keys :d, c: 3
|
69
|
+
# Array of hashes
|
70
|
+
expect([
|
71
|
+
{ a: 1, b: 2, c: 3 },
|
72
|
+
{ d: 4, e: 5, f: 6 },
|
73
|
+
{ g: 7, h: 8, i: 9 },
|
74
|
+
]).to include_keys_in_array(
|
75
|
+
[f: 6], # second line
|
76
|
+
[:a, :c], # first line
|
77
|
+
{ i: 9, h: 8 }, # third line
|
78
|
+
)
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
See [the spec](spec/rspec/hash_matchers/include_keys_spec.rb) for all
|
54
83
|
examples of matches and mismatches.
|
55
84
|
|
56
85
|
## Development
|
data/lib/rspec/hash_matchers.rb
CHANGED
@@ -2,7 +2,7 @@ require 'active_support'
|
|
2
2
|
|
3
3
|
module RSpec
|
4
4
|
module HashMatchers
|
5
|
-
module
|
5
|
+
module ContainExactlyKeys
|
6
6
|
extend RSpec::Matchers::DSL
|
7
7
|
|
8
8
|
matcher :contain_exactly_keys do |*expected|
|
@@ -44,5 +44,5 @@ module RSpec
|
|
44
44
|
end
|
45
45
|
|
46
46
|
RSpec.configure do |config|
|
47
|
-
config.include RSpec::HashMatchers::
|
47
|
+
config.include RSpec::HashMatchers::ContainExactlyKeys
|
48
48
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module HashMatchers
|
5
|
+
module IncludeKeys
|
6
|
+
extend RSpec::Matchers::DSL
|
7
|
+
|
8
|
+
matcher :include_keys do |*expected|
|
9
|
+
match do |actual|
|
10
|
+
hash_matchers_include_keys?(actual, expected)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
matcher :include_keys_in_array do |*expected|
|
15
|
+
match do |actual|
|
16
|
+
next false if not actual.is_a?(Array)
|
17
|
+
next false if actual.length != expected.length
|
18
|
+
actual = actual.clone
|
19
|
+
expected = expected.clone
|
20
|
+
result = true
|
21
|
+
while expected.any?
|
22
|
+
e = expected.shift
|
23
|
+
i = actual.find_index { |a| hash_matchers_include_keys?(a, e) }
|
24
|
+
break result = false if i.nil?
|
25
|
+
actual.delete_at(i)
|
26
|
+
end
|
27
|
+
result
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def hash_matchers_include_keys?(actual, expected)
|
32
|
+
return false if not actual.is_a?(Hash)
|
33
|
+
|
34
|
+
array = expected.is_a?(Hash) ? [expected] : expected.clone
|
35
|
+
hash = array.extract_options!
|
36
|
+
|
37
|
+
return false if array.any? { |k| not actual.include?(k) }
|
38
|
+
return false if hash.any? { |k, v| actual[k] != v }
|
39
|
+
true
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
RSpec.configure do |config|
|
46
|
+
config.include RSpec::HashMatchers::IncludeKeys
|
47
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-hash_matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oded Niv
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-expectations
|
@@ -98,7 +98,8 @@ files:
|
|
98
98
|
- bin/console
|
99
99
|
- bin/setup
|
100
100
|
- lib/rspec/hash_matchers.rb
|
101
|
-
- lib/rspec/hash_matchers/
|
101
|
+
- lib/rspec/hash_matchers/contain_exactly_keys.rb
|
102
|
+
- lib/rspec/hash_matchers/include_keys.rb
|
102
103
|
- lib/rspec/hash_matchers/version.rb
|
103
104
|
- rspec-hash_matchers.gemspec
|
104
105
|
homepage: https://github.com/odedniv/rspec-hash_matchers
|
@@ -121,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
122
|
version: '0'
|
122
123
|
requirements: []
|
123
124
|
rubyforge_project:
|
124
|
-
rubygems_version: 2.
|
125
|
+
rubygems_version: 2.6.11
|
125
126
|
signing_key:
|
126
127
|
specification_version: 4
|
127
128
|
summary: Better matchers for hashes.
|