yard-doctest 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +47 -13
- data/features/yard-doctest.feature +78 -27
- data/lib/yard-doctest.rb +70 -29
- data/lib/yard/doctest/example.rb +32 -15
- data/lib/yard/doctest/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca18f61aa4d2ab189d9e0edb48011a7423bf7a3c
|
4
|
+
data.tar.gz: 0a84c6dd74503b1abcfe4b5b494725f6af6800ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ff564c3d96544f560d361c385330967844c19d7de10bca805673261b3f36ceb48dcf29b57e24efe83533ff1e360d9da15a4dd61967316ef13a42accdfa14d61
|
7
|
+
data.tar.gz: 63d7ab908736488e14cd50ecbdde1c272d8f25399b00b5b3d340afbe6b3ccc1c65d01ff1bd64542f311e6eb195b261f2e8eeb89b2f773efd2d4a4ffa06421e02
|
data/README.md
CHANGED
@@ -98,11 +98,11 @@ $ bundle exec yard config -a autoload_plugins yard-doctest
|
|
98
98
|
Next, you'll need to create test helper, which will be required before each of your test. Think about it as `spec_helper.rb` in RSpec or `env.rb` in Cucumber. You should require everything necessary for your examples to run there.
|
99
99
|
|
100
100
|
```bash
|
101
|
-
$ touch
|
101
|
+
$ touch doctest_helper.rb
|
102
102
|
```
|
103
103
|
|
104
104
|
```ruby
|
105
|
-
#
|
105
|
+
# doctest_helper.rb
|
106
106
|
require 'lib/cat'
|
107
107
|
require 'lib/dog'
|
108
108
|
```
|
@@ -195,6 +195,8 @@ It is actually delegated to amazing [minitest](https://github.com/seattlerb/mini
|
|
195
195
|
|
196
196
|
## Advanced usage
|
197
197
|
|
198
|
+
### Test helper
|
199
|
+
|
198
200
|
You can define any methods and instance variables in test helper and they will be available in examples.
|
199
201
|
|
200
202
|
For example, if we change the examples for `Cat#can_hunt_dogs?` like that:
|
@@ -224,7 +226,7 @@ NameError: undefined local variable or method `cat' for Object:Class
|
|
224
226
|
If you don't want to create new instance of class each time (or include module if you're testing it), you can fix this by defining a method in test helper:
|
225
227
|
|
226
228
|
```ruby
|
227
|
-
#
|
229
|
+
# doctest_helper.rb
|
228
230
|
require 'lib/cat'
|
229
231
|
require 'lib/dog'
|
230
232
|
|
@@ -233,26 +235,58 @@ def cat
|
|
233
235
|
end
|
234
236
|
```
|
235
237
|
|
238
|
+
### Hooks
|
239
|
+
|
236
240
|
In case you need to do some preparations/cleanup between tests, hooks are at your service to be defined in test helper:
|
237
241
|
|
238
242
|
```ruby
|
239
|
-
YARD::Doctest.
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
+
YARD::Doctest.configure do |doctest|
|
244
|
+
doctest.before do
|
245
|
+
# this is called before each example and
|
246
|
+
# evaluated in the same context as example
|
247
|
+
# (i.e. has access to the same instance variables)
|
248
|
+
end
|
249
|
+
|
250
|
+
doctest.after do
|
251
|
+
# same as `before`, but runs after each example
|
252
|
+
end
|
253
|
+
|
254
|
+
doctest.after_run do
|
255
|
+
# runs after all the examples and
|
256
|
+
# has different context
|
257
|
+
# (i.e. no access to instance variables)
|
258
|
+
end
|
243
259
|
end
|
260
|
+
```
|
261
|
+
|
262
|
+
There is also a way to limit hooks to specific tests based on class/method name:
|
244
263
|
|
245
|
-
|
246
|
-
|
264
|
+
```ruby
|
265
|
+
YARD::Doctest.configure do |doctest|
|
266
|
+
doctest.before('MyClass') do
|
267
|
+
# this will only be called for doctests of `MyClass` class
|
268
|
+
# and all its methods (i.e. `MyClass.foo`, `MyClass#bar`)
|
269
|
+
end
|
270
|
+
|
271
|
+
doctest.after('MyClass#foo') do
|
272
|
+
# this will only be called for doctests of `MyClass#foo`
|
273
|
+
end
|
247
274
|
end
|
275
|
+
```
|
248
276
|
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
277
|
+
### Skip
|
278
|
+
|
279
|
+
You can skip running some of the tests:
|
280
|
+
|
281
|
+
```ruby
|
282
|
+
YARD::Doctest.configure do |doctest|
|
283
|
+
doctest.skip 'MyClass' # will skip doctests for `MyClass` and all its methods
|
284
|
+
doctest.skip 'MyClass#foo' # will skip doctests for `MyClass#foo`
|
253
285
|
end
|
254
286
|
```
|
255
287
|
|
288
|
+
### Rake
|
289
|
+
|
256
290
|
There is also a Rake task for you:
|
257
291
|
|
258
292
|
```ruby
|
@@ -11,7 +11,7 @@ Feature: yard doctest
|
|
11
11
|
Then the output should contain "doctest Doctests from @example tags"
|
12
12
|
|
13
13
|
Scenario: looks for files in app/lib directories by default
|
14
|
-
Given a file named "
|
14
|
+
Given a file named "doctest_helper.rb" with:
|
15
15
|
"""
|
16
16
|
require 'app/app'
|
17
17
|
require 'lib/lib'
|
@@ -36,7 +36,7 @@ Feature: yard doctest
|
|
36
36
|
Then the output should contain "2 runs, 2 assertions, 0 failures, 0 errors, 0 skips"
|
37
37
|
|
38
38
|
Scenario Outline: looks for files only in passed glob
|
39
|
-
Given a file named "
|
39
|
+
Given a file named "doctest_helper.rb" with:
|
40
40
|
"""
|
41
41
|
require 'app/app'
|
42
42
|
require 'lib/lib'
|
@@ -68,7 +68,7 @@ Feature: yard doctest
|
|
68
68
|
| app/app.rb |
|
69
69
|
|
70
70
|
Scenario: generates test names from unit name
|
71
|
-
Given a file named "
|
71
|
+
Given a file named "doctest_helper.rb" with:
|
72
72
|
"""
|
73
73
|
require 'app/app'
|
74
74
|
"""
|
@@ -109,7 +109,7 @@ Feature: yard doctest
|
|
109
109
|
And the output should contain "B#div"
|
110
110
|
|
111
111
|
Scenario: asserts using equality
|
112
|
-
Given a file named "
|
112
|
+
Given a file named "doctest_helper.rb" with:
|
113
113
|
"""
|
114
114
|
require 'app/app'
|
115
115
|
"""
|
@@ -129,7 +129,7 @@ Feature: yard doctest
|
|
129
129
|
"""
|
130
130
|
|
131
131
|
Scenario Outline: properly handles different return values
|
132
|
-
Given a file named "
|
132
|
+
Given a file named "doctest_helper.rb" with:
|
133
133
|
"""
|
134
134
|
require 'app/app'
|
135
135
|
"""
|
@@ -159,7 +159,7 @@ Feature: yard doctest
|
|
159
159
|
| 1.0 |
|
160
160
|
|
161
161
|
Scenario: handles multiple @example tags
|
162
|
-
Given a file named "
|
162
|
+
Given a file named "doctest_helper.rb" with:
|
163
163
|
"""
|
164
164
|
require 'app/app'
|
165
165
|
"""
|
@@ -177,7 +177,7 @@ Feature: yard doctest
|
|
177
177
|
Then the output should contain "2 runs, 2 assertions, 0 failures, 0 errors, 0 skips"
|
178
178
|
|
179
179
|
Scenario: handles multiple return comments
|
180
|
-
Given a file named "
|
180
|
+
Given a file named "doctest_helper.rb" with:
|
181
181
|
"""
|
182
182
|
require 'app/app'
|
183
183
|
"""
|
@@ -195,7 +195,7 @@ Feature: yard doctest
|
|
195
195
|
Then the output should contain "1 runs, 2 assertions, 0 failures, 0 errors, 0 skips"
|
196
196
|
|
197
197
|
Scenario: runs @example tags without return comment
|
198
|
-
Given a file named "
|
198
|
+
Given a file named "doctest_helper.rb" with:
|
199
199
|
"""
|
200
200
|
require 'app/app'
|
201
201
|
"""
|
@@ -211,7 +211,7 @@ Feature: yard doctest
|
|
211
211
|
Then the output should contain "1 runs, 0 assertions, 0 failures, 0 errors, 0 skips"
|
212
212
|
|
213
213
|
Scenario: handles `# =>` return comment
|
214
|
-
Given a file named "
|
214
|
+
Given a file named "doctest_helper.rb" with:
|
215
215
|
"""
|
216
216
|
require 'app/app'
|
217
217
|
"""
|
@@ -227,7 +227,7 @@ Feature: yard doctest
|
|
227
227
|
Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips"
|
228
228
|
|
229
229
|
Scenario: handles return comment on newline
|
230
|
-
Given a file named "
|
230
|
+
Given a file named "doctest_helper.rb" with:
|
231
231
|
"""
|
232
232
|
require 'app/app'
|
233
233
|
"""
|
@@ -244,7 +244,7 @@ Feature: yard doctest
|
|
244
244
|
Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips"
|
245
245
|
|
246
246
|
Scenario: handles multiple lines
|
247
|
-
Given a file named "
|
247
|
+
Given a file named "doctest_helper.rb" with:
|
248
248
|
"""
|
249
249
|
require 'app/app'
|
250
250
|
"""
|
@@ -263,7 +263,7 @@ Feature: yard doctest
|
|
263
263
|
Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips"
|
264
264
|
|
265
265
|
Scenario: names test with example title when it's present
|
266
|
-
Given a file named "
|
266
|
+
Given a file named "doctest_helper.rb" with:
|
267
267
|
"""
|
268
268
|
require 'app/app'
|
269
269
|
"""
|
@@ -279,7 +279,7 @@ Feature: yard doctest
|
|
279
279
|
Then the output should contain "#sum#test_0001_sums two numbers"
|
280
280
|
|
281
281
|
Scenario: doesn't name test when title is not present
|
282
|
-
Given a file named "
|
282
|
+
Given a file named "doctest_helper.rb" with:
|
283
283
|
"""
|
284
284
|
require 'app/app'
|
285
285
|
"""
|
@@ -295,7 +295,7 @@ Feature: yard doctest
|
|
295
295
|
Then the output should contain "#sum#test_0001_"
|
296
296
|
|
297
297
|
Scenario: adds unit definition to backtrace on failures
|
298
|
-
Given a file named "
|
298
|
+
Given a file named "doctest_helper.rb" with:
|
299
299
|
"""
|
300
300
|
require 'app/app'
|
301
301
|
"""
|
@@ -311,7 +311,7 @@ Feature: yard doctest
|
|
311
311
|
Then the output should contain "app/app.rb:3"
|
312
312
|
|
313
313
|
Scenario: has rake task to run the tests
|
314
|
-
Given a file named "
|
314
|
+
Given a file named "doctest_helper.rb" with:
|
315
315
|
"""
|
316
316
|
require 'app/app'
|
317
317
|
"""
|
@@ -335,7 +335,7 @@ Feature: yard doctest
|
|
335
335
|
Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips"
|
336
336
|
|
337
337
|
Scenario: requires doctest helper
|
338
|
-
Given a file named "
|
338
|
+
Given a file named "doctest_helper.rb" with:
|
339
339
|
"""
|
340
340
|
require 'app/app'
|
341
341
|
|
@@ -359,7 +359,7 @@ Feature: yard doctest
|
|
359
359
|
Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips"
|
360
360
|
|
361
361
|
Scenario: shares binding between asserts
|
362
|
-
Given a file named "
|
362
|
+
Given a file named "doctest_helper.rb" with:
|
363
363
|
"""
|
364
364
|
require 'app/app'
|
365
365
|
"""
|
@@ -378,7 +378,7 @@ Feature: yard doctest
|
|
378
378
|
Then the output should contain "1 runs, 2 assertions, 0 failures, 0 errors, 0 skips"
|
379
379
|
|
380
380
|
Scenario: does not share binding between examples
|
381
|
-
Given a file named "
|
381
|
+
Given a file named "doctest_helper.rb" with:
|
382
382
|
"""
|
383
383
|
require 'app/app'
|
384
384
|
"""
|
@@ -397,23 +397,67 @@ Feature: yard doctest
|
|
397
397
|
When I run `bundle exec yard doctest`
|
398
398
|
Then the output should contain "NameError: undefined local variable or method `a'"
|
399
399
|
|
400
|
-
Scenario: supports hooks
|
401
|
-
Given a file named "
|
400
|
+
Scenario: supports global hooks
|
401
|
+
Given a file named "doctest_helper.rb" with:
|
402
402
|
"""
|
403
403
|
require 'app/app'
|
404
404
|
|
405
|
-
@flag =
|
405
|
+
YARD::Doctest.before { @flag = false }
|
406
|
+
YARD::Doctest.after { @flag = true }
|
407
|
+
YARD::Doctest.after_run { puts 'Run after all by minitest' }
|
408
|
+
"""
|
409
|
+
And a file named "app/app.rb" with:
|
410
|
+
"""
|
411
|
+
# @example
|
412
|
+
# flag #=> false
|
413
|
+
def flag
|
414
|
+
@flag
|
415
|
+
end
|
416
|
+
"""
|
417
|
+
When I run `bundle exec yard doctest`
|
418
|
+
Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips"
|
419
|
+
And the output should contain "Run after all by minitest"
|
420
|
+
|
421
|
+
Scenario: supports test-name hooks
|
422
|
+
Given a file named "doctest_helper.rb" with:
|
423
|
+
"""
|
424
|
+
require 'app/app'
|
406
425
|
|
407
426
|
YARD::Doctest.before do
|
427
|
+
@flag = true
|
428
|
+
@foo = true
|
429
|
+
end
|
430
|
+
|
431
|
+
YARD::Doctest.before('#flag') do
|
408
432
|
@flag = false
|
433
|
+
@foo = false
|
434
|
+
end
|
435
|
+
"""
|
436
|
+
And a file named "app/app.rb" with:
|
437
|
+
"""
|
438
|
+
# @example
|
439
|
+
# flag #=> false
|
440
|
+
def flag
|
441
|
+
@flag && @foo
|
409
442
|
end
|
410
443
|
|
411
|
-
|
412
|
-
|
444
|
+
# @example
|
445
|
+
# foo #=> true
|
446
|
+
def foo
|
447
|
+
@foo && @flag
|
413
448
|
end
|
449
|
+
"""
|
450
|
+
When I run `bundle exec yard doctest`
|
451
|
+
Then the output should contain "2 runs, 2 assertions, 0 failures, 0 errors, 0 skips"
|
414
452
|
|
415
|
-
|
416
|
-
|
453
|
+
Scenario: can skip tests
|
454
|
+
Given a file named "doctest_helper.rb" with:
|
455
|
+
"""
|
456
|
+
require 'app/app'
|
457
|
+
|
458
|
+
YARD::Doctest.configure do |doctest|
|
459
|
+
doctest.skip '#flag'
|
460
|
+
doctest.skip 'A.foo'
|
417
461
|
end
|
418
462
|
"""
|
419
463
|
And a file named "app/app.rb" with:
|
@@ -423,7 +467,14 @@ Feature: yard doctest
|
|
423
467
|
def flag
|
424
468
|
@flag
|
425
469
|
end
|
470
|
+
|
471
|
+
class A
|
472
|
+
# @example
|
473
|
+
# A.foo => true
|
474
|
+
def self.foo
|
475
|
+
true
|
476
|
+
end
|
477
|
+
end
|
426
478
|
"""
|
427
479
|
When I run `bundle exec yard doctest`
|
428
|
-
Then the output should contain "
|
429
|
-
And the output should contain "Run after all by minitest"
|
480
|
+
Then the output should contain "0 runs, 0 assertions, 0 failures, 0 errors, 0 skips"
|
data/lib/yard-doctest.rb
CHANGED
@@ -9,38 +9,79 @@ require 'yard/doctest/version'
|
|
9
9
|
|
10
10
|
module YARD
|
11
11
|
module Doctest
|
12
|
+
extend self
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
block_given? ? @before = blk : @before
|
22
|
-
end
|
14
|
+
#
|
15
|
+
# Configures YARD doctest.
|
16
|
+
#
|
17
|
+
# @yield [self]
|
18
|
+
#
|
19
|
+
def configure
|
20
|
+
yield self
|
21
|
+
end
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
23
|
+
#
|
24
|
+
# Passed block called before each example
|
25
|
+
# or specific tests based on passed name.
|
26
|
+
#
|
27
|
+
# It is evaluated in the same context as example.
|
28
|
+
#
|
29
|
+
# @param [String] test
|
30
|
+
# @param [Proc] blk
|
31
|
+
#
|
32
|
+
def before(test = nil, &blk)
|
33
|
+
hooks[:before] << {test: test, block: blk} if block_given?
|
34
|
+
end
|
35
|
+
|
36
|
+
#
|
37
|
+
# Passed block called after each example
|
38
|
+
# or specific tests based on passed name.
|
39
|
+
#
|
40
|
+
# It is evaluated in the same context as example.
|
41
|
+
#
|
42
|
+
# @param [String] test
|
43
|
+
# @param [Proc] blk
|
44
|
+
#
|
45
|
+
def after(test = nil, &blk)
|
46
|
+
hooks[:after] << {test: test, block: blk} if block_given?
|
47
|
+
end
|
48
|
+
|
49
|
+
#
|
50
|
+
# Passed block called after all examples and
|
51
|
+
# evaluated in the different context from examples.
|
52
|
+
#
|
53
|
+
# It actually just sends block to `Minitest.after_run`.
|
54
|
+
#
|
55
|
+
# @param [Proc] blk
|
56
|
+
#
|
57
|
+
def after_run(&blk)
|
58
|
+
Minitest.after_run &blk
|
59
|
+
end
|
60
|
+
|
61
|
+
#
|
62
|
+
# Adds definition of test to be skipped.
|
63
|
+
#
|
64
|
+
# @param [Array<String>] test
|
65
|
+
#
|
66
|
+
def skip(test)
|
67
|
+
skips << test
|
68
|
+
end
|
69
|
+
|
70
|
+
#
|
71
|
+
# Array of tests to be skipped.
|
72
|
+
# @api private
|
73
|
+
#
|
74
|
+
def skips
|
75
|
+
@skips ||= []
|
76
|
+
end
|
33
77
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
#
|
42
|
-
def after_run(&blk)
|
43
|
-
Minitest.after_run &blk
|
78
|
+
#
|
79
|
+
# Returns hash with arrays of before/after hooks.
|
80
|
+
# @api private
|
81
|
+
#
|
82
|
+
def hooks
|
83
|
+
@hooks ||= {}.tap do |hash|
|
84
|
+
hash[:before], hash[:after] = [], []
|
44
85
|
end
|
45
86
|
end
|
46
87
|
|
data/lib/yard/doctest/example.rb
CHANGED
@@ -21,23 +21,24 @@ module YARD
|
|
21
21
|
|
22
22
|
Class.new(this.class).class_eval do
|
23
23
|
require 'minitest/autorun'
|
24
|
-
require '
|
24
|
+
require 'doctest_helper'
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
unless YARD::Doctest.skips.any? { |skip| this.definition.include?(skip) }
|
27
|
+
describe this.definition do
|
28
|
+
register_hooks(this.definition, YARD::Doctest.hooks)
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
it this.name do
|
31
|
+
this.asserts.each do |assert|
|
32
|
+
expected, actual = assert[:expected], assert[:actual]
|
33
|
+
actual = context.eval(actual)
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
35
|
+
unless expected.empty?
|
36
|
+
begin
|
37
|
+
assert_equal evaluate(expected), actual
|
38
|
+
rescue Minitest::Assertion => error
|
39
|
+
add_filepath_to_backtrace(error, this.filepath)
|
40
|
+
raise error
|
41
|
+
end
|
41
42
|
end
|
42
43
|
end
|
43
44
|
end
|
@@ -59,12 +60,28 @@ module YARD
|
|
59
60
|
|
60
61
|
def add_filepath_to_backtrace(exception, filepath)
|
61
62
|
backtrace = exception.backtrace
|
62
|
-
line = backtrace.find { |
|
63
|
+
line = backtrace.find { |l| l =~ %r(lib/yard/doctest/example) }
|
63
64
|
index = backtrace.index(line)
|
64
65
|
backtrace = backtrace.insert(index, filepath)
|
65
66
|
exception.set_backtrace backtrace
|
66
67
|
end
|
67
68
|
|
69
|
+
def self.register_hooks(definition, all_hooks)
|
70
|
+
all_hooks.each do |type, hooks|
|
71
|
+
hooks.each do |hook|
|
72
|
+
if hook[:test]
|
73
|
+
# test-name hooks
|
74
|
+
if definition.include?(hook[:test])
|
75
|
+
send(type) { evaluate(hook[:block]) }
|
76
|
+
end
|
77
|
+
else
|
78
|
+
# global hooks
|
79
|
+
send(type) { evaluate(hook[:block]) }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
68
85
|
end # Example
|
69
86
|
end # Doctest
|
70
87
|
end # YARD
|
data/lib/yard/doctest/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yard-doctest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Rodionov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: yard
|