outbacker 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,315 @@
1
+ require 'test_helper'
2
+
3
+ class OutbackerTest < Minitest::Test
4
+ extend DefineTestNamesWithStrings
5
+
6
+ def teardown
7
+ Outbacker.configure do |c|
8
+ c.blacklist = Outbacker::DEFAULT_BLACKLIST
9
+ c.whitelist = Outbacker::DEFAULT_WHITELIST
10
+ end
11
+ end
12
+
13
+ test "the right callbacks are invoked when callbacks are specified as methods" do
14
+ outcomes = random_outcome_list
15
+ input_values = input_list_that_results_in(outcomes)
16
+ expected_blocks_called = expected_blocks_called_list_for(outcomes)
17
+ actual_blocks_called = []
18
+
19
+ input_values.each do |input_value|
20
+ outbacked_domain_object.some_domain_method(input_value) do |on|
21
+ on.outcome_of_outcome_1 do |callback_block_arg|
22
+ actual_blocks_called << 'outcome 1'
23
+ end
24
+
25
+ on.outcome_of_outcome_2 do |callback_block_arg|
26
+ actual_blocks_called << 'outcome 2'
27
+ end
28
+
29
+ on.outcome_of_outcome_3 do |callback_block_arg|
30
+ actual_blocks_called << 'outcome 3'
31
+ end
32
+ end
33
+ end
34
+
35
+ assert_equal expected_blocks_called, actual_blocks_called
36
+ end
37
+
38
+ test "the right callbacks are invoked when callbacks are specified with symbols" do
39
+ outcomes = random_outcome_list
40
+ input_values = input_list_that_results_in(outcomes)
41
+ expected_blocks_called = expected_blocks_called_list_for(outcomes)
42
+ actual_blocks_called = []
43
+
44
+ input_values.each do |input_value|
45
+ outbacked_domain_object.some_domain_method(input_value) do |on_outcome|
46
+ on_outcome.of(:outcome_1) do |callback_block_arg|
47
+ actual_blocks_called << 'outcome 1'
48
+ end
49
+
50
+ on_outcome.of(:outcome_2) do |callback_block_arg|
51
+ actual_blocks_called << 'outcome 2'
52
+ end
53
+
54
+ on_outcome.of(:outcome_3) do |callback_block_arg|
55
+ actual_blocks_called << 'outcome 3'
56
+ end
57
+ end
58
+ end
59
+
60
+ assert_equal expected_blocks_called, actual_blocks_called
61
+ end
62
+
63
+
64
+ test "callback block arguments are passed in correctly" do
65
+ actual_block_arguments_passed = []
66
+
67
+ outbacked_domain_object.some_domain_method("input that will result in outcome 4") do |on|
68
+ on.outcome_of_outcome_4 do |callback_block_arg1, callback_block_arg2, callback_block_arg3|
69
+ actual_block_arguments_passed = [callback_block_arg1, callback_block_arg2, callback_block_arg3]
70
+ end
71
+
72
+ on.outcome_of_outcome_1 do |callback_block_arg|
73
+ actual_block_arguments_passed << [callback_block_arg]
74
+ end
75
+
76
+ on.outcome_of_outcome_2 do |callback_block_arg|
77
+ actual_block_arguments_passed << [callback_block_arg]
78
+ end
79
+ end
80
+
81
+ assert_equal ["outcome 4 block arg1", "outcome 4 block arg2", "outcome 4 block arg3"], actual_block_arguments_passed
82
+ end
83
+
84
+
85
+ test "callback block arguments are passed in correctly when callbacks are specified with symbols" do
86
+ actual_block_arguments_passed = []
87
+
88
+ result = outbacked_domain_object.some_domain_method("input that will result in outcome 4") do |on_outcome|
89
+ on_outcome.of(:outcome_4) do |callback_block_arg1, callback_block_arg2, callback_block_arg3|
90
+ actual_block_arguments_passed = [callback_block_arg1, callback_block_arg2, callback_block_arg3]
91
+ end
92
+
93
+ on_outcome.of(:outcome_1) do |callback_block_arg|
94
+ actual_block_arguments_passed << [callback_block_arg]
95
+ end
96
+
97
+ on_outcome.of(:outcome_2) do |callback_block_arg|
98
+ actual_block_arguments_passed << [callback_block_arg]
99
+ end
100
+ end
101
+
102
+
103
+ assert_equal ["outcome 4 block arg1", "outcome 4 block arg2", "outcome 4 block arg3"], actual_block_arguments_passed
104
+ end
105
+
106
+
107
+ test "exception raised when no callback is provided for the actual outcome" do
108
+ assert_raises(RuntimeError) {
109
+ outbacked_domain_object.some_domain_method("input that will result in outcome 4") do |on_outcome|
110
+ on_outcome.of(:outcome_1) do |callback_block_arg|
111
+ end
112
+
113
+ on_outcome.of(:outcome_2) do |callback_block_arg|
114
+ end
115
+ end
116
+ }
117
+ end
118
+
119
+
120
+ test "exception raised when the actual outcome is handled more than once" do
121
+ assert_raises(RuntimeError) {
122
+ outbacked_domain_object.some_domain_method("input that will result in outcome 4") do |on_outcome|
123
+ on_outcome.of(:outcome_4) do |callback_block_arg|
124
+ end
125
+
126
+ on_outcome.of(:outcome_4) do |callback_block_arg|
127
+ end
128
+ end
129
+ }
130
+ end
131
+
132
+ test "exception raised when no block is provided for a specific outcome and callbacks are specified with symbols" do
133
+ assert_raises(RuntimeError) {
134
+ outbacked_domain_object.some_domain_method("input that will result in outcome 1") do |on_outcome|
135
+ on_outcome.of(:outcome_1)
136
+ end
137
+ }
138
+ end
139
+
140
+ test "exception raised when no block is provided for a specific outcome and callbacks are specified with methods" do
141
+ assert_raises(RuntimeError) {
142
+ outbacked_domain_object.some_domain_method("input that will result in outcome 1") do |on|
143
+ on.outcome_of_outcome_1
144
+ end
145
+ }
146
+ end
147
+
148
+ test "exception raised when no outcome is triggered" do
149
+ assert_raises(RuntimeError) {
150
+ outbacked_domain_object.domain_method_with_no_outcome("input that will result in outcome 1") do |on_outcome|
151
+ on_outcome.of(:outcome_1) do |callback_block_arg|
152
+ end
153
+
154
+ on_outcome.of(:outcome_2) do |callback_block_arg|
155
+ end
156
+ end
157
+ }
158
+ end
159
+
160
+ test "exception raised when multiple outcomes are triggered" do
161
+ assert_raises(RuntimeError) {
162
+ outbacked_domain_object.domain_method_with_multiple_outcomes do |on_outcome|
163
+ on_outcome.of(:outcome_1) do
164
+ end
165
+
166
+ on_outcome.of(:outcome_2) do
167
+ end
168
+ end
169
+ }
170
+ end
171
+
172
+ test "when no callback block is provided it returns the callback key and any arguments that were to be passed to the callback" do
173
+ domain_object = outbacked_domain_object
174
+ outcomes = random_outcome_list
175
+ input_values = input_list_that_results_in(outcomes)
176
+ expected_return_values = outcomes.map { |outcome|
177
+ [:"outcome_#{outcome}", "outcome #{outcome} block argument"]
178
+ }
179
+ actual_return_values = []
180
+
181
+ input_values.each do |input_value|
182
+ actual_return_values << domain_object.some_domain_method(input_value)
183
+ end
184
+
185
+ assert_equal expected_return_values, actual_return_values
186
+ end
187
+
188
+
189
+
190
+ test "including within a subclass of ActiveRecord raises an exception" do
191
+ assert_raises(RuntimeError) {
192
+ class SomeActiveRecordClass < ActiveRecord::Base
193
+ include Outbacker
194
+ end
195
+ }
196
+ end
197
+
198
+
199
+ test "including within a subclass of ActionController raises an exception" do
200
+ assert_raises(RuntimeError) {
201
+ class SomeControllerClass < ActionController::Base
202
+ include Outbacker
203
+ end
204
+ }
205
+ end
206
+
207
+
208
+ test "including within a class that isn't a subclass of ActiveRecord does not raise an exception" do
209
+ class SomeNonActiveRecordOrControllerClass
210
+ include Outbacker
211
+ end
212
+
213
+ assert_respond_to SomeNonActiveRecordOrControllerClass.new, :with
214
+ end
215
+
216
+ test "trying to include a subclass of another blacklisted class raises an exception" do
217
+ class BlacklistedClass
218
+ end
219
+
220
+ Outbacker.configure do |c|
221
+ c.blacklist = [BlacklistedClass]
222
+ end
223
+
224
+ assert_raises(RuntimeError) {
225
+ class MyBlacklistedClass < BlacklistedClass
226
+ include Outbacker
227
+ end
228
+ }
229
+
230
+ end
231
+
232
+ test "when a whitelist is set, trying to include a subclass of a non-whitelisted class raises an exception" do
233
+ class WhitelistedClass
234
+ end
235
+
236
+ class NonWhitelistedClass
237
+ end
238
+
239
+ Outbacker.configure do |c|
240
+ c.whitelist = [WhitelistedClass]
241
+ end
242
+
243
+ assert_raises(RuntimeError) {
244
+ class MyNonWhitelistedClass < NonWhitelistedClass
245
+ include Outbacker
246
+ end
247
+ }
248
+
249
+ end
250
+
251
+ test "when a whitelist is set, trying to include a subclass of a whitelisted class doesn't raise an exception" do
252
+ class WhitelistedClass
253
+ include Outbacker
254
+ end
255
+
256
+ Outbacker.configure do |c|
257
+ c.whitelist = [WhitelistedClass]
258
+ end
259
+
260
+ assert_respond_to WhitelistedClass.new, :with
261
+ end
262
+
263
+
264
+
265
+ private
266
+
267
+ def random_outcome_list
268
+ [*1..3].shuffle
269
+ end
270
+
271
+ def input_list_that_results_in(outcomes)
272
+ outcomes.map { |outcome| "input that will result in outcome #{outcome}" }
273
+ end
274
+
275
+ def expected_blocks_called_list_for(outcomes)
276
+ outcomes.map { |outcome| "outcome #{outcome}" }
277
+ end
278
+
279
+ def outbacked_domain_object
280
+ SomeDomainObject.new
281
+ end
282
+ end
283
+
284
+
285
+ class SomeDomainObject
286
+ include Outbacker
287
+
288
+ def some_domain_method(arg, &outcome_handlers)
289
+ with(outcome_handlers) do |outcomes|
290
+ case arg
291
+ when 'input that will result in outcome 1'
292
+ outcomes.handle :outcome_1, "outcome 1 block argument"
293
+ when 'input that will result in outcome 2'
294
+ outcomes.handle :outcome_2, "outcome 2 block argument"
295
+ when 'input that will result in outcome 3'
296
+ outcomes.handle :outcome_3, "outcome 3 block argument"
297
+ when 'input that will result in outcome 4'
298
+ outcomes.handle :outcome_4, "outcome 4 block arg1", "outcome 4 block arg2", "outcome 4 block arg3"
299
+ end
300
+
301
+ end
302
+ end
303
+
304
+ def domain_method_with_no_outcome(arg, &outcome_handlers)
305
+ with(outcome_handlers) do |outcomes|
306
+ end
307
+ end
308
+
309
+ def domain_method_with_multiple_outcomes(&outcome_handlers)
310
+ with(outcome_handlers) do |outcomes|
311
+ outcomes.handle :outcome_1
312
+ outcomes.handle :outcome_2
313
+ end
314
+ end
315
+ end
@@ -0,0 +1,64 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
4
+ require 'minitest/autorun'
5
+ require 'minitest/pride'
6
+
7
+ #
8
+ # Stub out ActiveRecord just for testing purposes, so we don't
9
+ # need to load and have a dependency on Rails just to test that
10
+ # the module shouldn't be included directly within an
11
+ # ActiveRecord class.
12
+ #
13
+ module ActiveRecord
14
+ class Base
15
+ end
16
+ end
17
+
18
+ module ActionController
19
+ class Base
20
+ end
21
+ end
22
+
23
+ require 'outbacker'
24
+ require 'test_support/outbacker_stub'
25
+
26
+
27
+ #
28
+ # Allow for Rails-style test names, where test names can be defined with
29
+ # strings rather than a Ruby-method name with underscores.
30
+ #
31
+ # Usage:
32
+ #
33
+ # class
34
+ # extend DefineTestNamesWithStrings
35
+ # ...
36
+ # test "a descriptive test name" do
37
+ # ...
38
+ # end
39
+ # end
40
+ #
41
+ # Note: We could have just pulled this in from ActiveSupport::TestCase,
42
+ # but I wanted to avoid the dependency.
43
+ #
44
+ module DefineTestNamesWithStrings
45
+
46
+ # Helper to define a test method using a String. Under the hood, it replaces
47
+ # spaces with underscores and defines the test method.
48
+ #
49
+ # test "verify something" do
50
+ # ...
51
+ # end
52
+ def test(name, &block)
53
+ test_name = "test_#{name.gsub(/\s+|,/,'_')}".to_sym
54
+ defined = method_defined? test_name
55
+ raise "#{test_name} is already defined in #{self}" if defined
56
+ if block_given?
57
+ define_method(test_name, &block)
58
+ else
59
+ define_method(test_name) do
60
+ flunk "No implementation provided for #{name}"
61
+ end
62
+ end
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: outbacker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Garcia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '5.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '5.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: m
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.3.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: configurations
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 2.2.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 2.2.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: codeclimate-test-reporter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: |2
112
+ A micro library to keep conditional logic out of your Rails
113
+ controllers and help you write more intention-revealing
114
+ code with both skinny controllers and skinny models.
115
+ email:
116
+ - polypressure@outlook.com
117
+ executables: []
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - .gitignore
122
+ - .travis.yml
123
+ - CHANGELOG.md
124
+ - Gemfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - examples/app/controllers/appointments_controller.rb
129
+ - examples/app/domain/appointment_calendar.rb
130
+ - examples/config/initializers/outbacker.rb
131
+ - examples/test/controllers/appointments_controller_test.rb
132
+ - lib/outbacker.rb
133
+ - lib/outbacker/version.rb
134
+ - lib/test_support/outbacker_stub.rb
135
+ - outbacker.gemspec
136
+ - test/outbacker_stub_test.rb
137
+ - test/outbacker_test.rb
138
+ - test/test_helper.rb
139
+ homepage: https://github.com/polypressure/outbacker
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - '>='
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.2.2
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Drive complexity out of your Rails controllers once and for all, while keeping
163
+ your models fit and trim.
164
+ test_files:
165
+ - test/outbacker_stub_test.rb
166
+ - test/outbacker_test.rb
167
+ - test/test_helper.rb