rspec-partial-hash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c131a7b2ae65600e2e60e52b77a1a01efeb9b1a7
4
+ data.tar.gz: a20245f73ed886f5956ea392221dcae3394c1133
5
+ SHA512:
6
+ metadata.gz: a59e350458f773dbf192f8862f40db5c6f73beb5de8e8d5e888dd8ac700d0c5ffcdd74dce566330b7aae1a9656847ec82af513ab9eccb786b1e1a2972848876e
7
+ data.tar.gz: 8b6444c7fdb68dd25f9c60190a18cf81347115dc73480151e95944e387bc3e15814c9bb4f92aa5cbb98a6b126811d53f13852849c33c7a53e2dc7167e3e43495
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rspec-partial-hash.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alexander Staubo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ # Partial hash matcher for RSpec
2
+
3
+ This will match partial, arbitrarily nested hashes:
4
+
5
+ {
6
+ 'a' => 'foo', 'b' => {
7
+ 'c' => 42,
8
+ 'd' => {
9
+ 'e' => 'bar'
10
+ }
11
+ }
12
+ }.should include_partial_hash(
13
+ {
14
+ 'b' => {
15
+ 'd' => {
16
+ 'e' => 'bar'
17
+ }
18
+ }
19
+ }
20
+ )
21
+
22
+ In other words, it compares each level of the actual and expected hashes and ensures that the actual hash contains at least the keys and (non-hash) values contained in the expected hash level.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ require 'active_support/core_ext/hash/slice'
2
+
3
+ PartialHashMatcher = Class.new {
4
+ def self.partial_match?(expected, actual)
5
+ actual_slice = actual.slice(*expected.keys)
6
+ if actual_slice.keys == expected.keys
7
+ actual_slice.each do |key, value|
8
+ if value.respond_to?(:to_h) or value.is_a?(Hash)
9
+ return partial_match?(expected[key], value)
10
+ else
11
+ return false unless value == expected[key]
12
+ end
13
+ end
14
+ true
15
+ else
16
+ false
17
+ end
18
+ end
19
+ }
20
+
21
+ RSpec::Matchers.define :include_partial_hash do |expected|
22
+
23
+ match do |actual|
24
+ raise ArgumentError, "Expectation not a hash" unless
25
+ expected.respond_to?(:to_h) or expected.is_a?(Hash)
26
+ !actual.nil? && !expected.nil? && PartialHashMatcher.partial_match?(expected, actual)
27
+ end
28
+
29
+ end
@@ -0,0 +1,5 @@
1
+ module Rspec
2
+ module PartialHash
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rspec-partial-hash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rspec-partial-hash"
8
+ spec.version = Rspec::PartialHash::VERSION
9
+ spec.authors = ["Alexander Staubo"]
10
+ spec.email = ["alex@bengler.no"]
11
+ spec.summary =
12
+ spec.description = %q{Partial hash matching for RSpec.}
13
+ spec.homepage = "https://github.com/atombender/rspec-partial-hash"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "activesupport", '>= 3.0'
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,116 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#include_partial_hash' do
4
+
5
+ it 'accepts empty hash' do
6
+ {}.should include_partial_hash({})
7
+ end
8
+
9
+ it 'accepts identical hash' do
10
+ {
11
+ 'a' => {
12
+ 'b' => 42
13
+ }
14
+ }.should include_partial_hash(
15
+ {
16
+ 'a' => {
17
+ 'b' => 42
18
+ }
19
+ }
20
+ )
21
+ end
22
+
23
+ it 'accepts nested hash with some partial matches' do
24
+ {
25
+ 'a' => 'foo',
26
+ 'b' => {
27
+ 'c' => 42,
28
+ 'd' => {
29
+ 'e' => 'bar'
30
+ }
31
+ }
32
+ }.should include_partial_hash(
33
+ {
34
+ 'b' => {
35
+ 'd' => {
36
+ 'e' => 'bar'
37
+ }
38
+ }
39
+ }
40
+ )
41
+ end
42
+
43
+ it 'accepts empty expectation' do
44
+ {
45
+ 'a' => 'foo',
46
+ 'b' => {
47
+ 'c' => 42,
48
+ 'd' => {
49
+ 'e' => 'bar'
50
+ }
51
+ }
52
+ }.should include_partial_hash(
53
+ {
54
+ }
55
+ )
56
+ end
57
+
58
+ it 'accepts simple, non-nestedt hash' do
59
+ {
60
+ 'a' => 'foo',
61
+ 'b' => {
62
+ 'c' => 42,
63
+ 'd' => {
64
+ 'e' => 'bar'
65
+ }
66
+ }
67
+ }.should include_partial_hash(
68
+ {
69
+ 'a' => 'foo'
70
+ }
71
+ )
72
+ end
73
+
74
+ it 'rejects nil' do
75
+ {}.should_not include_partial_hash(nil)
76
+ end
77
+
78
+ [42, "string", 3.14, ['array']].each do |v|
79
+ it "throws ArgumentError if expectation is a #{v.class.name.downcase}" do
80
+ -> {
81
+ {}.should_not include_partial_hash(v)
82
+ }.should raise_error(ArgumentError)
83
+ end
84
+ end
85
+
86
+ it 'rejects non-matching nested hash' do
87
+ {
88
+ 'a' => 'foo',
89
+ 'b' => {
90
+ 'c' => 42
91
+ }
92
+ }.should_not include_partial_hash(
93
+ {
94
+ 'c' => {
95
+ 'c' => 42
96
+ }
97
+ }
98
+ )
99
+ end
100
+
101
+ it 'rejects non-matching nested key' do
102
+ {
103
+ 'a' => 'foo',
104
+ 'b' => {
105
+ 'c' => 42
106
+ }
107
+ }.should_not include_partial_hash(
108
+ {
109
+ 'b' => {
110
+ 'c' => 41
111
+ }
112
+ }
113
+ )
114
+ end
115
+
116
+ end
@@ -0,0 +1,5 @@
1
+ require 'bundler'
2
+ Bundler.require(:test)
3
+
4
+ require 'rspec'
5
+ require_relative '../lib/rspec-partial-hash/partial_hash'
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-partial-hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Staubo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '3.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: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Partial hash matching for RSpec.
70
+ email:
71
+ - alex@bengler.no
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/rspec-partial-hash/partial_hash.rb
82
+ - lib/rspec-partial-hash/version.rb
83
+ - rspec-partial-hash.gemspec
84
+ - spec/partial_hash_spec.rb
85
+ - spec/spec_helper.rb
86
+ homepage: https://github.com/atombender/rspec-partial-hash
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.0.3
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Partial hash matching for RSpec.
110
+ test_files:
111
+ - spec/partial_hash_spec.rb
112
+ - spec/spec_helper.rb
113
+ has_rdoc: