standard-procedure-plumbing 0.4.2 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,109 +0,0 @@
1
- require "spec_helper"
2
-
3
- RSpec.describe "Rubber Duck examples" do
4
- it "casts objects into duck types" do
5
- # standard:disable Lint/ConstantDefinitionInBlock
6
- module DuckExample
7
- Person = Plumbing::RubberDuck.define :first_name, :last_name, :email
8
- LikesFood = Plumbing::RubberDuck.define :favourite_food
9
-
10
- PersonData = Struct.new(:first_name, :last_name, :email, :favourite_food)
11
- CarData = Struct.new(:make, :model, :colour)
12
- end
13
-
14
- # standard:enable Lint/ConstantDefinitionInBlock
15
-
16
- @porsche_911 = DuckExample::CarData.new "Porsche", "911", "black"
17
- expect { @porsche_911.as DuckExample::Person }.to raise_error(TypeError)
18
-
19
- @alice = DuckExample::PersonData.new "Alice", "Aardvark", "alice@example.com", "Ice cream"
20
-
21
- @person = @alice.as DuckExample::Person
22
- expect(@person.first_name).to eq "Alice"
23
- expect(@person.email).to eq "alice@example.com"
24
- expect { @person.favourite_food }.to raise_error(NoMethodError)
25
-
26
- @hungry = @person.as DuckExample::LikesFood
27
- expect(@hungry.favourite_food).to eq "Ice cream"
28
- end
29
-
30
- it "casts objects into modules" do
31
- # standard:disable Lint/ConstantDefinitionInBlock
32
- module ModuleExample
33
- module Person
34
- def first_name = @first_name
35
-
36
- def last_name = @last_name
37
-
38
- def email = @email
39
- end
40
-
41
- module LikesFood
42
- def favourite_food = @favourite_food
43
- end
44
-
45
- PersonData = Struct.new(:first_name, :last_name, :email, :favourite_food)
46
- CarData = Struct.new(:make, :model, :colour)
47
- end
48
- # standard:enable Lint/ConstantDefinitionInBlock
49
- @porsche_911 = ModuleExample::CarData.new "Porsche", "911", "black"
50
- expect { @porsche_911.as ModuleExample::Person }.to raise_error(TypeError)
51
-
52
- @alice = ModuleExample::PersonData.new "Alice", "Aardvark", "alice@example.com", "Ice cream"
53
-
54
- @person = @alice.as ModuleExample::Person
55
- expect(@person.first_name).to eq "Alice"
56
- expect(@person.email).to eq "alice@example.com"
57
- expect { @person.favourite_food }.to raise_error(NoMethodError)
58
-
59
- @hungry = @person.as ModuleExample::LikesFood
60
- expect(@hungry.favourite_food).to eq "Ice cream"
61
- end
62
-
63
- it "casts objects into clases" do
64
- # standard:disable Lint/ConstantDefinitionInBlock
65
- module ClassExample
66
- class Person
67
- def initialize first_name, last_name, email
68
- @first_name = first_name
69
- @last_name = last_name
70
- @email = email
71
- end
72
-
73
- attr_reader :first_name
74
- attr_reader :last_name
75
- attr_reader :email
76
- end
77
-
78
- class PersonWhoLikesFood < Person
79
- def initialize first_name, last_name, email, favourite_food
80
- super(first_name, last_name, email)
81
- @favourite_food = favourite_food
82
- end
83
-
84
- attr_reader :favourite_food
85
- end
86
-
87
- class CarData
88
- def initialize make, model, colour
89
- @make = make
90
- @model = model
91
- @colour = colour
92
- end
93
- end
94
- end
95
- # standard:enable Lint/ConstantDefinitionInBlock
96
- @porsche_911 = ClassExample::CarData.new "Porsche", "911", "black"
97
- expect { @porsche_911.as ClassExample::Person }.to raise_error(TypeError)
98
-
99
- @alice = ClassExample::PersonWhoLikesFood.new "Alice", "Aardvark", "alice@example.com", "Ice cream"
100
-
101
- @person = @alice.as ClassExample::Person
102
- expect(@person.first_name).to eq "Alice"
103
- expect(@person.email).to eq "alice@example.com"
104
- expect { @person.favourite_food }.to raise_error(NoMethodError)
105
-
106
- @hungry = @person.as ClassExample::PersonWhoLikesFood
107
- expect(@hungry.favourite_food).to eq "Ice cream"
108
- end
109
- end
@@ -1,110 +0,0 @@
1
- RSpec.shared_examples "a pipe" do
2
- it "adds a block observer" do
3
- @pipe = described_class.start
4
- @observer = await do
5
- @pipe.add_observer do |event|
6
- puts event.type
7
- end
8
- end
9
- expect(await { @pipe.is_observer?(@observer) }).to eq true
10
- end
11
-
12
- it "adds a callable observer" do
13
- @pipe = described_class.start
14
- @proc = ->(event) { puts event.type }
15
-
16
- @pipe.add_observer @proc
17
-
18
- expect(await { @pipe.is_observer?(@proc) }).to eq true
19
- end
20
-
21
- it "does not allow an observer without a #call method" do
22
- @pipe = described_class.start
23
-
24
- expect { await { @pipe.add_observer(Object.new) } }.to raise_error(TypeError)
25
- end
26
-
27
- it "removes an observer" do
28
- @pipe = described_class.start
29
- @proc = ->(event) { puts event.type }
30
-
31
- @pipe.remove_observer @proc
32
-
33
- expect(await { @pipe.is_observer?(@proc) }).to eq false
34
- end
35
-
36
- it "does not send notifications for objects which are not events" do
37
- @pipe = described_class.start
38
- @results = []
39
- @observer = @pipe.add_observer do |event|
40
- @results << event
41
- end
42
-
43
- @pipe << Object.new
44
-
45
- sleep 0.1
46
- expect(@results).to eq []
47
- end
48
-
49
- it "notifies block observers" do
50
- @pipe = described_class.start
51
- @results = []
52
- @observer = @pipe.add_observer do |event|
53
- @results << event
54
- end
55
-
56
- @first_event = Plumbing::Event.new type: "first_event", data: {test: "event"}
57
- @second_event = Plumbing::Event.new type: "second_event", data: {test: "event"}
58
-
59
- @pipe << @first_event
60
- expect([@first_event]).to become_equal_to { @results }
61
-
62
- @pipe << @second_event
63
- expect([@first_event, @second_event]).to become_equal_to { @results }
64
- end
65
-
66
- it "notifies callable observers" do
67
- @pipe = described_class.start
68
- @results = []
69
- @observer = ->(event) { @results << event }
70
- @pipe.add_observer @observer
71
-
72
- @first_event = Plumbing::Event.new type: "first_event", data: {test: "event"}
73
- @second_event = Plumbing::Event.new type: "second_event", data: {test: "event"}
74
-
75
- @pipe << @first_event
76
- expect([@first_event]).to become_equal_to { @results }
77
-
78
- @pipe << @second_event
79
- expect([@first_event, @second_event]).to become_equal_to { @results }
80
- end
81
-
82
- it "ensures all observers are notified even if an observer raises an exception" do
83
- @pipe = described_class.start
84
- @results = []
85
- @failing_observer = @pipe.add_observer do |event|
86
- raise "Failed processing #{event.type}"
87
- end
88
- @working_observer = @pipe.add_observer do |event|
89
- @results << event
90
- end
91
-
92
- @event = Plumbing::Event.new type: "event", data: {test: "event"}
93
-
94
- @pipe << @event
95
-
96
- expect([@event]).to become_equal_to { @results }
97
- end
98
-
99
- it "shuts down the pipe" do
100
- @pipe = described_class.start
101
- @results = []
102
- @observer = ->(event) { @results << event }
103
- @pipe.add_observer @observer
104
-
105
- @pipe.shutdown
106
- @pipe.notify "ignore_me"
107
- sleep 0.2
108
- expect(@results).to be_empty
109
- end
110
- end
@@ -1,159 +0,0 @@
1
- require "spec_helper"
2
- require_relative "../../../lib/plumbing/actor/transporter"
3
-
4
- RSpec.describe Plumbing::Actor::Transporter do
5
- # standard:disable Lint/ConstantDefinitionInBlock
6
- class Record
7
- include GlobalID::Identification
8
- attr_reader :id
9
- def initialize id
10
- @id = id
11
- end
12
-
13
- def == other
14
- other.id == @id
15
- end
16
- end
17
- # standard:enable Lint/ConstantDefinitionInBlock
18
-
19
- before do
20
- GlobalID.app = "rspec"
21
- GlobalID::Locator.use :rspec do |gid, options|
22
- Record.new gid.model_id
23
- end
24
- end
25
-
26
- context "marshalling" do
27
- it "passes simple arguments" do
28
- @transporter = described_class.new
29
-
30
- @transport = @transporter.marshal "Hello"
31
- expect(@transport).to eq ["Hello"]
32
-
33
- @transport = @transporter.marshal 1, 2, 3
34
- expect(@transport).to eq [1, 2, 3]
35
- end
36
-
37
- it "copies arrays" do
38
- @transporter = described_class.new
39
-
40
- @source = [[1, 2, 3], [:this, :that]]
41
-
42
- @transport = @transporter.marshal(*@source)
43
- expect(@transport).to eq @source
44
- expect(@transport.first.object_id).to_not eq @source.first.object_id
45
- expect(@transport.last.object_id).to_not eq @source.last.object_id
46
- end
47
-
48
- it "copies hashss" do
49
- @transporter = described_class.new
50
-
51
- @source = [{first: "1", second: 2}]
52
-
53
- @transport = @transporter.marshal(*@source)
54
- expect(@transport).to eq @source
55
- expect(@transport.first.object_id).to_not eq @source.first.object_id
56
- end
57
-
58
- it "converts objects to Global ID strings" do
59
- @transporter = described_class.new
60
-
61
- @record = Record.new 123
62
- @global_id = @record.to_global_id.to_s
63
-
64
- @transport = @transporter.marshal @record
65
-
66
- expect(@transport).to eq [@global_id]
67
- end
68
-
69
- it "converts objects within arrays to Global ID strings" do
70
- @transporter = described_class.new
71
-
72
- @record = Record.new 123
73
- @global_id = @record.to_global_id.to_s
74
-
75
- @transport = @transporter.marshal [:this, @record]
76
-
77
- expect(@transport).to eq [[:this, @global_id]]
78
- end
79
-
80
- it "converts objects within hashes to Global ID strings" do
81
- @transporter = described_class.new
82
-
83
- @record = Record.new 123
84
- @global_id = @record.to_global_id.to_s
85
-
86
- @transport = @transporter.marshal this: "that", the_other: {embedded: @record}
87
-
88
- expect(@transport).to eq [{this: "that", the_other: {embedded: @global_id}}]
89
- end
90
- end
91
-
92
- context "unmarshalling" do
93
- it "passes simple arguments" do
94
- @transporter = described_class.new
95
-
96
- @transport = @transporter.unmarshal "Hello"
97
- expect(@transport).to eq ["Hello"]
98
-
99
- @transport = @transporter.unmarshal 1, 2, 3
100
- expect(@transport).to eq [1, 2, 3]
101
- end
102
-
103
- it "passes arrays" do
104
- @transporter = described_class.new
105
-
106
- @transport = @transporter.unmarshal [1, 2, 3], [:this, :that]
107
-
108
- expect(@transport.first.object_id).to_not eq [1, 2, 3]
109
- expect(@transport.last.object_id).to_not eq [:this, :that]
110
- end
111
-
112
- it "passes hashss and keyword arguments" do
113
- @transporter = described_class.new
114
-
115
- @transport = @transporter.unmarshal first: "1", second: 2
116
- expect(@transport).to eq [{first: "1", second: 2}]
117
- end
118
-
119
- it "passes mixtures of arrays and hashes" do
120
- @transporter = described_class.new
121
-
122
- @transport = @transporter.unmarshal :this, :that, first: "1", second: 2
123
- expect(@transport).to eq [:this, :that, {first: "1", second: 2}]
124
- end
125
-
126
- it "converts Global ID strings to objects" do
127
- @transporter = described_class.new
128
-
129
- @record = Record.new "123"
130
- @global_id = @record.to_global_id.to_s
131
-
132
- @transport = @transporter.unmarshal @global_id
133
-
134
- expect(@transport).to eq [@record]
135
- end
136
-
137
- it "converts Global ID strings within arrays to objects" do
138
- @transporter = described_class.new
139
-
140
- @record = Record.new "123"
141
- @global_id = @record.to_global_id.to_s
142
-
143
- @transport = @transporter.unmarshal :this, @global_id
144
-
145
- expect(@transport).to eq [:this, @record]
146
- end
147
-
148
- it "converts Global ID strings within hashes to objects" do
149
- @transporter = described_class.new
150
-
151
- @record = Record.new "123"
152
- @global_id = @record.to_global_id.to_s
153
-
154
- @transport = @transporter.unmarshal this: "that", the_other: {embedded: @global_id}
155
-
156
- expect(@transport).to eq [{this: "that", the_other: {embedded: @record}}]
157
- end
158
- end
159
- end