tilia-event 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,220 @@
1
+ require 'test_helper'
2
+
3
+ module Tilia
4
+ module Event
5
+ class ContinueCallbackTest < Minitest::Test
6
+ def test_init
7
+ ee = EventEmitter.new
8
+ assert_instance_of(EventEmitter, ee)
9
+ end
10
+
11
+ def test_listeners
12
+ ee = EventEmitter.new
13
+ callback1 = -> {}
14
+ callback2 = -> {}
15
+ ee.on('foo', callback1, 200)
16
+ ee.on('foo', callback2, 100)
17
+
18
+ assert_equal([callback2, callback1], ee.listeners('foo'))
19
+ end
20
+
21
+ def test_handle_event
22
+ arg_result = nil
23
+
24
+ ee = EventEmitter.new
25
+ ee.on('foo', ->(arg) { arg_result = arg })
26
+
27
+ assert(ee.emit('foo', ['bar']))
28
+ assert_equal('bar', arg_result)
29
+ end
30
+
31
+ def test_cancel_event
32
+ arg_result = 0
33
+
34
+ ee = EventEmitter.new
35
+ ee.on(
36
+ 'foo',
37
+ lambda do |_|
38
+ arg_result = 1
39
+ false
40
+ end
41
+ )
42
+ ee.on('foo', ->(_) { arg_result = 2 })
43
+
44
+ refute(ee.emit('foo', ['bar']))
45
+ assert_equal(1, arg_result)
46
+ end
47
+
48
+ def test_priority
49
+ arg_result = 0
50
+
51
+ ee = EventEmitter.new
52
+ ee.on(
53
+ 'foo',
54
+ lambda do |_|
55
+ arg_result = 1
56
+ false
57
+ end
58
+ )
59
+ ee.on(
60
+ 'foo',
61
+ lambda do |_|
62
+ arg_result = 2
63
+ false
64
+ end,
65
+ 1
66
+ )
67
+
68
+ refute(ee.emit('foo', ['bar']))
69
+ assert_equal(2, arg_result)
70
+ end
71
+
72
+ def test_priority2
73
+ result = []
74
+ ee = EventEmitter.new
75
+
76
+ ee.on('foo', -> { result << 'a' }, 200)
77
+ ee.on('foo', -> { result << 'b' }, 50)
78
+ ee.on('foo', -> { result << 'c' }, 300)
79
+ ee.on('foo', -> { result << 'd' })
80
+
81
+ ee.emit('foo')
82
+ assert_equal(%w(b d a c), result)
83
+ end
84
+
85
+ def test_remove_listener
86
+ result = false
87
+
88
+ call_back = -> { result = true }
89
+
90
+ ee = EventEmitter.new
91
+
92
+ ee.on('foo', call_back)
93
+ ee.emit('foo')
94
+
95
+ assert(result)
96
+ result = false
97
+
98
+ assert(ee.remove_listener('foo', call_back))
99
+
100
+ ee.emit('foo')
101
+ refute(result)
102
+ end
103
+
104
+ def test_remove_unknown_listener
105
+ result = false
106
+
107
+ call_back = -> { result = true }
108
+
109
+ ee = EventEmitter.new
110
+
111
+ ee.on('foo', call_back)
112
+ ee.emit('foo')
113
+
114
+ assert(result)
115
+ result = false
116
+
117
+ refute(ee.remove_listener('bar', call_back))
118
+
119
+ ee.emit('foo')
120
+ assert(result)
121
+ end
122
+
123
+ def test_remove_listener_twice
124
+ result = false
125
+
126
+ call_back = -> { result = true }
127
+
128
+ ee = EventEmitter.new
129
+
130
+ ee.on('foo', call_back)
131
+ ee.emit('foo')
132
+
133
+ assert(result)
134
+ result = false
135
+
136
+ assert(ee.remove_listener('foo', call_back))
137
+ refute(ee.remove_listener('foo', call_back))
138
+
139
+ ee.emit('foo')
140
+ refute(result)
141
+ end
142
+
143
+ def test_remove_all_listeners
144
+ result = false
145
+
146
+ call_back = -> { result = true }
147
+
148
+ ee = EventEmitter.new
149
+
150
+ ee.on('foo', call_back)
151
+ ee.emit('foo')
152
+
153
+ assert(result)
154
+ result = false
155
+
156
+ ee.remove_all_listeners('foo')
157
+
158
+ ee.emit('foo')
159
+ refute(result)
160
+ end
161
+
162
+ def test_remove_all_listeners_no_arg
163
+ result = false
164
+
165
+ call_back = -> { result = true }
166
+
167
+ ee = EventEmitter.new
168
+
169
+ ee.on('foo', call_back)
170
+ ee.emit('foo')
171
+
172
+ assert(result)
173
+ result = false
174
+
175
+ ee.remove_all_listeners
176
+
177
+ ee.emit('foo')
178
+ refute(result)
179
+ end
180
+
181
+ def test_once
182
+ result = 0
183
+
184
+ call_back = -> { result += 1 }
185
+
186
+ ee = EventEmitter.new
187
+ ee.once('foo', call_back)
188
+
189
+ ee.emit('foo')
190
+ ee.emit('foo')
191
+
192
+ assert_equal(1, result)
193
+ end
194
+
195
+ def test_priority_once
196
+ arg_result = 0
197
+
198
+ ee = EventEmitter.new
199
+ ee.once(
200
+ 'foo',
201
+ lambda do |_|
202
+ arg_result = 1
203
+ false
204
+ end
205
+ )
206
+ ee.once(
207
+ 'foo',
208
+ lambda do |_|
209
+ arg_result = 2
210
+ false
211
+ end,
212
+ 1
213
+ )
214
+
215
+ refute(ee.emit('foo', ['bar']))
216
+ assert_equal(2, arg_result)
217
+ end
218
+ end
219
+ end
220
+ end
@@ -0,0 +1,164 @@
1
+ require 'test_helper'
2
+
3
+ module Tilia
4
+ module Event
5
+ class PromiseTest < Minitest::Test
6
+ def test_success
7
+ final_value = 0
8
+ promise = Promise.new
9
+ promise.fulfill(1)
10
+
11
+ promise.then(->(value) { final_value = value + 2 })
12
+
13
+ assert_equal(3, final_value)
14
+ end
15
+
16
+ def test_fail
17
+ final_value = 0
18
+ promise = Promise.new
19
+ promise.reject(1)
20
+
21
+ promise.then(nil, ->(value) { final_value = value + 2 })
22
+ end
23
+
24
+ def test_chain
25
+ final_value = 0
26
+ promise = Promise.new
27
+ promise.fulfill(1)
28
+
29
+ promise.then(
30
+ ->(value) { final_value = value + 2 }
31
+ ).then(
32
+ ->(value) { final_value = value + 4 }
33
+ )
34
+
35
+ assert_equal(7, final_value)
36
+ end
37
+
38
+ def test_chain_promise
39
+ final_value = 0
40
+ promise = Promise.new
41
+ promise.fulfill(1)
42
+
43
+ sub_promise = Promise.new
44
+
45
+ promise.then(
46
+ ->(_) { sub_promise }
47
+ ).then(
48
+ ->(value) { final_value = value + 4 }
49
+ )
50
+
51
+ sub_promise.fulfill(2)
52
+
53
+ assert_equal(6, final_value)
54
+ end
55
+
56
+ def test_pending_result
57
+ final_value = 0
58
+ promise = Promise.new
59
+
60
+ promise.then(->(value) { final_value = value + 2 })
61
+
62
+ promise.fulfill(4)
63
+ assert_equal(6, final_value)
64
+ end
65
+
66
+ def test_pending_fail
67
+ final_value = 0
68
+ promise = Promise.new
69
+
70
+ promise.then(nil, ->(value) { final_value = value + 2 })
71
+
72
+ promise.reject(4)
73
+ assert_equal(6, final_value)
74
+ end
75
+
76
+ def test_executor_success
77
+ real_result = ''
78
+ _promise = (
79
+ Promise.new(->(success, _failing) { success.call('hi') })
80
+ ).then(
81
+ ->(result) { real_result = result }
82
+ )
83
+
84
+ assert_equal('hi', real_result)
85
+ end
86
+
87
+ def test_executor_fail
88
+ real_result = ''
89
+ _promise = (
90
+ Promise.new(->(_success, failing) { failing.call('hi') })
91
+ ).then(
92
+ ->(_result) { real_result = 'incorrect' },
93
+ ->(reason) { real_result = reason }
94
+ )
95
+
96
+ assert_equal('hi', real_result)
97
+ end
98
+
99
+ def test_fulfill_twice
100
+ promise = Promise.new
101
+ promise.fulfill(1)
102
+ assert_raises(PromiseAlreadyResolvedException) { promise.fulfill(1) }
103
+ end
104
+
105
+ def test_reject_twice
106
+ promise = Promise.new
107
+ promise.reject(1)
108
+ assert_raises(PromiseAlreadyResolvedException) { promise.reject(1) }
109
+ end
110
+
111
+ def test_from_failure_handler
112
+ ok = 0
113
+ promise = Promise.new
114
+ promise.error(
115
+ lambda do |reason|
116
+ assert_equal('foo', reason)
117
+ fail 'hi'
118
+ end
119
+ ).then(
120
+ ->(_) { ok = -1 },
121
+ ->(_) { ok = 1 }
122
+ )
123
+
124
+ assert_equal(0, ok)
125
+ promise.reject('foo')
126
+ assert_equal(1, ok)
127
+ end
128
+
129
+ def test_all
130
+ promise1 = Promise.new
131
+ promise2 = Promise.new
132
+
133
+ final_value = 0
134
+ Promise.all([promise1, promise2]).then(
135
+ ->(value) { final_value = value }
136
+ )
137
+
138
+ promise1.fulfill(1)
139
+ assert_equal(0, final_value)
140
+ promise2.fulfill(2)
141
+ assert_equal([1, 2], final_value)
142
+ end
143
+
144
+ def test_all_reject
145
+ promise1 = Promise.new
146
+ promise2 = Promise.new
147
+
148
+ final_value = 0
149
+ Promise.all([promise1, promise2]).then(
150
+ lambda do |_value|
151
+ final_value = 'foo'
152
+ 'test'
153
+ end,
154
+ ->(value) { final_value = value }
155
+ )
156
+
157
+ promise1.reject(1)
158
+ assert_equal(1, final_value)
159
+ promise2.reject(2)
160
+ assert_equal(1, final_value)
161
+ end
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,4 @@
1
+ require 'simplecov'
2
+ require 'minitest/autorun'
3
+
4
+ require 'tilia/event'
@@ -0,0 +1,13 @@
1
+ require File.join(File.dirname(__FILE__), 'lib', 'tilia', 'event', 'version')
2
+ Gem::Specification.new do |s|
3
+ s.name = 'tilia-event'
4
+ s.version = Tilia::Event::Version::VERSION
5
+ s.licenses = ['BSD-3-Clause']
6
+ s.summary = 'Port of the sabre-event library to ruby'
7
+ s.description = 'tilia-event is a library for lightweight event-based programming.'
8
+ s.author = 'Jakob Sack'
9
+ s.email = 'tilia@jakobsack.de'
10
+ s.files = `git ls-files`.split("\n")
11
+ s.homepage = 'https://github.com/tilia/tilia-event'
12
+ s.add_runtime_dependency 'activesupport', '~> 4.2'
13
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tilia-event
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Jakob Sack
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ description: tilia-event is a library for lightweight event-based programming.
28
+ email: tilia@jakobsack.de
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".gitignore"
34
+ - ".rubocop.yml"
35
+ - ".simplecov"
36
+ - ".travis.yml"
37
+ - CHANGELOG.sabre.md
38
+ - CONTRIBUTING.md
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE
42
+ - LICENSE.sabre
43
+ - README.md
44
+ - Rakefile
45
+ - lib/tilia/event.rb
46
+ - lib/tilia/event/event_emitter.rb
47
+ - lib/tilia/event/event_emitter_interface.rb
48
+ - lib/tilia/event/event_emitter_trait.rb
49
+ - lib/tilia/event/promise.rb
50
+ - lib/tilia/event/promise_already_resolved_exception.rb
51
+ - lib/tilia/event/version.rb
52
+ - test/continue_callback_test.rb
53
+ - test/event_emitter_test.rb
54
+ - test/promise_test.rb
55
+ - test/test_helper.rb
56
+ - tilia-event.gemspec
57
+ homepage: https://github.com/tilia/tilia-event
58
+ licenses:
59
+ - BSD-3-Clause
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.2.5
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Port of the sabre-event library to ruby
81
+ test_files: []
82
+ has_rdoc: