signal 1.0.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,123 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Signal do
4
- let(:observable) { Observable.new }
5
- let(:callable) { Callable.new }
6
-
7
- context 'add listener' do
8
- it 'runs listener' do
9
- callable.respond_to(:on_ready)
10
- observable.add_listener(callable)
11
- expect(callable).to receive(:called).with(no_args)
12
-
13
- observable.emit(:ready)
14
- end
15
-
16
- it 'returns itself' do
17
- expect(observable.add_listener(callable)).to eq(observable)
18
- end
19
- end
20
-
21
- context 'using blocks' do
22
- it 'triggers event' do
23
- observable.on(:ready, &callable)
24
- expect(callable).to receive(:called).with(no_args)
25
-
26
- observable.emit(:ready)
27
- end
28
-
29
- it 'triggers event with arguments' do
30
- observable.on(:ready, &callable)
31
- expect(callable).to receive(:called).with(1, 2, 3)
32
-
33
- observable.emit(:ready, 1, 2, 3)
34
- end
35
-
36
- it 'triggers before event' do
37
- observable.before(:ready, &callable)
38
- expect(callable).to receive(:called).with(no_args)
39
-
40
- observable.emit(:ready)
41
- end
42
-
43
- it 'triggers before event with arguments' do
44
- observable.before(:ready, &callable)
45
- expect(callable).to receive(:called).with(1, 2, 3)
46
-
47
- observable.emit(:ready, 1, 2, 3)
48
- end
49
-
50
- it 'triggers after event' do
51
- observable.after(:ready, &callable)
52
- expect(callable).to receive(:called).with(no_args)
53
-
54
- observable.emit(:ready)
55
- end
56
-
57
- it 'triggers after event with arguments' do
58
- observable.after(:ready, &callable)
59
- expect(callable).to receive(:called).with(1, 2, 3)
60
-
61
- observable.emit(:ready, 1, 2, 3)
62
- end
63
-
64
- it 'chains events' do
65
- before_callable = Callable.new
66
- on_callable = Callable.new
67
- after_callable = Callable.new
68
-
69
- observable
70
- .before(:ready, &before_callable)
71
- .on(:ready, &on_callable)
72
- .after(:ready, &after_callable)
73
-
74
- expect(before_callable).to receive(:called).with(no_args).ordered
75
- expect(on_callable).to receive(:called).with(no_args).ordered
76
- expect(after_callable).to receive(:called).with(no_args).ordered
77
-
78
- observable.emit(:ready)
79
- end
80
-
81
- it 'keeps context' do
82
- context = nil
83
- callable = -> { context = self }
84
- observable.on(:ready, &callable)
85
- observable.emit(:ready)
86
-
87
- expect(context).to eql(self)
88
- end
89
- end
90
-
91
- context 'using listeners' do
92
- it 'triggers event for listener' do
93
- callable.respond_to(:on_ready)
94
- observable.listeners << callable
95
- expect(callable).to receive(:called).with(no_args)
96
-
97
- observable.emit(:ready)
98
- end
99
-
100
- it 'triggers event for listener with arguments' do
101
- callable.respond_to(:on_ready)
102
- observable.listeners << callable
103
- expect(callable).to receive(:called).with(1, 2, 3)
104
-
105
- observable.emit(:ready, 1, 2, 3)
106
- end
107
- end
108
-
109
- context 'mixed listeners' do
110
- it 'calls all listeners' do
111
- callable.respond_to(:on_ready)
112
- another_callable = Callable.new
113
-
114
- expect(callable).to receive(:called).ordered
115
- expect(another_callable).to receive(:called).ordered
116
-
117
- observable
118
- .add_listener(callable)
119
- .on(:ready, &another_callable)
120
- .emit(:ready)
121
- end
122
- end
123
- end
data/spec/spec_helper.rb DELETED
@@ -1,25 +0,0 @@
1
- require 'codeclimate-test-reporter'
2
- CodeClimate::TestReporter.start
3
-
4
- require 'bundler/setup'
5
- require 'active_record'
6
-
7
- I18n.enforce_available_locales = false
8
-
9
- require 'signal'
10
- require 'support/observable'
11
- require 'support/observable_with_call'
12
- require 'support/callable'
13
- require 'support/user'
14
- require 'support/emitter'
15
-
16
- ActiveRecord::Base.establish_connection({
17
- :adapter => 'sqlite3',
18
- :database => ':memory:'
19
- })
20
-
21
- ActiveRecord::Schema.define(:version => 0) do
22
- create_table :users do |t|
23
- t.string :username
24
- end
25
- end
@@ -1,10 +0,0 @@
1
- class Callable
2
- def to_proc
3
- callable = self
4
- proc {|*args| callable.called(*args) }
5
- end
6
-
7
- def respond_to(method_name)
8
- define_singleton_method(method_name, &to_proc)
9
- end
10
- end
@@ -1,9 +0,0 @@
1
- class Emitter
2
- include Signal
3
-
4
- def call
5
- ActiveRecord::Base.transaction do
6
- emit(:success)
7
- end
8
- end
9
- end
@@ -1,3 +0,0 @@
1
- class Observable
2
- include Signal
3
- end
@@ -1,13 +0,0 @@
1
- class ObservableWithCall
2
- include Signal.call
3
-
4
- attr_reader :args
5
-
6
- def initialize(*args)
7
- @args = args
8
- end
9
-
10
- def call
11
- emit(:args, @args)
12
- end
13
- end
data/spec/support/user.rb DELETED
@@ -1,4 +0,0 @@
1
- class User < ActiveRecord::Base
2
- include Signal.active_record
3
- validates_presence_of :username
4
- end