events 0.9.6 → 0.9.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/events.rb +4 -3
- data/test/events_test.rb +156 -0
- data/test/{event_emitter_test.rb → node_transliterated_test.rb} +48 -48
- metadata +8 -11
data/lib/events.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Events # :nodoc:
|
2
|
-
UncaughtError = Class.new(
|
2
|
+
UncaughtError = Class.new(RuntimeError)
|
3
3
|
|
4
4
|
# The Events::Emitter mixin provides a clone of the Node.js EventEmitter API
|
5
5
|
# for Ruby.
|
@@ -73,7 +73,8 @@ module Events # :nodoc:
|
|
73
73
|
listeners = @listeners && @listeners.key?(event) && @listeners[event]
|
74
74
|
|
75
75
|
if event == :error && (!listeners || listeners.empty?)
|
76
|
-
raise args
|
76
|
+
raise *args if args.first.respond_to?(:exception)
|
77
|
+
raise Events::UncaughtError, args.first if args.first.is_a?(String)
|
77
78
|
raise Events::UncaughtError.new("Uncaught, unspecified 'error' event.")
|
78
79
|
elsif listeners
|
79
80
|
listeners.dup.each do |listener|
|
@@ -134,7 +135,7 @@ module Events # :nodoc:
|
|
134
135
|
end
|
135
136
|
once = OnceWrapper.new do |*args|
|
136
137
|
remove_listener(event, once)
|
137
|
-
listener.call(args)
|
138
|
+
listener.call(*args)
|
138
139
|
end
|
139
140
|
once.original = listener
|
140
141
|
|
data/test/events_test.rb
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "../lib/events"
|
3
|
+
require "stringio"
|
4
|
+
|
5
|
+
class TestEvents < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_emit_error_raises_without_handler
|
8
|
+
emitter = Events::EventEmitter.new
|
9
|
+
|
10
|
+
assert_raise(Events::UncaughtError) {emitter.emit(:error)}
|
11
|
+
assert_raise(Events::UncaughtError) {emitter.emit(:error, "error")}
|
12
|
+
assert(Events::UncaughtError < RuntimeError, "should be a subclass")
|
13
|
+
assert_raise(StandardError) {emitter.emit(:error, StandardError.new("error"))}
|
14
|
+
assert_raise(StandardError) {emitter.emit(:error, StandardError, "error")}
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_emit_error_raises_without_handler_with_correct_message
|
18
|
+
emitter = Events::EventEmitter.new
|
19
|
+
|
20
|
+
begin
|
21
|
+
emitter.emit(:error)
|
22
|
+
rescue => e
|
23
|
+
assert_equal("Uncaught, unspecified 'error' event.", e.message)
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
emitter.emit(:error, "test error message")
|
28
|
+
rescue => e
|
29
|
+
assert_equal("test error message", e.message)
|
30
|
+
end
|
31
|
+
|
32
|
+
begin
|
33
|
+
emitter.emit(:error, StandardError.new("error test message"))
|
34
|
+
rescue => e
|
35
|
+
assert_equal("error test message", e.message)
|
36
|
+
end
|
37
|
+
|
38
|
+
begin
|
39
|
+
emitter.emit(:error, StandardError, "message test error")
|
40
|
+
rescue => e
|
41
|
+
assert_equal("message test error", e.message)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_any_listener_that_responds_to_call_is_accepted
|
46
|
+
emitter = Events::EventEmitter.new
|
47
|
+
block_result, proc_result, lambda_result = nil
|
48
|
+
stringio = StringIO.new
|
49
|
+
object = Object.new
|
50
|
+
class << object
|
51
|
+
attr_reader :result
|
52
|
+
def call
|
53
|
+
@result = true
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
emitter.add_listener(:block) {block_result = true}
|
58
|
+
emitter.emit(:block)
|
59
|
+
assert(block_result, "block not called")
|
60
|
+
|
61
|
+
emitter.add_listener(:proc, Proc.new {proc_result = true})
|
62
|
+
emitter.emit(:proc)
|
63
|
+
assert(proc_result, "proc not called")
|
64
|
+
|
65
|
+
emitter.add_listener(:lambda, lambda {lambda_result = true})
|
66
|
+
emitter.emit(:lambda)
|
67
|
+
assert(lambda_result, "lambda not called")
|
68
|
+
|
69
|
+
emitter.add_listener(:method, stringio.method(:puts))
|
70
|
+
emitter.emit(:method)
|
71
|
+
assert_equal("\n", stringio.string)
|
72
|
+
|
73
|
+
emitter.add_listener(:object, object)
|
74
|
+
emitter.emit(:object)
|
75
|
+
assert(object.result, "object not called")
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_any_once_listener_that_responds_to_call_is_accepted
|
79
|
+
emitter = Events::EventEmitter.new
|
80
|
+
block_result, proc_result, lambda_result = nil
|
81
|
+
stringio = StringIO.new
|
82
|
+
object = Object.new
|
83
|
+
class << object
|
84
|
+
attr_reader :result
|
85
|
+
def call
|
86
|
+
@result = true
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
emitter.once(:block) {block_result = true}
|
91
|
+
emitter.emit(:block)
|
92
|
+
assert(block_result, "block not called")
|
93
|
+
|
94
|
+
emitter.once(:proc, Proc.new {proc_result = true})
|
95
|
+
emitter.emit(:proc)
|
96
|
+
assert(proc_result, "proc not called")
|
97
|
+
|
98
|
+
emitter.once(:lambda, lambda {lambda_result = true})
|
99
|
+
emitter.emit(:lambda)
|
100
|
+
assert(lambda_result, "lambda not called")
|
101
|
+
|
102
|
+
emitter.once(:method, stringio.method(:puts))
|
103
|
+
emitter.emit(:method)
|
104
|
+
assert_equal("\n", stringio.string)
|
105
|
+
|
106
|
+
emitter.once(:object, object)
|
107
|
+
emitter.emit(:object)
|
108
|
+
assert(object.result, "object not called")
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_listener_is_required_to_respond_to_call
|
112
|
+
emitter = Events::EventEmitter.new
|
113
|
+
|
114
|
+
assert_raise(ArgumentError) {emitter.on(:test)}
|
115
|
+
assert_raise(ArgumentError) {emitter.on(:test, "foo")}
|
116
|
+
assert_raise(ArgumentError) {emitter.once(:test)}
|
117
|
+
assert_raise(ArgumentError) {emitter.once(:test, "foo")}
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_multi_arguments
|
121
|
+
emitter = Events::EventEmitter.new
|
122
|
+
|
123
|
+
emitter.on(:block) do |a, b|
|
124
|
+
assert_equal("a", a)
|
125
|
+
assert_equal("b", b)
|
126
|
+
end
|
127
|
+
|
128
|
+
emitter.emit(:block, "a", "b")
|
129
|
+
|
130
|
+
emitter.on(:lambda, lambda do |a, b|
|
131
|
+
assert_equal("a", a)
|
132
|
+
assert_equal("b", b)
|
133
|
+
end)
|
134
|
+
|
135
|
+
emitter.emit(:lambda, "a", "b")
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_multi_arguments_once
|
139
|
+
emitter = Events::EventEmitter.new
|
140
|
+
|
141
|
+
emitter.once(:block) do |a, b|
|
142
|
+
assert_equal("a", a)
|
143
|
+
assert_equal("b", b)
|
144
|
+
end
|
145
|
+
|
146
|
+
emitter.emit(:block, "a", "b")
|
147
|
+
|
148
|
+
emitter.once(:lambda, lambda do |a, b|
|
149
|
+
assert_equal("a", a)
|
150
|
+
assert_equal("b", b)
|
151
|
+
end)
|
152
|
+
|
153
|
+
emitter.emit(:lambda, "a", "b")
|
154
|
+
end
|
155
|
+
|
156
|
+
end
|
@@ -122,6 +122,54 @@ class TestEventEmitter < Test::Unit::TestCase
|
|
122
122
|
assert_equal(0, e.listeners(:foo).length)
|
123
123
|
end
|
124
124
|
|
125
|
+
# /test/simple/test-event-emitter-num-args.js
|
126
|
+
def test_event_emitter_num_args
|
127
|
+
e = Events::EventEmitter.new
|
128
|
+
num_args_emited = []
|
129
|
+
|
130
|
+
e.on(:num_args) do |*args|
|
131
|
+
num_args = args.length
|
132
|
+
puts "num_args: #{num_args}"
|
133
|
+
num_args_emited.push(num_args)
|
134
|
+
end
|
135
|
+
|
136
|
+
puts "start"
|
137
|
+
|
138
|
+
e.emit(:num_args)
|
139
|
+
e.emit(:num_args, nil)
|
140
|
+
e.emit(:num_args, nil, nil)
|
141
|
+
e.emit(:num_args, nil, nil, nil)
|
142
|
+
e.emit(:num_args, nil, nil, nil, nil)
|
143
|
+
e.emit(:num_args, nil, nil, nil, nil, nil)
|
144
|
+
|
145
|
+
assert_equal([0, 1, 2, 3, 4, 5], num_args_emited)
|
146
|
+
end
|
147
|
+
|
148
|
+
# /test/simple/test-event-emitter-once.js
|
149
|
+
def test_event_emitter_once
|
150
|
+
e = Events::EventEmitter.new
|
151
|
+
times_hello_emited = 0
|
152
|
+
|
153
|
+
e.once(:hello) do |a, b|
|
154
|
+
times_hello_emited += 1
|
155
|
+
end
|
156
|
+
|
157
|
+
e.emit(:hello, "a", "b")
|
158
|
+
e.emit(:hello, "a", "b")
|
159
|
+
e.emit(:hello, "a", "b")
|
160
|
+
e.emit(:hello, "a", "b")
|
161
|
+
|
162
|
+
remove = Proc.new do
|
163
|
+
flunk("once->foo should not be emitted")
|
164
|
+
end
|
165
|
+
|
166
|
+
e.once(:foo, remove)
|
167
|
+
e.remove_listener(:foo, remove)
|
168
|
+
e.emit(:foo)
|
169
|
+
|
170
|
+
assert_equal(1, times_hello_emited)
|
171
|
+
end
|
172
|
+
|
125
173
|
# /test/simple/test-event-emitter-remove-all-listeners.js
|
126
174
|
def test_event_emitter_remove_all_listeners
|
127
175
|
listener = Proc.new {}
|
@@ -177,52 +225,4 @@ class TestEventEmitter < Test::Unit::TestCase
|
|
177
225
|
assert_equal([listener2], e3.listeners(:hello))
|
178
226
|
end
|
179
227
|
|
180
|
-
# /test/simple/test-event-emitter-once.js
|
181
|
-
def test_event_emitter_once
|
182
|
-
e = Events::EventEmitter.new
|
183
|
-
times_hello_emited = 0
|
184
|
-
|
185
|
-
e.once(:hello) do |a, b|
|
186
|
-
times_hello_emited += 1
|
187
|
-
end
|
188
|
-
|
189
|
-
e.emit(:hello, "a", "b")
|
190
|
-
e.emit(:hello, "a", "b")
|
191
|
-
e.emit(:hello, "a", "b")
|
192
|
-
e.emit(:hello, "a", "b")
|
193
|
-
|
194
|
-
remove = Proc.new do
|
195
|
-
flunk("once->foo should not be emitted")
|
196
|
-
end
|
197
|
-
|
198
|
-
e.once(:foo, remove)
|
199
|
-
e.remove_listener(:foo, remove)
|
200
|
-
e.emit(:foo)
|
201
|
-
|
202
|
-
assert_equal(1, times_hello_emited)
|
203
|
-
end
|
204
|
-
|
205
|
-
# /test/simple/test-event-emitter-num-args.js
|
206
|
-
def test_event_emitter_num_args
|
207
|
-
e = Events::EventEmitter.new
|
208
|
-
num_args_emited = []
|
209
|
-
|
210
|
-
e.on(:num_args) do |*args|
|
211
|
-
num_args = args.length
|
212
|
-
puts "num_args: #{num_args}"
|
213
|
-
num_args_emited.push(num_args)
|
214
|
-
end
|
215
|
-
|
216
|
-
puts "start"
|
217
|
-
|
218
|
-
e.emit(:num_args)
|
219
|
-
e.emit(:num_args, nil)
|
220
|
-
e.emit(:num_args, nil, nil)
|
221
|
-
e.emit(:num_args, nil, nil, nil)
|
222
|
-
e.emit(:num_args, nil, nil, nil, nil)
|
223
|
-
e.emit(:num_args, nil, nil, nil, nil, nil)
|
224
|
-
|
225
|
-
assert_equal([0, 1, 2, 3, 4, 5], num_args_emited)
|
226
|
-
end
|
227
|
-
|
228
228
|
end
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: events
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 55
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 9
|
9
|
-
-
|
10
|
-
version: 0.9.
|
8
|
+
- 7
|
9
|
+
version: 0.9.7
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Matthew Sadler
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2011-
|
17
|
+
date: 2011-08-06 00:00:00 +01:00
|
19
18
|
default_executable:
|
20
19
|
dependencies: []
|
21
20
|
|
@@ -29,7 +28,8 @@ extra_rdoc_files:
|
|
29
28
|
- readme.rdoc
|
30
29
|
files:
|
31
30
|
- lib/events.rb
|
32
|
-
- test/
|
31
|
+
- test/events_test.rb
|
32
|
+
- test/node_transliterated_test.rb
|
33
33
|
- readme.rdoc
|
34
34
|
has_rdoc: true
|
35
35
|
homepage: http://github.com/matsadler/rb-event-emitter
|
@@ -42,29 +42,26 @@ rdoc_options:
|
|
42
42
|
require_paths:
|
43
43
|
- lib
|
44
44
|
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
-
none: false
|
46
45
|
requirements:
|
47
46
|
- - ">="
|
48
47
|
- !ruby/object:Gem::Version
|
49
|
-
hash: 3
|
50
48
|
segments:
|
51
49
|
- 0
|
52
50
|
version: "0"
|
53
51
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
-
none: false
|
55
52
|
requirements:
|
56
53
|
- - ">="
|
57
54
|
- !ruby/object:Gem::Version
|
58
|
-
hash: 3
|
59
55
|
segments:
|
60
56
|
- 0
|
61
57
|
version: "0"
|
62
58
|
requirements: []
|
63
59
|
|
64
60
|
rubyforge_project:
|
65
|
-
rubygems_version: 1.3.
|
61
|
+
rubygems_version: 1.3.6
|
66
62
|
signing_key:
|
67
63
|
specification_version: 3
|
68
64
|
summary: Clone of the node.js EventEmitter api for Ruby
|
69
65
|
test_files:
|
70
|
-
- test/
|
66
|
+
- test/events_test.rb
|
67
|
+
- test/node_transliterated_test.rb
|