informator 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -0
- data/CHANGELOG.md +31 -0
- data/README.md +114 -46
- data/Rakefile +1 -1
- data/config/metrics/metric_fu.yml +2 -2
- data/config/metrics/rubocop.yml +4 -0
- data/informator.gemspec +12 -9
- data/lib/informator.rb +3 -73
- data/lib/informator/event.rb +35 -30
- data/lib/informator/publisher.rb +103 -0
- data/lib/informator/rspec.rb +3 -0
- data/lib/informator/subscriber.rb +14 -28
- data/lib/informator/version.rb +1 -1
- data/lib/rspec/shared.rb +104 -0
- data/spec/integration/en.yml +6 -0
- data/spec/integration/fr.yml +6 -0
- data/spec/integration/i18n.rb +21 -0
- data/spec/integration/rspec_spec.rb +51 -0
- data/spec/unit/informator/event_spec.rb +82 -28
- data/spec/unit/informator/publisher_spec.rb +168 -0
- data/spec/unit/informator/subscriber_spec.rb +26 -40
- metadata +59 -11
- data/lib/informator/reporter.rb +0 -67
- data/spec/unit/informator/reporter_spec.rb +0 -75
- data/spec/unit/informator_spec.rb +0 -153
@@ -1,75 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
describe Informator::Reporter do
|
4
|
-
|
5
|
-
subject(:reporter) { described_class.new }
|
6
|
-
|
7
|
-
describe ".new" do
|
8
|
-
|
9
|
-
it { is_expected.to be_frozen }
|
10
|
-
|
11
|
-
end # describe .new
|
12
|
-
|
13
|
-
describe "#events" do
|
14
|
-
|
15
|
-
subject { reporter.events }
|
16
|
-
it { is_expected.to eql [] }
|
17
|
-
|
18
|
-
end # describe #events
|
19
|
-
|
20
|
-
describe "#remember" do
|
21
|
-
|
22
|
-
subject { reporter.remember(*options) }
|
23
|
-
|
24
|
-
let(:options) { [:foo, "bar", baz: :qux] }
|
25
|
-
let(:event) { Informator::Event.new(*options) }
|
26
|
-
|
27
|
-
it "builds the event and adds it to #events" do
|
28
|
-
expect { subject }.to change { reporter.events }.by [event]
|
29
|
-
end
|
30
|
-
|
31
|
-
it { is_expected.to eql reporter }
|
32
|
-
|
33
|
-
end # describe #remember
|
34
|
-
|
35
|
-
describe "#notify" do
|
36
|
-
|
37
|
-
subject { reporter.notify(subscribers) }
|
38
|
-
|
39
|
-
let(:subscribers) { 2.times.map { double notify: nil } }
|
40
|
-
let!(:events) do
|
41
|
-
%i(foo bar).each(&reporter.method(:remember))
|
42
|
-
reporter.events.dup
|
43
|
-
end
|
44
|
-
|
45
|
-
it "notifies every subscriber on all #events" do
|
46
|
-
events.each do |event|
|
47
|
-
subscribers.each do |subscriber|
|
48
|
-
expect(subscriber).to receive(:notify).with(event).ordered
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
subject
|
53
|
-
end
|
54
|
-
|
55
|
-
it "clears #events" do
|
56
|
-
expect { subject }.to change { reporter.events }.to []
|
57
|
-
end
|
58
|
-
|
59
|
-
context "when error occured while publishing" do
|
60
|
-
|
61
|
-
before { allow(subscribers.first).to receive(:notify) { fail } }
|
62
|
-
|
63
|
-
it "removes tried #events" do
|
64
|
-
expect { subject rescue nil }
|
65
|
-
.to change { reporter.events }
|
66
|
-
.to [events.last]
|
67
|
-
end
|
68
|
-
|
69
|
-
end # context
|
70
|
-
|
71
|
-
it { is_expected.to eql events }
|
72
|
-
|
73
|
-
end # describe #notify
|
74
|
-
|
75
|
-
end # describe Informator::Reporter
|
@@ -1,153 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
describe Informator do
|
4
|
-
|
5
|
-
subject(:informator) { klass.new }
|
6
|
-
|
7
|
-
let(:klass) { Class.new { include Informator } }
|
8
|
-
let(:callback) { :on_received }
|
9
|
-
let(:listener) do
|
10
|
-
double(
|
11
|
-
callback => nil,
|
12
|
-
foo: nil,
|
13
|
-
bar: nil,
|
14
|
-
receive: nil,
|
15
|
-
freeze: nil
|
16
|
-
)
|
17
|
-
end
|
18
|
-
|
19
|
-
before { informator.subscribe listener, callback }
|
20
|
-
|
21
|
-
describe "#subscribe" do
|
22
|
-
|
23
|
-
it "subscribes listeners one-by-one" do
|
24
|
-
informator.subscribe(listener, :foo).subscribe(listener, :bar)
|
25
|
-
|
26
|
-
expect(listener).to receive(callback).ordered
|
27
|
-
expect(listener).to receive(:foo).ordered
|
28
|
-
expect(listener).to receive(:bar).ordered
|
29
|
-
informator.publish :info
|
30
|
-
end
|
31
|
-
|
32
|
-
it "doesn't duplicate listener's callbacks" do
|
33
|
-
informator.subscribe(listener, callback)
|
34
|
-
|
35
|
-
expect(listener).to receive(callback).once
|
36
|
-
informator.publish :info
|
37
|
-
end
|
38
|
-
|
39
|
-
it "subscribes listener with :receive callback by default" do
|
40
|
-
informator.subscribe(listener)
|
41
|
-
|
42
|
-
expect(listener).to receive(:receive)
|
43
|
-
informator.publish :info
|
44
|
-
end
|
45
|
-
|
46
|
-
end # describe #subscribe
|
47
|
-
|
48
|
-
describe "#remember" do
|
49
|
-
|
50
|
-
it "keeps remembered events unpublished" do
|
51
|
-
expect(listener).not_to receive(callback)
|
52
|
-
|
53
|
-
informator.remember :info
|
54
|
-
end
|
55
|
-
|
56
|
-
it "remembers events until publishing" do
|
57
|
-
expect(listener).to receive(callback).once
|
58
|
-
|
59
|
-
informator.remember :info
|
60
|
-
informator.publish
|
61
|
-
end
|
62
|
-
|
63
|
-
it "returns itself to allow chaining" do
|
64
|
-
expect(informator.remember(:info).remember(:alert)).to eql informator
|
65
|
-
end
|
66
|
-
|
67
|
-
end # describe #remember
|
68
|
-
|
69
|
-
describe "#publish" do
|
70
|
-
|
71
|
-
it "publishes events" do
|
72
|
-
expect(listener).to receive(callback) do |event|
|
73
|
-
expect(event).to be_kind_of Informator::Event
|
74
|
-
expect(event.type).to eq :alert
|
75
|
-
expect(event.messages).to eql %w(foo)
|
76
|
-
expect(event.attributes).to eql(bar: :baz)
|
77
|
-
end
|
78
|
-
|
79
|
-
informator.publish :alert, "foo", bar: :baz
|
80
|
-
end
|
81
|
-
|
82
|
-
it "publishes all remembered events at once" do
|
83
|
-
expect(listener).to receive(callback).twice
|
84
|
-
|
85
|
-
informator.remember :info
|
86
|
-
informator.publish :alert
|
87
|
-
end
|
88
|
-
|
89
|
-
it "can publish only remembered events without a new one" do
|
90
|
-
expect(listener).to receive(callback).once
|
91
|
-
|
92
|
-
informator.remember :info
|
93
|
-
informator.publish
|
94
|
-
end
|
95
|
-
|
96
|
-
it "returns events having been published" do
|
97
|
-
informator.remember :info
|
98
|
-
events = informator.publish :alert
|
99
|
-
|
100
|
-
expect(events).to be_kind_of Array
|
101
|
-
expect(events.map(&:class).uniq).to contain_exactly Informator::Event
|
102
|
-
expect(events.map(&:type)).to eql [:info, :alert]
|
103
|
-
end
|
104
|
-
|
105
|
-
it "forgets published events" do
|
106
|
-
informator.publish :info
|
107
|
-
expect(listener).not_to receive(callback)
|
108
|
-
|
109
|
-
informator.publish
|
110
|
-
end
|
111
|
-
|
112
|
-
end # describe #publish
|
113
|
-
|
114
|
-
describe "#publish!" do
|
115
|
-
|
116
|
-
it "publishes all remembered events at once" do
|
117
|
-
expect(listener).to receive(callback).twice
|
118
|
-
|
119
|
-
informator.remember :info
|
120
|
-
informator.publish!(:alert) rescue nil
|
121
|
-
end
|
122
|
-
|
123
|
-
it "can publish only remembered events without a new one" do
|
124
|
-
expect(listener).to receive(callback).once
|
125
|
-
|
126
|
-
informator.remember :info
|
127
|
-
informator.publish! rescue nil
|
128
|
-
end
|
129
|
-
|
130
|
-
it "forgets published events" do
|
131
|
-
informator.publish! :info rescue nil
|
132
|
-
expect(listener).not_to receive(callback)
|
133
|
-
|
134
|
-
informator.publish
|
135
|
-
end
|
136
|
-
|
137
|
-
it "throws :published" do
|
138
|
-
expect { informator.publish! }.to raise_error(ArgumentError)
|
139
|
-
expect { catch(:published) { informator.publish! } }.not_to raise_error
|
140
|
-
end
|
141
|
-
|
142
|
-
it "throws events having been published" do
|
143
|
-
informator.remember :info
|
144
|
-
events = catch(:published) { informator.publish! :alert }
|
145
|
-
|
146
|
-
expect(events).to be_kind_of Array
|
147
|
-
expect(events.map(&:class).uniq).to contain_exactly Informator::Event
|
148
|
-
expect(events.map(&:type)).to eql [:info, :alert]
|
149
|
-
end
|
150
|
-
|
151
|
-
end # describe #publish!
|
152
|
-
|
153
|
-
end # describe Informator
|