-
Notifications
You must be signed in to change notification settings - Fork 67
/
reduce_if_spec.rb
91 lines (71 loc) · 2.15 KB
/
reduce_if_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require 'spec_helper'
require 'test_doubles'
RSpec.describe LightService::Organizer do
class TestReduceIf
extend LightService::Organizer
def self.call(context)
with(context).reduce(actions)
end
def self.actions
[
TestDoubles::AddsOneAction,
reduce_if(->(ctx) { ctx.number == 1 },
TestDoubles::AddsOneAction)
]
end
end
let(:empty_context) { LightService::Context.make }
it 'reduces if the block evaluates to true' do
result = TestReduceIf.call(:number => 0)
expect(result).to be_success
expect(result[:number]).to eq(2)
end
it 'does not reduce if the block evaluates to false' do
result = TestReduceIf.call(:number => 2)
expect(result).to be_success
expect(result[:number]).to eq(3)
end
it 'will not reduce over a failed context' do
empty_context.fail!('Something bad happened')
result = TestReduceIf.call(empty_context)
expect(result).to be_failure
end
it 'does not reduce over a skipped context' do
empty_context.skip_remaining!('No more needed')
result = TestReduceIf.call(empty_context)
expect(result).to be_success
end
it "knows that it's being conditionally reduced from within an organizer" do
result = TestReduceIf.call(:number => 2)
expect(result.organized_by).to eq TestReduceIf
end
it 'skips actions within in its own scope' do
org = Class.new do
extend LightService::Organizer
def self.call
reduce(actions)
end
# rubocop:disable Metrics/AbcSize
def self.actions
[
reduce_if(
->(c) { !c.nil? },
[
execute(->(c) { c[:first_reduce_if] = true }),
execute(->(c) { c.skip_remaining! }),
execute(->(c) { c[:second_reduce_if] = true })
]
),
execute(->(c) { c[:last_outside] = true })
]
end
# rubocop:enable Metrics/AbcSize
end
result = org.call
aggregate_failures do
expect(result[:first_reduce_if]).to be true
expect(result[:second_reduce_if]).to be_nil
expect(result[:last_outside]).to be true
end
end
end