rspec-instrumentation-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
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rspec-instrumentation-matcher.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Vlad Verestiuc
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,34 @@
1
+ rspec-instrumentation-matcher
2
+ =============================
3
+
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rspec-instrumentation-matcher'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rspec-instrumentation-matcher
18
+
19
+ ## Usage
20
+
21
+ expect{ subject.do_something }.to instrument("key").with( foo: :bar )
22
+ expect{ subject.do_something }.to instrument("key").once
23
+ expect{ subject.do_something }.to instrument("key").at_least(1)
24
+ expect{ subject.do_something }.to instrument("key").at_most(10)
25
+ expect{ subject.do_something }.to instrument("key").times(5)
26
+
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,141 @@
1
+ require "rspec-instrumentation-matcher/version"
2
+
3
+ module RSpec
4
+ module Instrumentation
5
+ module Matcher
6
+ def instrument(subject)
7
+ InstrumentMatcher.new(subject)
8
+ end
9
+ class InstrumentMatcher
10
+
11
+ def initialize(subject)
12
+ @subject = subject
13
+ @payload = nil
14
+ @received = 0
15
+ @at_least = 1
16
+ end
17
+
18
+ def matches?(event_proc)
19
+ raise_block_syntax_error if block_given?
20
+
21
+ subscription = ActiveSupport::Notifications.subscribe @subject do |name, start, finish, id, _payload|
22
+ @payload = _payload
23
+ @received += 1
24
+ end
25
+
26
+ event_proc.call
27
+
28
+ ActiveSupport::Notifications.unsubscribe(subscription)
29
+
30
+ times? and at_least? and at_most? and with?
31
+
32
+ end
33
+
34
+ def failure_message
35
+ message = "Expected #{@subject} to be instrumented"
36
+ message << " at least #{count @at_least}" unless at_least?
37
+ message << " at most #{count @at_most}" unless at_most?
38
+ message << " exactly #{count @times}" unless times?
39
+
40
+ if !at_least? or !at_most? or !times?
41
+ message << " but was instrumented #{count @received}"
42
+ message << " and the payload should have been" unless with?
43
+ else
44
+ message << " with" unless with?
45
+ end
46
+
47
+ message << " #{@payload_expectation.inspect} instead of #{@payload.inspect}" unless with?
48
+
49
+
50
+ message
51
+
52
+ end
53
+
54
+ def at_least(value)
55
+ @at_least=value
56
+ self
57
+ end
58
+
59
+ def at_most(value)
60
+ @at_most = value
61
+ self
62
+ end
63
+
64
+ def once
65
+ times(1)
66
+ end
67
+
68
+ def never
69
+ times(0)
70
+ end
71
+
72
+
73
+
74
+ def times(value)
75
+ @times = value
76
+ self
77
+ end
78
+
79
+
80
+ def with(expectation)
81
+ @with = true
82
+ @payload_expectation = expectation
83
+ self
84
+ end
85
+
86
+
87
+ private
88
+ def count(value)
89
+ if value == 1
90
+ "1 time"
91
+ else
92
+ "#{value} times"
93
+ end
94
+ end
95
+
96
+ def at_least?
97
+ return true unless @at_least
98
+ @received <= @at_least
99
+ end
100
+
101
+ def at_most?
102
+ return true unless @at_most
103
+ @received >= @at_most
104
+ end
105
+
106
+ def times?
107
+ return true unless @times
108
+ @received == @times
109
+ end
110
+
111
+ def delta_payload
112
+ unmatched = @payload_expectation.reject do |key,value|
113
+ matched_value?(value, @payload[key]) if @payload.key? key
114
+ end
115
+ end
116
+
117
+ def with?
118
+ !@payload.nil? and delta_payload.size == 0
119
+ end
120
+
121
+
122
+ def matched_value?(expected, actual)
123
+ case expected
124
+ when actual then true
125
+ when Class then actual.is_a? expected
126
+ when Regexp then actual =~ expected
127
+ else false
128
+ end
129
+ end
130
+
131
+
132
+ end
133
+ end
134
+ end
135
+ end
136
+
137
+ module RSpec::Matchers
138
+ include RSpec::Instrumentation::Matcher
139
+ end
140
+
141
+
@@ -0,0 +1,7 @@
1
+ module Rspec
2
+ module Instrumentation
3
+ module Matcher
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ 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-instrumentation-matcher/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rspec-instrumentation-matcher"
8
+ gem.version = Rspec::Instrumentation::Matcher::VERSION
9
+ gem.authors = ["Vlad Verestiuc"]
10
+ gem.email = ["vlad.verestiuc@me.com"]
11
+ gem.description = %q{Rspec matcher for ActiveSupport::Notifications}
12
+ gem.summary = %q{Support for spec-ing ActiveSupport::Notifications.instrument calls}
13
+ gem.homepage = "https://github.com/vvlad/rspec-instrumentation-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_dependency 'rspec-expectations'
21
+ gem.add_dependency 'activesupport'
22
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-instrumentation-matcher
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Vlad Verestiuc
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec-expectations
16
+ type: :runtime
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ type: :runtime
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
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: Rspec matcher for ActiveSupport::Notifications
47
+ email:
48
+ - vlad.verestiuc@me.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/rspec-instrumentation-matcher.rb
59
+ - lib/rspec-instrumentation-matcher/version.rb
60
+ - rspec-instrumentation-matcher.gemspec
61
+ homepage: https://github.com/vvlad/rspec-instrumentation-matcher
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 1.8.23
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Support for spec-ing ActiveSupport::Notifications.instrument calls
85
+ test_files: []
86
+ has_rdoc: