wisper-rspec 0.0.2 → 0.0.3

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: 21502770eb4a955472b6c79fc17e2ef4714ccfc4
4
- data.tar.gz: 542574ad03f2c399bb665f4dcaf9a7a6488106d3
3
+ metadata.gz: 643aca16e7e08f43376f456256d0cfc5a4cb2621
4
+ data.tar.gz: aee3c2ee2e11a253f1dae2796340e0e8ed5474de
5
5
  SHA512:
6
- metadata.gz: f3cf54a835f388971225a8ab3cd0dba450d47a5b242a6f4ae5942f41e3a00ac289fe6e9c35c47539a8d4edb9922308a17252a2be447e8cb14ebec0a4c87cee0d
7
- data.tar.gz: 207eec7fc007cef723ed80cda2b521b6b57b5600f6dafa68735d6d970a3e77aedefc72e5e11e293767331704e4c44317d44b9aa3f298b65afa26ab148f51f2a9
6
+ metadata.gz: 3f0b3ed5f0bbccbd7ab38de8d59985d5f60ade1dad5990fd57886c0e5401ee45ab2b2bcb55d08516b47c8dfe3ed19f039c4e736e56e5f2639d33a3ca87443cd8
7
+ data.tar.gz: 9d06774d7b55f981b1e908fdb61618fab9af01a006c10864fc9f72afcde01f4c353d1526264ce8bfd25af79da9637f4acf0c2b9436b9b8cadf334319b334f2e1
@@ -0,0 +1 @@
1
+ 2.3
@@ -1,4 +1,7 @@
1
+ sudo: false
1
2
  language: ruby
3
+ before_install:
4
+ - gem install bundler
2
5
  script: rspec spec
3
6
  rvm:
4
7
  - '1.9.3'
@@ -1,3 +1,5 @@
1
+ 0.0.3 (7/Mar/2017)
2
+
1
3
  0.0.2 (8/Jan/2015)
2
4
 
3
5
  * Added support for argument matching [#1] by Gavin Todes (GAV1N)
data/README.md CHANGED
@@ -39,9 +39,26 @@ expect { publisher.execute }.to broadcast(:another_event, :arg_1, :arg_2)
39
39
 
40
40
  With event arguments, it matches only if the event is broadcast with those arguments. This assertion matches `broadcast(:another_event, :arg_1, :arg_2)` but not `broadcast(:another_event)`.
41
41
 
42
+ ```ruby
43
+ # with arguments matcher
44
+ expect { publisher.execute }.to broadcast(:event, hash_including(a: 2))
45
+ ```
42
46
 
47
+ Rspec values matcher can be used to match arguments. This assertion matches `broadcast(:another_event, a: 2, b: 1)` but not `broadcast(:another_event, a: 3)`
48
+
49
+ Matchers can be composed using [compound rspec matchers](http://www.rubydoc.info/gems/rspec-expectations/RSpec/Matchers/Composable):
43
50
 
44
-
51
+ ```ruby
52
+ expect {
53
+ publisher.execute(123)
54
+ publisher.execute(234)
55
+ }.to broadcast(:event, 123).and broadcast(:event, 234)
56
+
57
+ expect {
58
+ publisher.execute(123)
59
+ publisher.execute(234)
60
+ }.to broadcast(:event, 123).or broadcast(:event, 234)
61
+ ```
45
62
 
46
63
  ### Using message expections
47
64
 
@@ -1,8 +1,12 @@
1
1
  require 'rspec/expectations'
2
+ require 'rspec/mocks/argument_list_matcher'
2
3
 
3
4
  module Wisper
4
5
  module RSpec
5
6
  class EventRecorder
7
+ include ::RSpec::Mocks::ArgumentMatchers
8
+ attr_reader :broadcast_events
9
+
6
10
  def initialize
7
11
  @broadcast_events = []
8
12
  end
@@ -16,16 +20,18 @@ module Wisper
16
20
  end
17
21
 
18
22
  def broadcast?(event_name, *args)
19
- if args.size > 0
20
- @broadcast_events.include?([event_name.to_s, *args])
21
- else
22
- @broadcast_events.map(&:first).include?(event_name.to_s)
23
+ expected_args = args.size > 0 ? args : [any_args]
24
+ @broadcast_events.any? do |event_params|
25
+ matcher = ::RSpec::Mocks::ArgumentListMatcher.new(event_name.to_s, *expected_args)
26
+ matcher.args_match?(*event_params)
23
27
  end
24
28
  end
25
29
  end
26
30
 
27
31
  module BroadcastMatcher
28
32
  class Matcher
33
+ include ::RSpec::Matchers::Composable
34
+
29
35
  def initialize(event, *args)
30
36
  @event = event
31
37
  @args = args
@@ -36,18 +42,19 @@ module Wisper
36
42
  end
37
43
 
38
44
  def matches?(block)
39
- event_recorder = EventRecorder.new
45
+ @event_recorder = EventRecorder.new
40
46
 
41
- Wisper.subscribe(event_recorder) do
47
+ Wisper.subscribe(@event_recorder) do
42
48
  block.call
43
49
  end
44
50
 
45
- event_recorder.broadcast?(@event, *@args)
51
+ @event_recorder.broadcast?(@event, *@args)
46
52
  end
47
53
 
48
54
  def failure_message
49
55
  msg = "expected publisher to broadcast #{@event} event"
50
56
  msg += " with args: #{@args.inspect}" if @args.size > 0
57
+ msg << broadcast_events_list
51
58
  msg
52
59
  end
53
60
 
@@ -56,6 +63,22 @@ module Wisper
56
63
  msg += " with args: #{@args.inspect}" if @args.size > 0
57
64
  msg
58
65
  end
66
+
67
+ def broadcast_events_list
68
+ if @event_recorder.broadcast_events.any?
69
+ " (actual events broadcast: #{event_names.join(', ')})"
70
+ else
71
+ " (no events broadcast)"
72
+ end
73
+ end
74
+ private :broadcast_events_list
75
+
76
+ def event_names
77
+ @event_recorder.broadcast_events.map do |event|
78
+ event.size == 1 ? event[0] : "#{event[0]}(#{event[1..-1].join(", ")})"
79
+ end
80
+ end
81
+ private :event_names
59
82
  end
60
83
 
61
84
  def broadcast(event, *args)
@@ -63,4 +86,15 @@ module Wisper
63
86
  end
64
87
  end
65
88
  end
89
+
90
+ # Prior to being extracted from Wisper the matcher was namespaced as Rspec,
91
+ # it is now RSpec. This will raise a helpful message for those upgrading to
92
+ # Wisper 2.0
93
+ module Rspec
94
+ module BroadcastMatcher
95
+ def self.included(base)
96
+ raise 'Please include Wisper::RSpec::BroadcastMatcher instead of Wisper::Rspec::BroadcastMatcher (notice the capitalization of RSpec)'
97
+ end
98
+ end
99
+ end
66
100
  end
@@ -1,5 +1,5 @@
1
1
  module Wisper
2
2
  module Rspec
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -1,4 +1,4 @@
1
- require 'wisper/rspec/matchers'
1
+ require_relative '../../../lib/wisper/rspec/matchers'
2
2
 
3
3
  RSpec::configure do |config|
4
4
  config.include(Wisper::RSpec::BroadcastMatcher)
@@ -6,13 +6,13 @@ end
6
6
 
7
7
  describe 'broadcast matcher' do
8
8
  let(:publisher_class) { Class.new { include Wisper::Publisher } }
9
- let(:publisher) { publisher_class.new }
9
+ let(:publisher) { publisher_class.new }
10
10
 
11
11
  it 'passes when publisher broadcasts inside block' do
12
12
  expect { publisher.send(:broadcast, :foobar) }.to broadcast(:foobar)
13
13
  end
14
14
 
15
- context "with arguments" do
15
+ context 'with arguments' do
16
16
  it 'passes when publisher broadcasts inside block' do
17
17
  expect { publisher.send(:broadcast, :fizzbuzz, 12345) }.to broadcast(:fizzbuzz, 12345)
18
18
  end
@@ -21,11 +21,34 @@ describe 'broadcast matcher' do
21
21
  expect { publisher.send(:broadcast, :fizzbuzz, 12345) }.to broadcast(:fizzbuzz)
22
22
  end
23
23
 
24
+ it 'passes with rspect arguments matchers' do
25
+ expect { publisher.send(:broadcast, :fizzbuzz, 12345) }.to broadcast(:fizzbuzz, kind_of(Numeric))
26
+ end
27
+
28
+ it 'fails with rspec arguments matchers' do
29
+ expect { publisher.send(:broadcast, :fizzbuzz, 12345) }.to_not broadcast(:fizzbuzz, kind_of(Hash))
30
+ end
31
+
24
32
  it 'fails with incorrect arguments when publisher broadcasts inside block' do
25
33
  expect { publisher.send(:broadcast, :fizzbuzz, 12345) }.not_to broadcast(:fizzbuzz, 98765)
26
34
  end
27
35
  end
28
36
 
37
+ context 'with compound assertions' do
38
+ it 'passes when both values are expected' do
39
+ expect {
40
+ publisher.send(:broadcast, :fizzbuzz, 12345)
41
+ publisher.send(:broadcast, :fizzbuzz, 54321)
42
+ }.to broadcast(:fizzbuzz, 12345).and broadcast(:fizzbuzz, 54321)
43
+ end
44
+
45
+ it 'passes when either value is expected' do
46
+ expect {
47
+ publisher.send(:broadcast, :fizzbuzz, 54321)
48
+ }.to broadcast(:fizzbuzz, 12345).or broadcast(:fizzbuzz, 54321)
49
+ end
50
+ end
51
+
29
52
  it 'passes with not_to when publisher does not broadcast inside block' do
30
53
  expect { publisher }.not_to broadcast(:foobar)
31
54
  end
@@ -0,0 +1,9 @@
1
+ require_relative '../../../lib/wisper/rspec/matchers'
2
+
3
+ describe 'including mis-capitalized matcher' do
4
+ it 'raises an exception' do
5
+ expect do
6
+ Class.new.send(:include, Wisper::Rspec::BroadcastMatcher)
7
+ end.to raise_error(/include Wisper::RSpec::BroadcastMatcher instead of/)
8
+ end
9
+ end
@@ -0,0 +1,72 @@
1
+ require 'wisper'
2
+ require_relative '../../../../lib/wisper/rspec/matchers'
3
+
4
+ describe Wisper::RSpec::BroadcastMatcher::Matcher do
5
+ let(:event_name) { 'it_happened' }
6
+ let(:matcher) { described_class.new(event_name) }
7
+
8
+ let(:broadcaster) do
9
+ Class.new do
10
+ include Wisper.publisher
11
+
12
+ public :broadcast
13
+ end.new
14
+ end
15
+
16
+ context 'matching event broadcast' do
17
+ let(:block) do
18
+ Proc.new do
19
+ broadcaster.broadcast(event_name)
20
+ end
21
+ end
22
+
23
+ describe '#matches?' do
24
+ it 'returns true' do
25
+ expect(matcher.matches?(block)).to be_truthy
26
+ end
27
+ end
28
+ end
29
+
30
+ context 'no events broadcast' do
31
+ let(:block) { Proc.new {} }
32
+
33
+ describe '#matches?' do
34
+ it 'returns false' do
35
+ expect(matcher.matches?(block)).to be_falsey
36
+ end
37
+ end
38
+
39
+ describe '#failure_message' do
40
+ before { matcher.matches?(block) }
41
+
42
+ it 'has descriptive failure message' do
43
+ expect(matcher.failure_message).to eq "expected publisher to broadcast #{event_name} event (no events broadcast)"
44
+ end
45
+ end
46
+ end
47
+
48
+ context 'non-matching event broadcast' do
49
+ let(:block) do
50
+ Proc.new do
51
+ broadcaster.broadcast('event1')
52
+ broadcaster.send(:broadcast, 'event2', 12345, :foo)
53
+ end
54
+ end
55
+
56
+ describe '#matches?' do
57
+ it 'returns false' do
58
+ expect(matcher.matches?(block)).to be_falsey
59
+ end
60
+ end
61
+
62
+ describe '#failure_message' do
63
+ before { matcher.matches?(block) }
64
+
65
+ it 'has descriptive failure message' do
66
+ message = "expected publisher to broadcast it_happened event "\
67
+ "(actual events broadcast: event1, event2(12345, foo))"
68
+ expect(matcher.failure_message).to eq(message)
69
+ end
70
+ end
71
+ end
72
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wisper-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kris Leech
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-12 00:00:00.000000000 Z
11
+ date: 2017-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: wisper
@@ -75,6 +75,7 @@ extra_rdoc_files: []
75
75
  files:
76
76
  - ".gitignore"
77
77
  - ".rspec"
78
+ - ".ruby-version"
78
79
  - ".travis.yml"
79
80
  - CHANGELOG.md
80
81
  - Gemfile
@@ -87,7 +88,9 @@ files:
87
88
  - lib/wisper/rspec/version.rb
88
89
  - spec/spec_helper.rb
89
90
  - spec/wisper/rspec/matchers_spec.rb
91
+ - spec/wisper/rspec/old_matcher_spec.rb
90
92
  - spec/wisper/rspec/stub_wisper_publisher_spec.rb
93
+ - spec/wisper/rspec/unit/broadcast_matcher_spec.rb
91
94
  - wisper-rspec.gemspec
92
95
  homepage: https://github.com/krisleech/wisper-rspec
93
96
  licenses:
@@ -109,11 +112,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
112
  version: '0'
110
113
  requirements: []
111
114
  rubyforge_project:
112
- rubygems_version: 2.2.2
115
+ rubygems_version: 2.5.1
113
116
  signing_key:
114
117
  specification_version: 4
115
118
  summary: Rspec matchers and stubbing for Wisper
116
119
  test_files:
117
120
  - spec/spec_helper.rb
118
121
  - spec/wisper/rspec/matchers_spec.rb
122
+ - spec/wisper/rspec/old_matcher_spec.rb
119
123
  - spec/wisper/rspec/stub_wisper_publisher_spec.rb
124
+ - spec/wisper/rspec/unit/broadcast_matcher_spec.rb