rspec-deep-ignore-order-matcher 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
18
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alexey Mogilnikov
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.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # RSpec Deep Matcher
2
+
3
+ This gem adds a custom matcher to RSpec to recursively compare nested Ruby data-structures consisting of `Hash` and `Array` elements.
4
+ An order of elements in an array is ignored.
5
+
6
+ ## Install
7
+ ```
8
+ sudo gem install rspec-deep-ignore-order-matcher
9
+ ```
10
+ or add to your `Gemfile`
11
+ ```
12
+ gem 'rspec-deep-ignore-order-matcher'
13
+ ```
14
+
15
+ ## Example
16
+ ```
17
+ describe 'Products' do
18
+ it "should ignore order of product's tags" do
19
+ expected = [{ :product => { :title => 'Product 1', :tags => ['large', 'blue', 'heavy'] } }]
20
+ actual = [{ :product => { :title => 'Product 1', :tags => ['blue', 'large', 'heavy'] } }]
21
+ actual.should be_deep_equal expected
22
+ end
23
+ end
24
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,40 @@
1
+ require 'rspec'
2
+ require 'rspec-deep-ignore-order-matcher/version'
3
+
4
+ RSpec::Matchers.define :be_deep_equal do |expected|
5
+ match { |actual| m1 actual, expected }
6
+
7
+ failure_message_for_should do |actual|
8
+ "expected that #{actual} would be deep equal with #{expected}"
9
+ end
10
+
11
+ failure_message_for_should_not do |actual|
12
+ "expected that #{actual} would not be deep equal with #{expected}"
13
+ end
14
+
15
+ description do
16
+ "be deep equal with #{expected}"
17
+ end
18
+
19
+ def m1(actual, expected)
20
+ return arrays_matches?(actual, expected) if expected.is_a?(Array) && actual.is_a?(Array)
21
+ return hashes_matches?(actual, expected) if expected.is_a?(Hash) && actual.is_a?(Hash)
22
+ expected == actual
23
+ end
24
+
25
+ def arrays_matches?(actual, expected)
26
+ exp = expected.clone
27
+ actual.each do |a|
28
+ index = exp.find_index { |e| m1 a, e }
29
+ return false if index.nil?
30
+ exp.delete_at(index)
31
+ end
32
+ exp.length == 0
33
+ end
34
+
35
+ def hashes_matches?(actual, expected)
36
+ return false unless actual.keys == expected.keys
37
+ actual.each { |key, value| return false unless m1 value, expected[key] }
38
+ true
39
+ end
40
+ end
@@ -0,0 +1,9 @@
1
+ module Deep
2
+ module Ignore
3
+ module Order
4
+ module Matcher
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rspec-deep-ignore-order-matcher/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rspec-deep-ignore-order-matcher"
8
+ gem.version = Deep::Ignore::Order::Matcher::VERSION
9
+ gem.authors = ["Alexey Mogilnikov"]
10
+ gem.email = ["alexey@mogilnikov.name"]
11
+ gem.description = %q{This gem adds a custom matcher to RSpec to recursively compare nested Ruby data-structures consisting of `Hash` and `Array` elements. An order of elements in an array is ignored.}
12
+ gem.summary = %q{A custom matcher to RSpec to recursively compare nested Ruby data-structures consisting of `Hash` and `Array` elements.}
13
+ gem.homepage = 'https://github.com/amogil/rspec-deep-ignore-order-matcher'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'rspec', '~> 2.6'
21
+ gem.add_development_dependency 'simplecov'
22
+ end
@@ -0,0 +1,36 @@
1
+ require 'rspec'
2
+ require 'rspec-deep-ignore-order-matcher'
3
+
4
+ describe Deep::Ignore::Order::Matcher do
5
+
6
+ it 'should matches usual values' do
7
+ ['an_string', 1, 13.5, nil, [1, 2, 3], { :a => 1, :b => 2 }].each_slice(2) do |value1, value2|
8
+ value1.should be_deep_equal value1
9
+ value2.should be_deep_equal value2
10
+ value1.should_not be_deep_equal value2
11
+ value2.should_not be_deep_equal value1
12
+ end
13
+ end
14
+
15
+ it 'should ignore order in plain arrays' do
16
+ actual = Array.new(5) { Random.rand(1000) }
17
+ expected = actual.sort
18
+ actual.should be_deep_equal expected
19
+ end
20
+
21
+ it 'should match deep structs' do
22
+ actual = [{ :a => 1, :b => 'str', :c => [1, 2, 3] }, [{ :a => [2, { :a => 4 }] }, { :b => 2 }, { :c => 3 }]]
23
+ expected = [{ :a => 1, :b => 'str', :c => [3, 1, 2] }, [{ :b => 2 }, { :a => [{ :a => 4 }, 2] }, { :c => 3 }]]
24
+ actual.should be_deep_equal expected
25
+ actual[0][:c].push(4)
26
+ actual.should_not be_deep_equal expected
27
+ end
28
+
29
+ it 'should do not match partials' do
30
+ [1, 2, 3].should_not be_deep_equal [1, 2]
31
+ [1, 2].should_not be_deep_equal [1, 2, 3]
32
+ { :a => 1, :b => 2 }.should_not be_deep_equal({ :a => 1 })
33
+ { :a => 1 }.should_not be_deep_equal({ :a => 1, :b => 2 })
34
+ end
35
+
36
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-deep-ignore-order-matcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexey Mogilnikov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.6'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.6'
30
+ - !ruby/object:Gem::Dependency
31
+ name: simplecov
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: This gem adds a custom matcher to RSpec to recursively compare nested
47
+ Ruby data-structures consisting of `Hash` and `Array` elements. An order of elements
48
+ in an array is ignored.
49
+ email:
50
+ - alexey@mogilnikov.name
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - Gemfile
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - lib/rspec-deep-ignore-order-matcher.rb
61
+ - lib/rspec-deep-ignore-order-matcher/version.rb
62
+ - rspec-deep-ignore-order-matcher.gemspec
63
+ - spec/matcher_spec.rb
64
+ homepage: https://github.com/amogil/rspec-deep-ignore-order-matcher
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.24
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: A custom matcher to RSpec to recursively compare nested Ruby data-structures
88
+ consisting of `Hash` and `Array` elements.
89
+ test_files:
90
+ - spec/matcher_spec.rb