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: ea2a03da6e3912a91d08f5fc6108f17f0dcffac7
4
- data.tar.gz: 557ec4129a587ac8e6f5341e1cdff7e41485d49b
3
+ metadata.gz: a77222bcc3f5c23033cf02c2cccf606fe7c8a7dc
4
+ data.tar.gz: 33e3b8b32d907ed44d8422928489c2354a591cff
5
5
  SHA512:
6
- metadata.gz: 3df47e60b9de66c56e1c6d943c6271fde23006e1522a3ace80ff8a95a5f9a0d15008f3a5ebfac23ca3149ea697e602339f4a0f333bf4630e1b64c24fbf71f1d7
7
- data.tar.gz: 767c7411e29f503fc10230bb689a67bc6343171c3a41e946f5f03e580ac54f9c0063cf17e321b668ca8422371a161abeedb1e6dd6d1fab0dcf5315a32a1932a6
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/contain_exactly_spec.rb) for all
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
@@ -1,2 +1,3 @@
1
1
  require "rspec/hash_matchers/version"
2
- require "rspec/hash_matchers/contain_exactly"
2
+ require "rspec/hash_matchers/contain_exactly_keys"
3
+ require "rspec/hash_matchers/include_keys"
@@ -2,7 +2,7 @@ require 'active_support'
2
2
 
3
3
  module RSpec
4
4
  module HashMatchers
5
- module ContainExactly
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::ContainExactly
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
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module HashMatchers
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  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.1.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: 2015-06-30 00:00:00.000000000 Z
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/contain_exactly.rb
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.4.7
125
+ rubygems_version: 2.6.11
125
126
  signing_key:
126
127
  specification_version: 4
127
128
  summary: Better matchers for hashes.