less_interactions 0.1.1 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f66dc3d8874f056ca707580b54ba75439df9547
4
- data.tar.gz: 218adf942ea75603a46a59af10738475adcc6c51
3
+ metadata.gz: 4d6950164589b4d646f7ef8f183b9642c13dde61
4
+ data.tar.gz: 51cdc8d312de5087272ada094908634c0cace295
5
5
  SHA512:
6
- metadata.gz: 10d99fdaaee88851a7630740dee9e1c369ef8fcb25afa12a3ad0d1318167791ffecc9fcf2c2d18805c04bf4684f123b1121273d2afbb186b3c2ebabaf47f627a
7
- data.tar.gz: 2c1ef12cdf3604fc9e6282d88d46741805741345a1f136fa9186a9f1d20dcd3a1d1df74e600a352a6e5d045535f71a6d151f193b1978e9744e9e547183789a50
6
+ metadata.gz: 6630b11347f16225b5a8242b55bf5fbaa265f55a53c2e236a0db9172e2b238673aa19dd63ff8444694e8d7dc1bd9de831251072d73440932a21e3a8b43fdfc35
7
+ data.tar.gz: 5c50e9a6a8f9201b74eaf8535e3bff276e5d221e25b1cfda418edf1be19f3c2e4c605b7d17d2c48b505d1987a9c96dc1141a97d899e7731b730fb0d68f723f7d
data/README.rdoc CHANGED
@@ -82,7 +82,14 @@ After:
82
82
  #only one of these is required
83
83
  expects_any :need_this, :or, :this
84
84
 
85
- # method chain in use case is easy to see.
85
+ # A way to document what you are expecting to be returned
86
+ # Also creates setter and getter methods for each
87
+ returns :user
88
+ returns :was_this_successful
89
+
90
+ # Method chain in use case is easy to see.
91
+ # To have access to the getter and setter methods created with returns
92
+ # you need the run method to return self
86
93
  def run
87
94
  do_somthing_with some_other_thing_the_interaction_needs
88
95
  build_or_find_an_expense
@@ -93,8 +100,9 @@ After:
93
100
  update_budgets
94
101
  update_bank_accounts
95
102
  @expense
103
+ self
96
104
  end
97
-
105
+
98
106
  private
99
107
 
100
108
  # implementation of other methods
@@ -1,4 +1,7 @@
1
1
 
2
2
  require 'bundler/setup'
3
3
  require 'less_interactions/interaction'
4
- require 'less_interactions/response'
4
+ require 'less_interactions/response'
5
+ require 'less_interactions/expectation'
6
+ require 'less_interactions/multiple_choice_expectation'
7
+ require 'less_interactions/expectation_array'
@@ -0,0 +1,33 @@
1
+ # Verifies that some conditions are met for a member of a parameters hash
2
+ module Less
3
+ class Expectation
4
+ attr_reader :parameter
5
+ def initialize parameter, options = {}
6
+ @parameter = parameter
7
+ @allow_nil = options[:allow_nil]
8
+ end
9
+
10
+ def verify(params)
11
+ unless verifies_expectations?(params)
12
+ raise MissingParameterError, "Parameter empty :#{@parameter}"
13
+ end
14
+ end
15
+
16
+ def allows_nil?
17
+ @allow_nil
18
+ end
19
+
20
+ private
21
+
22
+ def verifies_expectations?(params)
23
+ if @allow_nil == nil || @allow_nil == false
24
+ params.has_key?(@parameter) && params[@parameter] != nil
25
+ else
26
+ true
27
+ end
28
+ end
29
+ end
30
+ class MissingParameterError < StandardError; end
31
+ end
32
+
33
+
@@ -0,0 +1,11 @@
1
+ module Less
2
+ class ExpectationArray < Array
3
+
4
+ def verify!(all_params)
5
+ self.each do |expectation|
6
+ expectation.verify(all_params)
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -1,4 +1,3 @@
1
-
2
1
  module Less
3
2
  class Interaction
4
3
  # Initialize the objects for an interaction.
@@ -12,18 +11,11 @@ module Less
12
11
  options[:context] = context # add context to the options so will get the ivar and getter
13
12
  end
14
13
 
15
- ex = self.class.expectations.dup
16
- n = ex.keep_if {|name, allow_nil| allow_nil.has_key?(:allow_nil) && allow_nil[:allow_nil]}
17
- nils = {}
18
- n.each do |name, val|
19
- nils[name] = nil
20
- end
21
- self.class.any_expectations.flatten.each {|e| nils[e] = nil}
22
-
23
- nils.merge(options).each do |name, value|
24
- instance_variable_set "@#{name}", value
25
- if respond_to?( "#{name}=" ) && !value.nil?
26
- send "#{name}=", value
14
+ self.all_params = options
15
+ set_instance_variables
16
+ options.each do |name, value|
17
+ if respond_to?( "#{name}=" )
18
+ send "#{name}=", value
27
19
  end
28
20
  end
29
21
  end
@@ -42,11 +34,11 @@ module Less
42
34
 
43
35
  # Run your interaction.
44
36
  # @param [Object] context
45
- # @param [Hash] options
37
+ # @param [Hash] params
46
38
  #
47
- # This will initialize your interaction with the options you pass to it and then call its {#run} method.
48
- def self.run(context = {}, options = {})
49
- me = new(context, options)
39
+ # This will initialize your interaction with the params you pass to it and then call its {#run} method.
40
+ def self.run(context = {}, params = {})
41
+ me = new(context, params)
50
42
  me.send :expectations_met?
51
43
  me.init
52
44
  me.run
@@ -66,69 +58,83 @@ module Less
66
58
  else
67
59
  options = {}
68
60
  end
69
- __setup_expecations parameters do |parameter|
61
+
62
+ parameters.each do |parameter|
63
+ add_reader(parameter)
70
64
  add_expectation(parameter, options)
71
65
  end
72
66
  end
73
67
 
74
68
  def self.expects_any *parameters
75
- __setup_expecations parameters do |parameter|
69
+ parameters.each do |parameter|
70
+ add_reader(parameter)
76
71
  end
77
72
  add_any_expectation(parameters)
78
73
  end
79
74
 
80
-
75
+ # Make an attr_accessor alias for what you are expecting to be returned
76
+ # Need to return self in the run method for to use this
77
+ def self.returns(*args)
78
+ attr_accessor(*args)
79
+ end
81
80
 
82
81
  private
83
82
 
84
- def self.__setup_expecations parameters
85
- parameters.each do |param|
86
- methods = (self.instance_methods + self.private_instance_methods)
87
- self.send(:attr_reader, param.to_sym) unless methods.member?(param.to_sym)
88
- yield param if block_given?
89
- end
83
+ def all_params
84
+ @__all_params
90
85
  end
91
86
 
92
- def self.add_expectation(parameter, options)
93
- expectations[parameter] = options
87
+ def all_params=(p)
88
+ @__all_params = p
94
89
  end
95
90
 
96
- def self.add_any_expectation(parameters)
97
- any_expectations << parameters
91
+ def set_instance_variables
92
+ all_params.each do |name, value|
93
+ instance_variable_set "@#{name}", value
94
+ end
95
+
96
+ self.class.expectations.each do |expectation|
97
+ name = expectation.parameter
98
+ if all_params.has_key?(name)
99
+ instance_variable_set "@#{name}", all_params[name]
100
+ elsif expectation.allows_nil?
101
+ instance_variable_set "@#{name}", nil
102
+ end
103
+ end
98
104
  end
99
105
 
100
- def expectations_met?
101
- __expects_mets? && __expects_any_mets?
106
+ def self.add_reader param
107
+ methods = (self.instance_methods + self.private_instance_methods)
108
+ self.send(:attr_reader, param.to_sym) unless methods.member?(param.to_sym)
102
109
  end
103
110
 
104
- def __expects_any_mets?
105
- self.class.any_expectations.each do |any_set|
106
- if any_set.all? {|e| instance_variable_get("@#{e}").nil?}
107
- raise MissingParameterError, "Parameters empty :#{any_set.to_s} (At least one of these must not be nil)"
108
- end
111
+ def self.add_expectation(parameter, options)
112
+ ex = Expectation.new(parameter, options)
113
+ if expectations.none? { |e| e.parameter == parameter }
114
+ expectations << ex
109
115
  end
110
- true
111
116
  end
112
117
 
113
- def __expects_mets?
114
- self.class.expectations.each do |param, param_options|
115
- unless param_options[:allow_nil]
116
- raise MissingParameterError, "Parameter empty :#{param.to_s}" if instance_variable_get("@#{param}").nil?
117
- end
118
+ def self.add_any_expectation(parameters)
119
+ new_ex = MultipleChoiceExpectation.new(parameters)
120
+ if any_expectations.none? {|ex| ex.parameters == new_ex.parameters }
121
+ any_expectations << new_ex
118
122
  end
119
- true
123
+ end
124
+
125
+ def expectations_met?
126
+ self.class.any_expectations.verify!(all_params) && self.class.expectations.verify!(all_params)
120
127
  end
121
128
 
122
129
  def self.any_expectations
123
- @any_expectations ||= []
130
+ @any_expectations ||= ExpectationArray.new()
124
131
  end
125
132
 
126
133
  def self.expectations
127
- @expectations ||= {}
134
+ @expectations ||= ExpectationArray.new()
128
135
  end
129
136
  end
130
137
 
131
138
 
132
139
  class InvalidInteractionError < StandardError; end
133
- class MissingParameterError < StandardError; end
134
140
  end
@@ -0,0 +1,32 @@
1
+ #Accepts multiple parameters and validates them to ensure at least one is present and not nil.
2
+ #Raise MissingParameterError if the correct parameters are not present.
3
+ module Less
4
+ class MultipleChoiceExpectation
5
+ attr_reader :parameters
6
+
7
+ def initialize parameters
8
+ @parameters = parameters
9
+ end
10
+
11
+ def verify(hash_to_verify)
12
+ unless verifies_expectations?(hash_to_verify)
13
+ raise MissingParameterError, "Parameters empty #{@parameters.map(&:to_sym)} (At least one of these must not be nil)"
14
+ end
15
+ end
16
+
17
+ def verifies_expectations?(hash_to_verify)
18
+ valid = @parameters.any? do |parameter|
19
+ hash_to_verify.has_key?(parameter) && !hash_to_verify[parameter].nil?
20
+ end
21
+ return valid
22
+ end
23
+
24
+ def all_params_are_not_nil?(hash_to_verify)
25
+ hash_to_verify.any? do |k,v|
26
+ v.nil?
27
+ end
28
+ end
29
+ end
30
+
31
+ class MissingParameterError < StandardError; end
32
+ end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Less
2
2
  class Interaction
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -0,0 +1,44 @@
1
+ require 'test_helper'
2
+ class ExpectationTest < Minitest::Test
3
+ include Less
4
+
5
+ should "parameters hash can meet an expectation" do
6
+ ex = Expectation.new(:name)
7
+ params = {name: "Mike", age: 27}
8
+ assert_nothing_raised do
9
+ ex.verify(params)
10
+ end
11
+ end
12
+
13
+ should "parameters hash fails when not meeting an expectation" do
14
+ ex = Expectation.new(:name)
15
+ params = {name: nil, age: 27}
16
+ assert_raises(MissingParameterError) do
17
+ ex.verify(params)
18
+ end
19
+ end
20
+
21
+ should "parameters hash fails if expectation is absent" do
22
+ ex = Expectation.new(:name)
23
+ params = {age: 27}
24
+ assert_raises(MissingParameterError) do
25
+ ex.verify(params)
26
+ end
27
+ end
28
+
29
+ should "parameters should not fail if allow nil is true parameters value is nil" do
30
+ ex = Expectation.new(:name, allow_nil: true)
31
+ params = {name: nil}
32
+ assert_nothing_raised do
33
+ ex.verify(params)
34
+ end
35
+ end
36
+
37
+ should "parameters should not fail if allow nil is true and no parameters are passed" do
38
+ ex = Expectation.new(:name, allow_nil: true)
39
+ params = {}
40
+ assert_nothing_raised do
41
+ ex.verify(params)
42
+ end
43
+ end
44
+ end
@@ -33,16 +33,6 @@ class InteractionTest < Minitest::Test
33
33
  assert_equal 1, i.instance_variable_get(:@a)
34
34
  end
35
35
 
36
- should "call the writer if there is one" do
37
- class InteractionWithWriter < Interaction
38
- expects :a, allow_nil: true
39
- def run; self; end
40
- def a= val; @a += 1; end
41
- end
42
- i = InteractionWithWriter.run a: 1
43
- assert_equal 2, i.instance_variable_get(:@a)
44
- end
45
-
46
36
  should "call the writer if there is one and value is not null" do
47
37
  class GobbledyGook; def gobble; "gook"; end; end
48
38
  class InteractionWithWriter < Interaction
@@ -80,39 +70,39 @@ class InteractionTest < Minitest::Test
80
70
  end
81
71
 
82
72
  should "run if an expected parameter is found" do
83
- class InteractionWithParameter < Less::Interaction
73
+ class InteractionWithParameterA < Less::Interaction
84
74
  expects :title
85
75
 
86
76
  def run; end
87
77
  end
88
- assert_nothing_raised { InteractionWithParameter.run(:title => "Hello, test") }
78
+ assert_nothing_raised { InteractionWithParameterA.run(:title => "Hello, test") }
89
79
  end
90
80
 
91
81
  should "run if an expected parameter is found, even if it is false" do
92
- class InteractionWithParameter < Less::Interaction
82
+ class InteractionWithParameterB < Less::Interaction
93
83
  expects :title
94
84
 
95
85
  def run; end
96
86
  end
97
- assert_nothing_raised { InteractionWithParameter.run(:title => false) }
87
+ assert_nothing_raised { InteractionWithParameterB.run(:title => false) }
98
88
  end
99
89
 
100
90
  should "run if an expected parameter is found, even if it is nil, if the option is specified" do
101
- class InteractionWithParameter < Less::Interaction
91
+ class InteractionWithParameterC < Less::Interaction
102
92
  expects :title, :allow_nil => true
103
93
 
104
94
  def run;end
105
95
  end
106
- assert_nothing_raised { InteractionWithParameter.run(:title => nil) }
96
+ assert_nothing_raised { InteractionWithParameterC.run(:title => nil) }
107
97
  end
108
98
 
109
99
  should "run if an expected parameter is found, even if it is nil, if the option is not specified, but is allow_nil and is called" do
110
- class InteractionWithParameter < Less::Interaction
100
+ class InteractionWithParameterD < Less::Interaction
111
101
  expects :title, :allow_nil => true
112
102
 
113
103
  def run; title;end
114
104
  end
115
- assert_nothing_raised { InteractionWithParameter.run() }
105
+ assert_nothing_raised { InteractionWithParameterD.run() }
116
106
  end
117
107
 
118
108
  should "set ivars from options on initialize" do
@@ -121,7 +111,6 @@ class InteractionTest < Minitest::Test
121
111
  assert_equal 2, i.instance_variable_get(:@b)
122
112
  end
123
113
 
124
-
125
114
  should "Convert first param to context on initialize" do
126
115
  i = Less::Interaction.new 1, b:2
127
116
  assert_equal 1, i.instance_variable_get(:@context)
@@ -143,7 +132,6 @@ class InteractionTest < Minitest::Test
143
132
  end
144
133
 
145
134
  should "be able to override an expects" do
146
-
147
135
  class OverrideExpects < Less::Interaction
148
136
  expects :object, allow_nil: true
149
137
 
@@ -183,7 +171,6 @@ class InteractionTest < Minitest::Test
183
171
  assert_equal "1", x.send(:int)
184
172
  end
185
173
 
186
-
187
174
  should "fail if all expects_any parameters are nil" do
188
175
  class AnyInteractionWithAllNilParameters < Less::Interaction
189
176
  expects_any :title, :a, :b
@@ -210,4 +197,52 @@ class InteractionTest < Minitest::Test
210
197
  assert_raises(MissingParameterError) { ExpectationsMetBeforeInit.run() }
211
198
  end
212
199
 
200
+ should "call the writer if there is one" do
201
+ class InteractionWithWriter < Interaction
202
+ expects :a, allow_nil: true
203
+ def run; self; end
204
+ def a= val; @a += 1; end
205
+ end
206
+ i = InteractionWithWriter.run a: 1
207
+ assert_equal 2, i.instance_variable_get(:@a)
208
+ end
209
+
210
+ should "call the writer even if the value of the param is nil" do
211
+ class InteractionWithWriterb < Interaction
212
+ expects :a, allow_nil: true
213
+ def run; self; end
214
+ def a= val; @a = 1; end
215
+ end
216
+ i = InteractionWithWriterb.run a: nil
217
+ assert_equal 1, i.instance_variable_get(:@a)
218
+ end
219
+
220
+ should "not call the writer if the param is not passed in" do
221
+ class InteractionWithWriterc < Interaction
222
+ expects :a, allow_nil: true
223
+ def run; self; end
224
+ def a= val; @a = 1; end
225
+ end
226
+ i = InteractionWithWriterc.run
227
+ assert_equal nil, i.instance_variable_get(:@a)
228
+ end
229
+
230
+ should "set a getter when returns is called" do
231
+ class InteractionWithReturns < Interaction
232
+ returns :a
233
+ def run; self; end
234
+ end
235
+ i = InteractionWithReturns.run(a: 1)
236
+ assert_equal 1, i.a
237
+ end
238
+
239
+ should "not set a getter if returns is not called" do
240
+ class InteractionWithReturns2 < Interaction
241
+ def run; self; end
242
+ end
243
+ i = InteractionWithReturns2.run(a: 1)
244
+ assert_raises NoMethodError do
245
+ i.a
246
+ end
247
+ end
213
248
  end
@@ -0,0 +1,61 @@
1
+ require 'test_helper'
2
+ class MultipleChoiceExpectation < Minitest::Test
3
+ include Less
4
+
5
+ should "parameters hash can meet an expectation" do
6
+ ex = MultipleChoiceExpectation.new([:name, :user, :id])
7
+ params = {name: "Mike"}
8
+ assert_nothing_raised do
9
+ ex.verify(params)
10
+ end
11
+ end
12
+
13
+ should "parameters hash fails if all parameters are nil" do
14
+ ex = MultipleChoiceExpectation.new([:name, :user, :id])
15
+ params = {name: nil, age: nil}
16
+ assert_raises (MissingParameterError) do
17
+ ex.verify(params)
18
+ end
19
+ end
20
+
21
+ should "parameters hash should pass when one parameter is not nil" do
22
+ ex = MultipleChoiceExpectation.new([:name, :age, :id])
23
+ params = {name: nil, age: 55}
24
+ assert_nothing_raised do
25
+ ex.verify(params)
26
+ end
27
+ end
28
+
29
+ should "parameters hash should pass when the second parameter is not nil" do
30
+ ex = MultipleChoiceExpectation.new([:name, :age, :id])
31
+ params = {age: 12}
32
+ assert_nothing_raised do
33
+ ex.verify(params)
34
+ end
35
+ end
36
+
37
+ should "parameters hash should pass even if some parameters are missing" do
38
+ ex = MultipleChoiceExpectation.new([:name, :age, :id])
39
+ params = {name: "Michael"}
40
+ assert_nothing_raised do
41
+ ex.verify(params)
42
+ end
43
+ end
44
+
45
+ should "parameters hash should fail if no parameters are passed in" do
46
+ ex = MultipleChoiceExpectation.new([:name, :age, :id])
47
+ params = {}
48
+ assert_raises (MissingParameterError) do
49
+ ex.verify(params)
50
+ end
51
+ end
52
+
53
+ should "parameters hash should fail if parameter is present and nil" do
54
+ ex = MultipleChoiceExpectation.new([:name, :age, :id])
55
+ params = {age: nil}
56
+ assert_raises (MissingParameterError) do
57
+ ex.verify(params)
58
+ end
59
+ end
60
+
61
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: less_interactions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugen Minciu
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-02-23 00:00:00.000000000 Z
12
+ date: 2016-05-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -62,10 +62,15 @@ files:
62
62
  - lib/generators/less/templates/interaction.rb
63
63
  - lib/generators/less/templates/test.rb
64
64
  - lib/less_interactions.rb
65
+ - lib/less_interactions/expectation.rb
66
+ - lib/less_interactions/expectation_array.rb
65
67
  - lib/less_interactions/interaction.rb
68
+ - lib/less_interactions/multiple_choice_expectation.rb
66
69
  - lib/less_interactions/response.rb
67
70
  - lib/version.rb
71
+ - test/expectation_test.rb
68
72
  - test/interaction_test.rb
73
+ - test/multiple_choice_expectation_test.rb
69
74
  - test/response_test.rb
70
75
  - test/test_helper.rb
71
76
  homepage: ''
@@ -93,7 +98,9 @@ signing_key:
93
98
  specification_version: 4
94
99
  summary: A better way to think about Rails application
95
100
  test_files:
101
+ - test/expectation_test.rb
96
102
  - test/interaction_test.rb
103
+ - test/multiple_choice_expectation_test.rb
97
104
  - test/response_test.rb
98
105
  - test/test_helper.rb
99
106
  has_rdoc: