rspec_in_context 0.1.2 → 0.1.3

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: 5642e59dbebb5dbff148e8ae509607566bc308da
4
- data.tar.gz: 9be37738f258a437096c8f7d96eff8d38f26c23e
3
+ metadata.gz: 6996caf5015fee4ad0b693cb7e89fc2e34afbf49
4
+ data.tar.gz: 9f9c259f3c10aad2b93fd7d7a9b51eef74d2d547
5
5
  SHA512:
6
- metadata.gz: db08c47163993a7bfb142d3339ad4e1915f249f139216a346bf5c472466288a0fed029c624723a183053868a91720edc682d1c48acd70bce3582e9240b7e0a41
7
- data.tar.gz: f771413d8739af72f3acbe88a2ff2ae6b75458fc6137d2f96836bf78f92f33c95df68f8bb00df9f119d7a8556023a9b6e151e02896fa6bb4fc10a44b6e38caa6
6
+ metadata.gz: 72b66d91112fbfcf6ebb2be0c524b531b8c77294988b9920da781070e2eea684501b458ab109122b50b5b89391183ccf4092e571b2db536c0ed5bfb934553dad
7
+ data.tar.gz: 4059ba675a52c63ae0213f1768dfc45446d6a3076a0fa47fe344b77d34cba5bfbc048a14f68b94cd28605889139a5063719ad58538b49914b8a3f6fe52c03799
data/README.md CHANGED
@@ -8,8 +8,6 @@ This gem is here to help you write better shared_examples in Rspec.
8
8
 
9
9
  Ever been bothered by the fact that they don't really behave like methods and that you can't pass it a block ? There you go: `rspec_in_context`
10
10
 
11
- **NOTE**: This is an alpha version. For now context are globally scoped.
12
-
13
11
  ## Installation
14
12
 
15
13
  Add this line to your application's Gemfile:
@@ -49,7 +47,7 @@ end
49
47
  You can define in_context block that are reusable almost anywhere.
50
48
  They completely look like normal Rspec.
51
49
 
52
- ##### Inside a Rspec block
50
+ ##### Inside a Rspec block (scoped)
53
51
 
54
52
  ```ruby
55
53
  # A in_context can be named with a symbol or a string
@@ -60,18 +58,22 @@ define_context :context_name do
60
58
  end
61
59
  ```
62
60
 
63
- ##### Outside a Rspec block
61
+ Those in_context will be scoped to their current `describe`/`context` block.
62
+
63
+ ##### Outside a Rspec block (globally)
64
64
 
65
- Outside of a test you have to use `RSpec.define_context`.
65
+ Outside of a test you have to use `RSpec.define_context`. Those in_context will be defined globally in your tests.
66
66
 
67
67
 
68
68
  ### Use the context
69
69
 
70
- Anywhere in your test description, use a `in_context` block to use a predefined in_context. **They don't need to be in the same file.** Example:
70
+ Anywhere in your test description, use a `in_context` block to use a predefined in_context.
71
+
72
+ **Important**: in_context are scoped to their current `describe`/`context` block. If you need globally defined context see `RSpec.define_context`
71
73
 
72
74
  ```ruby
73
75
  # A in_context can be named with a symbol or a string
74
- define_context :context_name do
76
+ RSpec.define_context :context_name do
75
77
  it 'works' do
76
78
  expect(true).to be_truthy
77
79
  end
@@ -86,6 +88,7 @@ end
86
88
  ### Things to know
87
89
 
88
90
  * You can chose exactly where your inside test will be used:
91
+
89
92
  By using `execute_tests` in your define context, the test passed when you *use* the context will be executed here
90
93
 
91
94
  ```ruby
@@ -109,8 +112,10 @@ in_context :context_name do
109
112
  end
110
113
  ```
111
114
 
112
- * You can add variable instantiation relative to your test where you exactly want
113
- It's an alias of `execute_tests` so you can't use both. But it let you describe what the block will do better.
115
+ * You can add variable instantiation relative to your test where you exactly want:
116
+
117
+ `instanciate_context` is an alias of `execute_tests` so you can't use both.
118
+ But it let you describe what the block will do better.
114
119
 
115
120
  * You can use variable in the in_context definition
116
121
 
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RspecInContext
4
+ module ContextManagement
5
+ def subclass(parent, description, args, registration_collection, &example_group_block)
6
+ subclass = super
7
+ RspecInContext::InContext.remove_context(subclass)
8
+ subclass
9
+ end
10
+ end
11
+ end
12
+
13
+ module RSpec
14
+ module Core
15
+ class ExampleGroup
16
+ class << self
17
+ prepend RspecInContext::ContextManagement
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RspecInContext
4
+ class NoContextFound < StandardError; end
5
+ Context = Struct.new(:block, :owner)
4
6
  module InContext
5
7
  class << self
6
8
  def included(base)
@@ -11,15 +13,20 @@ module RspecInContext
11
13
  @contexts ||= ActiveSupport::HashWithIndifferentAccess.new
12
14
  end
13
15
 
14
- def add_context(context_name, &block)
15
- contexts[context_name] = block
16
+ def add_context(context_name, owner = nil, &block)
17
+ contexts[context_name] = Context.new(block, owner)
16
18
  end
17
19
 
18
20
  def find_context(context_name)
19
- contexts[context_name] || raise("No context found with name #{context_name}")
21
+ contexts[context_name] ||
22
+ (raise NoContextFound, "No context found with name #{context_name}")
20
23
  end
21
24
 
22
- def define_context(context_name, &block)
25
+ def remove_context(current_class)
26
+ contexts.delete_if{ |_, context| context.owner == current_class }
27
+ end
28
+
29
+ def outside_define_context(context_name, &block)
23
30
  InContext.add_context(context_name, &block)
24
31
  end
25
32
  end
@@ -27,7 +34,7 @@ module RspecInContext
27
34
  module ClassMethods
28
35
  def in_context(context_name, *args, &block)
29
36
  Thread.current[:test_block] = block
30
- instance_exec(*args, &InContext.find_context(context_name))
37
+ instance_exec(*args, &InContext.find_context(context_name).block)
31
38
  end
32
39
 
33
40
  def execute_tests
@@ -36,7 +43,9 @@ module RspecInContext
36
43
  alias_method :instanciate_context, :execute_tests
37
44
 
38
45
  def define_context(context_name, &block)
39
- InContext.add_context(context_name, &block)
46
+ instance_exec do
47
+ InContext.add_context(context_name, hooks.instance_variable_get(:@owner), &block)
48
+ end
40
49
  end
41
50
  end
42
51
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RspecInContext
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
@@ -3,6 +3,7 @@
3
3
  require 'active_support/all'
4
4
  require "rspec_in_context/version"
5
5
  require "rspec_in_context/in_context"
6
+ require "rspec_in_context/context_management"
6
7
 
7
8
  module RspecInContext
8
9
  def self.included(base)
@@ -12,6 +13,6 @@ end
12
13
 
13
14
  module RSpec
14
15
  def self.define_context(name, &block)
15
- RspecInContext::InContext.define_context(name, &block)
16
+ RspecInContext::InContext.outside_define_context(name, &block)
16
17
  end
17
18
  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.2
4
+ version: 0.1.3
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-08-30 00:00:00.000000000 Z
11
+ date: 2018-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -170,6 +170,7 @@ files:
170
170
  - bin/console
171
171
  - bin/setup
172
172
  - lib/rspec_in_context.rb
173
+ - lib/rspec_in_context/context_management.rb
173
174
  - lib/rspec_in_context/in_context.rb
174
175
  - lib/rspec_in_context/version.rb
175
176
  - rspec_in_context.gemspec