minispec-metadata 0.1.0 → 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.
data/README.md
CHANGED
@@ -10,6 +10,11 @@ https://github.com/ordinaryzelig/minispec-metadata
|
|
10
10
|
```ruby
|
11
11
|
describe 'Usage', some: 'metadata' do
|
12
12
|
|
13
|
+
before do
|
14
|
+
# Example usefulness:
|
15
|
+
# Capybara.current_driver = metadata[:driver]
|
16
|
+
end
|
17
|
+
|
13
18
|
it 'defines a metadata method', more: 'metadata' do
|
14
19
|
metadata.must_equal(
|
15
20
|
some: 'metadata',
|
@@ -23,9 +28,67 @@ describe 'Usage', some: 'metadata' do
|
|
23
28
|
)
|
24
29
|
end
|
25
30
|
|
31
|
+
it 'provides a method to get the name of the spec' do
|
32
|
+
spec_name.must_equal 'provides a method to get the name of the spec'
|
33
|
+
end
|
34
|
+
|
35
|
+
describe MiniSpecMetadata::DescribeWithMetadata, 'additional description' do
|
36
|
+
|
37
|
+
it 'provides a method to get the descriptions' do
|
38
|
+
spec_descriptions.must_equal [MiniSpecMetadata::DescribeWithMetadata, 'additional description']
|
39
|
+
end
|
40
|
+
|
41
|
+
# I say "shortcut" because you can easily get this without this gem via
|
42
|
+
# self.class.desc
|
43
|
+
it 'provides a shortcut to get the main description' do
|
44
|
+
spec_description.must_equal MiniSpecMetadata::DescribeWithMetadata
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'provides a method to get only the additional description' do
|
48
|
+
spec_additional_description.must_equal 'additional description'
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
Another great use is to do a version of [Ryan Bates' VCR trick](http://railscasts.com/episodes/291-testing-with-vcr?view=asciicast).
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
describe 'VCR trick' do
|
60
|
+
|
61
|
+
it 'allows you to name your cassettes the same as the spec' do
|
62
|
+
VCR.use_cassette spec_name do
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
### Gotchas
|
69
|
+
|
70
|
+
If you registered a custom spec type and made it a subclass of MiniTest::Spec, e.g.:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
class IntegrationSpec < MiniTest::Spec
|
74
|
+
register_spec_type(/integration$/, self)
|
75
|
+
end
|
76
|
+
MiniTest
|
77
|
+
```
|
78
|
+
|
79
|
+
then you will need to change your superclass to be a subclass of the spec type that allows metadata
|
80
|
+
(see Implementation below).
|
81
|
+
Just change it like this:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
class IntegrationSpec < MiniTest::Spec.spec_type(//)
|
85
|
+
register_spec_type(/integration$/, self)
|
26
86
|
end
|
87
|
+
MiniTest
|
27
88
|
```
|
28
89
|
|
90
|
+
`MiniTest::Spec.spec_type` will always give you the top-level spec type, even if you don't use this gem.
|
91
|
+
|
29
92
|
## Installation
|
30
93
|
|
31
94
|
Add this line to your application's Gemfile:
|
@@ -39,3 +102,11 @@ And then execute:
|
|
39
102
|
Or install it yourself as:
|
40
103
|
|
41
104
|
$ gem install minispec-metadata
|
105
|
+
|
106
|
+
## Implementation (for transparency)
|
107
|
+
|
108
|
+
To add metadata to `it` blocks, I subclassed MiniTest::Spec.
|
109
|
+
In order to ensure that it gets used out of the box,
|
110
|
+
I changed the default Spec class to be the new subclass.
|
111
|
+
|
112
|
+
To add metadata to `describe` blocks, I included a module into the Object class.
|
@@ -4,12 +4,17 @@ module MiniSpecMetadata
|
|
4
4
|
def describe(desc, *args, &block)
|
5
5
|
metadata = args.last.is_a?(Hash) ? args.pop : {}
|
6
6
|
|
7
|
+
additional_description = args.first
|
7
8
|
description_class = super(desc, args, &block)
|
8
9
|
|
9
10
|
description_class.send :define_method, :description_metadata do
|
10
11
|
super().merge(metadata)
|
11
12
|
end
|
12
13
|
|
14
|
+
description_class.send :define_method, :spec_additional_description do
|
15
|
+
additional_description
|
16
|
+
end
|
17
|
+
|
13
18
|
description_class
|
14
19
|
end
|
15
20
|
|
@@ -2,10 +2,12 @@ module MiniSpecMetadata
|
|
2
2
|
class SpecWithMetadata < MiniTest::Spec
|
3
3
|
|
4
4
|
@@metadata = {}
|
5
|
+
@@spec_names = {}
|
5
6
|
|
6
7
|
def self.it(desc = 'anonymous', metadata = {}, &block)
|
7
8
|
name = super desc, &block
|
8
9
|
@@metadata[name] = metadata
|
10
|
+
@@spec_names[name] = desc
|
9
11
|
name
|
10
12
|
end
|
11
13
|
class << self
|
@@ -16,6 +18,25 @@ module MiniSpecMetadata
|
|
16
18
|
description_metadata.merge @@metadata.fetch(__name__, {})
|
17
19
|
end
|
18
20
|
|
21
|
+
# First arg passed to it block.
|
22
|
+
def spec_name
|
23
|
+
@@spec_names[__name__]
|
24
|
+
end
|
25
|
+
|
26
|
+
# Description args passed to describe.
|
27
|
+
# Additional description is included if given.
|
28
|
+
def spec_descriptions
|
29
|
+
[spec_description, spec_additional_description].compact
|
30
|
+
end
|
31
|
+
|
32
|
+
def spec_description
|
33
|
+
self.class.desc
|
34
|
+
end
|
35
|
+
|
36
|
+
def spec_additional_description
|
37
|
+
self.class.spec_additional_description
|
38
|
+
end
|
39
|
+
|
19
40
|
end
|
20
41
|
end
|
21
42
|
|
@@ -64,3 +64,27 @@ describe MiniSpecMetadata::DescribeWithMetadata, super_meta: 'data' do
|
|
64
64
|
end
|
65
65
|
|
66
66
|
end
|
67
|
+
|
68
|
+
describe MiniSpecMetadata::DescribeWithMetadata, 'additional description' do
|
69
|
+
|
70
|
+
it 'provides a method to get the descriptions' do
|
71
|
+
spec_descriptions.must_equal [MiniSpecMetadata::DescribeWithMetadata, 'additional description']
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'provides a shortcut to get the main description' do
|
75
|
+
spec_description.must_equal MiniSpecMetadata::DescribeWithMetadata
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'provides a method to get only the additional description' do
|
79
|
+
spec_additional_description.must_equal 'additional description'
|
80
|
+
end
|
81
|
+
|
82
|
+
describe 'nested describe with no additional description' do
|
83
|
+
|
84
|
+
it 'does not inherit additional description from parent' do
|
85
|
+
spec_additional_description.must_be_nil
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
@@ -47,3 +47,11 @@ describe MiniSpecMetadata::SpecWithMetadata do
|
|
47
47
|
end
|
48
48
|
|
49
49
|
end
|
50
|
+
|
51
|
+
describe MiniSpecMetadata::SpecWithMetadata, 'test name' do
|
52
|
+
|
53
|
+
it 'provides a method to get the name of the spec' do
|
54
|
+
spec_name.must_equal 'provides a method to get the name of the spec'
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minispec-metadata
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|