rspec_in_context 0.1.5 → 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: 79e623cca533412847e5cfa295efcf09c07013aa
4
- data.tar.gz: '039f464399eeb81e60b8ee82c81b0f943e4691d8'
3
+ metadata.gz: 5502f52a45bc99be8f07be7b7e76d0fc1c4a58f7
4
+ data.tar.gz: 851b066252e469281a83abb1690281486d3fe16c
5
5
  SHA512:
6
- metadata.gz: 8dd6e58537b886f938111cdf3679060599d15b6b20fd6388317f1577504fdcb0db2f5d57a55fa7f36caa58e6ddb18c59b779aa89abf5de37fc85070eeccec851
7
- data.tar.gz: c2ba4a408f479f43933a0bc4e8bacdf5634ee972307bed10eec548a27e7b3399440c3d7f40c9ea6451232c628d18c1fb3136d6d853e1f89f12deb1fa18116011
6
+ metadata.gz: e820c07e9f4f49a11535793d6a6db2783b65e8691d4d95a1b96cac52596195150e02c906bce4075632d87afe3b6ef78aea7da5db55cd74395d800087d20f1372
7
+ data.tar.gz: 90ddb16c809097c6cb223de747af28aac4ac8ee87397746aaf7f6c3f5c4fb735653a4555ff30addb8a7576ce5603467e7d061651ff604b07098a58d6210a2ea8
data/.rubocop.yml CHANGED
@@ -15,3 +15,6 @@ Metrics/BlockLength:
15
15
  - 'spec/**/*.rb'
16
16
  - 'Guardfile'
17
17
  - 'vendor/bundle'
18
+
19
+ Naming/UncommunicativeMethodParamName:
20
+ Enabled: false
data/README.md CHANGED
@@ -87,8 +87,9 @@ end
87
87
 
88
88
  ### Things to know
89
89
 
90
- * You can chose exactly where your inside test will be used:
90
+ #### Inside block execution
91
91
 
92
+ * You can chose exactly where your inside test will be used:
92
93
  By using `execute_tests` in your define context, the test passed when you *use* the context will be executed here
93
94
 
94
95
  ```ruby
@@ -117,6 +118,8 @@ end
117
118
  `instanciate_context` is an alias of `execute_tests` so you can't use both.
118
119
  But it let you describe what the block will do better.
119
120
 
121
+ #### Variable usage
122
+
120
123
  * You can use variable in the in_context definition
121
124
 
122
125
  ```ruby
@@ -140,6 +143,8 @@ in_context :context_name, :poire do
140
143
  end
141
144
  ```
142
145
 
146
+ #### Scoping
147
+
143
148
  * In_contexts can be scope inside one another
144
149
 
145
150
  ```ruby
@@ -175,6 +180,45 @@ in_context :context_name, :poire do
175
180
  end
176
181
  ```
177
182
 
183
+ * in_context are bound to their current scope
184
+
185
+ #### Namespacing
186
+
187
+ * You can add a namespace to a in_context definition
188
+
189
+ ```ruby
190
+ define_context "this is a namespaced context", namespace: "namespace name"
191
+ ```
192
+ Or
193
+ ```ruby
194
+ define_context "this is a namespaced context", ns: "namespace name"
195
+ ```
196
+ Or
197
+ ```ruby
198
+ RSpec.define_context "this is a namespaced context", ns: "namespace name"
199
+ ```
200
+
201
+ * When you want to use a namespaced in_context, you have two choice:
202
+
203
+ Ignore any namespace and it will try to find a corresponding in_context in any_namespace (the ones defined without namespace have the priority);
204
+ ```ruby
205
+ define_context "namespaced context", ns: "namespace name" do
206
+ [...]
207
+ end
208
+
209
+ in_context "namespaced context"
210
+ ```
211
+
212
+ Pass a namespace and it will look only in this context.
213
+ ```ruby
214
+ define_context "namespaced context", ns: "namespace name" do
215
+ [...]
216
+ end
217
+
218
+ in_context "namespaced context", namespace: "namespace name"
219
+ in_context "namespaced context", ns: "namespace name"
220
+ ```
221
+
178
222
  ## Development
179
223
 
180
224
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -2,40 +2,54 @@
2
2
 
3
3
  module RspecInContext
4
4
  class NoContextFound < StandardError; end
5
- Context = Struct.new(:block, :owner)
5
+ Context = Struct.new(:block, :owner, :name, :namespace)
6
6
  module InContext
7
+ GLOBAL_CONTEXT = :global_context
7
8
  class << self
8
9
  def included(base)
9
10
  base.extend ClassMethods
10
11
  end
11
12
 
12
13
  def contexts
13
- @contexts ||= ActiveSupport::HashWithIndifferentAccess.new
14
+ @contexts ||= HashWithIndifferentAccess.new { |hash, key| hash[key] = HashWithIndifferentAccess.new }
14
15
  end
15
16
 
16
- def add_context(context_name, owner = nil, &block)
17
- contexts[context_name] = Context.new(block, owner)
17
+ def add_context(context_name, owner = nil, namespace = nil, &block)
18
+ namespace ||= GLOBAL_CONTEXT
19
+ contexts[namespace][context_name] = Context.new(block, owner, context_name, namespace)
18
20
  end
19
21
 
20
- def find_context(context_name)
21
- contexts[context_name] ||
22
+ def find_context(context_name, namespace = nil)
23
+ if namespace&.present?
24
+ contexts[namespace][context_name]
25
+ else
26
+ contexts[GLOBAL_CONTEXT][context_name] || find_context_in_any_namespace(context_name)
27
+ end ||
22
28
  (raise NoContextFound, "No context found with name #{context_name}")
23
29
  end
24
30
 
31
+ def find_context_in_any_namespace(context_name)
32
+ valid_namespace = contexts.find{ |_, namespaced_contexts| namespaced_contexts[context_name] }&.last
33
+ valid_namespace[context_name] if valid_namespace
34
+ end
35
+
25
36
  def remove_context(current_class)
26
- contexts.delete_if{ |_, context| context.owner == current_class }
37
+ contexts.each_value do |namespaced_contexts|
38
+ namespaced_contexts.delete_if{ |_, context| context.owner == current_class }
39
+ end
27
40
  end
28
41
 
29
- def outside_define_context(context_name, &block)
30
- InContext.add_context(context_name, &block)
42
+ def outside_define_context(context_name, namespace, &block)
43
+ InContext.add_context(context_name, nil, namespace, &block)
31
44
  end
32
45
  end
33
46
 
34
47
  module ClassMethods
35
- def in_context(context_name, *args, &block)
48
+ def in_context(context_name, *args, namespace: nil, ns: nil, &block)
49
+ namespace ||= ns
36
50
  Thread.current[:test_block] = block
37
51
  context(context_name.to_s) do
38
- instance_exec(*args, &InContext.find_context(context_name).block)
52
+ instance_exec(*args, &InContext.find_context(context_name, namespace).block)
39
53
  end
40
54
  end
41
55
 
@@ -44,9 +58,10 @@ module RspecInContext
44
58
  end
45
59
  alias_method :instanciate_context, :execute_tests
46
60
 
47
- def define_context(context_name, &block)
61
+ def define_context(context_name, namespace: nil, ns: nil, &block)
62
+ namespace ||= ns
48
63
  instance_exec do
49
- InContext.add_context(context_name, hooks.instance_variable_get(:@owner), &block)
64
+ InContext.add_context(context_name, hooks.instance_variable_get(:@owner), namespace, &block)
50
65
  end
51
66
  end
52
67
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RspecInContext
4
- VERSION = "0.1.5"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -12,7 +12,8 @@ module RspecInContext
12
12
  end
13
13
 
14
14
  module RSpec
15
- def self.define_context(name, &block)
16
- RspecInContext::InContext.outside_define_context(name, &block)
15
+ def self.define_context(name, namespace: nil, ns: nil, &block)
16
+ namespace ||= ns
17
+ RspecInContext::InContext.outside_define_context(name, namespace, &block)
17
18
  end
18
19
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec_in_context
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis <Zaratan> Pasin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-06 00:00:00.000000000 Z
11
+ date: 2018-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -194,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
194
  version: '0'
195
195
  requirements: []
196
196
  rubyforge_project:
197
- rubygems_version: 2.5.2.3
197
+ rubygems_version: 2.6.14
198
198
  signing_key:
199
199
  specification_version: 4
200
200
  summary: This gem is here to help DRYing your tests cases by giving a better "shared_examples".