lopata 0.1.37 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 26144921e2b69dc4a941e05583c6fab3fc5f0e85a9f19e3b0f76b4762c560c09
4
- data.tar.gz: dcdb2f5e7507d897030f8977c1d58100ceeb53d7ac6dff8c6a9af7af8e5c23b2
3
+ metadata.gz: 343bc2e98af0f7d7af41cca5bbd2ed235810c767d69cb53ccb7d1634f5fd5de8
4
+ data.tar.gz: ae551d28c3d67713ba7aae4c79ab9e753092ebbfefb64df640cf0e3bc8ab6098
5
5
  SHA512:
6
- metadata.gz: f54ef97186d06cf3486e053adc5118007f1926694441aa5741fae31f0de42947ce2cc340c0be1839ffdc12d5ecf7b7961f861f43a27067aad59fefc933be597c
7
- data.tar.gz: 1e93d0d56949000c9bd621b6f033d3645bf3371540f7ed5b215c590641a1b86caf166b1f7679b89d6810d7c7307a78eb44075bd78a9427627300234c5f821f9a
6
+ metadata.gz: 2f0a2f0aae729ba534fd6498355a8df89c5c040ad3e2c90ddcc39f28a30d5c5e67ec4661287af145a9ebed0a5bf2eec1d2aeac837f39f915f3a5ef084896a5a0
7
+ data.tar.gz: 715c0ca557f97fe2d1ad00034c354d227ba5012952aa9d2ca356c1be32792993840bef889b1689c1c356f347b0488b317038946fb742b04d09c5aac8ed196f64
data/lib/lopata/role.rb CHANGED
@@ -1,110 +1,202 @@
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
+ @role_groups.flat_map do |group|
137
+ begin
138
+ @current_role_group = group
139
+ (options + diagonals).each(&:reset)
140
+ super()
141
+ ensure
142
+ @current_role_group = nil
143
+ end
144
+ end
145
+ end
146
+
147
+ private
148
+
149
+ def add_role_group(roles, use_all_roles, &block)
150
+ if @roles || @without_user
151
+ raise ArgumentError, 'Role groups (as/as_first with block) can not be combined with plain role declaration'
152
+ end
153
+ group = RoleGroup.new(roles.flatten, use_all_roles)
154
+ group.instance_exec(&block)
155
+ (@role_groups ||= []) << group
156
+ end
157
+
158
+ def raise_if_role_groups_defined
159
+ return unless @role_groups
160
+ raise ArgumentError, 'Plain role declaration can not be combined with role groups (as/as_first with block)'
161
+ end
162
+ end
163
+
164
+ # Set of roles with own variants. Variants defined in the group are applied to the group roles only.
165
+ #
166
+ # @see DSL#as
167
+ class RoleGroup
168
+ # @private
169
+ attr_reader :roles, :options, :diagonals
170
+
171
+ # @private
172
+ def initialize(roles, use_all_roles)
173
+ @roles, @use_all_roles = roles, use_all_roles
174
+ @options, @diagonals = [], []
175
+ end
176
+
177
+ # Define option for the role group.
178
+ # @see Lopata::ScenarioBuilder#option
179
+ def option(metadata_key, variants)
180
+ @options << Lopata::ScenarioBuilder::Option.new(metadata_key, variants)
181
+ end
182
+
183
+ # Define diagonal for the role group.
184
+ # @see Lopata::ScenarioBuilder#diagonal
185
+ def diagonal(metadata_key, variants)
186
+ @diagonals << Lopata::ScenarioBuilder::Diagonal.new(metadata_key, variants)
187
+ end
188
+
189
+ # @private
190
+ def role_options
191
+ @role_options ||= begin
192
+ role_variants = roles.map { |r| [Lopata.configuration.role_descriptions[r], r] }
193
+ [Lopata::ScenarioBuilder::Diagonal.new(:as, role_variants, @use_all_roles)]
194
+ end
195
+ end
196
+ end
197
+ end
198
+ end
199
+
200
+ Lopata::Scenario.include Lopata::Role::Methods
201
+ # Prepend the module to overload #diagonals method
110
202
  Lopata::ScenarioBuilder.prepend Lopata::Role::DSL