lopata 0.1.36 → 0.1.38

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.
@@ -1,519 +1,525 @@
1
- # Context for scenario creation.
2
- class Lopata::ScenarioBuilder
3
- # @private
4
- attr_reader :title, :common_metadata, :options, :diagonals
5
- # @private
6
- attr_accessor :shared_step, :group
7
-
8
- # Defines one or more scenarios.
9
- #
10
- # @example
11
- # Lopata.define 'scenario' do
12
- # setup 'test user'
13
- # action 'login'
14
- # verify 'home page displayed'
15
- # end
16
- #
17
- # Given block will be calculated in context of the ScenarioBuilder
18
- #
19
- # @param title [String] scenario unique title
20
- # @param metadata [Hash] metadata to be used within the scenario
21
- # @param block [Block] the scenario definition
22
- # @see Lopata.define
23
- def self.define(title, metadata = {}, &block)
24
- builder = new(title, metadata)
25
- builder.instance_exec &block
26
- builder.build
27
- end
28
-
29
- # @private
30
- def initialize(title, metadata = {})
31
- @title, @common_metadata = title, metadata
32
- @diagonals = []
33
- @options = []
34
- end
35
-
36
- # @private
37
- def build
38
- filters = Lopata.configuration.filters
39
- option_combinations.each do |option_set|
40
- metadata = common_metadata.merge(option_set.metadata)
41
- scenario_title = [title, option_set.title].compact.reject(&:empty?).join(' ')
42
- scenario = Lopata::Scenario::Execution.new(scenario_title, metadata)
43
-
44
- unless filters.empty?
45
- next unless filters.all? { |f| f[scenario] }
46
- end
47
-
48
- exec_steps = []
49
- steps_with_hooks.each do |step|
50
- next if step.condition && !step.condition.match?(scenario)
51
- step.execution_steps(scenario, parent: scenario.top).each { |s| exec_steps << s }
52
- end
53
- scenario.steps.push(*exec_steps.reject(&:teardown?))
54
- scenario.steps.push(*exec_steps.select(&:teardown?))
55
-
56
- world.scenarios << scenario
57
- end
58
- end
59
-
60
- # @!group Defining variants
61
-
62
- # Define option for the scenario.
63
- #
64
- # The scenario will be generated for all the options.
65
- # If more then one option given, the scenarios for all options combinations will be generated.
66
- #
67
- # @param metadata_key [Symbol] the key to access option data via metadata.
68
- # @param variants [Hash{String => Object}] variants for the option
69
- # Keys are titles of the variant, values are metadata values.
70
- #
71
- # @example
72
- # Lopata.define 'scenario' do
73
- # option :one, 'one' => 1, 'two' => 2
74
- # option :two, 'two' => 2, 'three' => 3
75
- # # will generate 4 scenarios:
76
- # # - 'scenario one two'
77
- # # - 'scenario one three'
78
- # # - 'scenario two two'
79
- # # - 'scenario two three'
80
- # end
81
- #
82
- # @see #diagonal
83
- def option(metadata_key, variants)
84
- @options << Option.new(metadata_key, variants)
85
- end
86
-
87
- # Define diagonal for the scenario.
88
- #
89
- # The scenario will be generated for all the variants of the diagonal.
90
- # Each variant of diagonal will be selected for at least one scenario.
91
- # It may be included in more then one scenario when other diagonal or option has more variants.
92
- #
93
- # @param metadata_key [Symbol] the key to access diagonal data via metadata.
94
- # @param variants [Hash{String => Object}] variants for the diagonal.
95
- # Keys are titles of the variant, values are metadata values.
96
- #
97
- # @example
98
- # Lopata.define 'scenario' do
99
- # option :one, 'one' => 1, 'two' => 2
100
- # diagonal :two, 'two' => 2, 'three' => 3
101
- # diagonal :three, 'three' => 3, 'four' => 4, 'five' => 5
102
- # # will generate 3 scenarios:
103
- # # - 'scenario one two three'
104
- # # - 'scenario two three four'
105
- # # - 'scenario one two five'
106
- # end
107
- #
108
- # @see #option
109
- def diagonal(metadata_key, variants)
110
- @diagonals << Diagonal.new(metadata_key, variants)
111
- end
112
-
113
- # Define additional metadata for the scenario
114
- #
115
- # @example
116
- # Lopata.define 'scenario' do
117
- # metadata key: 'value'
118
- # it 'metadata available' do
119
- # expect(metadata[:key]).to eq 'value'
120
- # end
121
- # end
122
- def metadata(hash)
123
- raise 'metadata expected to be a Hash' unless hash.is_a?(Hash)
124
- @common_metadata ||= {}
125
- @common_metadata.merge! hash
126
- end
127
-
128
- # Skip scenario for given variants combination
129
- #
130
- # @example
131
- # Lopata.define 'multiple options' do
132
- # option :one, 'one' => 1, 'two' => 2
133
- # option :two, 'two' => 2, 'three' => 3
134
- # skip_when { |opt| opt.metadata[:one] == opt.metadata[:two] }
135
- # it 'not equal' do
136
- # expect(one).to_not eq two
137
- # end
138
- # end
139
- #
140
- def skip_when(&block)
141
- @skip_when = block
142
- end
143
-
144
- # @private
145
- def skip?(option_set)
146
- @skip_when && @skip_when.call(option_set)
147
- end
148
-
149
- # @!endgroup
150
-
151
- # @!group Defining Steps
152
-
153
- # @private
154
- # @macro [attach] define_step_method
155
- # @!scope instance
156
- # @method $1
157
- def self.define_step_method(name)
158
- name_if = "%s_if" % name
159
- name_unless = "%s_unless" % name
160
- define_method name, ->(*args, **metadata, &block) { add_step(name, *args, metadata: metadata, &block) }
161
- define_method name_if, ->(condition, *args, **metadata, &block) {
162
- add_step(name, *args, metadata: metadata, condition: Lopata::Condition.new(condition), &block)
163
- }
164
- define_method name_unless, ->(condition, *args, **metadata, &block) {
165
- add_step(name, *args, condition: Lopata::Condition.new(condition, positive: false), metadata: metadata, &block)
166
- }
167
- end
168
-
169
- # Define setup step.
170
- # @example
171
- # setup do
172
- # end
173
- #
174
- # # setup from named shared step
175
- # setup 'create user'
176
- #
177
- # # setup with both shared step and code block
178
- # setup 'create user' do
179
- # @user.update(admin: true)
180
- # end
181
- #
182
- # Setup step used for set test data.
183
- # @overload setup(*steps, &block)
184
- # @param steps [Array<String, Symbol, Proc>] the steps to be runned as a part of setup.
185
- # String - name of shared step to be called.
186
- # Symbol - metadata key, referenced to shared step name.
187
- # Proc - in-place step implementation.
188
- # @param block [Block] The implementation of the step.
189
- define_step_method :setup
190
-
191
- # Define action step.
192
- # @example
193
- # action do
194
- # end
195
- #
196
- # # action from named shared step
197
- # action 'login'
198
- #
199
- # # setup with both shared step and code block
200
- # action 'login', 'go dashboard' do
201
- # @user.update(admin: true)
202
- # end
203
- #
204
- # Action step is used for emulate user or external system action
205
- #
206
- # @overload action(*steps, &block)
207
- # @param steps [Array<String, Symbol, Proc>] the steps to be runned as a part of action.
208
- # String - name of shared step to be called.
209
- # Symbol - metadata key, referenced to shared step name.
210
- # Proc - in-place step implementation.
211
- # @param block [Block] The implementation of the step.
212
- define_step_method :action
213
-
214
- # Define teardown step.
215
- # @example
216
- # setup { @user = User.create! }
217
- # teardown { @user.destroy }
218
- # Teardown step will be called at the end of scenario running.
219
- # But it suggested to be decared right after setup or action step which require teardown.
220
- #
221
- # @overload teardown(*steps, &block)
222
- # @param steps [Array<String, Symbol, Proc>] the steps to be runned as a part of teardown.
223
- # String - name of shared step to be called.
224
- # Symbol - metadata key, referenced to shared step name.
225
- # Proc - in-place step implementation.
226
- # @param block [Block] The implementation of the step.
227
- define_step_method :teardown
228
-
229
- # Define verify steps.
230
- # @example
231
- # verify 'home page displayed' # call shared step.
232
- # Usually for validation shared steps inclusion
233
- #
234
- # @overload verify(*steps, &block)
235
- # @param steps [Array<String, Symbol, Proc>] the steps to be runned as a part of verification.
236
- # String - name of shared step to be called.
237
- # Symbol - metadata key, referenced to shared step name.
238
- # Proc - in-place step implementation.
239
- # @param block [Block] The implementation of the step.
240
- define_step_method :verify
241
-
242
- # Define group of steps.
243
- # The metadata for the group may be overriden
244
- # @example
245
- # context 'the task', task: :created do
246
- # verify 'task setup'
247
- # it 'created' do
248
- # expect(metadata[:task]).to eq :created
249
- # end
250
- # end
251
- # Teardown steps within group will be called at the end of the group, not scenario
252
- # @overload context(title, **metadata, &block)
253
- # @param title [String] context title
254
- # @param metadata [Hash] the step additional metadata
255
- # @param block [Block] The implementation of the step.
256
- define_step_method :context
257
-
258
- # Define single validation step.
259
- # @example
260
- # it 'works' do
261
- # expect(1).to eq 1
262
- # end
263
- # @overload it(title, &block)
264
- # @param title [String] validation title
265
- # @param block [Block] The implementation of the step.
266
- define_step_method :it
267
-
268
- # Define runtime method for the scenario.
269
- #
270
- # @note
271
- # The method to be called via #method_missing, so it wont override already defined methods.
272
- #
273
- # @example
274
- # let(:square) { |num| num * num }
275
- # it 'calculated' do
276
- # expect(square(4)).to eq 16
277
- # end
278
- def let(method_name, &block)
279
- steps << Lopata::Step.new(:let) do
280
- execution.let(method_name, &block)
281
- end
282
- end
283
-
284
- # Define memorized runtime method for the scenario.
285
- #
286
- # @note
287
- # The method to be called via #method_missing, so it wont override already defined methods.
288
- #
289
- # @example
290
- # let!(:started) { Time.now }
291
- # it 'started early' do
292
- # first_started = started
293
- # expect(started).to eq first_started
294
- # end
295
- def let!(method_name, &block)
296
- steps << Lopata::Step.new(:let) do
297
- execution.let!(method_name, &block)
298
- end
299
- end
300
-
301
-
302
- # @!endgroup
303
-
304
- # @private
305
- def add_step(method_name, *args, condition: nil, metadata: {}, &block)
306
- step_class =
307
- case method_name
308
- when /^(setup|action|teardown|verify)/ then Lopata::ActionStep
309
- when /^(context)/ then Lopata::GroupStep
310
- else Lopata::Step
311
- end
312
- step = step_class.new(method_name, *args, condition: condition, shared_step: shared_step, &block)
313
- step.metadata = metadata
314
- steps << step
315
- end
316
-
317
- # @private
318
- def steps
319
- @steps ||= []
320
- end
321
-
322
- # @private
323
- def steps_with_hooks
324
- s = []
325
- unless Lopata.configuration.before_scenario_steps.empty?
326
- s << Lopata::ActionStep.new(:setup, *Lopata.configuration.before_scenario_steps)
327
- end
328
-
329
- s += steps
330
-
331
- unless Lopata.configuration.after_scenario_steps.empty?
332
- s << Lopata::ActionStep.new(:teardown, *Lopata.configuration.after_scenario_steps)
333
- end
334
-
335
- s
336
- end
337
-
338
- # @private
339
- def option_combinations
340
- combinations = combine([OptionSet.new], options + diagonals)
341
- while !diagonals.all?(&:complete?)
342
- combinations << OptionSet.new(*(options + diagonals).map(&:next_variant))
343
- end
344
- combinations.reject { |option_set| skip?(option_set) }
345
- end
346
-
347
- # @private
348
- def combine(source_combinations, rest_options)
349
- return source_combinations if rest_options.empty?
350
- combinations = []
351
- current_option = rest_options.shift
352
- source_combinations.each do |source_variants|
353
- current_option.level_variants.each do |v|
354
- combinations << (source_variants + OptionSet.new(v))
355
- end
356
- end
357
- combine(combinations, rest_options)
358
- end
359
-
360
- # @private
361
- def world
362
- Lopata.world
363
- end
364
-
365
- # Set of options for scenario
366
- class OptionSet
367
- # @private
368
- attr_reader :variants
369
-
370
- # @private
371
- def initialize(*variants)
372
- @variants = {}
373
- variants.compact.each { |v| self << v }
374
- end
375
-
376
- # @private
377
- def +(other_set)
378
- self.class.new(*@variants.values).tap do |sum|
379
- other_set.each { |v| sum << v }
380
- end
381
- end
382
-
383
- # @private
384
- def <<(variant)
385
- @variants[variant.key] = variant
386
- end
387
-
388
- # @private
389
- def [](key)
390
- @variants[key]
391
- end
392
-
393
- # @private
394
- def each(&block)
395
- @variants.values.each(&block)
396
- end
397
-
398
- # @private
399
- def title
400
- @variants.values.map(&:title).compact.join(' ')
401
- end
402
-
403
- # @return [Hash{Symbol => Object}] metadata for this option set
404
- def metadata
405
- @variants.values.inject({}) do |metadata, variant|
406
- metadata.merge(variant.metadata(self))
407
- end
408
- end
409
- end
410
-
411
- # @private
412
- class Variant
413
- attr_reader :key, :title, :value, :option
414
-
415
- def initialize(option, key, title, value)
416
- @option, @key, @title, @value = option, key, title, check_lambda_arity(value)
417
- end
418
-
419
- def metadata(option_set)
420
- data = { key => value }
421
- if value.is_a? Hash
422
- value.each do |k, v|
423
- sub_key = "%s_%s" % [key, k]
424
- data[sub_key.to_sym] = v
425
- end
426
- end
427
-
428
- option.available_metadata_keys.each do |key|
429
- data[key] = nil unless data.has_key?(key)
430
- end
431
-
432
- data.each do |key, v|
433
- data[key] = v.calculate(option_set) if v.is_a? CalculatedValue
434
- end
435
- data
436
- end
437
-
438
- def self.join(variants)
439
- title, metadata = nil, {}
440
- variants.each do |v|
441
- title = [title, v.title].compact.join(' ')
442
- metadata.merge!(v.metadata)
443
- end
444
- [title, metadata]
445
- end
446
-
447
- private
448
-
449
- # Лямдда будет передаваться как блок в instance_eval, которому плохеет, если пришло что-то с нулевой
450
- # arity. Поэтому для лямбд с нулевой arity делаем arity == 1
451
- def check_lambda_arity(v)
452
- if v.is_a?(Proc) && v.arity == 0
453
- ->(_) { instance_exec(&v) }
454
- else
455
- v
456
- end
457
- end
458
- end
459
-
460
- # @private
461
- class CalculatedValue
462
- def initialize(&block)
463
- @proc = block
464
- end
465
-
466
- def calculate(option_set)
467
- @proc.call(option_set)
468
- end
469
- end
470
-
471
- # @private
472
- class Option
473
- attr_reader :variants, :key, :use_all_variants
474
- def initialize(key, variants, use_all_variants = true)
475
- @key = key
476
- @variants =
477
- if variants.is_a? Hash
478
- variants.map { |title, value| Variant.new(self, key, title, value) }
479
- else
480
- # Array of arrays of two elements
481
- variants.map { |v| Variant.new(self, key, *v) }
482
- end
483
- @use_all_variants = use_all_variants
484
- end
485
-
486
- # Variants to apply at one level
487
- def level_variants
488
- variants
489
- end
490
-
491
- def next_variant
492
- @current ||= 0
493
- selected_variant = variants[@current]
494
- @current += 1
495
- @complete = true unless use_all_variants # not need to verify all variants, just use first ones.
496
- if @current >= variants.length
497
- @current = 0
498
- @complete = true # all variants have been selected
499
- end
500
- selected_variant
501
- end
502
-
503
- def available_metadata_keys
504
- @available_metadata_keys ||= variants
505
- .map(&:value).select { |v| v.is_a?(Hash) }.flat_map(&:keys).map { |k| "#{key}_#{k}".to_sym }.uniq
506
- end
507
- end
508
-
509
- # @private
510
- class Diagonal < Option
511
- def level_variants
512
- [next_variant]
513
- end
514
-
515
- def complete?
516
- @complete
517
- end
518
- end
519
- end
1
+ # Context for scenario creation.
2
+ class Lopata::ScenarioBuilder
3
+ # @private
4
+ attr_reader :title, :common_metadata, :options, :diagonals
5
+ # @private
6
+ attr_accessor :shared_step, :group
7
+
8
+ # Defines one or more scenarios.
9
+ #
10
+ # @example
11
+ # Lopata.define 'scenario' do
12
+ # setup 'test user'
13
+ # action 'login'
14
+ # verify 'home page displayed'
15
+ # end
16
+ #
17
+ # Given block will be calculated in context of the ScenarioBuilder
18
+ #
19
+ # @param title [String] scenario unique title
20
+ # @param metadata [Hash] metadata to be used within the scenario
21
+ # @param block [Block] the scenario definition
22
+ # @see Lopata.define
23
+ def self.define(title, metadata = {}, &block)
24
+ builder = new(title, metadata)
25
+ builder.instance_exec &block
26
+ builder.build
27
+ end
28
+
29
+ # @private
30
+ def initialize(title, metadata = {})
31
+ @title, @common_metadata = title, metadata
32
+ @diagonals = []
33
+ @options = []
34
+ end
35
+
36
+ # @private
37
+ def build
38
+ filters = Lopata.configuration.filters
39
+ option_combinations.each do |option_set|
40
+ metadata = common_metadata.merge(option_set.metadata)
41
+ scenario_title = [title, option_set.title].compact.reject(&:empty?).join(' ')
42
+ scenario = Lopata::Scenario::Execution.new(scenario_title, metadata)
43
+
44
+ unless filters.empty?
45
+ next unless filters.all? { |f| f[scenario] }
46
+ end
47
+
48
+ exec_steps = []
49
+ steps_with_hooks.each do |step|
50
+ next if step.condition && !step.condition.match?(scenario)
51
+ step.execution_steps(scenario, parent: scenario.top).each { |s| exec_steps << s }
52
+ end
53
+ scenario.steps.push(*exec_steps.reject(&:teardown?))
54
+ scenario.steps.push(*exec_steps.select(&:teardown?))
55
+
56
+ world.scenarios << scenario
57
+ end
58
+ end
59
+
60
+ # @!group Defining variants
61
+
62
+ # Define option for the scenario.
63
+ #
64
+ # The scenario will be generated for all the options.
65
+ # If more then one option given, the scenarios for all options combinations will be generated.
66
+ #
67
+ # @param metadata_key [Symbol] the key to access option data via metadata.
68
+ # @param variants [Hash{String => Object}] variants for the option
69
+ # Keys are titles of the variant, values are metadata values.
70
+ #
71
+ # @example
72
+ # Lopata.define 'scenario' do
73
+ # option :one, 'one' => 1, 'two' => 2
74
+ # option :two, 'two' => 2, 'three' => 3
75
+ # # will generate 4 scenarios:
76
+ # # - 'scenario one two'
77
+ # # - 'scenario one three'
78
+ # # - 'scenario two two'
79
+ # # - 'scenario two three'
80
+ # end
81
+ #
82
+ # @see #diagonal
83
+ def option(metadata_key, variants)
84
+ @options << Option.new(metadata_key, variants)
85
+ end
86
+
87
+ # Define diagonal for the scenario.
88
+ #
89
+ # The scenario will be generated for all the variants of the diagonal.
90
+ # Each variant of diagonal will be selected for at least one scenario.
91
+ # It may be included in more then one scenario when other diagonal or option has more variants.
92
+ #
93
+ # @param metadata_key [Symbol] the key to access diagonal data via metadata.
94
+ # @param variants [Hash{String => Object}] variants for the diagonal.
95
+ # Keys are titles of the variant, values are metadata values.
96
+ #
97
+ # @example
98
+ # Lopata.define 'scenario' do
99
+ # option :one, 'one' => 1, 'two' => 2
100
+ # diagonal :two, 'two' => 2, 'three' => 3
101
+ # diagonal :three, 'three' => 3, 'four' => 4, 'five' => 5
102
+ # # will generate 3 scenarios:
103
+ # # - 'scenario one two three'
104
+ # # - 'scenario two three four'
105
+ # # - 'scenario one two five'
106
+ # end
107
+ #
108
+ # @see #option
109
+ def diagonal(metadata_key, variants)
110
+ @diagonals << Diagonal.new(metadata_key, variants)
111
+ end
112
+
113
+ # Define additional metadata for the scenario
114
+ #
115
+ # @example
116
+ # Lopata.define 'scenario' do
117
+ # metadata key: 'value'
118
+ # it 'metadata available' do
119
+ # expect(metadata[:key]).to eq 'value'
120
+ # end
121
+ # end
122
+ def metadata(hash)
123
+ raise 'metadata expected to be a Hash' unless hash.is_a?(Hash)
124
+ @common_metadata ||= {}
125
+ @common_metadata.merge! hash
126
+ end
127
+
128
+ # Skip scenario for given variants combination
129
+ #
130
+ # @example
131
+ # Lopata.define 'multiple options' do
132
+ # option :one, 'one' => 1, 'two' => 2
133
+ # option :two, 'two' => 2, 'three' => 3
134
+ # skip_when { |opt| opt.metadata[:one] == opt.metadata[:two] }
135
+ # it 'not equal' do
136
+ # expect(one).to_not eq two
137
+ # end
138
+ # end
139
+ #
140
+ def skip_when(&block)
141
+ @skip_when = block
142
+ end
143
+
144
+ # @private
145
+ def skip?(option_set)
146
+ @skip_when && @skip_when.call(option_set)
147
+ end
148
+
149
+ # @!endgroup
150
+
151
+ # @!group Defining Steps
152
+
153
+ # @private
154
+ # @macro [attach] define_step_method
155
+ # @!scope instance
156
+ # @method $1
157
+ def self.define_step_method(name)
158
+ name_if = "%s_if" % name
159
+ name_unless = "%s_unless" % name
160
+ define_method name, ->(*args, **metadata, &block) { add_step(name, *args, metadata: metadata, &block) }
161
+ define_method name_if, ->(condition, *args, **metadata, &block) {
162
+ add_step(name, *args, metadata: metadata, condition: Lopata::Condition.new(condition), &block)
163
+ }
164
+ define_method name_unless, ->(condition, *args, **metadata, &block) {
165
+ add_step(name, *args, condition: Lopata::Condition.new(condition, positive: false), metadata: metadata, &block)
166
+ }
167
+ end
168
+
169
+ # Define setup step.
170
+ # @example
171
+ # setup do
172
+ # end
173
+ #
174
+ # # setup from named shared step
175
+ # setup 'create user'
176
+ #
177
+ # # setup with both shared step and code block
178
+ # setup 'create user' do
179
+ # @user.update(admin: true)
180
+ # end
181
+ #
182
+ # Setup step used for set test data.
183
+ # @overload setup(*steps, &block)
184
+ # @param steps [Array<String, Symbol, Proc>] the steps to be runned as a part of setup.
185
+ # String - name of shared step to be called.
186
+ # Symbol - metadata key, referenced to shared step name.
187
+ # Proc - in-place step implementation.
188
+ # @param block [Block] The implementation of the step.
189
+ define_step_method :setup
190
+
191
+ # Define action step.
192
+ # @example
193
+ # action do
194
+ # end
195
+ #
196
+ # # action from named shared step
197
+ # action 'login'
198
+ #
199
+ # # setup with both shared step and code block
200
+ # action 'login', 'go dashboard' do
201
+ # @user.update(admin: true)
202
+ # end
203
+ #
204
+ # Action step is used for emulate user or external system action
205
+ #
206
+ # @overload action(*steps, &block)
207
+ # @param steps [Array<String, Symbol, Proc>] the steps to be runned as a part of action.
208
+ # String - name of shared step to be called.
209
+ # Symbol - metadata key, referenced to shared step name.
210
+ # Proc - in-place step implementation.
211
+ # @param block [Block] The implementation of the step.
212
+ define_step_method :action
213
+
214
+ # Define teardown step.
215
+ # @example
216
+ # setup { @user = User.create! }
217
+ # teardown { @user.destroy }
218
+ # Teardown step will be called at the end of scenario running.
219
+ # But it suggested to be decared right after setup or action step which require teardown.
220
+ #
221
+ # @overload teardown(*steps, &block)
222
+ # @param steps [Array<String, Symbol, Proc>] the steps to be runned as a part of teardown.
223
+ # String - name of shared step to be called.
224
+ # Symbol - metadata key, referenced to shared step name.
225
+ # Proc - in-place step implementation.
226
+ # @param block [Block] The implementation of the step.
227
+ define_step_method :teardown
228
+
229
+ # Define verify steps.
230
+ # @example
231
+ # verify 'home page displayed' # call shared step.
232
+ # Usually for validation shared steps inclusion
233
+ #
234
+ # @overload verify(*steps, &block)
235
+ # @param steps [Array<String, Symbol, Proc>] the steps to be runned as a part of verification.
236
+ # String - name of shared step to be called.
237
+ # Symbol - metadata key, referenced to shared step name.
238
+ # Proc - in-place step implementation.
239
+ # @param block [Block] The implementation of the step.
240
+ define_step_method :verify
241
+
242
+ # Define group of steps.
243
+ # The metadata for the group may be overriden
244
+ # @example
245
+ # context 'the task', task: :created do
246
+ # verify 'task setup'
247
+ # it 'created' do
248
+ # expect(metadata[:task]).to eq :created
249
+ # end
250
+ # end
251
+ # Teardown steps within group will be called at the end of the group, not scenario
252
+ # @overload context(title, **metadata, &block)
253
+ # @param title [String] context title
254
+ # @param metadata [Hash] the step additional metadata
255
+ # @param block [Block] The implementation of the step.
256
+ define_step_method :context
257
+
258
+ # Define single validation step.
259
+ # @example
260
+ # it 'works' do
261
+ # expect(1).to eq 1
262
+ # end
263
+ # @overload it(title, &block)
264
+ # @param title [String] validation title
265
+ # @param block [Block] The implementation of the step.
266
+ define_step_method :it
267
+
268
+ # Define runtime method for the scenario.
269
+ #
270
+ # @note
271
+ # The method to be called via #method_missing, so it wont override already defined methods.
272
+ #
273
+ # @example
274
+ # let(:square) { |num| num * num }
275
+ # it 'calculated' do
276
+ # expect(square(4)).to eq 16
277
+ # end
278
+ def let(method_name, &block)
279
+ steps << Lopata::Step.new(:let) do
280
+ execution.let(method_name, &block)
281
+ end
282
+ end
283
+
284
+ # Define memorized runtime method for the scenario.
285
+ #
286
+ # @note
287
+ # The method to be called via #method_missing, so it wont override already defined methods.
288
+ #
289
+ # @example
290
+ # let!(:started) { Time.now }
291
+ # it 'started early' do
292
+ # first_started = started
293
+ # expect(started).to eq first_started
294
+ # end
295
+ def let!(method_name, &block)
296
+ steps << Lopata::Step.new(:let) do
297
+ execution.let!(method_name, &block)
298
+ end
299
+ end
300
+
301
+
302
+ # @!endgroup
303
+
304
+ # @private
305
+ def add_step(method_name, *args, condition: nil, metadata: {}, &block)
306
+ step_class =
307
+ case method_name
308
+ when /^(setup|action|teardown|verify)/ then Lopata::ActionStep
309
+ when /^(context)/ then Lopata::GroupStep
310
+ else Lopata::Step
311
+ end
312
+ step = step_class.new(method_name, *args, condition: condition, shared_step: shared_step, &block)
313
+ step.metadata = metadata
314
+ steps << step
315
+ end
316
+
317
+ # @private
318
+ def steps
319
+ @steps ||= []
320
+ end
321
+
322
+ # @private
323
+ def steps_with_hooks
324
+ s = []
325
+ unless Lopata.configuration.before_scenario_steps.empty?
326
+ s << Lopata::ActionStep.new(:setup, *Lopata.configuration.before_scenario_steps)
327
+ end
328
+
329
+ s += steps
330
+
331
+ unless Lopata.configuration.after_scenario_steps.empty?
332
+ s << Lopata::ActionStep.new(:teardown, *Lopata.configuration.after_scenario_steps)
333
+ end
334
+
335
+ s
336
+ end
337
+
338
+ # @private
339
+ def option_combinations
340
+ combinations = combine([OptionSet.new], options + diagonals)
341
+ while !diagonals.all?(&:complete?)
342
+ combinations << OptionSet.new(*(options + diagonals).map(&:next_variant))
343
+ end
344
+ combinations.reject { |option_set| skip?(option_set) }
345
+ end
346
+
347
+ # @private
348
+ def combine(source_combinations, rest_options)
349
+ return source_combinations if rest_options.empty?
350
+ combinations = []
351
+ current_option = rest_options.shift
352
+ source_combinations.each do |source_variants|
353
+ current_option.level_variants.each do |v|
354
+ combinations << (source_variants + OptionSet.new(v))
355
+ end
356
+ end
357
+ combine(combinations, rest_options)
358
+ end
359
+
360
+ # @private
361
+ def world
362
+ Lopata.world
363
+ end
364
+
365
+ # Set of options for scenario
366
+ class OptionSet
367
+ # @private
368
+ attr_reader :variants
369
+
370
+ # @private
371
+ def initialize(*variants)
372
+ @variants = {}
373
+ variants.compact.each { |v| self << v }
374
+ end
375
+
376
+ # @private
377
+ def +(other_set)
378
+ self.class.new(*@variants.values).tap do |sum|
379
+ other_set.each { |v| sum << v }
380
+ end
381
+ end
382
+
383
+ # @private
384
+ def <<(variant)
385
+ @variants[variant.key] = variant
386
+ end
387
+
388
+ # @private
389
+ def [](key)
390
+ @variants[key]
391
+ end
392
+
393
+ # @private
394
+ def each(&block)
395
+ @variants.values.each(&block)
396
+ end
397
+
398
+ # @private
399
+ def title
400
+ @variants.values.map(&:title).compact.join(' ')
401
+ end
402
+
403
+ # @return [Hash{Symbol => Object}] metadata for this option set
404
+ def metadata
405
+ @variants.values.inject({}) do |metadata, variant|
406
+ metadata.merge(variant.metadata(self))
407
+ end
408
+ end
409
+ end
410
+
411
+ # @private
412
+ class Variant
413
+ attr_reader :key, :title, :value, :option
414
+
415
+ def initialize(option, key, title, value)
416
+ @option, @key, @title, @value = option, key, title, check_lambda_arity(value)
417
+ end
418
+
419
+ def metadata(option_set)
420
+ data = { key => value }
421
+ if value.is_a? Hash
422
+ value.each do |k, v|
423
+ sub_key = "%s_%s" % [key, k]
424
+ data[sub_key.to_sym] = v
425
+ end
426
+ end
427
+
428
+ option.available_metadata_keys.each do |key|
429
+ data[key] = nil unless data.has_key?(key)
430
+ end
431
+
432
+ data.each do |key, v|
433
+ data[key] = v.calculate(option_set) if v.is_a? CalculatedValue
434
+ end
435
+ data
436
+ end
437
+
438
+ def self.join(variants)
439
+ title, metadata = nil, {}
440
+ variants.each do |v|
441
+ title = [title, v.title].compact.join(' ')
442
+ metadata.merge!(v.metadata)
443
+ end
444
+ [title, metadata]
445
+ end
446
+
447
+ private
448
+
449
+ # Лямдда будет передаваться как блок в instance_eval, которому плохеет, если пришло что-то с нулевой
450
+ # arity. Поэтому для лямбд с нулевой arity делаем arity == 1
451
+ def check_lambda_arity(v)
452
+ if v.is_a?(Proc) && v.arity == 0
453
+ ->(_) { instance_exec(&v) }
454
+ else
455
+ v
456
+ end
457
+ end
458
+ end
459
+
460
+ # @private
461
+ class CalculatedValue
462
+ def initialize(&block)
463
+ @proc = block
464
+ end
465
+
466
+ def calculate(option_set)
467
+ @proc.call(option_set)
468
+ end
469
+ end
470
+
471
+ # @private
472
+ class Option
473
+ attr_reader :variants, :key, :use_all_variants
474
+ def initialize(key, variants, use_all_variants = true)
475
+ @key = key
476
+ @variants =
477
+ if variants.is_a? Hash
478
+ variants.map { |title, value| Variant.new(self, key, title, value) }
479
+ else
480
+ # Array of arrays of two elements
481
+ variants.map { |v| Variant.new(self, key, *v) }
482
+ end
483
+ @use_all_variants = use_all_variants
484
+ end
485
+
486
+ # Variants to apply at one level
487
+ def level_variants
488
+ variants
489
+ end
490
+
491
+ def next_variant
492
+ @current ||= 0
493
+ selected_variant = variants[@current]
494
+ @current += 1
495
+ @complete = true unless use_all_variants # not need to verify all variants, just use first ones.
496
+ if @current >= variants.length
497
+ @current = 0
498
+ @complete = true # all variants have been selected
499
+ end
500
+ selected_variant
501
+ end
502
+
503
+ # Start variants selection from the beginning.
504
+ def reset
505
+ @current = nil
506
+ @complete = nil
507
+ end
508
+
509
+ def available_metadata_keys
510
+ @available_metadata_keys ||= variants
511
+ .map(&:value).select { |v| v.is_a?(Hash) }.flat_map(&:keys).map { |k| "#{key}_#{k}".to_sym }.uniq
512
+ end
513
+ end
514
+
515
+ # @private
516
+ class Diagonal < Option
517
+ def level_variants
518
+ [next_variant]
519
+ end
520
+
521
+ def complete?
522
+ @complete
523
+ end
524
+ end
525
+ end