useful_matchers 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MjZhODc2MmI3MTBkODg3MDdjMjhhNmVhYWI1M2FlY2EyNTdiYWMyZQ==
5
+ data.tar.gz: !binary |-
6
+ ODA0NmZkNWM5ODk0YzhjM2I1ZjA0ZjMwMzkzMTZmOTU0ZWNkN2NlYg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YjMyNTRhZWNjNDNjOTUxYzY0N2E1N2I3M2YzOTIwODljZmEwZTBhNDhiZjEx
10
+ ZTY0YWYyM2IxNTdkZDIzNDlkZWYyZGEwOGQwYjc3OTY2YzU5OWFjN2Q5YTY5
11
+ OWZlYWNmMmVkOTJhOWJlYmY5NjdhMDIyY2QzZDBmM2I1MGE4OGE=
12
+ data.tar.gz: !binary |-
13
+ ZDE1MDFhMmY1NDJlMTQ1YTZmMmFiZjQzMTZhMDFlOTI2NmE2ZDQwMzg1Zjlj
14
+ YWNlY2QyNzhmOTkwM2RiOWUzNzYzYTg0ZGI0NGM1NDg3MzJkN2MwZjY3YmEz
15
+ YzFiNGYyZmE1NzMyZTYxYjRhODY3MTYzYTQyYmMxNjQ5MDk4YWU=
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in useful_matchers.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2014 Ersin Akinci, Two Maestros LLC
2
+ Portions copyright (c) 2006-2014, Tammer Saleh, thoughtbot, inc.
3
+
4
+ MIT License
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # UsefulMatchers
2
+
3
+ A useful collection of matchers for RSpec.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'useful_matchers'
10
+
11
+ Or install it yourself as:
12
+
13
+ $ gem install useful_matchers
14
+
15
+ ## Usage
16
+
17
+ Add the following to your spec_helper.rb file:
18
+
19
+ require 'useful_matchers/matchers'
20
+
21
+ Then start using the matchers in your specs.
22
+
23
+ ## Matchers
24
+
25
+ include_key(key)
26
+ include_key(key).with_value(value)
27
+
28
+ TODO
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it ( https://github.com/twomaestros/useful-matchers/fork )
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,2 @@
1
+ require "useful_matchers/version"
2
+ require "useful_matchers/matchers"
@@ -0,0 +1,31 @@
1
+ require 'useful_matchers/matchers/independent'
2
+
3
+ module UsefulMatchers
4
+ module Matchers
5
+ end
6
+ end
7
+
8
+ def recurse_to_leaf_modules(mod)
9
+ leaf_nodes = []
10
+ next_level = mod.constants.collect {
11
+ |c| mod.const_get(c)
12
+ }.select {
13
+ |c| c.instance_of?(Module)
14
+ }.each do |child|
15
+ leaf_nodes.concat recurse_to_leaf_modules child
16
+ end
17
+
18
+ if next_level.empty? && mod.instance_of?(Module)
19
+ return [mod]
20
+ else
21
+ return leaf_nodes
22
+ end
23
+ end
24
+
25
+ if RSpec.respond_to?(:configure)
26
+ RSpec.configure do |config|
27
+ recurse_to_leaf_modules(UsefulMatchers::Matchers).each do |mod|
28
+ config.include mod
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ require 'useful_matchers/matchers/independent/hashes'
2
+
3
+ module UsefulMatchers
4
+ # Matchers that don't depend on Rails
5
+ module Independent
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'useful_matchers/matchers/independent/hashes/include_key_and_value'
2
+
3
+ module UsefulMatchers
4
+ module Matchers
5
+ module Independent
6
+ module Hashes
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,90 @@
1
+ module UsefulMatchers
2
+ module Matchers
3
+ module Independent
4
+ module Hashes
5
+ def include_key(key)
6
+ IncludeKeyAndValueMatcher.new key
7
+ end
8
+
9
+ class IncludeKeyAndValueMatcher
10
+ def initialize(key)
11
+ @key = key
12
+
13
+ @value = nil
14
+ # Use boolean flag since we can't test whether @value is set by testing whether @value is nil
15
+ # because we should be able to evaluate nil values.
16
+ @value_set = false
17
+ @subject = nil
18
+ end
19
+
20
+ def matches?(subject)
21
+ @subject = subject
22
+
23
+ if @value_set
24
+ ensure_subject_has_target_key && ensure_target_key_corresponds_to_target_value
25
+ else
26
+ ensure_subject_has_target_key
27
+ end
28
+ end
29
+
30
+ def with_value(value)
31
+ @value = value
32
+ @value_set = true
33
+ self
34
+ end
35
+
36
+ def failure_message
37
+ if @value_set
38
+ "expected that the value for key #{format_key} in the hash is #{format_value}"
39
+ else
40
+ "expected that the hash contains #{format_key} as a key"
41
+ end
42
+ end
43
+ alias failure_message_for_should failure_message
44
+
45
+ def failure_message_when_negated
46
+ if @value_set
47
+ "expected that the value for key #{format_key} in the hash isn't #{format_value}"
48
+ else
49
+ "expected that the hash doesn't contain #{format_key} as a key"
50
+ end
51
+ end
52
+ alias failure_message_for_should_not failure_message_when_negated
53
+
54
+ private
55
+
56
+ def ensure_subject_has_target_key
57
+ @subject.keys.include?(@key)
58
+ end
59
+
60
+ def ensure_target_key_corresponds_to_target_value
61
+ @subject[@key] == @value
62
+ end
63
+
64
+ def format_key
65
+ format_parameter(@key)
66
+ end
67
+
68
+ def format_value
69
+ format_parameter(@value)
70
+ end
71
+
72
+ def format_parameter(param)
73
+ formatted_param = param
74
+
75
+ case param
76
+ when ''
77
+ formatted_param = 'empty string'
78
+ when nil
79
+ formatted_param = 'nil'
80
+ when Symbol
81
+ formatted_param = ":#{param.to_s}"
82
+ end
83
+
84
+ formatted_param
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,3 @@
1
+ module UsefulMatchers
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe UsefulMatchers::Matchers::Independent::Hashes::IncludeKeyAndValueMatcher do
4
+ let(:matched_hash) { { key => value } }
5
+ let(:partially_mismatched_hash) { { key => 'Adams' } }
6
+ let(:totally_mismatched_hash) { { john: 'Adams' } }
7
+
8
+ context 'when the target key and value are typical' do
9
+ let(:key) { :george }
10
+ let(:value) { 'Washington' }
11
+ let(:key_only_msg) { "expected that the hash contains :george as a key" }
12
+ let(:key_with_value_msg) { "expected that the value for key :george in the hash is Washington" }
13
+ let(:neg_key_only_msg) { "expected that the hash doesn't contain :george as a key" }
14
+ let(:neg_key_with_value_msg) { "expected that the value for key :george in the hash isn't Washington" }
15
+ it_behaves_like 'an IncludeKeyAndValueMatcher'
16
+ end
17
+
18
+ context 'when the target key and value are nils' do
19
+ let(:key) { nil }
20
+ let(:value) { nil }
21
+ let(:key_only_msg) { "expected that the hash contains nil as a key" }
22
+ let(:key_with_value_msg) { "expected that the value for key nil in the hash is nil" }
23
+ let(:neg_key_only_msg) { "expected that the hash doesn't contain nil as a key" }
24
+ let(:neg_key_with_value_msg) { "expected that the value for key nil in the hash isn't nil" }
25
+ it_behaves_like 'an IncludeKeyAndValueMatcher'
26
+ end
27
+
28
+ context 'when the target key and value are empty strings' do
29
+ let(:key) { '' }
30
+ let(:value) { '' }
31
+ let(:key_only_msg) { "expected that the hash contains empty string as a key" }
32
+ let(:key_with_value_msg) { "expected that the value for key empty string in the hash is empty string" }
33
+ let(:neg_key_only_msg) { "expected that the hash doesn't contain empty string as a key" }
34
+ let(:neg_key_with_value_msg) { "expected that the value for key empty string in the hash isn't empty string" }
35
+ it_behaves_like 'an IncludeKeyAndValueMatcher'
36
+ end
37
+ end
@@ -0,0 +1,11 @@
1
+ PROJECT_ROOT = File.expand_path('../..', __FILE__)
2
+ Dir[ File.join(PROJECT_ROOT, 'spec/support/**/*.rb') ].each { |file| require file }
3
+
4
+ require 'useful_matchers/matchers'
5
+
6
+ RSpec.configure do |config|
7
+ config.filter_run :focus => true
8
+ config.run_all_when_everything_filtered = true
9
+ config.order = "random"
10
+ config.color = true
11
+ end
@@ -0,0 +1,73 @@
1
+ shared_examples "an IncludeKeyAndValueMatcher" do
2
+ context 'when evaluating a matched hash (target key and target value are present)' do
3
+ context 'when positiviely evaluating' do
4
+ it 'succeeds (in matching just the key)' do
5
+ expect(matched_hash).to include_key(key)
6
+ end
7
+ it 'succeeds (in matching both the key and the value)' do
8
+ expect(matched_hash).to include_key(key).with_value(value)
9
+ end
10
+ end
11
+
12
+ context 'when negatively evaluating' do
13
+ it 'fails (in matching just the key)' do
14
+ expect {
15
+ expect(matched_hash).to_not include_key(key)
16
+ }.to fail_with_message(neg_key_only_msg)
17
+ end
18
+ it 'fails (in matching both the key and the value)' do
19
+ expect {
20
+ expect(matched_hash).to_not include_key(key).with_value(value)
21
+ }.to fail_with_message(neg_key_with_value_msg)
22
+ end
23
+ end
24
+ end
25
+
26
+ context 'when evaluating a partially mismatched hash (only target key is present)' do
27
+ context 'when positiviely evaluating' do
28
+ it 'succeeds (in matching just the key)' do
29
+ expect(partially_mismatched_hash).to include_key(key)
30
+ end
31
+ it 'fails (in matching both the key and the value)' do
32
+ expect {
33
+ expect(partially_mismatched_hash).to include_key(key).with_value(value)
34
+ }.to fail_with_message(key_with_value_msg)
35
+ end
36
+ end
37
+
38
+ context 'when negatively evaluating' do
39
+ it 'fails (in matching just the key)' do
40
+ expect {
41
+ expect(partially_mismatched_hash).to_not include_key(key)
42
+ }.to fail_with_message(neg_key_only_msg)
43
+ end
44
+ it 'succeeds (in matching both the key and the value)' do
45
+ expect(partially_mismatched_hash).to_not include_key(key).with_value(value)
46
+ end
47
+ end
48
+ end
49
+
50
+ context 'when evaluating a totally mismatched hash (neither target key nor target value are present)' do
51
+ context 'when positiviely evaluating' do
52
+ it 'fails (in matching just the key)' do
53
+ expect {
54
+ expect(totally_mismatched_hash).to include_key(key)
55
+ }.to fail_with_message(key_only_msg)
56
+ end
57
+ it 'fails (in matching both the key and the value)' do
58
+ expect {
59
+ expect(totally_mismatched_hash).to include_key(key).with_value(value)
60
+ }.to fail_with_message(key_with_value_msg)
61
+ end
62
+ end
63
+
64
+ context 'when negatively evaluating' do
65
+ it 'succeeds (in matching just the key)' do
66
+ expect(totally_mismatched_hash).to_not include_key(key)
67
+ end
68
+ it 'succeeds (in matching both the key and the value)' do
69
+ expect(totally_mismatched_hash).to_not include_key(key).with_value(value)
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,70 @@
1
+ # Copyright (c) 2006-2014, Tammer Saleh, thoughtbot, inc.
2
+
3
+ # Permission is hereby granted, free of charge, to any person
4
+ # obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without
6
+ # restriction, including without limitation the rights to use,
7
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the
9
+ # Software is furnished to do so, subject to the following
10
+ # conditions:
11
+
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ # OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ # Taken from shoulda_matchers:
25
+ # https://github.com/thoughtbot/shoulda-matchers/blob/master/spec/support/fail_with_message_matcher.rb
26
+
27
+ RSpec::Matchers.define :fail_with_message do |expected|
28
+ def supports_block_expectations?
29
+ true
30
+ end
31
+
32
+ match do |block|
33
+ @actual = nil
34
+
35
+ begin
36
+ block.call
37
+ rescue RSpec::Expectations::ExpectationNotMetError => ex
38
+ @actual = ex.message
39
+ end
40
+
41
+ @actual && @actual == expected
42
+ end
43
+
44
+ def failure_message
45
+ msg = "Expectation should have failed with message '#{expected}'"
46
+
47
+ if @actual
48
+ msg << ", actually failed with '#{@actual}'"
49
+ else
50
+ msg << ", but did not fail."
51
+ end
52
+
53
+ msg
54
+ end
55
+
56
+ def failure_message_for_should
57
+ failure_message
58
+ end
59
+
60
+ def failure_message_when_negated
61
+ msg = "Expectation should not have failed with message '#{expected}'"
62
+ msg << ", but did."
63
+
64
+ msg
65
+ end
66
+
67
+ def failure_message_for_should_not
68
+ failure_message_when_negated
69
+ end
70
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'useful_matchers/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "useful_matchers"
8
+ spec.version = UsefulMatchers::VERSION
9
+ spec.authors = ["Two Maestros"]
10
+ spec.email = ["team@twomaestros.com"]
11
+ spec.summary = %q{A set of useful matchers for RSpec}
12
+ spec.description = %q{A set of useful matchers for RSpec}
13
+ spec.homepage = "http://github.com/twomaestros/useful-matchers"
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.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: useful_matchers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Two Maestros
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-17 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A set of useful matchers for RSpec
56
+ email:
57
+ - team@twomaestros.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - lib/useful_matchers.rb
68
+ - lib/useful_matchers/matchers.rb
69
+ - lib/useful_matchers/matchers/independent.rb
70
+ - lib/useful_matchers/matchers/independent/hashes.rb
71
+ - lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb
72
+ - lib/useful_matchers/version.rb
73
+ - spec/matchers/independent/hashes/include_key_and_value_spec.rb
74
+ - spec/spec_helper.rb
75
+ - spec/support/examples/matchers/independent/hashes/include_key_and_value_examples.rb
76
+ - spec/support/matchers/fail_with_message_matcher.rb
77
+ - useful_matchers.gemspec
78
+ homepage: http://github.com/twomaestros/useful-matchers
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.2.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: A set of useful matchers for RSpec
102
+ test_files:
103
+ - spec/matchers/independent/hashes/include_key_and_value_spec.rb
104
+ - spec/spec_helper.rb
105
+ - spec/support/examples/matchers/independent/hashes/include_key_and_value_examples.rb
106
+ - spec/support/matchers/fail_with_message_matcher.rb