lopata 0.1.37 → 0.1.39

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
  SHA256:
3
- metadata.gz: 26144921e2b69dc4a941e05583c6fab3fc5f0e85a9f19e3b0f76b4762c560c09
4
- data.tar.gz: dcdb2f5e7507d897030f8977c1d58100ceeb53d7ac6dff8c6a9af7af8e5c23b2
3
+ metadata.gz: 6b391941e9c4d4e42dca98434fc3ccae1275ec3681cc38a3c3aded8fbabff909
4
+ data.tar.gz: 7c8a4d068f8ffdf583413f0fa156441c20d4cab95643a430c02d8b28c7c0fdea
5
5
  SHA512:
6
- metadata.gz: f54ef97186d06cf3486e053adc5118007f1926694441aa5741fae31f0de42947ce2cc340c0be1839ffdc12d5ecf7b7961f861f43a27067aad59fefc933be597c
7
- data.tar.gz: 1e93d0d56949000c9bd621b6f033d3645bf3371540f7ed5b215c590641a1b86caf166b1f7679b89d6810d7c7307a78eb44075bd78a9427627300234c5f821f9a
6
+ metadata.gz: efbd16b2aea6b5debe630e5739a730cda91de1311682d01527aabc56c5b755055dc9aaae4a003ad317a7b5739450db117423c491be628f077b6d94af4f21476f
7
+ data.tar.gz: 2a7db79eda5d90471d111c54093fab7470055bcedd54aa75b58e5b101af5007671a3f3315f92f4d0baf48086ed1800f7790e9db07d5219867abe915355fb7c44
data/lib/lopata/role.rb CHANGED
@@ -1,110 +1,214 @@
1
- module Lopata
2
- # Adds ability to run scenarios by given user roles
3
- #
4
- # @example Usage
5
- # require 'lopata/role'
6
- # Lopata.configure do |c|
7
- # c.role_descriptions = {
8
- # user: 'User',
9
- # admin: 'Admin'
10
- # }
11
- # c.default_role = :user
12
- # c.before_scenaro 'setup user'
13
- # end
14
- # Lopata.shared_step 'setup user' do
15
- # setup { @user = User.create(role: current_role) if current_role }
16
- # cleanup :user
17
- # end
18
- # Lopata.define 'login' do
19
- # # will generate 2 scenarios, one for :user and one for :admin
20
- # as :user, :admin
21
- # action 'login'
22
- # # verify the user is logged in
23
- # end
24
- #
25
- # @see Lopata::Configuration#role_descriptions
26
- # @see Lopata::Configuration#default_role
27
- module Role
28
- # To be included in Lopata::Scenario
29
- module Methods
30
- # @return [Symbol, nil] user role for current scenario or nil, if scenario is running without user.
31
- def current_role
32
- metadata[:as]
33
- end
34
- end
35
-
36
- # To be included in Lopata::ScenarioBuilder
37
- module DSL
38
- # Enumerate the roles the scenario to be runned under.
39
- # If not invoked the default role only will be used.
40
- #
41
- # The scenario should be set to use the role via before_scenario step using #current_role param.
42
- #
43
- # @param args [Array<Symbol>] list of roles the scenario to be runned with.
44
- # @param block [Block] the block to calculate role from scenario metadata.
45
- def as(*args, &block)
46
- @roles = args.flatten
47
- @roles << Lopata::ScenarioBuilder::CalculatedValue.new(&block) if block_given?
48
- @use_all_roles = true
49
- @role_options = nil
50
- end
51
-
52
- # Enumerate the roles the scenario can be run.
53
- #
54
- # The scenario should be set to use the role via before_scenario step using #current_role param.
55
- #
56
- # Only first role will be used if scenario has no options or diagonales. Some first roles will be used if
57
- # options or diagonales are declared, in order of appearence.
58
- #
59
- # Use this to describe possible roles, but not needed to run scenario with all of them in order to save time
60
- # of running.
61
- #
62
- # @param args [Array<Symbol>] list of roles the scenario to be runned with.
63
- def as_first(*args, &block)
64
- @roles = args.flatten
65
- @use_all_roles = false
66
- @role_options = nil
67
- end
68
-
69
- # @private
70
- def role_options
71
- @role_options ||= build_role_options
72
- end
73
-
74
- # Marks scenario to be runned without user.
75
- #
76
- # @example
77
- # Lopata.define 'scenario withou user' do
78
- # without_user
79
- # it 'does not define the user' do
80
- # expect(current_role).to be_nil
81
- # end
82
- # end
83
- def without_user
84
- @without_user = true
85
- end
86
-
87
- # @private
88
- def build_role_options
89
- return [] unless roles
90
- role_variants = roles.map { |r| [Lopata.configuration.role_descriptions[r], r] }
91
- [Lopata::ScenarioBuilder::Diagonal.new(:as, role_variants, @use_all_roles)]
92
- end
93
-
94
- # @private
95
- def roles
96
- return false if @without_user
97
- @roles ||= [Lopata.configuration.default_role].compact
98
- end
99
-
100
- # @private
101
- def diagonals
102
- super + role_options
103
- end
104
- end
105
- end
106
- end
107
-
108
- Lopata::Scenario.include Lopata::Role::Methods
109
- # Prepend the module to overload #diagonals method
1
+ module Lopata
2
+ # Adds ability to run scenarios by given user roles
3
+ #
4
+ # @example Usage
5
+ # require 'lopata/role'
6
+ # Lopata.configure do |c|
7
+ # c.role_descriptions = {
8
+ # user: 'User',
9
+ # admin: 'Admin'
10
+ # }
11
+ # c.default_role = :user
12
+ # c.before_scenaro 'setup user'
13
+ # end
14
+ # Lopata.shared_step 'setup user' do
15
+ # setup { @user = User.create(role: current_role) if current_role }
16
+ # cleanup :user
17
+ # end
18
+ # Lopata.define 'login' do
19
+ # # will generate 2 scenarios, one for :user and one for :admin
20
+ # as :user, :admin
21
+ # action 'login'
22
+ # # verify the user is logged in
23
+ # end
24
+ #
25
+ # @see Lopata::Configuration#role_descriptions
26
+ # @see Lopata::Configuration#default_role
27
+ module Role
28
+ # To be included in Lopata::Scenario
29
+ module Methods
30
+ # @return [Symbol, nil] user role for current scenario or nil, if scenario is running without user.
31
+ def current_role
32
+ metadata[:as]
33
+ end
34
+ end
35
+
36
+ # To be included in Lopata::ScenarioBuilder
37
+ module DSL
38
+ # Enumerate the roles the scenario to be runned under.
39
+ # If not invoked the default role only will be used.
40
+ #
41
+ # The scenario should be set to use the role via before_scenario step using #current_role param.
42
+ #
43
+ # When block without arguments is given, it defines a role group: the variants (diagonals and options)
44
+ # declared inside the block are applied to the given roles only. Several role groups may be declared,
45
+ # the scenarios are generated for each group separately.
46
+ #
47
+ # @example Role groups
48
+ # Lopata.define 'scenario' do
49
+ # as :user, :admin do
50
+ # diagonal :pc, 'one' => 1, 'two' => 2
51
+ # end
52
+ # as_first :guest do
53
+ # diagonal :pc, 'three' => 3
54
+ # end
55
+ # end
56
+ #
57
+ # @param args [Array<Symbol>] list of roles the scenario to be runned with.
58
+ # @param block [Block] the block to calculate role from scenario metadata (block with one argument),
59
+ # or the role group definition (block without arguments).
60
+ def as(*args, &block)
61
+ return add_role_group(args, true, &block) if block && block.arity == 0
62
+ raise_if_role_groups_defined
63
+ @roles = args.flatten
64
+ @roles << Lopata::ScenarioBuilder::CalculatedValue.new(&block) if block_given?
65
+ @use_all_roles = true
66
+ @role_options = nil
67
+ end
68
+
69
+ # Enumerate the roles the scenario can be run.
70
+ #
71
+ # The scenario should be set to use the role via before_scenario step using #current_role param.
72
+ #
73
+ # Only first role will be used if scenario has no options or diagonales. Some first roles will be used if
74
+ # options or diagonales are declared, in order of appearence.
75
+ #
76
+ # Use this to describe possible roles, but not needed to run scenario with all of them in order to save time
77
+ # of running.
78
+ #
79
+ # When block is given, it defines a role group. See #as for details.
80
+ #
81
+ # @param args [Array<Symbol>] list of roles the scenario to be runned with.
82
+ def as_first(*args, &block)
83
+ return add_role_group(args, false, &block) if block
84
+ raise_if_role_groups_defined
85
+ @roles = args.flatten
86
+ @use_all_roles = false
87
+ @role_options = nil
88
+ end
89
+
90
+ # @private
91
+ def role_options
92
+ @role_options ||= build_role_options
93
+ end
94
+
95
+ # Marks scenario to be runned without user.
96
+ #
97
+ # @example
98
+ # Lopata.define 'scenario withou user' do
99
+ # without_user
100
+ # it 'does not define the user' do
101
+ # expect(current_role).to be_nil
102
+ # end
103
+ # end
104
+ def without_user
105
+ @without_user = true
106
+ end
107
+
108
+ # @private
109
+ def build_role_options
110
+ return [] unless roles
111
+ role_variants = roles.map { |r| [Lopata.configuration.role_descriptions[r], r] }
112
+ [Lopata::ScenarioBuilder::Diagonal.new(:as, role_variants, @use_all_roles)]
113
+ end
114
+
115
+ # @private
116
+ def roles
117
+ return false if @without_user
118
+ @roles ||= [Lopata.configuration.default_role].compact
119
+ end
120
+
121
+ # @private
122
+ def diagonals
123
+ return super + role_options unless @current_role_group
124
+ super + @current_role_group.diagonals + @current_role_group.role_options
125
+ end
126
+
127
+ # @private
128
+ def options
129
+ return super unless @current_role_group
130
+ super + @current_role_group.options
131
+ end
132
+
133
+ # @private
134
+ def option_combinations
135
+ return super() unless @role_groups
136
+ share_role_group_metadata_keys
137
+ @role_groups.flat_map do |group|
138
+ begin
139
+ @current_role_group = group
140
+ (options + diagonals).each(&:reset)
141
+ super()
142
+ ensure
143
+ @current_role_group = nil
144
+ end
145
+ end
146
+ end
147
+
148
+ private
149
+
150
+ # Variants of the same metadata key may be declared in several role groups. Each group knows only the keys
151
+ # of its own variants, so the keys used in other groups are not defined (NoMethodError on access).
152
+ # Share the keys across the groups to get nil for them, as for the variants of a single option.
153
+ def share_role_group_metadata_keys
154
+ all = @role_groups.flat_map { |g| g.options + g.diagonals }
155
+ all.group_by(&:key).each_value do |same_key|
156
+ keys = same_key.flat_map(&:available_metadata_keys).uniq
157
+ same_key.each { |o| o.available_metadata_keys = keys }
158
+ end
159
+ end
160
+
161
+ def add_role_group(roles, use_all_roles, &block)
162
+ if @roles || @without_user
163
+ raise ArgumentError, 'Role groups (as/as_first with block) can not be combined with plain role declaration'
164
+ end
165
+ group = RoleGroup.new(roles.flatten, use_all_roles)
166
+ group.instance_exec(&block)
167
+ (@role_groups ||= []) << group
168
+ end
169
+
170
+ def raise_if_role_groups_defined
171
+ return unless @role_groups
172
+ raise ArgumentError, 'Plain role declaration can not be combined with role groups (as/as_first with block)'
173
+ end
174
+ end
175
+
176
+ # Set of roles with own variants. Variants defined in the group are applied to the group roles only.
177
+ #
178
+ # @see DSL#as
179
+ class RoleGroup
180
+ # @private
181
+ attr_reader :roles, :options, :diagonals
182
+
183
+ # @private
184
+ def initialize(roles, use_all_roles)
185
+ @roles, @use_all_roles = roles, use_all_roles
186
+ @options, @diagonals = [], []
187
+ end
188
+
189
+ # Define option for the role group.
190
+ # @see Lopata::ScenarioBuilder#option
191
+ def option(metadata_key, variants)
192
+ @options << Lopata::ScenarioBuilder::Option.new(metadata_key, variants)
193
+ end
194
+
195
+ # Define diagonal for the role group.
196
+ # @see Lopata::ScenarioBuilder#diagonal
197
+ def diagonal(metadata_key, variants)
198
+ @diagonals << Lopata::ScenarioBuilder::Diagonal.new(metadata_key, variants)
199
+ end
200
+
201
+ # @private
202
+ def role_options
203
+ @role_options ||= begin
204
+ role_variants = roles.map { |r| [Lopata.configuration.role_descriptions[r], r] }
205
+ [Lopata::ScenarioBuilder::Diagonal.new(:as, role_variants, @use_all_roles)]
206
+ end
207
+ end
208
+ end
209
+ end
210
+ end
211
+
212
+ Lopata::Scenario.include Lopata::Role::Methods
213
+ # Prepend the module to overload #diagonals method
110
214
  Lopata::ScenarioBuilder.prepend Lopata::Role::DSL