rspec-context_helper 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 +7 -0
- data/.rspec +2 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +17 -0
- data/Dockerfile +43 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +21 -0
- data/README.adoc +276 -0
- data/Rakefile +12 -0
- data/docker-compose.yml +37 -0
- data/docs/workflow.md +52 -0
- data/entrypoint.sh +9 -0
- data/lib/rspec/context_helper/version.rb +7 -0
- data/lib/rspec/context_helper.rb +109 -0
- data/sig/rspec/context_helper.rbs +6 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 74d51db0cb19ab254e02862acae935baf036b6309e65211a92b5447e9bbdd073
|
4
|
+
data.tar.gz: 1cf6b1ff42898e24569fa31c71a2205c76ae6080959617c5bfd99a522ae2ea8f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cd5d107a0e450bb619a00265063f90f83629178212bac185ff9984ac6b2df955930aaa9821497a731fe63e97b0e394181bcbcc1cceddd721b8dbf401b957e982
|
7
|
+
data.tar.gz: 8db3088d169d05a35be6fbb585839e28e34785b55edf706587fdbefb31ae74176243c1a2cb471fe28e013732c81b47ddc889a379410bd30abdf994328045d636
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## [0.2.0](https://github.com/masaakiaoyagi/rspec-context_helper.rb/compare/v0.1.0...v0.2.0) (2022-06-09)
|
4
|
+
|
5
|
+
|
6
|
+
### Features
|
7
|
+
|
8
|
+
* add docker files for testing ([58bec40](https://github.com/masaakiaoyagi/rspec-context_helper.rb/commit/58bec4016b8e2df3cb76ead1078a5c753045a93a))
|
9
|
+
* change metadata and shared context parameter name ([cdb9d17](https://github.com/masaakiaoyagi/rspec-context_helper.rb/commit/cdb9d17ddfdfefb483bd22568f480cce5fc9cc52))
|
10
|
+
* implement context helper ([aabae85](https://github.com/masaakiaoyagi/rspec-context_helper.rb/commit/aabae8551fc945f7b1a90230426bd4c350a871aa))
|
11
|
+
* implement metadata and shared optiions ([8f1a8fa](https://github.com/masaakiaoyagi/rspec-context_helper.rb/commit/8f1a8fa68ef55db10eaa9d9c47926f70a65603ef))
|
12
|
+
|
13
|
+
|
14
|
+
### Bug Fixes
|
15
|
+
|
16
|
+
* fix to pass _meta and _shared parameters correctly ([2fc7099](https://github.com/masaakiaoyagi/rspec-context_helper.rb/commit/2fc7099f7253f08d8d22dbc2ab00ebd7be875585))
|
17
|
+
* remove pry-byebug dependency ([c0ca561](https://github.com/masaakiaoyagi/rspec-context_helper.rb/commit/c0ca561655a59fb4f3be1e4d1932a54da3a94743))
|
data/Dockerfile
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
ARG RUBY_VERSION
|
2
|
+
|
3
|
+
FROM ruby:${RUBY_VERSION}-slim
|
4
|
+
|
5
|
+
ARG GROUPNAME=docker
|
6
|
+
ARG USERNAME=docker
|
7
|
+
ARG UID
|
8
|
+
ARG GID
|
9
|
+
ARG RUNTIME_PACKAGES="git less"
|
10
|
+
ARG BUILD_PACKAGES="build-essential"
|
11
|
+
ARG WORKSPACE_DIR=/workspace
|
12
|
+
ARG WORKSPACE_CONTAINER_DIR=/workspace-container
|
13
|
+
|
14
|
+
RUN set -x \
|
15
|
+
&& groupadd -g ${GID} ${GROUPNAME} \
|
16
|
+
&& useradd -u ${UID} -g ${GROUPNAME} -m ${USERNAME}
|
17
|
+
|
18
|
+
RUN set -x \
|
19
|
+
&& apt-get -y update \
|
20
|
+
&& apt-get -y upgrade \
|
21
|
+
&& apt-get -y install --no-install-recommends ${RUNTIME_PACKAGES} ${BUILD_PACKAGES}
|
22
|
+
|
23
|
+
RUN set -x \
|
24
|
+
&& mkdir -p ${WORKSPACE_DIR} \
|
25
|
+
&& chown -R ${UID}:${GID} ${WORKSPACE_DIR}
|
26
|
+
WORKDIR ${WORKSPACE_DIR}
|
27
|
+
|
28
|
+
RUN set -x \
|
29
|
+
&& mkdir -p ${WORKSPACE_CONTAINER_DIR} \
|
30
|
+
&& chown -R ${UID}:${GID} ${WORKSPACE_CONTAINER_DIR}
|
31
|
+
|
32
|
+
COPY --chown=${UID}:${GID} . .
|
33
|
+
|
34
|
+
USER ${UID}
|
35
|
+
|
36
|
+
RUN set -x \
|
37
|
+
&& gem install bundler \
|
38
|
+
&& bundle config path ${WORKSPACE_CONTAINER_DIR}/vendor/bundle \
|
39
|
+
&& bundle install \
|
40
|
+
&& mv Gemfile.lock ${WORKSPACE_CONTAINER_DIR}
|
41
|
+
|
42
|
+
ENV WORKSPACE_CONTAINER_DIR=${WORKSPACE_CONTAINER_DIR}
|
43
|
+
ENTRYPOINT ["/workspace/entrypoint.sh"]
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Masaaki Aoyagi <masaaki.aoyagi@gmail.com>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.adoc
ADDED
@@ -0,0 +1,276 @@
|
|
1
|
+
# RSpec::ContextHelper
|
2
|
+
|
3
|
+
https://github.com/masaakiaoyagi/rspec-context_helper.rb/releases[image:https://img.shields.io/github/v/release/masaakiaoyagi/rspec-context_helper.rb?include_prereleases[GitHub release (latest SemVer including pre-releases)]]
|
4
|
+
https://github.com/masaakiaoyagi/rspec-context_helper.rb/actions/workflows/test.yml[image:https://github.com/masaakiaoyagi/rspec-context_helper.rb/actions/workflows/test.yml/badge.svg[test]]
|
5
|
+
https://opensource.org/licenses/MIT[image:https://img.shields.io/badge/License-MIT-yellow.svg[License: MIT]]
|
6
|
+
https://conventionalcommits.org[image:https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white[Conventional Commits]]
|
7
|
+
|
8
|
+
This helper library is for writing tests concisely.
|
9
|
+
|
10
|
+
You can write a test as follows.
|
11
|
+
```ruby
|
12
|
+
example_with("value is zero", value: 0) { expect(value).to eq 0 }
|
13
|
+
```
|
14
|
+
Above is the same as below.
|
15
|
+
```ruby
|
16
|
+
context "value is zero" do
|
17
|
+
let(:value) { 0 }
|
18
|
+
it { expect(value).to eq 0 }
|
19
|
+
end
|
20
|
+
```
|
21
|
+
|
22
|
+
That's basically all there is to it, but I think it will be more potent when used with a https://relishapp.com/rspec/rspec-expectations/v/3-11/docs/custom-matchers[custom matcher].
|
23
|
+
|
24
|
+
.Example of ActiveModel validation tests using a custom matcher
|
25
|
+
[%collapsible]
|
26
|
+
====
|
27
|
+
```ruby
|
28
|
+
class Account
|
29
|
+
include ActiveModel::Model
|
30
|
+
include ActiveModel::Attributes
|
31
|
+
attribute :name, :string
|
32
|
+
validates :name, presence: true, length: { in: 3..20 }, format: { with: /\A[0-9a-zA-Z]*\z/, message: "alphanumeric characters only" }
|
33
|
+
end
|
34
|
+
|
35
|
+
let(:account) { Account.new(name: name) }
|
36
|
+
before do
|
37
|
+
account.valid?
|
38
|
+
end
|
39
|
+
|
40
|
+
# There is no "have_error" matcher, so you need to create one.
|
41
|
+
example_with(name: " ") { expect(account).to have_error.on(:name).with(:blank) }
|
42
|
+
example_with(name: "a" * 2) { expect(account).to have_error.on(:name).with(:too_short, count: 3) }
|
43
|
+
example_with(name: "a" * 3) { expect(account).not_to have_error }
|
44
|
+
example_with(name: "a" * 20) { expect(account).not_to have_error }
|
45
|
+
example_with(name: "a" * 21) { expect(account).to have_error.on(:name).with(:too_long, count: 20) }
|
46
|
+
example_with(name: "a0a") { expect(account).not_to have_error }
|
47
|
+
example_with(name: "a a") { expect(account).to have_error.on(:name).with(:invalid) }
|
48
|
+
example_with(name: "a@a") { expect(account).to have_error.on(:name).with("alphanumeric characters only") }
|
49
|
+
```
|
50
|
+
====
|
51
|
+
|
52
|
+
## Installation
|
53
|
+
|
54
|
+
. Add the dependency to your `Gemfile`:
|
55
|
+
+
|
56
|
+
```ruby
|
57
|
+
group :test do
|
58
|
+
gem "rspec-context_helper"
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
. Run `bundle install`
|
63
|
+
|
64
|
+
## Usage
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
require "rspec-context_helper"
|
68
|
+
```
|
69
|
+
|
70
|
+
See https://github.com/masaakiaoyagi/rspec-context_helper.rb/blob/main/spec/rspec/context_helper_spec.rb[spec] how to write a test.
|
71
|
+
|
72
|
+
### API
|
73
|
+
|
74
|
+
#### `example_with(description = nil, _meta: nil, _shared: nil, **values, &block)`
|
75
|
+
Defines an exmaple with metadata, shared context and local variables.
|
76
|
+
|
77
|
+
##### parameters
|
78
|
+
.`description`: context description
|
79
|
+
[%collapsible]
|
80
|
+
====
|
81
|
+
If description is omitted, it is automatically generated from other parameters.
|
82
|
+
====
|
83
|
+
|
84
|
+
.`_meta`: https://relishapp.com/rspec/rspec-core/v/3-11/docs/metadata/user-defined-metadata[metadata] to be defined
|
85
|
+
[%collapsible]
|
86
|
+
====
|
87
|
+
.examples
|
88
|
+
```ruby
|
89
|
+
_meta: :foo
|
90
|
+
_meta: [:foo, :bar]
|
91
|
+
_meta: { foo: "1" }
|
92
|
+
_meta: [:foo, bar: 2]
|
93
|
+
```
|
94
|
+
====
|
95
|
+
|
96
|
+
.`_shared`: https://relishapp.com/rspec/rspec-core/v/3-11/docs/example-groups/shared-context[shared context] to be included
|
97
|
+
[%collapsible]
|
98
|
+
====
|
99
|
+
.examples
|
100
|
+
```ruby
|
101
|
+
_shared: :foo
|
102
|
+
_shared: [:foo, :bar]
|
103
|
+
_shared: { foo: "1" }
|
104
|
+
_shared: [:foo, bar: 2]
|
105
|
+
_shared: { foo: [:arg1, :arg2] }
|
106
|
+
_shared: { foo: { opt1: :bar } }
|
107
|
+
_shared: { foo: [:arg1, opt1: :bar] }
|
108
|
+
```
|
109
|
+
====
|
110
|
+
|
111
|
+
.`values`: helper methods to be defined by https://relishapp.com/rspec/rspec-core/v/3-11/docs/helper-methods/let-and-let[let]
|
112
|
+
[%collapsible]
|
113
|
+
====
|
114
|
+
.You need to use a proc in order to call helper methods in the example context.
|
115
|
+
[%collapsible]
|
116
|
+
=====
|
117
|
+
```ruby
|
118
|
+
example_with(foo: bar, bar: 2) { expect(foo).to eq 2 }
|
119
|
+
# => undefined local variable or method `bar'
|
120
|
+
example_with(foo: -> { bar }, bar: 2) { expect(foo).to eq 2 }
|
121
|
+
# => OK
|
122
|
+
```
|
123
|
+
=====
|
124
|
+
|
125
|
+
.So you need to use a *nested* proc in order to define a helper method that returns a proc.
|
126
|
+
[%collapsible]
|
127
|
+
=====
|
128
|
+
```ruby
|
129
|
+
example_with(foo: -> { "proc" }) { expect(foo.call).to eq "proc" }
|
130
|
+
# => undefined method `call' for "proc":String
|
131
|
+
example_with(foo: -> { -> { "proc" } }) { expect(foo.call).to eq "proc" }
|
132
|
+
# => OK
|
133
|
+
```
|
134
|
+
=====
|
135
|
+
|
136
|
+
.examples
|
137
|
+
```ruby
|
138
|
+
foo: "1"
|
139
|
+
foo: "1", bar: 2
|
140
|
+
foo: -> { bar }, bar: 2
|
141
|
+
foo: -> { -> { "proc" } }
|
142
|
+
```
|
143
|
+
====
|
144
|
+
|
145
|
+
##### examples
|
146
|
+
.`example_with("description") { expect(true).to eq true }`
|
147
|
+
[%collapsible]
|
148
|
+
====
|
149
|
+
same as
|
150
|
+
```ruby
|
151
|
+
context "description" do
|
152
|
+
it { expect(true).to eq true }
|
153
|
+
end
|
154
|
+
```
|
155
|
+
====
|
156
|
+
|
157
|
+
.`example_with(value: 1) { expect(value).to eq 1 }`
|
158
|
+
[%collapsible]
|
159
|
+
====
|
160
|
+
same as
|
161
|
+
```ruby
|
162
|
+
context "when value is 1" do
|
163
|
+
let(:value) { 1 }
|
164
|
+
it { expect(value).to eq 1 }
|
165
|
+
end
|
166
|
+
```
|
167
|
+
====
|
168
|
+
|
169
|
+
.`example_with(_shared: "logged in") { expect(logged_in).to eq true }`
|
170
|
+
[%collapsible]
|
171
|
+
====
|
172
|
+
same as
|
173
|
+
```ruby
|
174
|
+
context "when logged in" do
|
175
|
+
include_context "logged in"
|
176
|
+
it { expect(logged_in).to eq true }
|
177
|
+
end
|
178
|
+
```
|
179
|
+
====
|
180
|
+
|
181
|
+
.`example_with(_meta: :bar) { |e| expect(e.metadata[:bar]).to eq true }`
|
182
|
+
[%collapsible]
|
183
|
+
====
|
184
|
+
same as
|
185
|
+
```ruby
|
186
|
+
context "", :bar do
|
187
|
+
it { |e| expect(e.metadata[:bar]).to eq true }
|
188
|
+
end
|
189
|
+
```
|
190
|
+
====
|
191
|
+
|
192
|
+
.`example_with(_meta: { foo: 1 }) { |e| expect(e.metadata[:foo]).to eq 1 }`
|
193
|
+
[%collapsible]
|
194
|
+
====
|
195
|
+
same as
|
196
|
+
```ruby
|
197
|
+
context "", foo: 1 do
|
198
|
+
it { |e| expect(e.metadata[:foo]).to eq 1 }
|
199
|
+
end
|
200
|
+
```
|
201
|
+
====
|
202
|
+
|
203
|
+
#### `context_with(description = nil, _meta: nil, _shared: nil, **values, &block)`
|
204
|
+
Defines an example group with metadata, shared context and local variables.
|
205
|
+
|
206
|
+
##### parameters
|
207
|
+
same as `example_with`
|
208
|
+
|
209
|
+
##### examples
|
210
|
+
.`context_with("description") { ... }`
|
211
|
+
[%collapsible]
|
212
|
+
====
|
213
|
+
same as
|
214
|
+
```ruby
|
215
|
+
context "description" do
|
216
|
+
...
|
217
|
+
end
|
218
|
+
```
|
219
|
+
====
|
220
|
+
|
221
|
+
.`context_with(value: 1) { ... }`
|
222
|
+
[%collapsible]
|
223
|
+
====
|
224
|
+
same as
|
225
|
+
```ruby
|
226
|
+
context "when value is 1" do
|
227
|
+
let(:value) { 1 }
|
228
|
+
...
|
229
|
+
end
|
230
|
+
```
|
231
|
+
====
|
232
|
+
|
233
|
+
.`context_with(_shared: "logged in") { ... }`
|
234
|
+
[%collapsible]
|
235
|
+
====
|
236
|
+
same as
|
237
|
+
```ruby
|
238
|
+
context "when logged in" do
|
239
|
+
include_context "logged in"
|
240
|
+
...
|
241
|
+
end
|
242
|
+
```
|
243
|
+
====
|
244
|
+
|
245
|
+
.`context_with(_meta: :bar) { ... }`
|
246
|
+
[%collapsible]
|
247
|
+
====
|
248
|
+
same as
|
249
|
+
```ruby
|
250
|
+
context "", :bar do
|
251
|
+
...
|
252
|
+
end
|
253
|
+
```
|
254
|
+
====
|
255
|
+
|
256
|
+
.`context_with(_meta: { foo: 1 }) { ... }`
|
257
|
+
[%collapsible]
|
258
|
+
====
|
259
|
+
same as
|
260
|
+
```ruby
|
261
|
+
context "", foo: 1 do
|
262
|
+
...
|
263
|
+
end
|
264
|
+
```
|
265
|
+
====
|
266
|
+
|
267
|
+
## Development
|
268
|
+
|
269
|
+
### Run tests
|
270
|
+
```sh
|
271
|
+
$ docker compose run --rm 3.1 bundle exec rspec
|
272
|
+
```
|
273
|
+
|
274
|
+
## See also
|
275
|
+
* https://github.com/rspec/rspec-metagem[RSpec]
|
276
|
+
* https://github.com/masaakiaoyagi/spectator-context_helper.cr[Crystal version]
|
data/Rakefile
ADDED
data/docker-compose.yml
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
x-base:
|
2
|
+
base: &base
|
3
|
+
build: &build
|
4
|
+
context: .
|
5
|
+
args: &build-args
|
6
|
+
UID: 1000
|
7
|
+
GID: 1000
|
8
|
+
volumes:
|
9
|
+
- .:/workspace
|
10
|
+
|
11
|
+
services:
|
12
|
+
"3.1":
|
13
|
+
<<: *base
|
14
|
+
image: rspec-context_helper.rb:3.1
|
15
|
+
build:
|
16
|
+
<<: *build
|
17
|
+
args:
|
18
|
+
<<: *build-args
|
19
|
+
RUBY_VERSION: 3.1
|
20
|
+
|
21
|
+
"3.0":
|
22
|
+
<<: *base
|
23
|
+
image: rspec-context_helper.rb:3.0
|
24
|
+
build:
|
25
|
+
<<: *build
|
26
|
+
args:
|
27
|
+
<<: *build-args
|
28
|
+
RUBY_VERSION: 3.0
|
29
|
+
|
30
|
+
"2.7":
|
31
|
+
<<: *base
|
32
|
+
image: rspec-context_helper.rb:2.7
|
33
|
+
build:
|
34
|
+
<<: *build
|
35
|
+
args:
|
36
|
+
<<: *build-args
|
37
|
+
RUBY_VERSION: 2.7
|
data/docs/workflow.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Workflow
|
2
|
+
|
3
|
+
## Development
|
4
|
+
|
5
|
+
```mermaid
|
6
|
+
gitGraph
|
7
|
+
|
8
|
+
commit tag: "v0.0.1"
|
9
|
+
branch develop order: 3
|
10
|
+
commit
|
11
|
+
commit
|
12
|
+
branch feature order: 4
|
13
|
+
commit
|
14
|
+
commit
|
15
|
+
checkout develop
|
16
|
+
merge feature
|
17
|
+
checkout main
|
18
|
+
merge develop
|
19
|
+
branch release-please order: 2
|
20
|
+
commit
|
21
|
+
checkout main
|
22
|
+
merge release-please tag: "v0.0.2"
|
23
|
+
```
|
24
|
+
|
25
|
+
### Release
|
26
|
+
|
27
|
+
```mermaid
|
28
|
+
sequenceDiagram
|
29
|
+
|
30
|
+
actor user as Developer
|
31
|
+
participant main as GitHub<br />main Branch
|
32
|
+
participant pr as GitHub<br />Pull Request
|
33
|
+
participant release as GitHub<br />Release
|
34
|
+
participant runner as GitHub<br />Actions Runner
|
35
|
+
participant gem as rubygems.org
|
36
|
+
|
37
|
+
user ->> main: push
|
38
|
+
main -->> runner: run release-please workflow
|
39
|
+
runner -->> release: look for latest release
|
40
|
+
Note over runner: update version
|
41
|
+
runner -->> main: collect commits since all latest releases
|
42
|
+
Note over runner: update CHANGELOG
|
43
|
+
runner -->> pr: create PR
|
44
|
+
user ->> gem: disable MFA
|
45
|
+
user ->> pr: merge
|
46
|
+
pr -->> main: merge PR
|
47
|
+
main -->> runner: run release-please workflow
|
48
|
+
runner -->> release: create Release
|
49
|
+
Note over runner: create gem
|
50
|
+
runner -->> gem: publish gem
|
51
|
+
user ->> gem: enable MFA
|
52
|
+
```
|
data/entrypoint.sh
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rspec"
|
4
|
+
|
5
|
+
require_relative "context_helper/version"
|
6
|
+
|
7
|
+
module RSpec
|
8
|
+
module ContextHelper
|
9
|
+
module DSL
|
10
|
+
def example_with(description = nil, _meta: nil, _shared: nil, **values, &block)
|
11
|
+
context_with(description, _meta: _meta, _shared: _shared, **values) do
|
12
|
+
example { |*args| instance_exec(*args, &block) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def context_with(description = nil, _meta: nil, _shared: nil, **values, &block)
|
17
|
+
tags, metadata = Utils.split_options(_meta)
|
18
|
+
shared = Utils.to_h(_shared)
|
19
|
+
description ||= Utils.to_description(tags, metadata, shared, values)
|
20
|
+
|
21
|
+
context description, *tags, **metadata do
|
22
|
+
shared.each do |name, _args|
|
23
|
+
args, opts = Utils.split_options(_args)
|
24
|
+
include_context name, *args, **opts
|
25
|
+
end
|
26
|
+
values.each do |key, value|
|
27
|
+
let(key) do
|
28
|
+
value.is_a?(::Proc) ? instance_exec(&value) : value
|
29
|
+
end
|
30
|
+
end
|
31
|
+
instance_exec(&block)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
module Utils
|
37
|
+
class << self
|
38
|
+
def split_options(value)
|
39
|
+
if value.is_a?(::Hash)
|
40
|
+
[[], value]
|
41
|
+
else
|
42
|
+
value = Array(value)
|
43
|
+
value.last.is_a?(::Hash) ? [value, value.pop] : [value, {}]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_h(value)
|
48
|
+
case value
|
49
|
+
when nil
|
50
|
+
{}
|
51
|
+
when ::Array
|
52
|
+
*rest, last = value
|
53
|
+
if last.is_a?(::Hash)
|
54
|
+
rest.map { |i| [i, nil] }.to_h.merge(last)
|
55
|
+
else
|
56
|
+
value.map { |i| [i, nil] }.to_h
|
57
|
+
end
|
58
|
+
when ::Hash
|
59
|
+
value
|
60
|
+
else
|
61
|
+
{ value => nil }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def to_description(tags, metadata, shared, values)
|
66
|
+
to_sentence(to_tag_words(tags) + to_metadata_words(metadata) + to_shared_words(shared) + to_values_words(values)).then do |desc|
|
67
|
+
desc.empty? ? desc : "when #{desc}"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# TODO: whether to include tag in the description
|
72
|
+
private def to_tag_words(tags)
|
73
|
+
[]
|
74
|
+
end
|
75
|
+
|
76
|
+
# TODO: whether to include metadata in the description
|
77
|
+
private def to_metadata_words(metadata)
|
78
|
+
[]
|
79
|
+
end
|
80
|
+
|
81
|
+
private def to_shared_words(shared)
|
82
|
+
shared.map { |k, v| k }
|
83
|
+
end
|
84
|
+
|
85
|
+
private def to_values_words(values)
|
86
|
+
values.map { |k, v| "#{k} is #{v.inspect}" }
|
87
|
+
end
|
88
|
+
|
89
|
+
private def to_sentence(words)
|
90
|
+
case words.size
|
91
|
+
when 0
|
92
|
+
""
|
93
|
+
when 1
|
94
|
+
words[0]
|
95
|
+
when 2
|
96
|
+
words.join(" and ")
|
97
|
+
else
|
98
|
+
*rest, last = words
|
99
|
+
[rest.join(", "), last].join(" and ")
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
RSpec.configure do |config|
|
108
|
+
config.extend RSpec::ContextHelper::DSL
|
109
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec-context_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Masaaki Aoyagi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-06-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: context helpers for RSpec
|
28
|
+
email:
|
29
|
+
- masaaki.aoyagi@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".rspec"
|
35
|
+
- ".rubocop.yml"
|
36
|
+
- CHANGELOG.md
|
37
|
+
- Dockerfile
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.adoc
|
41
|
+
- Rakefile
|
42
|
+
- docker-compose.yml
|
43
|
+
- docs/workflow.md
|
44
|
+
- entrypoint.sh
|
45
|
+
- lib/rspec/context_helper.rb
|
46
|
+
- lib/rspec/context_helper/version.rb
|
47
|
+
- sig/rspec/context_helper.rbs
|
48
|
+
homepage: https://github.com/masaakiaoyagi/rspec-context_helper.rb
|
49
|
+
licenses:
|
50
|
+
- MIT
|
51
|
+
metadata:
|
52
|
+
homepage_uri: https://github.com/masaakiaoyagi/rspec-context_helper.rb
|
53
|
+
source_code_uri: https://github.com/masaakiaoyagi/rspec-context_helper.rb
|
54
|
+
changelog_uri: https://github.com/masaakiaoyagi/rspec-context_helper.rb/blob/main/CHANGELOG.md
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 2.7.0
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubygems_version: 3.3.7
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: context helpers for RSpec
|
74
|
+
test_files: []
|